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