Add more filesystem wrappers that DoM needs.
[libdcp.git] / src / filesystem.h
1 /*
2     Copyright (C) 2012-2023 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #ifndef LIBDCP_FILESYSTEM_H
36 #define LIBDCP_FILESYSTEM_H
37
38
39 #include <boost/filesystem.hpp>
40
41
42 namespace dcp
43 {
44 namespace filesystem
45 {
46
47 boost::filesystem::path absolute(boost::filesystem::path const& path);
48 boost::filesystem::path canonical(boost::filesystem::path const& path);
49 boost::filesystem::path weakly_canonical(boost::filesystem::path const& path);
50 boost::filesystem::path change_extension(boost::filesystem::path const& from, std::string const& new_extension);
51 void copy(boost::filesystem::path const& from, boost::filesystem::path const& to);
52 void copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to);
53 void copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec);
54 void copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::filesystem::copy_option ec);
55 bool create_directory(boost::filesystem::path const& path);
56 bool create_directory(boost::filesystem::path const& path, boost::system::error_code& ec);
57 bool create_directories(boost::filesystem::path const& path);
58 bool create_directories(boost::filesystem::path const& path, boost::system::error_code& ec);
59 void create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to);
60 void create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec);
61 void create_symlink(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec);
62 void current_path(boost::filesystem::path const& path);
63 boost::filesystem::path current_path();
64 bool exists(boost::filesystem::path const& path);
65 bool exists(boost::filesystem::path const& path, boost::system::error_code& ec);
66 std::string extension(boost::filesystem::path const& path);
67 bool is_directory(boost::filesystem::path const& path);
68 bool is_empty(boost::filesystem::path const& path);
69 bool is_regular_file(boost::filesystem::path const& path);
70 uintmax_t file_size(boost::filesystem::path const& path);
71 uintmax_t file_size(boost::filesystem::path const& path, boost::system::error_code& ec);
72 uintmax_t hard_link_count(boost::filesystem::path const& path);
73 std::time_t last_write_time(boost::filesystem::path const& path);
74 std::time_t last_write_time(boost::filesystem::path const& path, boost::system::error_code& ec);
75 bool remove(boost::filesystem::path const& path);
76 bool remove(boost::filesystem::path const& path, boost::system::error_code& ec);
77 uintmax_t remove_all(boost::filesystem::path const& path);
78 uintmax_t remove_all(boost::filesystem::path const& path, boost::system::error_code& ec);
79 void rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path);
80 void rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path, boost::system::error_code& ec);
81 boost::filesystem::space_info space(boost::filesystem::path const& path);
82
83
84 class directory_entry
85 {
86 public:
87         directory_entry() {}
88         directory_entry(boost::filesystem::path const& path);
89
90         boost::filesystem::path path() const;
91         operator boost::filesystem::path const&() const;
92
93 private:
94         boost::filesystem::path _path;
95 };
96
97
98 class directory_iterator
99 {
100 public:
101         directory_iterator() = default;
102         directory_iterator(boost::filesystem::path const& path);
103         directory_iterator(boost::filesystem::path const& path, boost::system::error_code& ec);
104
105         directory_iterator& operator++();
106         directory_entry operator*() const;
107         directory_entry* operator->() const;
108         bool operator!=(directory_iterator const& other) const;
109
110 private:
111         boost::filesystem::directory_iterator _wrapped;
112         mutable directory_entry _entry;
113 };
114
115
116 directory_iterator const& begin(directory_iterator const& iter);
117 directory_iterator end(directory_iterator const&);
118
119
120
121 class recursive_directory_iterator
122 {
123 public:
124         recursive_directory_iterator() = default;
125         recursive_directory_iterator(boost::filesystem::path const& path);
126
127         recursive_directory_iterator& operator++();
128         directory_entry operator*() const;
129         directory_entry* operator->() const;
130         bool operator!=(recursive_directory_iterator const& other) const;
131
132 private:
133         boost::filesystem::recursive_directory_iterator _wrapped;
134         mutable directory_entry _entry;
135 };
136
137
138 recursive_directory_iterator const& begin(recursive_directory_iterator const& iter);
139 recursive_directory_iterator end(recursive_directory_iterator const&);
140
141
142 boost::filesystem::path fix_long_path(boost::filesystem::path long_path);
143 boost::filesystem::path unfix_long_path(boost::filesystem::path long_path);
144
145 }
146 }
147
148
149 #endif