Ensure parent folders exist before extract

This commit is contained in:
topjohnwu 2022-08-07 04:06:18 -07:00
parent 07b45f39df
commit 1b4a6850b8
3 changed files with 17 additions and 10 deletions

View File

@ -23,19 +23,21 @@ int fd_pathat(int dirfd, const char *name, char *path, size_t size) {
return 0; return 0;
} }
int mkdirs(string path, mode_t mode) { int mkdirs(const char *path, mode_t mode) {
char buf[4096];
strlcpy(buf, path, sizeof(buf));
errno = 0; errno = 0;
for (char *p = path.data() + 1; *p; ++p) { for (char *p = &buf[1]; *p; ++p) {
if (*p == '/') { if (*p == '/') {
*p = '\0'; *p = '\0';
if (mkdir(path.data(), mode) == -1) { if (mkdir(buf, mode) == -1) {
if (errno != EEXIST) if (errno != EEXIST)
return -1; return -1;
} }
*p = '/'; *p = '/';
} }
} }
if (mkdir(path.data(), mode) == -1) { if (mkdir(buf, mode) == -1) {
if (errno != EEXIST) if (errno != EEXIST)
return -1; return -1;
} }

View File

@ -60,7 +60,7 @@ struct mmap_data : public byte_data {
ssize_t fd_path(int fd, char *path, size_t size); ssize_t fd_path(int fd, char *path, size_t size);
int fd_pathat(int dirfd, const char *name, char *path, size_t size); int fd_pathat(int dirfd, const char *name, char *path, size_t size);
int mkdirs(std::string path, mode_t mode); int mkdirs(const char *path, mode_t mode);
void rm_rf(const char *path); void rm_rf(const char *path);
void mv_path(const char *src, const char *dest); void mv_path(const char *src, const char *dest);
void mv_dir(int src, int dest); void mv_dir(int src, int dest);

View File

@ -1,5 +1,6 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h>
#include <algorithm> #include <algorithm>
#include <base.hpp> #include <base.hpp>
@ -79,17 +80,21 @@ void cpio::extract_entry(const entry_map::value_type &e, const char *file) {
fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file); fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
unlink(file); unlink(file);
rmdir(file); rmdir(file);
// Make sure parent folders exist
char *parent = dirname(file);
xmkdirs(parent, 0755);
if (S_ISDIR(e.second->mode)) { if (S_ISDIR(e.second->mode)) {
::mkdir(file, e.second->mode & 0777); xmkdir(file, e.second->mode & 0777);
} else if (S_ISREG(e.second->mode)) { } else if (S_ISREG(e.second->mode)) {
int fd = creat(file, e.second->mode & 0777); int fd = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, e.second->mode & 0777);
xwrite(fd, e.second->data, e.second->filesize); xwrite(fd, e.second->data, e.second->filesize);
fchown(fd, e.second->uid, e.second->gid); fchown(fd, e.second->uid, e.second->gid);
close(fd); close(fd);
} else if (S_ISLNK(e.second->mode)) { } else if (S_ISLNK(e.second->mode) && e.second->filesize < 4096) {
auto target = strndup((char *) e.second->data, e.second->filesize); char target[4096];
memcpy(target, e.second->data, e.second->filesize);
target[e.second->filesize] = '\0';
symlink(target, file); symlink(target, file);
free(target);
} }
} }