[llvm] r185059 - Add a convenience createUniqueDirectory function.
Rafael Espindola
rafael.espindola at gmail.com
Wed Jun 26 20:45:32 PDT 2013
Author: rafael
Date: Wed Jun 26 22:45:31 2013
New Revision: 185059
URL: http://llvm.org/viewvc/llvm-project?rev=185059&view=rev
Log:
Add a convenience createUniqueDirectory function.
There are a few valid situation where we care about the structure inside a
directory, but not about the directory itself. A simple example is for unit
testing directory traversal.
PathV1 had a function like this, add one to V2 and port existing users of the
created temp file and delete it hack to using it.
Modified:
llvm/trunk/include/llvm/Support/FileSystem.h
llvm/trunk/lib/Support/Path.cpp
llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
llvm/trunk/unittests/Support/Path.cpp
Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=185059&r1=185058&r2=185059&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Wed Jun 26 22:45:31 2013
@@ -568,11 +568,16 @@ error_code status_known(const Twine &pat
/// otherwise a platform specific error_code.
error_code unique_file(const Twine &model, int &result_fd,
SmallVectorImpl<char> &result_path,
- bool makeAbsolute = true, unsigned mode = owner_read | owner_write);
+ bool makeAbsolute = true,
+ unsigned mode = owner_read | owner_write);
/// @brief Simpler version for clients that don't want an open file.
error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
- bool MakeAbsolute = true, unsigned Mode = owner_read | owner_write);
+ bool MakeAbsolute = true,
+ unsigned Mode = owner_read | owner_write);
+
+error_code createUniqueDirectory(const Twine &Prefix,
+ SmallVectorImpl<char> &ResultPath);
/// @brief Canonicalize path.
///
Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=185059&r1=185058&r2=185059&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Wed Jun 26 22:45:31 2013
@@ -642,6 +642,17 @@ error_code unique_file(const Twine &Mode
return fs::remove(P);
}
+error_code createUniqueDirectory(const Twine &Prefix,
+ SmallVectorImpl<char> &ResultPath) {
+ // FIXME: This is double inefficient. We compute a unique file name, created
+ // it, delete it and keep only the directory.
+ error_code EC = unique_file(Prefix + "-%%%%%%/dummy", ResultPath);
+ if (EC)
+ return EC;
+ path::remove_filename(ResultPath);
+ return error_code::success();
+}
+
error_code make_absolute(SmallVectorImpl<char> &path) {
StringRef p(path.data(), path.size());
Modified: llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FileOutputBufferTest.cpp?rev=185059&r1=185058&r2=185059&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/FileOutputBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/FileOutputBufferTest.cpp Wed Jun 26 22:45:31 2013
@@ -30,12 +30,8 @@ TEST(FileOutputBuffer, Test) {
// Create unique temporary directory for these tests
SmallString<128> TestDirectory;
{
- int fd;
ASSERT_NO_ERROR(
- fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
- TestDirectory));
- ::close(fd);
- TestDirectory = path::parent_path(TestDirectory);
+ fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
}
// TEST 1: Verify commit case.
Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=185059&r1=185058&r2=185059&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Wed Jun 26 22:45:31 2013
@@ -147,13 +147,9 @@ protected:
SmallString<128> TestDirectory;
virtual void SetUp() {
- int fd;
ASSERT_NO_ERROR(
- fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
- TestDirectory));
+ fs::createUniqueDirectory("file-system-test", TestDirectory));
// We don't care about this specific file.
- ::close(fd);
- TestDirectory = path::parent_path(TestDirectory);
errs() << "Test Directory: " << TestDirectory << '\n';
errs().flush();
}
More information about the llvm-commits
mailing list