Imported Upstream version 1.3.2
[ppastats-pkg-ubuntu.git] / src / 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 <fts.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <plog.h>
31 #include <pio.h>
32
33 /* Directory separator is \ when cross-compiling for MS Windows
34    systems */
35 #if defined(__MINGW32__)
36 #define DIRSEP ('\\')
37 #else
38 #define DIRSEP '/'
39 #endif
40
41 #define FCOPY_BUF_SZ 4096
42
43 int is_dir(const char *path)
44 {
45         struct stat st;
46
47         int ret = lstat(path, &st);
48
49         if (ret == 0 && S_ISDIR(st.st_mode))
50                 return 1;
51
52         return 0;
53 }
54
55 int is_file(const char *path)
56 {
57         struct stat st;
58
59         int ret = lstat(path, &st);
60
61         if (ret == 0 && S_ISREG(st.st_mode))
62                 return 1;
63
64         return 0;
65 }
66
67 char *dir_normalize(const char *dpath)
68 {
69         char *npath;
70         int n;
71
72         if (!dpath || !strlen(dpath))
73                 return NULL;
74
75         npath = strdup(dpath);
76
77         n = strlen(npath);
78
79         if (n > 1 && npath[n - 1] == '/')
80                 npath[n - 1] = '\0';
81
82         return npath;
83 }
84
85 static char **paths_add(char **paths, int n, char *path)
86 {
87         char **result;
88
89         result = malloc((n+1) * sizeof(void *));
90
91         memcpy(result + 1, paths, n * sizeof(void *));
92
93         *result = path;
94
95         return result;
96 }
97
98 char **dir_list(const char *dpath, int (*filter) (const char *))
99 {
100         struct dirent *ent;
101         DIR *dir;
102         char **paths, *path, *name, **tmp;
103         int n;
104
105         dir = opendir(dpath);
106
107         if (!dir)
108                 return NULL;
109
110         n = 1;
111         paths = malloc(sizeof(void *));
112         *paths = NULL;
113
114         while ((ent = readdir(dir)) != NULL) {
115                 name = ent->d_name;
116
117                 if (!strcmp(name, ".") || !strcmp(name, ".."))
118                         continue;
119
120                 path = path_append(dpath, name);
121
122                 if (!filter || filter(path)) {
123                         tmp = paths_add(paths, n, path);
124                         free(paths);
125                         paths = tmp;
126
127                         n++;
128                 } else {
129                         free(path);
130                 }
131         }
132
133         closedir(dir);
134
135         return paths;
136 }
137
138 void paths_free(char **paths)
139 {
140         char **paths_cur;
141
142         paths_cur = paths;
143         while (*paths_cur) {
144                 free(*paths_cur);
145
146                 paths_cur++;
147         }
148
149         free(paths);
150 }
151
152 char *file_get_content(const char *fpath)
153 {
154         long size;
155
156         char *page;
157
158         size = file_get_size(fpath);
159         if (size == -1) {
160                 page = NULL;
161
162         } else if (size == 0) {
163                 page = malloc(1);
164                 *page = '\0';
165
166         } else {
167                 FILE *fp = fopen(fpath, "rb");
168                 if (fp) {
169                         page = malloc(size + 1);
170                         if (!page || size != fread(page, 1, size, fp)) {
171                                 free(page);
172                                 page = NULL;
173                         } else {
174                                 *(page + size) = '\0';
175                         }
176
177                         fclose(fp);
178                 } else {
179                         page = NULL;
180                 }
181         }
182
183         return page;
184 }
185
186 long file_get_size(const char *path)
187 {
188         FILE *fp;
189         long size;
190
191         if (!is_file(path))
192                 return -1;
193
194         fp = fopen(path, "rb");
195         if (fp) {
196                 if (fseek(fp, 0, SEEK_END) == -1)
197                         size = -1;
198                 else
199                         size = ftell(fp);
200
201                 fclose(fp);
202         } else {
203                 size = -1;
204         }
205
206         return size;
207 }
208
209 #define FCOPY_BUF_SZ 4096
210 static int FILE_copy(FILE *src, FILE *dst)
211 {
212         int ret = 0;
213         char *buf = malloc(FCOPY_BUF_SZ);
214         int n;
215
216         if (!buf)
217                 return FILE_COPY_ERROR_ALLOC_BUFFER;
218
219         while (!ret) {
220                 n = fread(buf, 1, FCOPY_BUF_SZ, src);
221                 if (n) {
222                         if (fwrite(buf, 1, n, dst) != n)
223                                 ret = FILE_COPY_ERROR_WRITE;
224                 } else {
225                         if (!feof(src))
226                                 ret = FILE_COPY_ERROR_READ;
227                         else
228                                 break;
229                 }
230         }
231
232         free(buf);
233
234         return ret;
235 }
236
237 int
238 file_copy(const char *src, const char *dst)
239 {
240         FILE *fsrc, *fdst;
241         int ret = 0;
242
243         log_fct("copy %s to %s", src, dst);
244
245         fsrc = fopen(src, "r");
246
247         if (fsrc) {
248                 fdst = fopen(dst, "w+");
249
250                 if (fdst) {
251                         ret = FILE_copy(fsrc, fdst);
252                         fclose(fdst);
253                 } else {
254                         ret = FILE_COPY_ERROR_OPEN_DST;
255                 }
256
257                 fclose(fsrc);
258         } else {
259                 ret = FILE_COPY_ERROR_OPEN_SRC;
260         }
261
262         return ret;
263 }
264
265 char *path_append(const char *dir, const char *path)
266 {
267         char *ret, *ndir;
268
269         ndir = dir_normalize(dir);
270
271         if (!ndir && (!path || !strlen(path)))
272                 ret = NULL;
273
274         else if (!ndir) {
275                 ret = strdup(path);
276
277         } else if (!path || !strlen(path)) {
278                 return ndir;
279
280         } else {
281                 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
282                 strcpy(ret, ndir);
283                 strcat(ret, "/");
284                 strcat(ret, path);
285         }
286
287         free(ndir);
288
289         return ret;
290 }
291
292 void mkdirs(const char *dirs, mode_t mode)
293 {
294         char *c, *dir;
295         int i;
296
297         log_fct("mkdirs %s", dirs);
298
299         c = (char *)dirs;
300         dir = malloc(strlen(dirs) + 1);
301
302         i = 0;
303         while (*c) {
304                 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
305                         strncpy(dir, dirs, i);
306                         dir[i] = '\0';
307                         mkdir(dir, mode);
308                 }
309
310                 c++;
311                 i++;
312         }
313
314         mkdir(dirs, mode);
315
316         free(dir);
317 }
318
319 void
320 file_copy_print_error(int code, const char *src, const char *dst)
321 {
322         switch (code) {
323         case 0:
324                 break;
325         case FILE_COPY_ERROR_OPEN_SRC:
326                 printf("File copy error: failed to open %s.\n", src);
327                 break;
328         case FILE_COPY_ERROR_OPEN_DST:
329                 printf("File copy error: failed to open %s.\n", dst);
330                 break;
331         case FILE_COPY_ERROR_READ:
332                 printf("File copy error: failed to read %s.\n", src);
333                 break;
334         case FILE_COPY_ERROR_WRITE:
335                 printf("File copy error: failed to write %s.\n", src);
336                 break;
337         case FILE_COPY_ERROR_ALLOC_BUFFER:
338                 printf("File copy error: failed to allocate buffer.\n");
339                 break;
340         default:
341                 printf("File copy error: unknown error %d.\n", code);
342         }
343 }
344
345 int dir_rcopy(const char *src, const char *dst)
346 {
347         int ret;
348         char **paths;
349         FTS *ftsp;
350         FTSENT *p, *chp;
351         int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR;
352         char *p_dst, *n_dst;
353
354         log_fct_enter();
355
356         log_fct("copy dir %s to %s", src, dst);
357
358         paths = malloc(2 * sizeof(char *));
359         paths[0] = strdup(src);
360         paths[1] = NULL;
361
362         ftsp = fts_open(paths, fts_options, NULL);
363         if (!ftsp)
364                 return 1;
365
366         chp = fts_children(ftsp, 0);
367         if (!chp)
368                 return 0;
369
370         n_dst = dir_normalize(dst);
371
372         while ((p = fts_read(ftsp)) != NULL) {
373                 switch (p->fts_info) {
374                 case FTS_D:
375                         p_dst = path_append(n_dst,
376                                             p->fts_path + strlen(src) + 1);
377                         mkdirs(p_dst, 0777);
378                         free(p_dst);
379                         break;
380                 case FTS_F:
381                         p_dst = path_append(n_dst,
382                                             p->fts_path + strlen(src) + 1);
383                         file_copy(p->fts_path, p_dst);
384                         free(p_dst);
385                         break;
386                 default:
387                         break;
388                 }
389         }
390         fts_close(ftsp);
391
392         free(n_dst);
393         free(paths);
394
395         ret = 0;
396
397         log_fct_exit();
398
399         return ret;
400 }