Skip to content

Commit fd2709b

Browse files
committed
ScopedTempDirectory stores error type
#improvement
1 parent 3497b03 commit fd2709b

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

src/lib/Support/Path.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ ScopedTempFile(
487487

488488
ScopedTempDirectory::
489489
~ScopedTempDirectory() {
490-
if (ok_)
490+
if (*this)
491491
{
492492
llvm::sys::fs::remove_directories(path_);
493493
}
@@ -498,11 +498,12 @@ ScopedTempDirectory(
498498
llvm::StringRef prefix)
499499
{
500500
llvm::SmallString<128> tempPath;
501-
ok_ = !llvm::sys::fs::createUniqueDirectory(prefix, tempPath);
502-
if (ok_)
501+
if (llvm::sys::fs::createUniqueDirectory(prefix, tempPath))
503502
{
504-
path_ = tempPath;
503+
status_ = ErrorStatus::CannotCreateDirectories;
504+
return;
505505
}
506+
path_ = tempPath;
506507
}
507508

508509
ScopedTempDirectory::
@@ -513,19 +514,33 @@ ScopedTempDirectory(
513514
llvm::SmallString<128> tempPath(root);
514515
llvm::sys::path::append(tempPath, dir);
515516
bool const exists = llvm::sys::fs::exists(tempPath);
516-
if (exists)
517+
if (exists &&
518+
llvm::sys::fs::remove_directories(tempPath))
517519
{
518-
ok_ = !llvm::sys::fs::remove_directories(tempPath);
519-
if (!ok_)
520-
{
521-
return;
522-
}
520+
status_ = ErrorStatus::CannotDeleteExisting;
521+
return;
523522
}
524-
ok_ = !llvm::sys::fs::create_directory(tempPath);
525-
if (ok_)
523+
if (llvm::sys::fs::create_directories(tempPath))
526524
{
527-
path_ = tempPath;
525+
status_ = ErrorStatus::CannotCreateDirectories;
526+
return;
527+
}
528+
path_ = tempPath;
529+
}
530+
531+
Error
532+
ScopedTempDirectory::
533+
error() const
534+
{
535+
if (status_ == ErrorStatus::CannotDeleteExisting)
536+
{
537+
return Error("Failed to delete existing directory");
538+
}
539+
if (status_ == ErrorStatus::CannotCreateDirectories)
540+
{
541+
return Error("Failed to create directories");
528542
}
543+
return Error();
529544
}
530545

531546
} // mrdocs

src/lib/Support/Path.hpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ class ScopedTempFile
8585
*/
8686
class ScopedTempDirectory
8787
{
88+
// Status of the directory
89+
enum class ErrorStatus
90+
{
91+
None,
92+
CannotDeleteExisting,
93+
CannotCreateDirectories
94+
};
95+
8896
clang::mrdocs::SmallPathString path_;
89-
bool ok_ = false;
97+
ErrorStatus status_ = ErrorStatus::None;
9098
public:
9199
/** Destructor
92100
@@ -129,12 +137,27 @@ class ScopedTempDirectory
129137

130138
/** Returns `true` if the directory was created successfully.
131139
*/
132-
operator bool() const { return ok_; }
140+
operator bool() const
141+
{
142+
return status_ == ErrorStatus::None;
143+
}
144+
145+
/** Returns `true` if the directory was not created successfully.
146+
*/
147+
bool
148+
failed() const
149+
{
150+
return status_ != ErrorStatus::None;
151+
}
133152

134153
/** Returns the path to the temporary directory.
135154
*/
136155
std::string_view path() const { return static_cast<llvm::StringRef>(path_); }
137156

157+
/** Returns the error status of the directory.
158+
*/
159+
Error error() const;
160+
138161
/** Convert temp directory to a std::string_view
139162
*/
140163
operator std::string_view() const { return path(); }

src/tool/GenerateAction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ DoGenerateAction(
138138
compilationDatabasePath,
139139
"The compilation database path argument is missing");
140140
ScopedTempDirectory tempDir(config->settings().output, ".temp");
141+
if (tempDir.failed())
142+
{
143+
report::error("Failed to create temporary directory: {}", tempDir.error());
144+
return Unexpected(tempDir.error());
145+
}
141146
std::string buildPath = files::appendPath(tempDir, "build");
142147
Expected<std::string> const compileCommandsPathExp =
143148
generateCompileCommandsFile(

0 commit comments

Comments
 (0)