Skip to content

Commit

Permalink
[llvm][Support] Deprecate llvm::writeFileAtomically API
Browse files Browse the repository at this point in the history
We're in favor of the llvm::writeToOutput API, and all
writeFileAtomically usages have been migrated to writeToOutput.

Differential Revision: https://reviews.llvm.org/D153740
  • Loading branch information
hokein committed Jul 6, 2023
1 parent c26e398 commit 7aafea0
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 150 deletions.
35 changes: 0 additions & 35 deletions llvm/include/llvm/Support/FileUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,6 @@ namespace llvm {
void releaseFile() { DeleteIt = false; }
};

enum class atomic_write_error {
failed_to_create_uniq_file = 0,
output_stream_error,
failed_to_rename_temp_file
};

class AtomicFileWriteError : public llvm::ErrorInfo<AtomicFileWriteError> {
public:
AtomicFileWriteError(atomic_write_error Error) : Error(Error) {}

void log(raw_ostream &OS) const override;

const atomic_write_error Error;
static char ID;

private:
// Users are not expected to use error_code.
std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
};

// atomic_write_error + whatever the Writer can return

/// Creates a unique file with name according to the given \p TempPathModel,
/// writes content of \p Buffer to the file and renames it to \p FinalPath.
///
/// \returns \c AtomicFileWriteError in case of error.
llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
StringRef Buffer);

llvm::Error
writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
std::function<llvm::Error(llvm::raw_ostream &)> Writer);

/// FilePermssionsApplier helps to copy permissions from an input file to
/// an output one. It memorizes the status of the input file and can apply
/// permissions and dates to the output file.
Expand Down
60 changes: 0 additions & 60 deletions llvm/lib/Support/FileUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,64 +267,6 @@ int llvm::DiffFilesWithTolerance(StringRef NameA,
return CompareFailed;
}

void llvm::AtomicFileWriteError::log(raw_ostream &OS) const {
OS << "atomic_write_error: ";
switch (Error) {
case atomic_write_error::failed_to_create_uniq_file:
OS << "failed_to_create_uniq_file";
return;
case atomic_write_error::output_stream_error:
OS << "output_stream_error";
return;
case atomic_write_error::failed_to_rename_temp_file:
OS << "failed_to_rename_temp_file";
return;
}
llvm_unreachable("unknown atomic_write_error value in "
"failed_to_rename_temp_file::log()");
}

llvm::Error llvm::writeFileAtomically(StringRef TempPathModel,
StringRef FinalPath, StringRef Buffer) {
return writeFileAtomically(TempPathModel, FinalPath,
[&Buffer](llvm::raw_ostream &OS) {
OS.write(Buffer.data(), Buffer.size());
return llvm::Error::success();
});
}

llvm::Error llvm::writeFileAtomically(
StringRef TempPathModel, StringRef FinalPath,
std::function<llvm::Error(llvm::raw_ostream &)> Writer) {
SmallString<128> GeneratedUniqPath;
int TempFD;
if (sys::fs::createUniqueFile(TempPathModel, TempFD, GeneratedUniqPath)) {
return llvm::make_error<AtomicFileWriteError>(
atomic_write_error::failed_to_create_uniq_file);
}
llvm::FileRemover RemoveTmpFileOnFail(GeneratedUniqPath);

raw_fd_ostream OS(TempFD, /*shouldClose=*/true);
if (llvm::Error Err = Writer(OS)) {
return Err;
}

OS.close();
if (OS.has_error()) {
OS.clear_error();
return llvm::make_error<AtomicFileWriteError>(
atomic_write_error::output_stream_error);
}

if (sys::fs::rename(/*from=*/GeneratedUniqPath, /*to=*/FinalPath)) {
return llvm::make_error<AtomicFileWriteError>(
atomic_write_error::failed_to_rename_temp_file);
}

RemoveTmpFileOnFail.releaseFile();
return Error::success();
}

Expected<FilePermissionsApplier>
FilePermissionsApplier::create(StringRef InputFilename) {
sys::fs::file_status Status;
Expand Down Expand Up @@ -389,5 +331,3 @@ Error FilePermissionsApplier::apply(

return Error::success();
}

char llvm::AtomicFileWriteError::ID;
1 change: 0 additions & 1 deletion llvm/unittests/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ add_llvm_unittest(SupportTests
ExtensibleRTTITest.cpp
FileCollectorTest.cpp
FileOutputBufferTest.cpp
FileUtilitiesTest.cpp
FormatVariadicTest.cpp
FSUniqueIDTest.cpp
GlobPatternTest.cpp
Expand Down
54 changes: 0 additions & 54 deletions llvm/unittests/Support/FileUtilitiesTest.cpp

This file was deleted.

0 comments on commit 7aafea0

Please sign in to comment.