LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc_file.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Apache Software Foundation 2019
7
8#pragma once
9
10#include <unistd.h>
11#include <sys/stat.h>
12
13#include <string>
14#include <string_view>
15#include <system_error>
16#include <chrono>
17
18#include "swoc/swoc_version.h"
19#include "swoc/TextView.h"
20
21namespace swoc { inline namespace SWOC_VERSION_NS {
22namespace file {
23
24using file_time_type = std::chrono::system_clock::time_point;
25
26enum class file_type : signed char {
27 none = 0,
28 not_found = -1,
29 regular = 1,
30 directory = 2,
31 symlink = 3,
32 block = 4,
33 character = 5,
34 fifo = 6,
35 socket = 7,
36 unknown = 8
37};
38
40static constexpr int NO_FD = -1;
41
43struct unique_fd {
45
52 explicit unique_fd(int fd) : _fd(fd) {}
53
55 unique_fd(self_type const &) = delete;
56
63 unique_fd(self_type &&that) : _fd(that._fd) { that._fd = NO_FD; }
64
67 if (_fd != NO_FD) {
68 ::close(_fd);
69 _fd = NO_FD;
70 };
71 }
72
77 int
79 auto zret = _fd;
80 _fd = NO_FD;
81 return zret;
82 }
83
85 operator int() const { return _fd; }
86
87protected:
88 int _fd = NO_FD;
89};
90
93class path {
94 using self_type = path;
95
96public:
98 static constexpr char SEPARATOR = '/';
99
101 path() = default;
102
104 path(const self_type &that) = default;
105
107 path(self_type &&that) = default;
108
110 explicit path(const char *src);
111
113 path(std::string_view src);
114
116 path(std::string const &p);
117
119 path(std::string &&that);
120
122 self_type &operator=(const self_type &that) = default;
123
125 self_type &operator=(self_type &&that) = default;
126
128 self_type &operator=(std::string_view p);
129
131 self_type &operator=(std::string const &s);
132
134 self_type &operator=(std::string &&s);
135
137 self_type &operator=(char const *s);
138
147 self_type &operator/=(const self_type &that);
148
157 self_type &operator/=(std::string_view that);
158
160 bool empty() const;
161
163 bool is_absolute() const;
164
166 bool is_relative() const;
167
169 self_type parent_path() const;
170
172 self_type relative_path() const;
173
179 self_type filename() const;
180
182 char const *c_str() const;
183
185 std::string const &string() const;
186
192 self_type &reserve(size_t n);
193
195 swoc::TextView view() const;
196
197protected:
198 std::string _path;
199};
200
203 using self_type = file_status;
204
205public:
207 file_type type() const;
208
210 mode_t mode() const;
211
212protected:
213 struct ::stat _stat;
214 file_type _type = file_type::none;
215
216 void init();
217
218 friend self_type status(const path &file, std::error_code &ec) noexcept;
219 friend int file_type(const self_type &);
220 friend uintmax_t file_size(const self_type &);
221 friend bool is_regular_file(const self_type &);
222 friend bool is_dir(const self_type &);
223 friend bool is_char_device(const self_type &);
224 friend bool is_block_device(const self_type &);
225 friend bool exists(const self_type &);
226 friend file_time_type last_write_time(file_status const &fs);
227 friend file_time_type access_time(file_status const &fs);
228 friend file_time_type status_time(file_status const &fs);
229};
230
237file_status status(const path &file, std::error_code &ec) noexcept;
238
239// Related free functions.
240// These are separate because they are not part of std::filesystem::path.
241
243[[deprecated("Use file_status::type")]] int file_type(const file_status &fs);
244
246bool is_regular_file(const file_status &fs);
247
249bool is_dir(const file_status &p);
250
252bool is_char_device(const file_status &fs);
253
255bool is_block_device(const file_status &fs);
256
258uintmax_t file_size(const file_status &fs);
259
261bool is_readable(const path &s);
262
264bool exists(const path &p);
265
267bool exists(const file_status &fs);
268
279path absolute(path const &src, std::error_code &ec);
280
283
286
288path canonical(const path &p, std::error_code &ec);
289
299bool create_directory(const path &p, std::error_code &ec, mode_t mode = 0775) noexcept;
300
310bool create_directories(const path &p, std::error_code &ec, mode_t mode = 0775) noexcept;
311
321bool copy(const path &from, const path &to, std::error_code &ec);
322
329bool remove(const path &path, std::error_code &ec);
330
337uintmax_t remove_all(const path &path, std::error_code &ec);
338
340[[deprecated("See last_write_time")]] file_time_type modify_time(file_status const &fs);
342file_time_type last_write_time(file_status const &fs);
343
349file_time_type last_write_time(path const &p, std::error_code &ec);
350
352file_time_type access_time(file_status const &fs);
354file_time_type status_time(file_status const &fs);
355
362std::string load(const path &p, std::error_code &ec);
363
364/* ------------------------------------------------------------------- */
365
366inline path::path(char const *src) : _path(src) {}
367
368inline path::path(std::string_view base) : _path(base) {}
369
370inline path::path(std::string const &p) : _path(p) {}
371
372inline path::path(std::string &&that) : _path(std::move(that)) {}
373
374inline path &
375path::operator=(std::string_view p) {
376 _path.assign(p);
377 return *this;
378}
379
380inline path &
381path::operator=(std::string const &s) {
382 _path.assign(s);
383 return *this;
384}
385
386inline path &
387path::operator=(std::string &&s) {
388 _path.assign(std::move(s));
389 return *this;
390}
391
392inline path &
393path::operator=(char const *s) {
394 _path.assign(s);
395 return *this;
396}
397
398inline char const *
399path::c_str() const {
400 return _path.c_str();
401}
402
403inline std::string const &
405 return _path;
406}
407
408inline swoc::TextView
409path::view() const {
410 return {_path};
411}
412
413inline bool
414path::empty() const {
415 return _path.empty();
416}
417
418inline bool
420 return !_path.empty() && '/' == _path[0];
421}
422
423inline bool
425 return !this->is_absolute();
426}
427
428inline path &
429path::operator/=(const self_type &that) {
430 return *this /= std::string_view(that._path);
431}
432
437inline bool
438operator==(path const &lhs, path const &rhs) {
439 return lhs.view() == rhs.view();
440}
441
446inline bool
447operator!=(path const &lhs, path const &rhs) {
448 return lhs.view() != rhs.view();
449}
450
455inline path
456operator/(const path &lhs, const path &rhs) {
457 return path(lhs) /= rhs;
458}
459
464inline path
465operator/(path &&lhs, const path &rhs) {
466 return path(std::move(lhs)) /= rhs;
467}
468
473inline path
474operator/(const path &lhs, std::string_view rhs) {
475 return path(lhs) /= rhs;
476}
477
478inline auto
479path::reserve(size_t n) -> self_type & {
480 _path.reserve(n);
481 return *this;
482}
483
488inline path
489operator/(path &&lhs, std::string_view rhs) {
490 return path(std::move(lhs)) /= rhs;
491}
492
493// Can remove "enum" after the function @c file_type is removed.
494inline enum file_type
496 return _type;
497}
498
499inline mode_t
501 return _stat.st_mode;
502}
503
504inline bool
506 return fs._type == file_type::character;
507}
508
509inline bool
511 return fs._type == file_type::block;
512}
513
514inline bool
516 return fs._type == file_type::regular;
517}
518
519inline bool
520is_dir(const file_status &fs) {
521 return fs._type == file_type::directory;
522}
523
524inline bool
525exists(const file_status &fs) {
526 return fs._type != file_type::none && fs._type != file_type::not_found;
527}
528
529inline file_time_type
531 return last_write_time(fs);
532}
533} // namespace file
534
535class BufferWriter;
536namespace bwf {
537struct Spec;
538}
539
540BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, file::path const &p);
541}} // namespace swoc::SWOC_VERSION_NS
542
543namespace std {
545template <> struct hash<swoc::file::path> {
546 size_t
547 operator()(swoc::file::path const &path) const {
548 return hash<string_view>()(path.view());
549 }
550};
551} // namespace std
Information about a file.
Definition swoc_file.h:202
friend bool is_dir(const self_type &)
Check if the path is to a directory.
Definition swoc_file.h:520
friend file_time_type last_write_time(file_status const &fs)
Definition swoc_file.cc:195
friend bool is_block_device(const self_type &)
Check if the path is to a block device.
Definition swoc_file.h:510
friend file_time_type access_time(file_status const &fs)
Definition swoc_file.cc:200
friend bool is_regular_file(const self_type &)
Check if the path is to a regular file.
Definition swoc_file.h:515
friend bool exists(const self_type &)
Check if path exists.
Definition swoc_file.h:525
friend bool is_char_device(const self_type &)
Check if the path is to a character device.
Definition swoc_file.h:505
struct::stat _stat
File information.
Definition swoc_file.h:213
file_type type() const
Definition swoc_file.h:495
friend uintmax_t file_size(const self_type &)
Size of the file or block device.
Definition swoc_file.cc:112
friend self_type status(const path &file, std::error_code &ec) noexcept
Definition swoc_file.cc:92
mode_t mode() const
Definition swoc_file.h:500
friend int file_type(const self_type &)
Return the file type value.
Definition swoc_file.cc:107
friend file_time_type status_time(file_status const &fs)
Definition swoc_file.cc:205
self_type & operator=(const self_type &that)=default
Replace the path with a copy of that.
bool empty() const
Check if the path is empty.
Definition swoc_file.h:414
self_type & operator=(self_type &&that)=default
Replace the path with the contents of that.
static constexpr char SEPARATOR
Default path separator.
Definition swoc_file.h:98
path(self_type &&that)=default
Move constructor.
path(const self_type &that)=default
Copy constructor - copies the path.
swoc::TextView view() const
A view of the path.
Definition swoc_file.h:409
self_type & reserve(size_t n)
Definition swoc_file.h:479
char const * c_str() const
Access the path explicitly.
Definition swoc_file.h:399
self_type relative_path() const
Definition swoc_file.cc:30
self_type filename() const
Definition swoc_file.cc:38
self_type & operator/=(const self_type &that)
Definition swoc_file.h:429
std::string _path
File path.
Definition swoc_file.h:198
self_type parent_path() const
Path of the parent.
Definition swoc_file.cc:23
bool is_relative() const
Check if the path is not absolute.
Definition swoc_file.h:424
std::string const & string() const
The path as a string.
Definition swoc_file.h:404
bool is_absolute() const
Check if the path is absolute.
Definition swoc_file.h:419
path()=default
Default construct empty path.
STL namespace.
For template deduction guides.
Definition ArenaWriter.cc:9
BufferWriter & bwformat(BufferWriter &w, bwf::Spec const &spec, std::string_view sv)
Definition bw_format.cc:649
unique_fd self_type
Self reference type.
Definition swoc_file.h:44
unique_fd(self_type const &)=delete
Non-copyable.
~unique_fd()
Close the file dscriptor.
Definition swoc_file.h:66
unique_fd(self_type &&that)
Definition swoc_file.h:63
bool exists(const path &p)
Check if path exists.
Definition swoc_file.cc:117
uintmax_t file_size(const file_status &fs)
Size of the file or block device.
Definition swoc_file.cc:112
path temp_directory_path()
Directory location suitable for temporary files.
Definition swoc_file.cc:224
uintmax_t remove_all(const path &p, std::error_code &ec)
Definition swoc_file.cc:375
file_time_type last_write_time(file_status const &fs)
Definition swoc_file.cc:195
std::string load(const path &p, std::error_code &ec)
Definition swoc_file.cc:454
bool copy(const path &from, const path &to, std::error_code &ec)
Definition swoc_file.cc:327
bool create_directories(const path &p, std::error_code &ec, mode_t mode) noexcept
Definition swoc_file.cc:294
file_time_type access_time(file_status const &fs)
Definition swoc_file.cc:200
bool is_readable(const path &p)
Check if file is readable.
Definition swoc_file.cc:219
path absolute(path const &src, std::error_code &ec)
Definition swoc_file.cc:124
path canonical(const path &p, std::error_code &ec)
Definition swoc_file.cc:251
bool create_directory(const path &path, std::error_code &ec, mode_t mode) noexcept
Definition swoc_file.cc:272
bool remove(path const &p, std::error_code &ec)
Definition swoc_file.cc:430
file_status status(path const &file, std::error_code &ec) noexcept
Definition swoc_file.cc:92
path current_path()
Current working directory.
Definition swoc_file.cc:237
file_time_type status_time(file_status const &fs)
Definition swoc_file.cc:205
file_time_type modify_time(file_status const &fs)
Definition swoc_file.h:530
path operator/(const path &lhs, const path &rhs)
Definition swoc_file.h:456
bool is_block_device(const file_status &fs)
Check if the path is to a block device.
Definition swoc_file.h:510
bool is_char_device(const file_status &fs)
Check if the path is to a character device.
Definition swoc_file.h:505
bool operator!=(path const &lhs, path const &rhs)
Definition swoc_file.h:447
bool operator==(path const &lhs, path const &rhs)
Definition swoc_file.h:438
bool is_regular_file(const file_status &fs)
Check if the path is to a regular file.
Definition swoc_file.h:515
bool is_dir(const file_status &p)
Check if the path is to a directory.
Definition swoc_file.h:520