-
Notifications
You must be signed in to change notification settings - Fork 13
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
Move file operations to <filesystem> where possible #113
Open
matthewtff
wants to merge
2
commits into
abyss7:master
Choose a base branch
from
matthewtff:move-to-filesystem
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ namespace base { | |
|
||
class Literal { | ||
public: | ||
inline const char* c_str() const { return str_; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is it used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
inline operator const char*() const { return str_; } | ||
inline size_t size() const { return strlen(str_); } | ||
inline bool operator==(const Literal& other) const { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#include <base/file/file.h> | ||
|
||
#include <base/c_utils.h> | ||
#include <base/const_string.h> | ||
#include <base/string_utils.h> | ||
|
||
#include STL(cstdio) | ||
#include STL_EXPERIMENTAL(filesystem) | ||
#include STL(system_error) | ||
|
||
namespace dist_clang { | ||
|
||
namespace { | ||
|
||
bool GetStatus(const Path& path, | ||
std::experimental::filesystem::file_status* status, | ||
String* error) { | ||
CHECK(status); | ||
std::error_code ec; | ||
*status = std::experimental::filesystem::status(path, ec); | ||
if (ec) { | ||
if (error) { | ||
*error = ec.message(); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
namespace base { | ||
|
||
// static | ||
bool File::IsFile(const Path& path, String* error) { | ||
std::experimental::filesystem::file_status status; | ||
if (!GetStatus(path, &status, error)) { | ||
return false; | ||
} | ||
return !std::experimental::filesystem::is_directory(status); | ||
} | ||
|
||
bool File::Hash(Immutable* output, const List<Literal>& skip_list, | ||
String* error) { | ||
DCHECK(IsValid()); | ||
|
||
Immutable tmp_output; | ||
if (!Read(&tmp_output, error)) { | ||
return false; | ||
} | ||
|
||
for (const char* skip : skip_list) { | ||
if (tmp_output.find(skip) != String::npos) { | ||
if (error) { | ||
error->assign("Skip-list hit: " + String(skip)); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
output->assign(base::Hexify(tmp_output.Hash())); | ||
return true; | ||
} | ||
|
||
// static | ||
ui64 File::Size(const Path& path, String* error) { | ||
std::error_code ec; | ||
const auto file_size = std::experimental::filesystem::file_size(path, ec); | ||
if (ec) { | ||
if (error) { | ||
*error = ec.message(); | ||
} | ||
return 0; | ||
} | ||
return static_cast<ui64>(file_size); | ||
} | ||
|
||
// static | ||
bool File::Read(const Path& path, Immutable* output, String* error) { | ||
File file(path); | ||
|
||
if (!file.IsValid()) { | ||
file.GetCreationError(error); | ||
return false; | ||
} | ||
|
||
return file.Read(output, error); | ||
} | ||
|
||
// static | ||
bool File::Exists(const Path& path, String* error) { | ||
std::experimental::filesystem::file_status status; | ||
if (!GetStatus(path, &status, error)) { | ||
return false; | ||
} | ||
return std::experimental::filesystem::exists(status) && | ||
!std::experimental::filesystem::is_directory(status); | ||
} | ||
|
||
// static | ||
bool File::Hash(const Path& path, Immutable* output, | ||
const List<Literal>& skip_list, String* error) { | ||
File file(path); | ||
|
||
if (!file.IsValid()) { | ||
file.GetCreationError(error); | ||
return false; | ||
} | ||
|
||
return file.Hash(output, skip_list, error); | ||
} | ||
|
||
// static | ||
bool File::Copy(const Path& src_path, const Path& dst_path, String* error) { | ||
File src(src_path); | ||
|
||
if (!src.IsValid()) { | ||
src.GetCreationError(error); | ||
return false; | ||
} | ||
|
||
return src.CopyInto(dst_path, error); | ||
} | ||
|
||
// static | ||
bool File::Move(const Path& src, const Path& dst, String* error) { | ||
std::error_code ec; | ||
std::experimental::filesystem::rename(src, dst, ec); | ||
if (ec) { | ||
if (error) { | ||
*error = ec.message(); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
// static | ||
bool File::Delete(const Path& path, String* error) { | ||
if (std::remove(path.c_str())) { | ||
GetLastError(error); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace base | ||
|
||
} // namespace dist_clang |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const Perms&
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should fit the machine word, but I'll make it a const ref no difference for me.