BuildCache
file_utils.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2018 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_FILE_UTILS_HPP_
21 #define BUILDCACHE_FILE_UTILS_HPP_
22 
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26 
27 #include <base/time_utils.hpp>
28 
29 namespace bcache {
30 namespace file {
35 class tmp_file_t {
36 public:
40  tmp_file_t(const std::string& dir, const std::string& extension);
41 
43  ~tmp_file_t();
44 
45  const std::string& path() const {
46  return m_path;
47  }
48 
49 private:
50  std::string m_path;
51 };
52 
62 public:
63  scoped_work_dir_t(const std::string& new_work_dir);
65 
66 private:
67  std::string m_old_work_dir;
68 };
69 
71 class file_info_t {
72 public:
73  file_info_t(const std::string& path,
76  const int64_t size,
77  const bool is_dir)
78  : m_path(path),
79  m_modify_time(modify_time),
80  m_access_time(access_time),
81  m_size(size),
82  m_is_dir(is_dir) {
83  }
84 
86  const std::string& path() const {
87  return m_path;
88  }
89 
93  return m_modify_time;
94  }
95 
99  return m_access_time;
100  }
101 
104  int64_t size() const {
105  return m_size;
106  }
107 
109  bool is_dir() const {
110  return m_is_dir;
111  }
112 
113 private:
114  std::string m_path;
115  time::seconds_t m_modify_time;
116  time::seconds_t m_access_time;
117  int64_t m_size;
118  bool m_is_dir;
119 };
120 
127 class exe_path_t {
128 public:
129  exe_path_t(const std::string real_path,
130  const std::string virtual_path,
131  const std::string invoked_as)
132  : m_real_path(real_path), m_virtual_path(virtual_path), m_invoked_as(invoked_as) {
133  }
134 
136  const std::string& real_path() const {
137  return m_real_path;
138  }
139 
143  const std::string& virtual_path() const {
144  return m_virtual_path;
145  }
146 
148  const std::string& invoked_as() const {
149  return m_invoked_as;
150  }
151 
152 private:
153  std::string m_real_path;
154  std::string m_virtual_path;
155  std::string m_invoked_as;
156 };
157 
164 std::string append_path(const std::string& path, const std::string& append);
165 std::string append_path(const std::string& path, const char* append);
167 
173 std::string canonicalize_path(const std::string& path);
174 
179 std::string get_extension(const std::string& path);
180 
185 std::string change_extension(const std::string& path, const std::string& new_ext);
186 
192 std::string get_file_part(const std::string& path, const bool include_ext = true);
193 
198 std::string get_dir_part(const std::string& path);
199 
202 std::string get_temp_dir();
203 
206 std::string get_user_home_dir();
207 
211 std::string get_cwd();
212 
216 void set_cwd(const std::string& path);
217 
224 std::string resolve_path(const std::string& path);
225 
231 exe_path_t find_executable(const std::string& program, const std::string& exclude = std::string());
232 
236 file_info_t get_file_info(const std::string& path);
237 
241 std::string human_readable_size(const int64_t byte_size);
242 
247 std::vector<file_info_t> walk_directory(const std::string& path);
248 
252 void create_dir(const std::string& path);
253 
261 void create_dir_with_parents(const std::string& path);
262 
267 void remove_file(const std::string& path, const bool ignore_errors = false);
268 
273 void remove_dir(const std::string& path, const bool ignore_errors = false);
274 
278 bool dir_exists(const std::string& path);
279 
283 bool file_exists(const std::string& path);
284 
289 void move(const std::string& from_path, const std::string& to_path);
290 
295 void copy(const std::string& from_path, const std::string& to_path);
296 
303 void link_or_copy(const std::string& from_path, const std::string& to_path);
304 
308 void touch(const std::string& path);
309 
314 std::string read(const std::string& path);
315 
320 void write(const std::string& data, const std::string& path);
321 
328 void write_atomic(const std::string& data, const std::string& path);
329 
335 void append(const std::string& data, const std::string& path);
336 
339 std::string get_unique_id();
340 
341 } // namespace file
342 } // namespace bcache
343 
344 #endif // BUILDCACHE_FILE_UTILS_HPP_
bcache::file::file_info_t::path
const std::string & path() const
Definition: file_utils.hpp:86
bcache::file::file_info_t::access_time
time::seconds_t access_time() const
Definition: file_utils.hpp:98
bcache::file::dir_exists
bool dir_exists(const std::string &path)
Check if a directory exists.
Definition: file_utils.cpp:630
bcache::file::file_info_t
Information about a file.
Definition: file_utils.hpp:71
bcache::file::append_path
std::string append_path(const std::string &path, const std::string &append)
Append two paths.
Definition: file_utils.cpp:250
bcache::file::exe_path_t
Path to an executable file.
Definition: file_utils.hpp:127
bcache::time::seconds_t
int64_t seconds_t
Time in seconds since the Unix epoch.
Definition: time_utils.hpp:31
bcache::file::canonicalize_path
std::string canonicalize_path(const std::string &path)
Get the canonical form of a path.
Definition: file_utils.cpp:261
bcache::file::tmp_file_t
A helper class for handling temporary files and directories.
Definition: file_utils.hpp:35
bcache::file::human_readable_size
std::string human_readable_size(const int64_t byte_size)
Convert a size to a human readable string.
Definition: file_utils.cpp:971
bcache::file::create_dir_with_parents
void create_dir_with_parents(const std::string &path)
Create a directory and its parent directories.
Definition: file_utils.cpp:594
bcache::file::write_atomic
void write_atomic(const std::string &data, const std::string &path)
Write a string to a file in an atomic fashion.
Definition: file_utils.cpp:849
bcache::file::touch
void touch(const std::string &path)
Touch the file to update the modification time.
Definition: file_utils.cpp:751
bcache::file::file_info_t::size
int64_t size() const
Definition: file_utils.hpp:104
bcache::file::set_cwd
void set_cwd(const std::string &path)
Set the current working directory.
Definition: file_utils.cpp:459
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::file::append
void append(const std::string &data, const std::string &path)
Append a string to a file.
Definition: file_utils.cpp:862
bcache::file::tmp_file_t::~tmp_file_t
~tmp_file_t()
Remove the temporary file or directory (if any).
Definition: file_utils.cpp:225
bcache::file::exe_path_t::invoked_as
const std::string & invoked_as() const
Definition: file_utils.hpp:148
bcache::file::exe_path_t::virtual_path
const std::string & virtual_path() const
Definition: file_utils.hpp:143
bcache::file::resolve_path
std::string resolve_path(const std::string &path)
Resolve a path.
Definition: file_utils.cpp:470
bcache::file::get_extension
std::string get_extension(const std::string &path)
Get the file extension of a path.
Definition: file_utils.cpp:322
bcache::file::file_info_t::is_dir
bool is_dir() const
Definition: file_utils.hpp:109
bcache::file::move
void move(const std::string &from_path, const std::string &to_path)
Move a file from an old location to a new location.
Definition: file_utils.cpp:660
bcache::file::tmp_file_t::tmp_file_t
tmp_file_t(const std::string &dir, const std::string &extension)
Construct a temporary file name.
Definition: file_utils.cpp:217
bcache::file::remove_file
void remove_file(const std::string &path, const bool ignore_errors)
Remove an existing file.
Definition: file_utils.cpp:607
bcache::file::write
void write(const std::string &data, const std::string &path)
Write a string to a file.
Definition: file_utils.cpp:816
bcache::file::get_unique_id
std::string get_unique_id()
Create a unique ID string.
Definition: file_utils.cpp:1083
bcache::file::copy
void copy(const std::string &from_path, const std::string &to_path)
Make a full copy of a file.
Definition: file_utils.cpp:679
bcache::file::create_dir
void create_dir(const std::string &path)
Create a directory.
Definition: file_utils.cpp:583
bcache::file::change_extension
std::string change_extension(const std::string &path, const std::string &new_ext)
Change the file extension of a path.
Definition: file_utils.cpp:334
bcache::file::get_dir_part
std::string get_dir_part(const std::string &path)
Get the directory part of a path.
Definition: file_utils.cpp:355
bcache::file::link_or_copy
void link_or_copy(const std::string &from_path, const std::string &to_path)
Make a hard link or a full copy of a file.
Definition: file_utils.cpp:728
bcache::file::get_file_part
std::string get_file_part(const std::string &path, const bool include_ext)
Get the file name part of a path.
Definition: file_utils.cpp:346
bcache::file::scoped_work_dir_t
A helper class for temporarily changing the current working dir (CWD).
Definition: file_utils.hpp:61
bcache::file::find_executable
exe_path_t find_executable(const std::string &program, const std::string &exclude)
Find the true path to an executable file.
Definition: file_utils.cpp:512
bcache::file::file_info_t::modify_time
time::seconds_t modify_time() const
Definition: file_utils.hpp:92
bcache::file::get_user_home_dir
std::string get_user_home_dir()
Get the user home directory.
Definition: file_utils.cpp:396
bcache::file::exe_path_t::real_path
const std::string & real_path() const
Definition: file_utils.hpp:136
bcache::file::get_file_info
file_info_t get_file_info(const std::string &path)
Get file information about a single file or directory.
Definition: file_utils.cpp:924
bcache::file::file_exists
bool file_exists(const std::string &path)
Check if a file exists.
Definition: file_utils.cpp:648
bcache::file::get_cwd
std::string get_cwd()
Get the current working directory.
Definition: file_utils.cpp:439
bcache::file::get_temp_dir
std::string get_temp_dir()
Get a temporary directory for this user and process.
Definition: file_utils.cpp:360
bcache::file::read
std::string read(const std::string &path)
Read a file into a string.
Definition: file_utils.cpp:775
bcache::file::walk_directory
std::vector< file_info_t > walk_directory(const std::string &path)
Walk a directory and its subdirectories.
Definition: file_utils.cpp:990
bcache::file::remove_dir
void remove_dir(const std::string &path, const bool ignore_errors)
Remove a directory and all its contents (recursively).
Definition: file_utils.cpp:618