Skip to content

Invert if statement in filesystem for clearer intent. #31865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from May 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/Basic/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ std::error_code swift::atomicallyWritingToFile(
if (!OS.hasValue()) {
std::error_code error;
OS.emplace(outputPath, error, fs::F_None);
if (error)
if (error) {
return error;
}
}

action(OS.getValue());
Expand All @@ -169,8 +170,9 @@ swift::areFilesDifferent(const llvm::Twine &source,
bool allowDestinationErrors) {
namespace fs = llvm::sys::fs;

if (fs::equivalent(source, destination))
if (fs::equivalent(source, destination)) {
return FileDifference::IdenticalFile;
}

OpenFileRAII sourceFile;
fs::file_status sourceStatus;
Expand All @@ -187,8 +189,9 @@ swift::areFilesDifferent(const llvm::Twine &source,
/// DifferentContents return, depending on `allowDestinationErrors`.
auto convertDestinationError = [=](std::error_code error) ->
llvm::ErrorOr<FileDifference> {
if (allowDestinationErrors)
if (allowDestinationErrors){
return FileDifference::DifferentContents;
}
return error;
};

Expand All @@ -204,34 +207,39 @@ swift::areFilesDifferent(const llvm::Twine &source,
}

uint64_t size = sourceStatus.getSize();
if (size != destStatus.getSize())
if (size != destStatus.getSize()) {
// If the files are different sizes, they must be different.
return FileDifference::DifferentContents;
if (size == 0)
}
if (size == 0) {
// If both files are zero size, they must be the same.
return FileDifference::SameContents;
}

// The two files match in size, so we have to compare the bytes to determine
// if they're the same.
std::error_code sourceRegionErr;
fs::mapped_file_region sourceRegion(fs::convertFDToNativeFile(sourceFile.fd),
fs::mapped_file_region::readonly,
size, 0, sourceRegionErr);
if (sourceRegionErr)
if (sourceRegionErr) {
return sourceRegionErr;
}

std::error_code destRegionErr;
fs::mapped_file_region destRegion(fs::convertFDToNativeFile(destFile.fd),
fs::mapped_file_region::readonly,
size, 0, destRegionErr);

if (destRegionErr)
if (destRegionErr) {
return convertDestinationError(destRegionErr);
}

if (0 == memcmp(sourceRegion.const_data(), destRegion.const_data(), size))
return FileDifference::SameContents;
if (memcmp(sourceRegion.const_data(), destRegion.const_data(), size) != 0) {
return FileDifference::DifferentContents;
}

return FileDifference::DifferentContents;
return FileDifference::SameContents;
}

std::error_code swift::moveFileIfDifferent(const llvm::Twine &source,
Expand Down