pm

barely a pack manager
git clone git://z3bra.org/pm
Log | Files | Refs | README | LICENSE

commit cce6b1cec50531e60e5e20a0eb14ae914446bf50
parent bb81e744a877d4cc21bc2ddee1e15a9da906d058
Author: z3bra <willyatmailoodotorg>
Date:   Fri Jan  8 08:45:44 2016

Make deletion process more robust regarding file checking

Diffstat:
 pm.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/pm.c b/pm.c @@ -229,7 +229,7 @@ list_local(int fd, char *datadir) if ((meta = open(tmp, O_RDONLY)) < 0) { perror("open"); closedir(d); - return (1); + return 1; } len = read(meta, tmp, 32); tmp[len - 1] = 0; @@ -353,7 +353,6 @@ unpack(char *root, char *in) return r; } - printf("+ %s\n", archive_entry_pathname(e)); r = archive_write_header(disk, e); if (r != ARCHIVE_OK) return r; @@ -385,7 +384,7 @@ delete_content(FILE *f) /* remove trailing '\n' */ if ((len = strnlen(file, PATH_MAX)) == 0) - return -1; + return 0; file[len - 1] = 0; len--; @@ -396,14 +395,22 @@ delete_content(FILE *f) len--; } - /* if file doesn't exist anymore, it's all good :) */ - if (stat(file, &st) < 0 && errno == ENOENT) + /* + * if file doesn't exist anymore, it's all good :) + * we use lstat here so that dangling links can be delt with too + */ + if (lstat(file, &st) < 0 && errno == ENOENT) return 0; if (S_ISDIR(st.st_mode) && is_empty(file)) return rmdir(file); - return unlink(file); + if (unlink(file) < 0) { + perror("unlink"); + return -1; + } + + return 0; } @@ -413,12 +420,12 @@ delete_content(FILE *f) int delete(const char *datadir, const char *rootfs, const char *name) { - char *cwd; FILE *f; char meta[PATH_MAX]; snprintf(meta, PATH_MAX, "%s/%s/version", datadir, name); - unlink(meta); + if (unlink(meta) < 0) + return ERR_DELETE; snprintf(meta, PATH_MAX, "%s/%s/files", datadir, name); if ((f = fopen(meta, "r")) == NULL) { @@ -426,22 +433,15 @@ delete(const char *datadir, const char *rootfs, const char *name) return -1; } - /* hack to allow relative path for metadata */ - cwd = getcwd(NULL, PATH_MAX); if (chdir(rootfs) < 0) { perror("chdir"); return -1; } - if (delete_content(f) < 0) - return ERR_DELETE; - + /* ignore errors so everything gets deleted */ + delete_content(f); fclose(f); - /* hack again */ - chdir(cwd); - free(cwd); - if (unlink(meta) < 0) return ERR_DELETE;