diff options
author | Timo Teräs <timo.teras@iki.fi> | 2022-04-05 10:53:52 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2022-04-05 10:53:52 +0300 |
commit | 191e2d412dfdfd5ac0c49be3752494e0708fa060 (patch) | |
tree | 797350f80cd05aff2c906ef2611909b6c6ff9c8a /src/io.c | |
parent | 232067b3d1399d97b5058440bd8282031053fa7c (diff) | |
download | apk-tools-191e2d412dfdfd5ac0c49be3752494e0708fa060.tar.gz apk-tools-191e2d412dfdfd5ac0c49be3752494e0708fa060.tar.bz2 apk-tools-191e2d412dfdfd5ac0c49be3752494e0708fa060.tar.xz apk-tools-191e2d412dfdfd5ac0c49be3752494e0708fa060.zip |
io: move make_dirs as apk_make_dirs and use it
Diffstat (limited to 'src/io.c')
-rw-r--r-- | src/io.c | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -38,6 +38,21 @@ static inline int atfd_error(int atfd) return atfd < -1 && atfd != AT_FDCWD; } +int apk_make_dirs(int root_fd, const char *dirname, mode_t dirmode, mode_t parentmode) +{ + char parentdir[PATH_MAX], *slash; + + if (faccessat(root_fd, dirname, F_OK, 0) == 0) return 0; + if (mkdirat(root_fd, dirname, dirmode) == 0) return 0; + if (errno != ENOENT || !parentmode) return -1; + + slash = strrchr(dirname, '/'); + if (!slash || slash == dirname || slash-dirname+1 >= sizeof parentdir) return -1; + strlcpy(parentdir, dirname, slash-dirname+1); + if (apk_make_dirs(root_fd, parentdir, parentmode, parentmode) < 0) return -1; + return mkdirat(root_fd, dirname, dirmode); +} + ssize_t apk_write_fully(int fd, const void *ptr, size_t size) { ssize_t i = 0, r; |