summaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c95
1 files changed, 47 insertions, 48 deletions
diff --git a/src/util.c b/src/util.c
index 10135ba..26089f1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -50,7 +50,7 @@ void statistics(DB *db) {
assert(db);
- if ((ret = db->stat(db, &statp, 0)) != 0) {
+ if ((ret = db->stat(db, NULL, &statp, DB_FAST_STAT)) != 0) {
db->err(db, ret, "DB->stat");
return;
}
@@ -62,7 +62,7 @@ void statistics(DB *db) {
char* normalize_path(char *s) {
char *l, *p, *d;
- // deletes /./ and //
+ /* deletes /./ and // */
if (*s == '/')
l = p = d = s+1;
@@ -167,6 +167,7 @@ int isdirectory(const char *path) {
static int copy_fd_sendfile(int sfd, int dfd, off_t l) {
off_t sfo, o;
+ int first = 1;
if ((sfo = lseek(sfd, 0, SEEK_CUR)) == (off_t) -1)
o = 0;
@@ -174,16 +175,20 @@ static int copy_fd_sendfile(int sfd, int dfd, off_t l) {
o = sfo;
while (l > 0) {
- size_t m = l > MAX_SENDFILE_SIZE ? MAX_SENDFILE_SIZE : l;
+ size_t m = MIN(l, MAX_SENDFILE_SIZE);
ssize_t r;
- if ((r = sendfile(dfd, sfd, &o, m)) <= 0)
+ if ((r = sendfile(dfd, sfd, &o, m)) <= 0) {
+ assert(first);
return -1;
+ }
l -= r;
if (sfo != (off_t) -1)
sfo += r;
+
+ first = 0;
}
if (sfo != (off_t) -1)
@@ -226,9 +231,12 @@ int expand_file(int fd, off_t l) {
int copy_fd(int sfd, int dfd, off_t l) {
off_t sfo = 0, dfo = 0, msfo = 0, mdfo = 0;
size_t m = 0, sm = 0, dm = 0;
- void *sp, *dp;
+ uint8_t *sp, *dp;
static size_t psize = 0;
+ if (l == 0)
+ return 0;
+
#ifdef USE_SENDFILE
if (copy_fd_sendfile(sfd, dfd, l) >= 0)
return 0;
@@ -248,7 +256,7 @@ int copy_fd(int sfd, int dfd, off_t l) {
if (l > BUFSIZE) {
- m = (size_t) (l < MMAPSIZE ? l : MMAPSIZE);
+ m = (size_t) MIN(l, MMAPSIZE);
if ((sfo = lseek(sfd, 0, SEEK_CUR)) != (off_t) -1) {
off_t s;
@@ -276,7 +284,6 @@ int copy_fd(int sfd, int dfd, off_t l) {
if ((dp = mmap(NULL, dm, PROT_READ|PROT_WRITE, MAP_SHARED, dfd, mdfo)) != MAP_FAILED)
madvise(dp, dm, MADV_SEQUENTIAL);
}
-
}
if (sp == MAP_FAILED && dp == MAP_FAILED) { /* copy fd to fd */
@@ -289,7 +296,7 @@ int copy_fd(int sfd, int dfd, off_t l) {
while (l > 0) {
off_t n;
- size_t m = l > BUFSIZE ? BUFSIZE : l;
+ size_t m = MIN(l, BUFSIZE);
if ((n = loop_read(sfd, buf, m)) != m) {
@@ -347,9 +354,9 @@ int copy_fd(int sfd, int dfd, off_t l) {
return 0;
}
- m = (size_t) (l < MMAPSIZE ? l : MMAPSIZE);
+ m = (size_t) (MIN(l, MMAPSIZE));
- mdfo = (dfo/psize)*psize;
+ mdfo = (off_t) (dfo/psize)*psize;
dm = m+(dfo-mdfo);
if ((dp = mmap(NULL, dm, PROT_READ|PROT_WRITE, MAP_SHARED, dfd, mdfo)) == MAP_FAILED) {
fprintf(stderr, "mmap(): %s\n", strerror(errno));
@@ -386,9 +393,9 @@ int copy_fd(int sfd, int dfd, off_t l) {
return 0;
}
- m = (size_t) (l < MMAPSIZE ? l : MMAPSIZE);
+ m = (size_t) (MIN(l, MMAPSIZE));
- msfo = (sfo/psize)*psize;
+ msfo = (off_t) (sfo/psize)*psize;
sm = m+(sfo-msfo);
if ((sp = mmap(NULL, sm, PROT_READ, MAP_SHARED, sfd, msfo)) == MAP_FAILED) {
fprintf(stderr, "mmap(): %s\n", strerror(errno));
@@ -423,7 +430,7 @@ int copy_fd(int sfd, int dfd, off_t l) {
return 0;
}
- m = (size_t) (l < MMAPSIZE ? l : MMAPSIZE);
+ m = (size_t) (MIN(l, MMAPSIZE));
msfo = (off_t) (sfo/psize)*psize;
sm = m+(sfo-msfo);
@@ -510,7 +517,6 @@ int move_file(const char *src, const char *dst, int c) {
return 0;
}
-
int prune_empty_directories(const char *path, const char *root) {
char rroot[PATH_MAX],
rpath[PATH_MAX];
@@ -526,13 +532,7 @@ int prune_empty_directories(const char *path, const char *root) {
for (;;) {
char *e;
- if (!rpath[0])
- break;
-
- if (!strcmp(rpath, "/"))
- break;
-
- if (!strcmp(rpath, rroot))
+ if (!rpath[0] || !strcmp(rpath, "/") || !strcmp(rpath, rroot))
break;
if (rmdir(rpath) < 0) {
@@ -550,7 +550,6 @@ int prune_empty_directories(const char *path, const char *root) {
break;
*e = 0;
-
}
return 0;
@@ -686,36 +685,36 @@ int rm_rf(const char *root, int rec) {
while ((de = readdir(dir))) {
char path[PATH_MAX];
+ struct stat st;
- if (!strcmp(de->d_name, "."))
- continue;
-
- if (!strcmp(de->d_name, ".."))
+ if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
snprintf(path, sizeof(path), "%s/%s", root, de->d_name);
- if (rec) {
- struct stat st;
-
- if (stat(path, &st) < 0) {
- fprintf(stderr, "stat(\"%s\"): %s\n", path, strerror(errno));
- goto finish;
- }
+ if (lstat(path, &st) < 0) {
+ fprintf(stderr, "stat(\"%s\"): %s\n", path, strerror(errno));
+ goto finish;
+ }
- if (S_ISDIR(st.st_mode)) {
-
+ if (S_ISDIR(st.st_mode)) {
+ if (rec) {
if (rm_rf(path, rec) < 0)
- return -1;
+ goto finish;
+ } else {
+ if (rmdir(path) < 0) {
+ fprintf(stderr, "rmdir(\"%s\"): %s\n", path, strerror(errno));
+ goto finish;
+ }
+ }
- continue;
+ } else {
+
+ if (unlink(path) < 0) {
+ fprintf(stderr, "unlink(\"%s\"): %s\n", path, strerror(errno));
+ goto finish;
}
}
-
- if (unlink(path) < 0) {
- fprintf(stderr, "unlink(\"%s\"): %s\n", path, strerror(errno));
- goto finish;
- }
}
if (rmdir(root) < 0) {
@@ -733,35 +732,35 @@ finish:
}
ssize_t loop_read(int fd, void *d, size_t l) {
- void *p = d;
+ uint8_t *p = d;
while (l > 0) {
ssize_t r;
if ((r = read(fd, p, l)) <= 0)
- return p-d > 0 ? p-d : r;
+ return p-(uint8_t*) d > 0 ? p- (uint8_t*)d : r;
p += r;
l -= r;
}
- return p-d;
+ return p-(uint8_t*)d;
}
ssize_t loop_write(int fd, const void *d, size_t l) {
- const void *p = d;
+ const uint8_t *p = d;
while (l > 0) {
ssize_t r;
if ((r = write(fd, p, l)) <= 0)
- return p-d > 0 ? p-d : r;
+ return p-(uint8_t*)d > 0 ? p-(uint8_t*) d : r;
p += r;
l -= r;
}
- return p-d;
+ return p-(uint8_t*) d;
}
char *snprint_off(char *s, size_t l, off_t off) {