bool POSIXFilesystemNode::create(bool isDir) { bool success; if (isDir) { success = mkdir_norecurse(_path.c_str()); } else { int fd = open(_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0755); success = fd >= 0; if (fd >= 0) { close(fd); } } if (success) { setFlags(); if (_isValid) { if (_isDirectory != isDir) warning("failed to create %s: got %s", isDir ? "directory" : "file", _isDirectory ? "directory" : "file"); return _isDirectory == isDir; } warning("POSIXFilesystemNode: %s() was a success, but stat indicates there is no such %s", isDir ? "mkdir" : "creat", isDir ? "directory" : "file"); return false; } return false; }
bool assureDirectoryExists(const Common::String &dir, const char *prefix) { // Check whether the prefix exists if one is supplied. if (prefix) { if (!path_is_valid(prefix)) return false; else if (!path_is_directory(prefix)) return false; } // Obtain absolute path. Common::String path; if (prefix) { path = prefix; path += '/'; path += dir; } else { path = dir; } path = Common::normalizePath(path, '/'); const Common::String::iterator end = path.end(); Common::String::iterator cur = path.begin(); if (*cur == '/') ++cur; do { if (cur + 1 != end) { if (*cur != '/') continue; // It is kind of ugly and against the purpose of Common::String to // insert 0s inside, but this is just for a local string and // simplifies the code a lot. *cur = '\0'; } if (!mkdir_norecurse(path.c_str())) { if (errno == EEXIST) { if (!path_is_valid(path.c_str())) return false; else if (!path_is_directory(path.c_str())) return false; } else return false; } *cur = '/'; } while (cur++ != end); return true; }
/** * path_mkdir: * @dir : directory * * Create directory on filesystem. * * Returns: true (1) if directory could be created, otherwise false (0). **/ bool path_mkdir(const char *dir) { const char *target = NULL; /* Use heap. Real chance of stack overflow if we recurse too hard. */ char *basedir = strdup(dir); bool ret = false; if (!basedir) return false; path_parent_dir(basedir); if (!*basedir || !strcmp(basedir, dir)) goto end; if (path_is_directory(basedir)) { target = dir; ret = mkdir_norecurse(dir); } else { target = basedir; ret = path_mkdir(basedir); if (ret) { target = dir; ret = mkdir_norecurse(dir); } } end: if (target && !ret) printf("Failed to create directory: \"%s\".\n", target); free(basedir); return ret; }