/*
- * 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
#include "config.h"
#include <dirent.h>
-#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <sys/types.h>
#include <plog.h>
#include <pio.h>
/* Directory separator is \ when cross-compiling for MS Windows
- systems */
+ * systems
+ */
#if defined(__MINGW32__)
#define DIRSEP ('\\')
#else
return 0;
}
-char *dir_normalize(const char *dpath)
+static char *dir_normalize(const char *dpath)
{
char *npath;
int n;
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;
} 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
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;
-}