X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Fpio.c;h=1942348b6af01e709a335fdce4c81cb62b70eba2;hp=b7d3839af5dd101b6f033769a4c4d79c83dd3e16;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hpb=8a73e67a25fc6035e4fabacaa20a65dee016d659 diff --git a/src/lib/pio.c b/src/lib/pio.c index b7d3839..1942348 100644 --- a/src/lib/pio.c +++ b/src/lib/pio.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 jeanfi@gmail.com + * Copyright (C) 2010-2017 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,18 +20,17 @@ #include "config.h" #include -#include #include #include #include #include -#include #include #include /* Directory separator is \ when cross-compiling for MS Windows - systems */ + * systems + */ #if defined(__MINGW32__) #define DIRSEP ('\\') #else @@ -64,7 +63,7 @@ int is_file(const char *path) return 0; } -char *dir_normalize(const char *dpath) +static char *dir_normalize(const char *dpath) { char *npath; int n; @@ -151,11 +150,13 @@ void paths_free(char **paths) char *file_get_content(const char *fpath) { - long size; - + long size, n; char *page; + log_fct_enter(); + size = file_get_size(fpath); + if (size == -1) { page = NULL; @@ -165,46 +166,54 @@ char *file_get_content(const char *fpath) } else { FILE *fp = fopen(fpath, "rb"); + if (fp) { page = malloc(size + 1); - if (!page || size != fread(page, 1, size, fp)) { - free(page); - return NULL; - } - *(page + size) = '\0'; + if (page) { + clearerr(fp); + n = fread(page, 1, size, fp); + if (n != size && ferror(fp)) { + free(page); + page = NULL; + } else { + *(page + n) = '\0'; + } + } fclose(fp); } else { + log_debug("failed to open %s", fpath); page = NULL; } } + log_fct_exit(); + return page; } long file_get_size(const char *path) { FILE *fp; + long size; if (!is_file(path)) return -1; fp = fopen(path, "rb"); if (fp) { - long size; - if (fseek(fp, 0, SEEK_END) == -1) - return -1; - - size = ftell(fp); + size = -1; + else + size = ftell(fp); fclose(fp); - - return size; + } else { + size = -1; } - return -1; + return size; } #define FCOPY_BUF_SZ 4096 @@ -342,60 +351,3 @@ file_copy_print_error(int code, const char *src, const char *dst) printf("File copy error: unknown error %d.\n", code); } } - -int dir_rcopy(const char *src, const char *dst) -{ - int ret; - char **paths; - FTS *ftsp; - FTSENT *p, *chp; - int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR; - char *p_dst, *n_dst; - - log_fct_enter(); - - log_fct("copy dir %s to %s", src, dst); - - paths = malloc(2 * sizeof(char *)); - paths[0] = strdup(src); - paths[1] = NULL; - - ftsp = fts_open(paths, fts_options, NULL); - if (!ftsp) - return 1; - - chp = fts_children(ftsp, 0); - if (!chp) - return 0; - - n_dst = dir_normalize(dst); - - while ((p = fts_read(ftsp)) != NULL) { - switch (p->fts_info) { - case FTS_D: - p_dst = path_append(n_dst, - p->fts_path + strlen(src) + 1); - mkdirs(p_dst, 0777); - free(p_dst); - break; - case FTS_F: - p_dst = path_append(n_dst, - p->fts_path + strlen(src) + 1); - file_copy(p->fts_path, p_dst); - free(p_dst); - break; - default: - break; - } - } - fts_close(ftsp); - - free(n_dst); - free(paths); - - ret = 0; - - log_fct_exit(); - - return ret; -}