aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2016-07-25 00:08:53 +0530
committerMike Frysinger <vapier@gentoo.org>2016-11-12 02:10:03 -0500
commite21ad3cd0055f90cc01f43d7a7357d1fabdbc5fa (patch)
treec3a56d78917cb0bbe614dd4f93cd6d9b1ddde0b8 /paxinc.c
parentlddtree.py: work around pyelftools API change (diff)
downloadpax-utils-e21ad3cd0055f90cc01f43d7a7357d1fabdbc5fa.tar.gz
pax-utils-e21ad3cd0055f90cc01f43d7a7357d1fabdbc5fa.tar.bz2
pax-utils-e21ad3cd0055f90cc01f43d7a7357d1fabdbc5fa.zip
split out fs related helper funcs as lib code
This way we can use the funcs in other modules. It makes scanelf a little bigger (~1k), but shouldn't be a big deal overall.
Diffstat (limited to 'paxinc.c')
-rw-r--r--paxinc.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/paxinc.c b/paxinc.c
index 068aa60..64a3069 100644
--- a/paxinc.c
+++ b/paxinc.c
@@ -167,3 +167,34 @@ void color_init(bool disable)
if (disable)
NORM = RED = YELLOW = "";
}
+
+/* File system helpers. */
+int root_fd = AT_FDCWD;
+
+FILE *fopenat_r(int dir_fd, const char *path)
+{
+ int fd = openat(dir_fd, path, O_RDONLY|O_CLOEXEC);
+ if (fd == -1)
+ return NULL;
+ return fdopen(fd, "re");
+}
+
+const char *root_rel_path(const char *path)
+{
+ /*
+ * openat() will ignore the dirfd if path starts with
+ * a /, so consume all of that noise
+ *
+ * XXX: we don't handle relative paths like ../ that
+ * break out of the --root option, but for now, just
+ * don't do that :P.
+ */
+ if (root_fd != AT_FDCWD) {
+ while (*path == '/')
+ ++path;
+ if (*path == '\0')
+ path = ".";
+ }
+
+ return path;
+}