Skip to content

Commit

Permalink
Move file operations to <filesystem> where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewtff committed Jun 30, 2017
1 parent 18e824c commit 14bbdf8
Show file tree
Hide file tree
Showing 55 changed files with 679 additions and 783 deletions.
6 changes: 5 additions & 1 deletion dist-clang.files
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,7 @@ src/base/file/pipe.h
src/base/file/pipe_linux.cc
src/base/file/pipe_mac.cc
src/base/file_utils.h
src/base/file_utils_posix.cc
src/base/file_utils.cc
src/base/file_utils_test.cc
src/base/future.h
src/base/future_test.cc
Expand Down Expand Up @@ -2283,7 +2283,10 @@ src/client/clean_command.hh
src/client/command.cc
src/client/command.hh
src/client/command_test.cc
src/client/configuration.cc
src/client/configuration.hh
src/client/configuration.proto
src/client/configuration_test.cc
src/client/driver_command.cc
src/client/driver_command.hh
src/daemon/absorber.cc
Expand Down Expand Up @@ -8735,6 +8738,7 @@ src/third_party/snappy/exported/snappy_unittest.cc
src/third_party/snappy/linux/snappy-stubs-public.h
src/third_party/snappy/mac/snappy-stubs-public.h
src/third_party/snappy/win/snappy-stubs-public.h
tools/clang/main.cc
tools/args_parser/BUILD.gn
tools/args_parser/main.cc
tools/get_report.py
2 changes: 1 addition & 1 deletion src/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ shared_library("base") {
"file/pipe_linux.cc",
"file/pipe_mac.cc",
"file_utils.h",
"file_utils_posix.cc",
"file_utils.cc",
"future.h",
"locked_list.h",
"locked_queue.h",
Expand Down
6 changes: 6 additions & 0 deletions src/base/aliases.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include STL(unordered_set)
#include STL(vector)

#include STL_EXPERIMENTAL(filesystem)

namespace dist_clang {

namespace base {
Expand Down Expand Up @@ -67,6 +69,10 @@ using Mutex = std::mutex;
template <class U, class V = U>
using Pair = std::pair<U, V>;

using Path = std::experimental::filesystem::path;

using Perms = std::experimental::filesystem::perms;

using Seconds = std::chrono::seconds;

template <class T>
Expand Down
39 changes: 30 additions & 9 deletions src/base/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@
#include <stdlib.h>
#include <string.h>

#include STL_EXPERIMENTAL(filesystem)
#include STL(system_error)

namespace dist_clang {
namespace base {

inline bool ChangeCurrentDir(Immutable path, String* error) {
if (chdir(path.c_str()) == -1) {
GetLastError(error);
inline bool ChangeCurrentDir(const Path& path, String* error) {
std::error_code ec;
std::experimental::filesystem::current_path(path, ec);
if (ec) {
if (error) {
*error = ec.message();
}
return false;
}
return true;
}

inline Immutable GetCurrentDir(String* error) {
UniquePtr<char[]> buf(new char[PATH_MAX]);
if (!getcwd(buf.get(), PATH_MAX)) {
GetLastError(error);
return Immutable();
inline Path GetCurrentDir(String* error) {
std::error_code ec;
const auto& current_dir = std::experimental::filesystem::current_path(ec);
if (ec && error) {
*error = ec.message();
}
return buf;
return current_dir;
}

inline Literal GetEnv(Literal env_name, Literal default_env) {
Expand All @@ -52,5 +59,19 @@ inline void GetLastError(String* error) {
}
}

inline bool SetPermissions(const Path& path, const Perms permissions,
String* error) {
std::error_code ec;
std::experimental::filesystem::permissions(path.c_str(), permissions, ec);
if (ec) {
if (error) {
*error = ec.message();
}
return false;
}
return true;
}


} // namespace base
} // namespace dist_clang
11 changes: 6 additions & 5 deletions src/base/c_utils_forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
namespace dist_clang {
namespace base {

bool ChangeCurrentDir(Immutable path, String* error = nullptr);
String CreateTempFile(String* error = nullptr);
String CreateTempFile(const char suffix[], String* error = nullptr);
Immutable GetCurrentDir(String* error = nullptr);
bool ChangeCurrentDir(const Path& path, String* error = nullptr);
Path CreateTempFile(String* error = nullptr);
Path CreateTempFile(const char suffix[], String* error = nullptr);
Path GetCurrentDir(String* error = nullptr);
Literal GetEnv(Literal env_name, Literal default_env = Literal::empty);
Literal GetHomeDir(String* error = nullptr);
void GetLastError(String* error);
bool GetSelfPath(String& result, String* error = nullptr);
Literal SetEnv(Literal env_name, const String& value, String* error = nullptr);
bool SetPermissions(const String& path, int mask, String* error = nullptr);
bool SetPermissions(const Path& path, const Perms permissions,
String* error = nullptr);

} // namespace base
} // namespace dist_clang
21 changes: 6 additions & 15 deletions src/base/c_utils_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ inline Literal SetEnv(Literal env_name, const String& value, String* error) {
return old_value;
}

inline String CreateTempFile(String* error) {
inline Path CreateTempFile(String* error) {
char buf[] = "/tmp/clangd-XXXXXX.files";
#if defined(OS_LINUX)
const int fd = mkostemps(buf, 6, O_CLOEXEC);
Expand All @@ -35,13 +35,13 @@ inline String CreateTempFile(String* error) {
#endif
if (fd == -1) {
GetLastError(error);
return String();
return Path();
}
close(fd);
return String(buf);
return Path(buf);
}

inline String CreateTempFile(const char suffix[], String* error) {
inline Path CreateTempFile(const char suffix[], String* error) {
const char prefix[] = "/tmp/clangd-XXXXXX";
const size_t prefix_size = sizeof(prefix) - 1;
UniquePtr<char[]> buf(new char[prefix_size + strlen(suffix) + 1]);
Expand All @@ -59,11 +59,10 @@ inline String CreateTempFile(const char suffix[], String* error) {
#endif
if (fd == -1) {
GetLastError(error);
return String();
return Path();
}
close(fd);
const auto result = String(buf.get());
return result;
return Path(buf.get());
}

inline Literal GetHomeDir(String* error) {
Expand Down Expand Up @@ -104,13 +103,5 @@ inline bool GetSelfPath(String& result, String* error) {
return true;
}

inline bool SetPermissions(const String& path, int mask, String* error) {
if (chmod(path.c_str(), mask) == -1) {
GetLastError(error);
return false;
}
return true;
}

} // namespace base
} // namespace dist_clang
1 change: 1 addition & 0 deletions src/base/const_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace base {

class Literal {
public:
inline const char* c_str() const { return str_; }
inline operator const char*() const { return str_; }
inline size_t size() const { return strlen(str_); }
inline bool operator==(const Literal& other) const {
Expand Down
29 changes: 14 additions & 15 deletions src/base/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace base {

class File final : public Data {
public:
explicit File(const String& path); // Open read-only file
explicit File(const Path& path); // Open read-only file

using Handle::Close;

Expand All @@ -17,38 +17,37 @@ class File final : public Data {
}
}

static bool IsFile(const String& path, String* error = nullptr);
static bool IsExecutable(const String& path, String* error = nullptr);
static bool Exists(const String& path, String* error = nullptr);
static bool IsFile(const Path& path, String* error = nullptr);
static bool IsExecutable(const Path& path, String* error = nullptr);
static bool Exists(const Path& path, String* error = nullptr);

ui64 Size(String* error = nullptr) const;
bool Read(Immutable* output, String* error = nullptr);
bool Hash(Immutable* output, const List<Literal>& skip_list = List<Literal>(),
String* error = nullptr);

bool CopyInto(const String& dst_path, String* error = nullptr);
bool CopyInto(const Path& dst_path, String* error = nullptr);

static ui64 Size(const String& path, String* error = nullptr);
static bool Read(const String& path, Immutable* output,
static ui64 Size(const Path& path, String* error = nullptr);
static bool Read(const Path& path, Immutable* output,
String* error = nullptr);
static bool Write(const String& path, Immutable input,
static bool Write(const Path& path, Immutable input,
String* error = nullptr);
static bool Hash(const String& path, Immutable* output,
static bool Hash(const Path& path, Immutable* output,
const List<Literal>& skip_list = List<Literal>(),
String* error = nullptr);

static bool Copy(const String& src_path, const String& dst_path,
static bool Copy(const Path& src_path, const Path& dst_path,
String* error = nullptr);
static bool Link(const String& src_path, const String& dst_path,
static bool Move(const Path& src, const Path& dst,
String* error = nullptr);
static bool Move(const String& src, const String& dst,
String* error = nullptr);
static bool Delete(const String& path, String* error = nullptr);
static bool Delete(const Path& path, String* error = nullptr);

private:
File(const String& path, ui64 size); // Open truncated write-only file
File(const Path& path, ui64 size); // Open truncated write-only file
bool Close(String* error = nullptr);

String path_;
String error_;
String move_on_close_;
};
Expand Down
Loading

0 comments on commit 14bbdf8

Please sign in to comment.