SAM: remove Global Variable violations
[platform/framework/web/download-provider.git] / provider / download-provider-utils.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <sys/time.h>
25 #include <sys/statfs.h>
26 #include <unistd.h>
27
28 #include "download-provider-log.h"
29
30 char *dp_strdup(char *src)
31 {
32         char *dest = NULL;
33         size_t src_len = 0;
34
35         if (src == NULL) {
36                 TRACE_ERROR("[CHECK PARAM]");
37                 return NULL;
38         }
39
40         src_len = strlen(src);
41         if (src_len <= 0) {
42                 TRACE_ERROR("[CHECK PARAM] len[%zd]", src_len);
43                 return NULL;
44         }
45
46         dest = (char *)calloc(src_len + 1, sizeof(char));
47         if (dest == NULL) {
48                 TRACE_ERROR("[CHECK] allocation");
49                 return NULL;
50         }
51         memcpy(dest, src, src_len * sizeof(char));
52         dest[src_len] = '\0';
53
54         return dest;
55 }
56
57 int dp_is_file_exist(const char *file_path)
58 {
59         struct stat file_state;
60         int stat_ret;
61
62         if (file_path == NULL) {
63                 TRACE_ERROR("[NULL-CHECK] file path is NULL");
64                 return -1;
65         }
66
67         stat_ret = stat(file_path, &file_state);
68
69         if (stat_ret == 0)
70                 if (file_state.st_mode & S_IFREG)
71                         return 0;
72
73         return -1;
74 }
75
76 long dp_get_file_modified_time(const char *file_path)
77 {
78         struct stat file_state;
79         int stat_ret;
80
81         if (file_path == NULL) {
82                 TRACE_ERROR("[NULL-CHECK] file path is NULL");
83                 return -1;
84         }
85
86         stat_ret = stat(file_path, &file_state);
87         if (stat_ret == 0)
88                 return file_state.st_mtime;
89         return -1;
90 }
91
92 int dp_remove_file(const char *file_path)
93 {
94         if ((file_path != NULL && strlen(file_path) > 0) &&
95                         dp_is_file_exist(file_path) == 0) {
96                 if (unlink(file_path) != 0) {
97                         TRACE_ERROR("failed to remove file");
98                         return -1;
99                 }
100                 return 0;
101         }
102         return -1;
103 }