Add an optional 'bool makeAbsolute' in llvm::sys::fs::unique_file function.

If true and 'model' parameter is not an absolute path, a temp directory will be prepended.
Make it true by default to match current behaviour.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136310 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Argyrios Kyrtzidis 2011-07-28 00:29:20 +00:00
parent 7b2958392c
commit 814450a429
3 changed files with 29 additions and 20 deletions

View File

@ -427,10 +427,13 @@ error_code symlink_status(const Twine &path, file_status &result);
/// @param model Name to base unique path off of. /// @param model Name to base unique path off of.
/// @param result_fs Set to the opened file's file descriptor. /// @param result_fs Set to the opened file's file descriptor.
/// @param result_path Set to the opened file's absolute path. /// @param result_path Set to the opened file's absolute path.
/// @param makeAbsolute If true and @model is not an absolute path, a temp
/// directory will be prepended.
/// @results errc::success if result_{fd,path} have been successfully set, /// @results errc::success if result_{fd,path} have been successfully set,
/// otherwise a platform specific error_code. /// otherwise a platform specific error_code.
error_code unique_file(const Twine &model, int &result_fd, error_code unique_file(const Twine &model, int &result_fd,
SmallVectorImpl<char> &result_path); SmallVectorImpl<char> &result_path,
bool makeAbsolute = true);
/// @brief Canonicalize path. /// @brief Canonicalize path.
/// ///

View File

@ -342,19 +342,22 @@ error_code status(const Twine &path, file_status &result) {
} }
error_code unique_file(const Twine &model, int &result_fd, error_code unique_file(const Twine &model, int &result_fd,
SmallVectorImpl<char> &result_path) { SmallVectorImpl<char> &result_path,
bool makeAbsolute) {
SmallString<128> Model; SmallString<128> Model;
model.toVector(Model); model.toVector(Model);
// Null terminate. // Null terminate.
Model.c_str(); Model.c_str();
// Make model absolute by prepending a temp directory if it's not already. if (makeAbsolute) {
bool absolute = path::is_absolute(Twine(Model)); // Make model absolute by prepending a temp directory if it's not already.
if (!absolute) { bool absolute = path::is_absolute(Twine(Model));
SmallString<128> TDir; if (!absolute) {
if (error_code ec = TempDir(TDir)) return ec; SmallString<128> TDir;
path::append(TDir, Twine(Model)); if (error_code ec = TempDir(TDir)) return ec;
Model.swap(TDir); path::append(TDir, Twine(Model));
Model.swap(TDir);
}
} }
// Replace '%' with random chars. From here on, DO NOT modify model. It may be // Replace '%' with random chars. From here on, DO NOT modify model. It may be

View File

@ -501,7 +501,8 @@ handle_status_error:
} }
error_code unique_file(const Twine &model, int &result_fd, error_code unique_file(const Twine &model, int &result_fd,
SmallVectorImpl<char> &result_path) { SmallVectorImpl<char> &result_path,
bool makeAbsolute) {
// Use result_path as temp storage. // Use result_path as temp storage.
result_path.set_size(0); result_path.set_size(0);
StringRef m = model.toStringRef(result_path); StringRef m = model.toStringRef(result_path);
@ -509,17 +510,19 @@ error_code unique_file(const Twine &model, int &result_fd,
SmallVector<wchar_t, 128> model_utf16; SmallVector<wchar_t, 128> model_utf16;
if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec; if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec;
// Make model absolute by prepending a temp directory if it's not already. if (makeAbsolute) {
bool absolute = path::is_absolute(m); // Make model absolute by prepending a temp directory if it's not already.
bool absolute = path::is_absolute(m);
if (!absolute) {
SmallVector<wchar_t, 64> temp_dir; if (!absolute) {
if (error_code ec = TempDir(temp_dir)) return ec; SmallVector<wchar_t, 64> temp_dir;
// Handle c: by removing it. if (error_code ec = TempDir(temp_dir)) return ec;
if (model_utf16.size() > 2 && model_utf16[1] == L':') { // Handle c: by removing it.
model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2); if (model_utf16.size() > 2 && model_utf16[1] == L':') {
model_utf16.erase(model_utf16.begin(), model_utf16.begin() + 2);
}
model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
} }
model_utf16.insert(model_utf16.begin(), temp_dir.begin(), temp_dir.end());
} }
// Replace '%' with random chars. From here on, DO NOT modify model. It may be // Replace '%' with random chars. From here on, DO NOT modify model. It may be