- char *dir = dirents[q->dir_index].entry->name;
-
- for (char *sep = dir;; ++sep) {
- if (*sep == '\\' || *sep == 0) {
- char temp = *sep;
- *sep = 0;
-
- if (strlen(dir) > 0) {
- int ret;
-
- if (debug)
- printf("Creating directory '%s': ", dir);
-
- if (!dirents[q->dir_index].created && mkdirat(dir_fd, dir, 0755) != 0) {
- if (errno != EEXIST) {
- *sep = temp;
- fprintf(stderr, "Error %d: cannot create directory %s\n", errno, dir);
+ if (!dirents[q->dir_index].created) {
+ char *component = dir_name;
+ bool last_component = false;
+ for (char *sep = dir_name;; ++sep) {
+ if (*sep == '/' || *sep == 0) {
+ if (*sep == 0)
+ last_component = true;
+ char temp = *sep;
+ *sep = 0;
+ if (strlen(component) > 0) {
+ if (debug) printf("Component: '%s' %d bytes @ %p\n", component, strlen(component), component);
+ int ret = mkdirat(dir_fd, component, 0755);
+ if (ret == 0 || (ret == -1 && errno == EEXIST)) {
+ if (debug)
+ printf("Directory '%s' %s\n", component, ret == 0 ? "created" : "already exists");
+ dir_fd_last = dir_fd;
+ dir_fd = openat(dir_fd_last, component, O_DIRECTORY);
+ close(dir_fd_last);
+ if (dir_fd == -1) {
+ fprintf(stderr, "Error %d: cannot open directory '%s': ", errno, component);
+ perror(NULL);
+ *sep = temp;
+ break; // cannot continue if unable to open a path component
+ } else if (last_component == true) {
+ dirents[q->dir_index].created = true;
+ }
+ } else {
+ fprintf(stderr, "Error %d: cannot create directory '%s': ", errno, component);