From 221439164eb6683a5af35540b24b5620e5d8ab19 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 8 Apr 2018 22:12:08 -0500 Subject: Add mkpath() for common case of mkpathat(), and #define magic constants. --- lib/lib.c | 6 ++++++ lib/lib.h | 4 ++++ toys/pending/mdev.c | 3 +-- toys/pending/tar.c | 2 +- toys/posix/cpio.c | 2 +- toys/posix/patch.c | 3 +-- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index ea678011..27d14be7 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -204,6 +204,12 @@ int mkpathat(int atfd, char *dir, mode_t lastmode, int flags) return 0; } +// The common case +int mkpath(char *dir) +{ + return mkpathat(AT_FDCWD, dir, 0, MKPATHAT_MAKE); +} + // Split a path into linked list of components, tracking head and tail of list. // Filters out // entries with no contents. struct string_list **splitpath(char *path, struct string_list **list) diff --git a/lib/lib.h b/lib/lib.h index 676c47b3..ccf342cc 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -191,7 +191,11 @@ void perror_exit_raw(char *msg); ssize_t readall(int fd, void *buf, size_t len); ssize_t writeall(int fd, void *buf, size_t len); off_t lskip(int fd, off_t offset); +#define MKPATHAT_MKLAST 1 +#define MKPATHAT_MAKE 2 +#define MKPATHAT_VERBOSE 4 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags); +int mkpath(char *dir); struct string_list **splitpath(char *path, struct string_list **list); char *readfileat(int dirfd, char *name, char *buf, off_t *len); char *readfile(char *name, char *buf, off_t len); diff --git a/toys/pending/mdev.c b/toys/pending/mdev.c index 9ee1f642..cab51e11 100644 --- a/toys/pending/mdev.c +++ b/toys/pending/mdev.c @@ -187,8 +187,7 @@ found_device: return; } - if (strchr(device_name, '/')) - mkpathat(AT_FDCWD, toybuf, 0, 2); + if (strchr(device_name, '/')) mkpath(toybuf); if (mknod(toybuf, mode | type, dev_makedev(major, minor)) && errno != EEXIST) perror_exit("mknod %s failed", toybuf); diff --git a/toys/pending/tar.c b/toys/pending/tar.c index 4d6979db..c2946225 100644 --- a/toys/pending/tar.c +++ b/toys/pending/tar.c @@ -378,7 +378,7 @@ static void extract_to_disk(struct archive_handler *tar) if (file_hdr->name[flags-1] == '/') file_hdr->name[flags-1] = 0; //Regular file with preceding path if ((s = strrchr(file_hdr->name, '/'))) { - if (mkpathat(AT_FDCWD, file_hdr->name, 00, 2) && errno !=EEXIST) { + if (mkpath(file_hdr->name) && errno !=EEXIST) { error_msg(":%s: not created", file_hdr->name); return; } diff --git a/toys/posix/cpio.c b/toys/posix/cpio.c index 90c8107f..6ce3ef1c 100644 --- a/toys/posix/cpio.c +++ b/toys/posix/cpio.c @@ -134,7 +134,7 @@ void cpio_main(void) if (toys.optflags & (FLAG_t|FLAG_v)) puts(name); - if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) { + if (!test && strrchr(name, '/') && mkpath(name)) { perror_msg("mkpath '%s'", name); test++; } diff --git a/toys/posix/patch.c b/toys/posix/patch.c index fbad1fb9..181af2a1 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -397,8 +397,7 @@ void patch_main(void) if ((!strcmp(oldname, "/dev/null") || !oldsum) && access(name, F_OK)) { printf("creating %s\n", name); - if (mkpathat(AT_FDCWD, name, 0, 2)) - perror_exit("mkpath %s", name); + if (mkpath(name)) perror_exit("mkpath %s", name); TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666); } else { printf("patching %s\n", name); -- cgit v1.2.3