llvm.org GIT mirror llvm / 9a7f450
Thread premissions through sys::fs::create_director{y|ies} git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244268 91177308-0d34-0410-b5e6-96231b3b80d8 Frederic Riss 5 years ago
5 changed file(s) with 38 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
284284 /// specific error_code. If IgnoreExisting is false, also returns
285285 /// error if the directory already existed.
286286 std::error_code create_directories(const Twine &path,
287 bool IgnoreExisting = true);
287 bool IgnoreExisting = true,
288 perms Perms = owner_all | group_all);
288289
289290 /// @brief Create the directory in path.
290291 ///
292293 /// @returns errc::success if is_directory(path), otherwise a platform
293294 /// specific error_code. If IgnoreExisting is false, also returns
294295 /// error if the directory already existed.
295 std::error_code create_directory(const Twine &path, bool IgnoreExisting = true);
296 std::error_code create_directory(const Twine &path, bool IgnoreExisting = true,
297 perms Perms = owner_all | group_all);
296298
297299 /// @brief Create a link from \a from to \a to.
298300 ///
784784 "occurred above!");
785785 }
786786
787 std::error_code create_directories(const Twine &Path, bool IgnoreExisting) {
787 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
788 perms Perms) {
788789 SmallString<128> PathStorage;
789790 StringRef P = Path.toStringRef(PathStorage);
790791
791792 // Be optimistic and try to create the directory
792 std::error_code EC = create_directory(P, IgnoreExisting);
793 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
793794 // If we succeeded, or had any error other than the parent not existing, just
794795 // return it.
795796 if (EC != errc::no_such_file_or_directory)
801802 if (Parent.empty())
802803 return EC;
803804
804 if ((EC = create_directories(Parent)))
805 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
805806 return EC;
806807
807 return create_directory(P, IgnoreExisting);
808 return create_directory(P, IgnoreExisting, Perms);
808809 }
809810
810811 std::error_code copy_file(const Twine &From, const Twine &To) {
218218 return std::error_code();
219219 }
220220
221 std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
221 std::error_code create_directory(const Twine &path, bool IgnoreExisting,
222 perms Perms) {
222223 SmallString<128> path_storage;
223224 StringRef p = path.toNullTerminatedStringRef(path_storage);
224225
225 if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
226 if (::mkdir(p.begin(), Perms) == -1) {
226227 if (errno != EEXIST || !IgnoreExisting)
227228 return std::error_code(errno, std::generic_category());
228229 }
181181 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
182182 }
183183
184 std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
184 std::error_code create_directory(const Twine &path, bool IgnoreExisting,
185 perms Perms) {
185186 SmallVector path_utf16;
186187
187188 if (std::error_code ec = widenPath(path, path_utf16))
1717 #ifdef LLVM_ON_WIN32
1818 #include
1919 #include
20 #endif
21
22 #ifdef LLVM_ON_UNIX
23 #include
2024 #endif
2125
2226 using namespace llvm;
457461 errc::file_exists);
458462 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
459463
464 #ifdef LLVM_ON_UNIX
465 // Set a 0000 umask so that we can test our directory permissions.
466 mode_t OldUmask = ::umask(0000);
467
468 fs::file_status Status;
469 ASSERT_NO_ERROR(
470 fs::create_directory(Twine(TestDirectory) + "baz500", false,
471 fs::perms::owner_read | fs::perms::owner_exe));
472 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
473 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
474 fs::perms::owner_read | fs::perms::owner_exe);
475 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
476 fs::perms::all_all));
477 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
478 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
479
480 // Restore umask to be safe.
481 ::umask(OldUmask);
482 #endif
483
460484 #ifdef LLVM_ON_WIN32
461485 // Prove that create_directories() can handle a pathname > 248 characters,
462486 // which is the documented limit for CreateDirectory().