]> git.localhorst.tv Git - gong.git/blob - src/io/filesystem.hpp
code, assets, and other stuff stolen from blank
[gong.git] / src / io / filesystem.hpp
1 #ifndef GONG_IO_FILESYSTEM_HPP_
2 #define GONG_IO_FILESYSTEM_HPP_
3
4 #include <ctime>
5 #include <string>
6
7
8 namespace gong {
9 namespace io {
10
11 /// check if give path points to an existing directory
12 bool is_dir(const char *);
13 inline bool is_dir(const std::string &s) {
14         return is_dir(s.c_str());
15 }
16 /// check if give path points to an existing file
17 bool is_file(const char *);
18 inline bool is_file(const std::string &s) {
19         return is_file(s.c_str());
20 }
21 /// get timestamp of last modification
22 std::time_t file_mtime(const char *);
23 inline std::time_t file_mtime(const std::string &s) {
24         return file_mtime(s.c_str());
25 }
26
27 /// create given directory
28 /// @return true if the directory was created
29 ///         the directory might already exist, see errno
30 bool make_dir(const char *);
31 inline bool make_dir(const std::string &s) {
32         return make_dir(s.c_str());
33 }
34 /// create given directory and all parents
35 /// @return true if the directory was created or already exists
36 bool make_dirs(const std::string &);
37
38 /// remove given file
39 /// @return true on success
40 bool remove_file(const std::string &);
41 /// recursively remove given directory
42 /// may leave the directory partially removed on failure
43 /// @return true if the directory was completely removed
44 bool remove_dir(const std::string &);
45
46
47 /// Create a temporary directory with lifetime tie to the instance's.
48 /// Note that the directory may survive its object if removal fails
49 /// for any reason, e.g. another process changing permissions.
50 class TempDir {
51
52 public:
53         TempDir();
54         ~TempDir();
55
56         TempDir(const TempDir &) = delete;
57         TempDir &operator =(const TempDir &) = delete;
58
59 public:
60         const std::string &Path() const noexcept { return path; }
61
62 private:
63         std::string path;
64
65 };
66
67 }
68 }
69
70 #endif