Merge tag 'upstream/1.0.4'
[psensor-pkg-debian.git] / src / lib / pio.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #define _LARGEFILE_SOURCE 1
20 #include "config.h"
21
22 #include <dirent.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <plog.h>
30 #include <pio.h>
31
32 /* Directory separator is \ when cross-compiling for MS Windows
33    systems */
34 #if defined(__MINGW32__)
35 #define DIRSEP ('\\')
36 #else
37 #define DIRSEP '/'
38 #endif
39
40 #define FCOPY_BUF_SZ 4096
41
42 int is_dir(const char *path)
43 {
44         struct stat st;
45
46         int ret = lstat(path, &st);
47
48         if (ret == 0 && S_ISDIR(st.st_mode))
49                 return 1;
50
51         return 0;
52 }
53
54 int is_file(const char *path)
55 {
56         struct stat st;
57
58         int ret = lstat(path, &st);
59
60         if (ret == 0 && S_ISREG(st.st_mode))
61                 return 1;
62
63         return 0;
64 }
65
66 char *dir_normalize(const char *dpath)
67 {
68         char *npath;
69         int n;
70
71         if (!dpath || !strlen(dpath))
72                 return NULL;
73
74         npath = strdup(dpath);
75
76         n = strlen(npath);
77
78         if (n > 1 && npath[n - 1] == '/')
79                 npath[n - 1] = '\0';
80
81         return npath;
82 }
83
84 static char **paths_add(char **paths, int n, char *path)
85 {
86         char **result;
87
88         result = malloc((n+1) * sizeof(void *));
89
90         memcpy(result + 1, paths, n * sizeof(void *));
91
92         *result = path;
93
94         return result;
95 }
96
97 char **dir_list(const char *dpath, int (*filter) (const char *))
98 {
99         struct dirent *ent;
100         DIR *dir;
101         char **paths, *path, *name, **tmp;
102         int n;
103
104         dir = opendir(dpath);
105
106         if (!dir)
107                 return NULL;
108
109         n = 1;
110         paths = malloc(sizeof(void *));
111         *paths = NULL;
112
113         while ((ent = readdir(dir)) != NULL) {
114                 name = ent->d_name;
115
116                 if (!strcmp(name, ".") || !strcmp(name, ".."))
117                         continue;
118
119                 path = path_append(dpath, name);
120
121                 if (!filter || filter(path)) {
122                         tmp = paths_add(paths, n, path);
123                         free(paths);
124                         paths = tmp;
125
126                         n++;
127                 } else {
128                         free(path);
129                 }
130         }
131
132         closedir(dir);
133
134         return paths;
135 }
136
137 void paths_free(char **paths)
138 {
139         char **paths_cur;
140
141         paths_cur = paths;
142         while (*paths_cur) {
143                 free(*paths_cur);
144
145                 paths_cur++;
146         }
147
148         free(paths);
149 }
150
151 char *file_get_content(const char *fpath)
152 {
153         long size;
154
155         char *page;
156
157         size = file_get_size(fpath);
158         if (size == -1) {
159                 page = NULL;
160
161         } else if (size == 0) {
162                 page = malloc(1);
163                 *page = '\0';
164
165         } else {
166                 FILE *fp = fopen(fpath, "rb");
167                 if (fp) {
168                         page = malloc(size + 1);
169                         if (!page || size != fread(page, 1, size, fp)) {
170                                 free(page);
171                                 page = NULL;
172                         } else {
173                                 *(page + size) = '\0';
174                         }
175
176                         fclose(fp);
177                 } else {
178                         page = NULL;
179                 }
180         }
181
182         return page;
183 }
184
185 long file_get_size(const char *path)
186 {
187         FILE *fp;
188         long size;
189
190         if (!is_file(path))
191                 return -1;
192
193         fp = fopen(path, "rb");
194         if (fp) {
195                 if (fseek(fp, 0, SEEK_END) == -1)
196                         size = -1;
197                 else
198                         size = ftell(fp);
199
200                 fclose(fp);
201         } else {
202                 size = -1;
203         }
204
205         return size;
206 }
207
208 #define FCOPY_BUF_SZ 4096
209 static int FILE_copy(FILE *src, FILE *dst)
210 {
211         int ret = 0;
212         char *buf = malloc(FCOPY_BUF_SZ);
213         int n;
214
215         if (!buf)
216                 return FILE_COPY_ERROR_ALLOC_BUFFER;
217
218         while (!ret) {
219                 n = fread(buf, 1, FCOPY_BUF_SZ, src);
220                 if (n) {
221                         if (fwrite(buf, 1, n, dst) != n)
222                                 ret = FILE_COPY_ERROR_WRITE;
223                 } else {
224                         if (!feof(src))
225                                 ret = FILE_COPY_ERROR_READ;
226                         else
227                                 break;
228                 }
229         }
230
231         free(buf);
232
233         return ret;
234 }
235
236 int
237 file_copy(const char *src, const char *dst)
238 {
239         FILE *fsrc, *fdst;
240         int ret = 0;
241
242         log_fct("copy %s to %s", src, dst);
243
244         fsrc = fopen(src, "r");
245
246         if (fsrc) {
247                 fdst = fopen(dst, "w+");
248
249                 if (fdst) {
250                         ret = FILE_copy(fsrc, fdst);
251                         fclose(fdst);
252                 } else {
253                         ret = FILE_COPY_ERROR_OPEN_DST;
254                 }
255
256                 fclose(fsrc);
257         } else {
258                 ret = FILE_COPY_ERROR_OPEN_SRC;
259         }
260
261         return ret;
262 }
263
264 char *path_append(const char *dir, const char *path)
265 {
266         char *ret, *ndir;
267
268         ndir = dir_normalize(dir);
269
270         if (!ndir && (!path || !strlen(path)))
271                 ret = NULL;
272
273         else if (!ndir) {
274                 ret = strdup(path);
275
276         } else if (!path || !strlen(path)) {
277                 return ndir;
278
279         } else {
280                 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
281                 strcpy(ret, ndir);
282                 strcat(ret, "/");
283                 strcat(ret, path);
284         }
285
286         free(ndir);
287
288         return ret;
289 }
290
291 void mkdirs(const char *dirs, mode_t mode)
292 {
293         char *c, *dir;
294         int i;
295
296         log_fct("mkdirs %s", dirs);
297
298         c = (char *)dirs;
299         dir = malloc(strlen(dirs) + 1);
300
301         i = 0;
302         while (*c) {
303                 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
304                         strncpy(dir, dirs, i);
305                         dir[i] = '\0';
306                         mkdir(dir, mode);
307                 }
308
309                 c++;
310                 i++;
311         }
312
313         mkdir(dirs, mode);
314
315         free(dir);
316 }
317
318 void
319 file_copy_print_error(int code, const char *src, const char *dst)
320 {
321         switch (code) {
322         case 0:
323                 break;
324         case FILE_COPY_ERROR_OPEN_SRC:
325                 printf("File copy error: failed to open %s.\n", src);
326                 break;
327         case FILE_COPY_ERROR_OPEN_DST:
328                 printf("File copy error: failed to open %s.\n", dst);
329                 break;
330         case FILE_COPY_ERROR_READ:
331                 printf("File copy error: failed to read %s.\n", src);
332                 break;
333         case FILE_COPY_ERROR_WRITE:
334                 printf("File copy error: failed to write %s.\n", src);
335                 break;
336         case FILE_COPY_ERROR_ALLOC_BUFFER:
337                 printf("File copy error: failed to allocate buffer.\n");
338                 break;
339         default:
340                 printf("File copy error: unknown error %d.\n", code);
341         }
342 }