static int _create_dir_recursive(const char *dir) { char *orig, *s; int rc, r = 0; log_verbose("Creating directory \"%s\"", dir); /* Create parent directories */ orig = s = strdup(dir); if (!s) { log_error("Failed to duplicate directory name."); return 0; } while ((s = strchr(s, '/')) != NULL) { *s = '\0'; if (*orig) { rc = mkdir(orig, 0777); if (rc < 0) { if (errno == EEXIST) { if (!_is_dir(orig)) goto_out; } else { if (errno != EROFS) log_sys_error("mkdir", orig); goto out; } } } *s++ = '/'; } /* Create final directory */ rc = mkdir(dir, 0777); if (rc < 0) { if (errno == EEXIST) { if (!_is_dir(dir)) goto_out; } else { if (errno != EROFS) log_sys_error("mkdir", orig); goto out; } } r = 1; out: free(orig); return r; }
int pmixp_fixrights(char *path, uid_t uid, mode_t mode) { char nested_path[PATH_MAX]; DIR *dp; struct dirent *ent; int rc; /* * Make sure that "directory" exists and is a directory. */ if (1 != (rc = _is_dir(path))) { PMIXP_ERROR("path=\"%s\" is not a directory", path); return (rc == 0) ? -1 : rc; } if ((dp = opendir(path)) == NULL) { PMIXP_ERROR_STD("cannot open path=\"%s\"", path); return -1; } while ((ent = readdir(dp)) != NULL) { if (0 == xstrcmp(ent->d_name, ".") || 0 == xstrcmp(ent->d_name, "..")) { /* skip special dir's */ continue; } snprintf(nested_path, sizeof(nested_path), "%s/%s", path, ent->d_name); if (_is_dir(nested_path)) { if ((rc = _file_fix_rights(nested_path, uid, mode))) { PMIXP_ERROR_STD("cannot fix permissions for " "\"%s\"", nested_path); return -1; } pmixp_rmdir_recursively(nested_path); } else { if ((rc = _file_fix_rights(nested_path, uid, mode))) { PMIXP_ERROR_STD("cannot fix permissions for " "\"%s\"", nested_path); return -1; } } } closedir(dp); return 0; }
int pmixp_rmdir_recursively(char *path) { char nested_path[PATH_MAX]; DIR *dp; struct dirent *ent; int rc; /* * Make sure that "directory" exists and is a directory. */ if (1 != (rc = _is_dir(path))) { PMIXP_ERROR("path=\"%s\" is not a directory", path); return (rc == 0) ? -1 : rc; } if ((dp = opendir(path)) == NULL) { PMIXP_ERROR_STD("cannot open path=\"%s\"", path); return -1; } while ((ent = readdir(dp)) != NULL) { if (0 == strcmp(ent->d_name, ".") || 0 == strcmp(ent->d_name, "..")) { /* skip special dir's */ continue; } snprintf(nested_path, sizeof(nested_path), "%s/%s", path, ent->d_name); if (_is_dir(nested_path)) { pmixp_rmdir_recursively(nested_path); } else { unlink(nested_path); } } closedir(dp); if ((rc = rmdir(path))) { PMIXP_ERROR_STD("Cannot remove path=\"%s\"", path); } return rc; }
fs_t* fs_t::create(const std::string& dd) { std::string data_directory; if(!dd.size()) data_directory = "."; else if((dd[dd.size()-1] == '/') || (dd[dd.size()-1] == '\\')) data_directory = std::string(dd,0,dd.size()-1); else data_directory = dd; data_directory += '/'; if(!_is_dir(data_directory.c_str())) data_error(dd << " is not a directory"); return new fs_t(data_directory); }
bool fs_t::is_dir(const std::string& path) const { return _is_dir(canocial(path).c_str()); }