summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-06-05 20:14:45 +0000
committerLennart Poettering <lennart@poettering.net>2005-06-05 20:14:45 +0000
commitb2972e79c5d11dfa0625bf43838cb65209f7e632 (patch)
treeee45296a668255b7ad63dfc0e0963209a7299093
parent37621a019f8fb5e0ac2e506b45279ab7ba67a167 (diff)
* cleanup trash recursively
* ignore some more files * compile as C99 with -pedantic * upgrade to Berkeley DB 4.3 * add some tests for rm_rf() and copy_fd() * remove some minor warnings * minor fixes git-svn-id: file:///home/lennart/svn/public/syrep/trunk@76 07ea20a6-d2c5-0310-9e02-9ef735347d72
-rwxr-xr-xbootstrap.sh2
-rw-r--r--configure.ac16
-rw-r--r--src/Makefile.am8
-rw-r--r--src/cleanup.c2
-rw-r--r--src/context.c2
-rw-r--r--src/copy-fd-test.c72
-rw-r--r--src/dbutil.c2
-rw-r--r--src/extract.c2
-rw-r--r--src/list.c4
-rw-r--r--src/md5util.c10
-rw-r--r--src/md5util.h7
-rw-r--r--src/merge.c2
-rw-r--r--src/package.c29
-rw-r--r--src/rm-rf-test.c40
-rw-r--r--src/syrep.c15
-rw-r--r--src/update.c2
-rw-r--r--src/util.c95
17 files changed, 216 insertions, 94 deletions
diff --git a/bootstrap.sh b/bootstrap.sh
index b3b326e..784a9d7 100755
--- a/bootstrap.sh
+++ b/bootstrap.sh
@@ -39,7 +39,7 @@ else
run_versioned automake 1.7 -a -c
autoconf -Wall
- CFLAGS="-g -O0" ./configure --sysconfdir=/etc "$@"
+ CFLAGS="-g -O1" ./configure --sysconfdir=/etc "$@"
make clean
fi
diff --git a/configure.ac b/configure.ac
index 11e59c3..2ada65d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -33,7 +33,11 @@ AC_SUBST(PACKAGE_URL, [http://0pointer.de/lennart/projects/syrep/])
# If using GCC specifiy some additional parameters
if test "x$GCC" = "xyes" ; then
- CFLAGS="$CFLAGS -pipe -Wall -W -Wno-unused-parameter"
+ CFLAGS="$CFLAGS -pipe -W -Wall -pedantic"
+
+ AC_LANG_CONFTEST([int main() {}])
+ $CC -c conftest.c -std=c99 -Wno-unused-parameter $CFLAGS > /dev/null 2> /dev/null && CFLAGS="$CFLAGS -std=c99 -Wno-unused-parameter"
+ rm -f conftest.o
fi
if type -p stow > /dev/null && test -d /usr/local/stow ; then
@@ -46,21 +50,21 @@ AC_CHECK_LIB([z], [inflate],, [AC_MSG_ERROR([*** Sorry, you have to install zlib
AC_CHECK_HEADER([zlib.h],, [AC_MSG_ERROR([*** Sorry, you have to install the zlib headers ***])])
# Check for Berkeley DB (needs to be improved)
-AC_CHECK_HEADER([db.h],, [AC_MSG_ERROR([*** Sorry, you have to install the Berkeley Database Library (libdb) 4.2 or newer ***])])
-AC_CHECK_LIB([db], [db_create_4002],, [AC_MSG_ERROR([*** Sorry, you have to install the Berkeley Database Library (libdb) 4.2 or newer ***])])
+AC_CHECK_HEADER([db.h],, [AC_MSG_ERROR([*** Sorry, you have to install the Berkeley Database Library (libdb) 4.3 or newer ***])])
+AC_CHECK_LIB([db], [__db_db_create_4003],, [AC_MSG_ERROR([*** Sorry, you have to install the Berkeley Database Library (libdb) 4.3 or newer ***])])
LIBS="$LIBS -ldb"
# Checking libdb version number
-AC_MSG_CHECKING([for Berkeley libdb 4.2])
+AC_MSG_CHECKING([for Berkeley libdb 4.3])
AC_LANG_PUSH(C)
AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
#include <db.h>
-#if (DB_VERSION_MAJOR != 4) || (DB_VERSION_MINOR != 2)
+#if (DB_VERSION_MAJOR != 4) || (DB_VERSION_MINOR != 3)
#error "foo"
#endif
]])],
[AC_MSG_RESULT([yes])],
-[AC_MSG_FAILURE([Berkeley libdb 4.2 required])])
+[AC_MSG_FAILURE([Berkeley libdb 4.3 required])])
AC_LANG_POP(C)
# Check for Linux sendfile()
diff --git a/src/Makefile.am b/src/Makefile.am
index fb877d3..d3911b1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,8 +16,11 @@
# along with syrep; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+AM_CPPFLAGS=-I../lib -D_GNU_SOURCE
+
bin_PROGRAMS = syrep
-AM_CPPFLAGS=-I../lib
+
+noinst_PROGRAMS = rm-rf-test copy-fd-test
syrep_SOURCES = cache.c cache.h \
update.c update.h \
@@ -43,6 +46,9 @@ syrep_SOURCES = cache.c cache.h \
forget.c forget.h
syrep_LDADD=@LIBOBJS@
+rm_rf_test_SOURCES = util.c rm-rf-test.c util.h cmdline.c cmdline.h
+copy_fd_test_SOURCES = util.c copy-fd-test.c util.h cmdline.c cmdline.h
+
EXTRA_DIST = syrep.ggo getopt.c getopt1.c mkdtemp.c
MAINTAINERCLEANFILES =
BUILT_SOURCES =
diff --git a/src/cleanup.c b/src/cleanup.c
index 447697d..f362240 100644
--- a/src/cleanup.c
+++ b/src/cleanup.c
@@ -40,7 +40,7 @@ int cleanup(const char *root) {
snprintf(p, sizeof(p), "%s/.syrep/" SYREP_TRASHDIR, root);
- if (rm_rf(p, 0) < 0)
+ if (rm_rf(p, 1) < 0)
return -1;
if (args.verbose_flag)
diff --git a/src/context.c b/src/context.c
index 9fe37bb..6611d03 100644
--- a/src/context.c
+++ b/src/context.c
@@ -118,7 +118,7 @@ struct syrep_db_context* db_context_open(const char *filename, int force) {
assert(k);
if ((f = fopen(path, "r"))) {
- if (fscanf(f, "%i", &c->timestamp) != 1)
+ if (fscanf(f, "%u", &c->timestamp) != 1)
c->timestamp = 0;
fclose(f);
}
diff --git a/src/copy-fd-test.c b/src/copy-fd-test.c
new file mode 100644
index 0000000..76e0572
--- /dev/null
+++ b/src/copy-fd-test.c
@@ -0,0 +1,72 @@
+/* $Id$ */
+
+/***
+ This file is part of syrep.
+
+ syrep is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ syrep is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+ License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with syrep; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+
+#include "util.h"
+#include "cmdline.h"
+
+struct gengetopt_args_info args;
+
+int interrupted;
+
+int main(int argc, char *argv[]) {
+ int fda = -1, fdb = -1, r = -1;
+ off_t l;
+
+ assert(argc >= 3);
+
+ if ((fda = open(argv[1], O_RDONLY)) < 0 ||
+ (fdb = open(argv[2], O_RDWR|O_EXCL|O_CREAT, 0666)) < 0) {
+ fprintf(stderr, "open(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ if ((l = filesize(fda)) == (off_t) -1) {
+ fprintf(stderr, "filesize(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ if (copy_fd(fda, fdb, l) < 0) {
+ fprintf(stderr, "copy_fd(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ r = 0;
+
+
+finish:
+
+ if (fda >= 0)
+ close(fda);
+
+ if (fdb >= 0)
+ close(fdb);
+
+ return r;
+}
diff --git a/src/dbutil.c b/src/dbutil.c
index b13f244..fee6ab1 100644
--- a/src/dbutil.c
+++ b/src/dbutil.c
@@ -240,7 +240,7 @@ static uint32_t csum_name(const struct syrep_name *name) {
assert(name);
a = adler32(0, NULL, 0);
- a = adler32(a, name->path, strlen(name->path));
+ a = adler32(a, (uint8_t*) name->path, strlen(name->path));
/*fprintf(stderr, "csum: %s -> %u\n", name->path, a);*/
diff --git a/src/extract.c b/src/extract.c
index 2e63d14..35ffd9c 100644
--- a/src/extract.c
+++ b/src/extract.c
@@ -42,7 +42,7 @@ static int cb(struct package *p, const char *name, const char *path, void *u) {
if (errno == ENOENT)
return 0;
- fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno));
+ fprintf(stderr, "access(%s) failed: %s\n", path, strerror(errno));
return -1;
}
diff --git a/src/list.c b/src/list.c
index f1763eb..8db5906 100644
--- a/src/list.c
+++ b/src/list.c
@@ -111,7 +111,7 @@ static int sort_entry_cmp(const void *_a, const void *_b) {
return 1;
return strncmp(a->name.path, b->name.path, PATH_MAX);
-};
+}
int list(struct syrep_db_context *c) {
int r = -1, ret;
@@ -182,7 +182,7 @@ int list(struct syrep_db_context *c) {
DB_BTREE_STAT *statp;
int ret;
- if ((ret = c->db_id_meta->stat(c->db_id_meta, &statp, 0)) != 0)
+ if ((ret = c->db_id_meta->stat(c->db_id_meta, NULL, &statp, DB_FAST_STAT)) != 0)
break;
m_sort_array = statp->bt_ndata;
diff --git a/src/md5util.c b/src/md5util.c
index a6009e3..209f0bb 100644
--- a/src/md5util.c
+++ b/src/md5util.c
@@ -35,9 +35,9 @@
#include "md5.h"
#include "syrep.h"
-void fhex(const unsigned char *bin, int len, char *txt) {
+void fhex(const uint8_t *bin, size_t len, char *txt) {
static const char hex[] = "0123456789abcdef";
- int i;
+ size_t i;
for (i = 0; i < len; i++) {
txt[i*2] = hex[bin[i]>>4];
@@ -48,7 +48,7 @@ void fhex(const unsigned char *bin, int len, char *txt) {
#define MMAPSIZE (100*1024*1024)
#define BUFSIZE (1024*1024)
-int fdmd5(int fd, off_t l, char *md) {
+int fdmd5(int fd, off_t l, uint8_t md[]) {
void *d;
off_t o = 0;
size_t m;
@@ -71,7 +71,7 @@ int fdmd5(int fd, off_t l, char *md) {
m = l < MMAPSIZE ? l : MMAPSIZE;
- while (l && ((d = mmap(NULL, m, PROT_READ, MAP_SHARED, fd, o)) != MAP_FAILED)) {
+ while (l > 0 && ((d = mmap(NULL, m, PROT_READ, MAP_SHARED, fd, o)) != MAP_FAILED)) {
md5_append(&s, d, m);
munmap(d, m);
@@ -136,7 +136,7 @@ finish:
return r;
}
-int fmd5(const char *fn, char *md) {
+int fmd5(const char *fn, uint8_t md[]) {
int fd = -1, r = -1;
if ((fd = open(fn, O_RDONLY)) < 0) {
diff --git a/src/md5util.h b/src/md5util.h
index 373d0b8..9755b0f 100644
--- a/src/md5util.h
+++ b/src/md5util.h
@@ -22,12 +22,13 @@
***/
#include <sys/types.h>
+#include <inttypes.h>
-void fhex(const unsigned char *bin, int len, char *txt);
+void fhex(const uint8_t *bin, size_t len, char *txt);
#define fhex_md5(bin,txt) fhex((bin),16,(txt))
-int fdmd5(int fd, off_t l, char *md);
+int fdmd5(int fd, off_t l, uint8_t md[]);
-int fmd5(const char *fn, char *md);
+int fmd5(const char *fn, uint8_t md[]);
#endif
diff --git a/src/merge.c b/src/merge.c
index a6c303d..581e8b9 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -377,7 +377,7 @@ int merge(struct syrep_db_context *c1, struct syrep_db_context *c2, const char*
goto finish;
if (!args.keep_trash_flag)
- if (rm_rf(cb_info.trash_dir, 0) < 0)
+ if (rm_rf(cb_info.trash_dir, 1) < 0)
goto finish;
r = 0;
diff --git a/src/package.c b/src/package.c
index fa4346a..1fc9a07 100644
--- a/src/package.c
+++ b/src/package.c
@@ -59,7 +59,7 @@ struct package {
int count;
int read_fd, write_fd;
z_stream read_z, write_z;
- void *read_zbuf, *write_zbuf;
+ uint8_t *read_zbuf, *write_zbuf;
int x_endianess;
int compressed;
struct package_item *items;
@@ -116,7 +116,7 @@ static ssize_t package_read(struct package *p, void *d, size_t l) {
}
if (!n) {
- r = (void*) p->read_z.next_out - d;
+ r = (uint8_t*) p->read_z.next_out - (uint8_t*) d;
goto finish;
}
@@ -127,7 +127,7 @@ static ssize_t package_read(struct package *p, void *d, size_t l) {
if ((z = inflate(&p->read_z, 0)) != Z_OK) {
if (z == Z_STREAM_END) {
- r = (void*) p->read_z.next_out - d;
+ r = (uint8_t*) p->read_z.next_out - (uint8_t*) d;
goto finish;
}
@@ -174,7 +174,7 @@ static ssize_t package_write(struct package *p, void *d, size_t l) {
goto finish;
}
- t = (void*) p->write_z.next_out - p->write_zbuf;
+ t = (uint8_t*) p->write_z.next_out - p->write_zbuf;
if (t) {
ssize_t n;
@@ -184,8 +184,8 @@ static ssize_t package_write(struct package *p, void *d, size_t l) {
goto finish;
}
- if (n != t) {
- if ((r = (void*) p->write_z.next_in - d) > 0)
+ if ((size_t) n != t) {
+ if ((r = (uint8_t*) p->write_z.next_in - (uint8_t*) d) > 0)
r --;
goto finish;
@@ -215,7 +215,7 @@ static int copy_deflate(struct package *p, int sfd, off_t l) {
size_t t = MIN(l, CBUFSIZE);
ssize_t n;
- if ((n = loop_read(sfd, buf, t)) != t) {
+ if ((n = loop_read(sfd, buf, t)) != (ssize_t) t) {
fprintf(stderr, "read() : %s\n", n < 0 ? strerror(errno) : "EOF");
goto finish;
}
@@ -245,18 +245,18 @@ static int copy_inflate(struct package *p, int dfd, off_t l) {
}
while (l > 0) {
- size_t t = MIN(l, CBUFSIZE);
+ size_t t = (size_t) (MIN(l, CBUFSIZE));
ssize_t n;
- if (package_read(p, buf, t) != t)
+ if (package_read(p, buf, t) != (ssize_t) t)
goto finish;
- if ((n = loop_write(dfd, buf, t)) != t) {
+ if ((n = loop_write(dfd, buf, t)) != (ssize_t) t) {
fprintf(stderr, "write() : %s\n", n < 0 ? strerror(errno) : "EOF");
goto finish;
}
- l -= n;
+ l -= t;
}
r = 0;
@@ -361,9 +361,9 @@ static int close_write_fd(struct package *p) {
z = deflate(&p->write_z, Z_FINISH);
- t = (void*) p->write_z.next_out - p->write_zbuf;
+ t = (uint8_t*) p->write_z.next_out - p->write_zbuf;
if (t) {
- if ((n = loop_write(p->write_fd, p->write_zbuf, t)) != t) {
+ if ((n = loop_write(p->write_fd, p->write_zbuf, t)) != (ssize_t) t) {
fprintf(stderr, "loop_write(): %s\n", n < 0 ? strerror(errno) : "EOF");
r = -1;
break;
@@ -499,7 +499,6 @@ static int write_item(struct package *p, struct package_item *i) {
goto fail;
}
-
if ((r = package_write(p, &l, 8)) != 8) {
if (r >= 0)
fprintf(stderr, "Short write\n");
@@ -596,8 +595,6 @@ struct package* package_open(const char *fn, int force) {
fprintf(stderr, "zlib initialisation failure\n");
goto finish;
}
-
-
}
diff --git a/src/rm-rf-test.c b/src/rm-rf-test.c
new file mode 100644
index 0000000..990c095
--- /dev/null
+++ b/src/rm-rf-test.c
@@ -0,0 +1,40 @@
+/* $Id$ */
+
+/***
+ This file is part of syrep.
+
+ syrep is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ syrep is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+ License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with syrep; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+
+#include "util.h"
+#include "cmdline.h"
+
+struct gengetopt_args_info args;
+
+int interrupted;
+
+
+int main(int argc, char *argv[]) {
+
+ assert(argc >= 2);
+
+ rm_rf(argv[1], 1);
+}
diff --git a/src/syrep.c b/src/syrep.c
index 7325178..c9ad001 100644
--- a/src/syrep.c
+++ b/src/syrep.c
@@ -256,7 +256,8 @@ finish:
static int do_foreach(int (*func) (struct syrep_db_context *c), int m) {
char *path = NULL;
- int r = 1, i;
+ int r = 1;
+ unsigned i;
struct syrep_db_context *c = NULL;
if (args.inputs_num < 1)
@@ -328,7 +329,8 @@ finish:
static int do_update(void) {
char *path = NULL;
- int r = 1, i;
+ int r = 1;
+ unsigned i;
struct syrep_db_context *c = NULL;
struct syrep_md_cache *cache = NULL;
@@ -424,7 +426,8 @@ finish:
}
static int do_cleanup(void) {
- int r = 1, i;
+ int r = 1;
+ unsigned i;
if (args.inputs_num < 1)
fprintf(stderr, "WARNING: No repository specified!\n");
@@ -455,7 +458,8 @@ finish:
static int do_forget(void) {
char *path = NULL;
- int r = 1, i;
+ int r = 1;
+ unsigned i;
struct syrep_db_context *c = NULL, *target = NULL;
if (args.inputs_num < 1)
@@ -519,7 +523,7 @@ static void sigint(int s) {
}
static void free_args(void) {
- int i;
+ unsigned i;
for (i = 0; i < args.inputs_num; i++)
free(args.inputs[i]);
@@ -527,7 +531,6 @@ static void free_args(void) {
free(args.inputs);
}
-
static int help(const char *argv0) {
fprintf(stderr,
diff --git a/src/update.c b/src/update.c
index 5cc176e..69244e1 100644
--- a/src/update.c
+++ b/src/update.c
@@ -204,7 +204,7 @@ static int iterate_dir(struct syrep_db_context *c, struct syrep_md_cache *cache,
normalize_path(p);
- if (stat(p, &st) < 0) {
+ if (lstat(p, &st) < 0) {
fprintf(stderr, "stat(%s) failed: %s\n", p, strerror(errno));
continue;
}
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) {