Skip to content

Commit 14bbdf8

Browse files
committed
Move file operations to <filesystem> where possible
1 parent 18e824c commit 14bbdf8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+679
-783
lines changed

dist-clang.files

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ src/base/file/pipe.h
22152215
src/base/file/pipe_linux.cc
22162216
src/base/file/pipe_mac.cc
22172217
src/base/file_utils.h
2218-
src/base/file_utils_posix.cc
2218+
src/base/file_utils.cc
22192219
src/base/file_utils_test.cc
22202220
src/base/future.h
22212221
src/base/future_test.cc
@@ -2283,7 +2283,10 @@ src/client/clean_command.hh
22832283
src/client/command.cc
22842284
src/client/command.hh
22852285
src/client/command_test.cc
2286+
src/client/configuration.cc
2287+
src/client/configuration.hh
22862288
src/client/configuration.proto
2289+
src/client/configuration_test.cc
22872290
src/client/driver_command.cc
22882291
src/client/driver_command.hh
22892292
src/daemon/absorber.cc
@@ -8735,6 +8738,7 @@ src/third_party/snappy/exported/snappy_unittest.cc
87358738
src/third_party/snappy/linux/snappy-stubs-public.h
87368739
src/third_party/snappy/mac/snappy-stubs-public.h
87378740
src/third_party/snappy/win/snappy-stubs-public.h
8741+
tools/clang/main.cc
87388742
tools/args_parser/BUILD.gn
87398743
tools/args_parser/main.cc
87408744
tools/get_report.py

src/base/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ shared_library("base") {
4242
"file/pipe_linux.cc",
4343
"file/pipe_mac.cc",
4444
"file_utils.h",
45-
"file_utils_posix.cc",
45+
"file_utils.cc",
4646
"future.h",
4747
"locked_list.h",
4848
"locked_queue.h",

src/base/aliases.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include STL(unordered_set)
1919
#include STL(vector)
2020

21+
#include STL_EXPERIMENTAL(filesystem)
22+
2123
namespace dist_clang {
2224

2325
namespace base {
@@ -67,6 +69,10 @@ using Mutex = std::mutex;
6769
template <class U, class V = U>
6870
using Pair = std::pair<U, V>;
6971

72+
using Path = std::experimental::filesystem::path;
73+
74+
using Perms = std::experimental::filesystem::perms;
75+
7076
using Seconds = std::chrono::seconds;
7177

7278
template <class T>

src/base/c_utils.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717

18+
#include STL_EXPERIMENTAL(filesystem)
19+
#include STL(system_error)
20+
1821
namespace dist_clang {
1922
namespace base {
2023

21-
inline bool ChangeCurrentDir(Immutable path, String* error) {
22-
if (chdir(path.c_str()) == -1) {
23-
GetLastError(error);
24+
inline bool ChangeCurrentDir(const Path& path, String* error) {
25+
std::error_code ec;
26+
std::experimental::filesystem::current_path(path, ec);
27+
if (ec) {
28+
if (error) {
29+
*error = ec.message();
30+
}
2431
return false;
2532
}
2633
return true;
2734
}
2835

29-
inline Immutable GetCurrentDir(String* error) {
30-
UniquePtr<char[]> buf(new char[PATH_MAX]);
31-
if (!getcwd(buf.get(), PATH_MAX)) {
32-
GetLastError(error);
33-
return Immutable();
36+
inline Path GetCurrentDir(String* error) {
37+
std::error_code ec;
38+
const auto& current_dir = std::experimental::filesystem::current_path(ec);
39+
if (ec && error) {
40+
*error = ec.message();
3441
}
35-
return buf;
42+
return current_dir;
3643
}
3744

3845
inline Literal GetEnv(Literal env_name, Literal default_env) {
@@ -52,5 +59,19 @@ inline void GetLastError(String* error) {
5259
}
5360
}
5461

62+
inline bool SetPermissions(const Path& path, const Perms permissions,
63+
String* error) {
64+
std::error_code ec;
65+
std::experimental::filesystem::permissions(path.c_str(), permissions, ec);
66+
if (ec) {
67+
if (error) {
68+
*error = ec.message();
69+
}
70+
return false;
71+
}
72+
return true;
73+
}
74+
75+
5576
} // namespace base
5677
} // namespace dist_clang

src/base/c_utils_forward.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
namespace dist_clang {
77
namespace base {
88

9-
bool ChangeCurrentDir(Immutable path, String* error = nullptr);
10-
String CreateTempFile(String* error = nullptr);
11-
String CreateTempFile(const char suffix[], String* error = nullptr);
12-
Immutable GetCurrentDir(String* error = nullptr);
9+
bool ChangeCurrentDir(const Path& path, String* error = nullptr);
10+
Path CreateTempFile(String* error = nullptr);
11+
Path CreateTempFile(const char suffix[], String* error = nullptr);
12+
Path GetCurrentDir(String* error = nullptr);
1313
Literal GetEnv(Literal env_name, Literal default_env = Literal::empty);
1414
Literal GetHomeDir(String* error = nullptr);
1515
void GetLastError(String* error);
1616
bool GetSelfPath(String& result, String* error = nullptr);
1717
Literal SetEnv(Literal env_name, const String& value, String* error = nullptr);
18-
bool SetPermissions(const String& path, int mask, String* error = nullptr);
18+
bool SetPermissions(const Path& path, const Perms permissions,
19+
String* error = nullptr);
1920

2021
} // namespace base
2122
} // namespace dist_clang

src/base/c_utils_posix.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ inline Literal SetEnv(Literal env_name, const String& value, String* error) {
2323
return old_value;
2424
}
2525

26-
inline String CreateTempFile(String* error) {
26+
inline Path CreateTempFile(String* error) {
2727
char buf[] = "/tmp/clangd-XXXXXX.files";
2828
#if defined(OS_LINUX)
2929
const int fd = mkostemps(buf, 6, O_CLOEXEC);
@@ -35,13 +35,13 @@ inline String CreateTempFile(String* error) {
3535
#endif
3636
if (fd == -1) {
3737
GetLastError(error);
38-
return String();
38+
return Path();
3939
}
4040
close(fd);
41-
return String(buf);
41+
return Path(buf);
4242
}
4343

44-
inline String CreateTempFile(const char suffix[], String* error) {
44+
inline Path CreateTempFile(const char suffix[], String* error) {
4545
const char prefix[] = "/tmp/clangd-XXXXXX";
4646
const size_t prefix_size = sizeof(prefix) - 1;
4747
UniquePtr<char[]> buf(new char[prefix_size + strlen(suffix) + 1]);
@@ -59,11 +59,10 @@ inline String CreateTempFile(const char suffix[], String* error) {
5959
#endif
6060
if (fd == -1) {
6161
GetLastError(error);
62-
return String();
62+
return Path();
6363
}
6464
close(fd);
65-
const auto result = String(buf.get());
66-
return result;
65+
return Path(buf.get());
6766
}
6867

6968
inline Literal GetHomeDir(String* error) {
@@ -104,13 +103,5 @@ inline bool GetSelfPath(String& result, String* error) {
104103
return true;
105104
}
106105

107-
inline bool SetPermissions(const String& path, int mask, String* error) {
108-
if (chmod(path.c_str(), mask) == -1) {
109-
GetLastError(error);
110-
return false;
111-
}
112-
return true;
113-
}
114-
115106
} // namespace base
116107
} // namespace dist_clang

src/base/const_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace base {
1616

1717
class Literal {
1818
public:
19+
inline const char* c_str() const { return str_; }
1920
inline operator const char*() const { return str_; }
2021
inline size_t size() const { return strlen(str_); }
2122
inline bool operator==(const Literal& other) const {

src/base/file/file.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace base {
77

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

1212
using Handle::Close;
1313

@@ -17,38 +17,37 @@ class File final : public Data {
1717
}
1818
}
1919

20-
static bool IsFile(const String& path, String* error = nullptr);
21-
static bool IsExecutable(const String& path, String* error = nullptr);
22-
static bool Exists(const String& path, String* error = nullptr);
20+
static bool IsFile(const Path& path, String* error = nullptr);
21+
static bool IsExecutable(const Path& path, String* error = nullptr);
22+
static bool Exists(const Path& path, String* error = nullptr);
2323

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

29-
bool CopyInto(const String& dst_path, String* error = nullptr);
29+
bool CopyInto(const Path& dst_path, String* error = nullptr);
3030

31-
static ui64 Size(const String& path, String* error = nullptr);
32-
static bool Read(const String& path, Immutable* output,
31+
static ui64 Size(const Path& path, String* error = nullptr);
32+
static bool Read(const Path& path, Immutable* output,
3333
String* error = nullptr);
34-
static bool Write(const String& path, Immutable input,
34+
static bool Write(const Path& path, Immutable input,
3535
String* error = nullptr);
36-
static bool Hash(const String& path, Immutable* output,
36+
static bool Hash(const Path& path, Immutable* output,
3737
const List<Literal>& skip_list = List<Literal>(),
3838
String* error = nullptr);
3939

40-
static bool Copy(const String& src_path, const String& dst_path,
40+
static bool Copy(const Path& src_path, const Path& dst_path,
4141
String* error = nullptr);
42-
static bool Link(const String& src_path, const String& dst_path,
42+
static bool Move(const Path& src, const Path& dst,
4343
String* error = nullptr);
44-
static bool Move(const String& src, const String& dst,
45-
String* error = nullptr);
46-
static bool Delete(const String& path, String* error = nullptr);
44+
static bool Delete(const Path& path, String* error = nullptr);
4745

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

50+
String path_;
5251
String error_;
5352
String move_on_close_;
5453
};

0 commit comments

Comments
 (0)