tAvoid chdir() and work with absolute path - pm - barely a pack manager
(HTM) git clone git://z3bra.org/pm
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 6e855748b4844361a463e1e0d3a48f04c45eefdf
(DIR) parent 9aee903d9fa062136b5e605cc0b43e7c03252d49
(HTM) Author: z3bra <willyatmailoodotorg>
Date: Sun, 19 Jun 2016 01:18:18 +0200
Avoid chdir() and work with absolute path
Diffstat:
M pm.c | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
---
(DIR) diff --git a/pm.c b/pm.c
t@@ -76,7 +76,7 @@ int inspect_system(int fd, const char *datadir);
int write_metadata(const char *datadir, struct pack *pack);
int write_entry(struct archive *a, struct archive *w);
int delete_node(char *path);
-int delete_content(char *map, size_t size);
+int delete_content(const char *rootfs, char *map, size_t size);
/* action wrappers around CLI arguments */
int install(const char *rootfs, const char *datadir, char *path);
t@@ -392,7 +392,7 @@ pack_delete(const char *rootfs, const char *datadir, struct pack *p)
{
int fd;
char *addr = NULL;
- char tmp[PATH_MAX], cwd[PATH_MAX];
+ char tmp[PATH_MAX];
struct stat st;
snprintf(tmp, PATH_MAX, "%s/%s/files", datadir, p->name);
t@@ -411,16 +411,9 @@ pack_delete(const char *rootfs, const char *datadir, struct pack *p)
close(fd);
}
- getcwd(cwd, PATH_MAX);
- if (chdir(rootfs) < 0) {
- perror(rootfs);
- close(fd);
- return ERR_DELETE;
- }
-
log(LOG_VERBOSE, "deleting installed files\n");
/* ignore errors so everything gets deleted */
- if (delete_content(addr, st.st_size) < 0) {
+ if (delete_content(rootfs, addr, st.st_size) < 0) {
fprintf(stderr, "%s: pack not removed\n", p->name);
close(fd);
return ERR_DELETE;
t@@ -447,8 +440,6 @@ pack_delete(const char *rootfs, const char *datadir, struct pack *p)
log(LOG_DEBUG, "- %s\n", tmp);
rmdir(tmp);
- chdir(cwd);
-
return 0;
}
t@@ -682,9 +673,10 @@ delete_node(char *path)
* if the entry doesn't exist, it will be ignored
*/
int
-delete_content(char *map, size_t size)
+delete_content(const char *rootfs, char *map, size_t size)
{
char *path = NULL;
+ char tmp[PATH_MAX] = "";
size_t off;
if (size < 1)
t@@ -710,7 +702,8 @@ delete_content(char *map, size_t size)
path = (off < size-1 ? &map[off] + (off>0?1:0) : NULL);
if (path != NULL && strnlen(path, PATH_MAX) > 0) {
- if (delete_node(path) < 0)
+ snprintf(tmp, PATH_MAX, "%s%s", rootfs, path);
+ if (delete_node(tmp) < 0)
return ERR_DELETE;
}
} while (off > 0);