Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
e93545dd | 20 | /*************************************************************************** |
6a93d84a | 21 | * Copyright (C) 2004-2008 by Intra2net AG * |
e93545dd GE |
22 | * * |
23 | ***************************************************************************/ | |
24 | ||
25 | #ifndef __FILEFUNC_HXX | |
26 | #define __FILEFUNC_HXX | |
27 | ||
90246b4a | 28 | #include "userfunc.hpp" |
deab8f59 | 29 | #include <set> |
0107a75b | 30 | |
a287a306 | 31 | namespace I2n |
6a93d84a | 32 | { |
6a93d84a TJ |
33 | /** |
34 | * @brief helper class representing a file state. | |
35 | * | |
36 | * This basically wraps struct stat and provides some nicer access to the values. | |
37 | */ | |
38 | class Stat | |
39 | { | |
a287a306 TJ |
40 | public: |
41 | Stat(); | |
42 | Stat(const std::string& path, bool follow_links= true); | |
43 | ~Stat(); | |
44 | ||
45 | void recheck(); | |
46 | ||
47 | bool is_valid() const { return Valid; } | |
48 | ||
49 | bool exists() const { return Valid; } | |
50 | std::string path() const { return Path; } | |
51 | ||
52 | dev_t device() const { return Device; } | |
53 | ino_t inode() const { return Inode; } | |
54 | mode_t mode() const { return Mode; } | |
55 | nlink_t nlink() const { return NumLinks; } | |
56 | uid_t uid() const { return Uid; } | |
57 | gid_t gid() const { return Gid; } | |
58 | dev_t rdev() const { return DeviceType; } | |
59 | off_t size() const { return Size; } | |
60 | time_t atime() const { return Atime; } | |
61 | time_t mtime() const { return Mtime; } | |
62 | time_t ctime() const { return Ctime; } | |
63 | ||
64 | nlink_t num_hard_links() const { return NumLinks; } | |
65 | dev_t device_type() const { return DeviceType; } | |
66 | time_t last_modified_time() const { return Mtime; } | |
67 | time_t created_time() const { return Ctime; } | |
68 | ||
69 | /* | |
70 | ** unix style like type queries: | |
71 | */ | |
72 | ||
73 | bool is_lnk() const { return IsLink; } | |
74 | bool is_reg() const { return IsRegular; } | |
75 | bool is_dir() const { return IsDirectory; } | |
76 | bool is_chr() const { return IsCharacterDevice; } | |
77 | bool is_blk() const { return IsBlockDevice; } | |
78 | bool is_fifo() const { return IsFifo; } | |
79 | bool is_sock() const { return IsSocket; } | |
80 | ||
81 | /* | |
82 | ** readable style type queries: | |
83 | */ | |
84 | ||
85 | bool is_link () const { return IsLink; } | |
86 | bool is_regular() const { return IsRegular; } | |
87 | bool is_regular_file() const { return IsRegular; } | |
88 | bool is_directory() const { return IsDirectory; } | |
89 | bool is_character_device() const {return IsCharacterDevice; } | |
90 | bool is_block_device() const { return IsBlockDevice; } | |
91 | bool is_socket() const { return IsSocket; } | |
92 | ||
93 | bool is_device() const { return IsCharacterDevice or IsBlockDevice; } | |
94 | ||
95 | ||
96 | /* | |
97 | ** "high level" queries | |
98 | */ | |
99 | ||
100 | bool is_same_as(const Stat& rhs); | |
101 | bool is_same_device_as(const Stat& rhs); | |
102 | ||
103 | /* | |
104 | ** convenience methods | |
105 | */ | |
106 | ||
107 | operator bool() const { return Valid; } | |
108 | ||
109 | protected: | |
110 | ||
111 | void stat(const std::string& path, bool follow_links= true); | |
112 | void clear(); | |
113 | ||
114 | protected: | |
115 | std::string Path; | |
116 | bool FollowLinks; | |
117 | ||
118 | bool Valid; | |
119 | ||
120 | dev_t Device; | |
121 | ino_t Inode; | |
122 | mode_t Mode; | |
123 | nlink_t NumLinks; | |
124 | uid_t Uid; | |
125 | gid_t Gid; | |
126 | dev_t DeviceType; | |
127 | off_t Size; | |
128 | time_t Atime; | |
129 | time_t Mtime; | |
130 | time_t Ctime; | |
131 | ||
132 | bool IsLink : 1; | |
133 | bool IsRegular : 1; | |
134 | bool IsDirectory : 1; | |
135 | bool IsCharacterDevice : 1; | |
136 | bool IsBlockDevice : 1; | |
137 | bool IsFifo : 1; | |
138 | bool IsSocket : 1; | |
6a93d84a TJ |
139 | }; // eo class Stat |
140 | ||
141 | /* | |
142 | ** misc tool functions | |
143 | ** (basically wrappers around system functions) | |
144 | */ | |
145 | ||
146 | bool path_exists(const std::string& path); | |
147 | bool file_exists(const std::string& path); | |
148 | long file_size (const std::string &name); | |
9d1eb8c8 | 149 | bool file_content_differs(const std::string &old_filename, const std::string &new_filename); |
6a93d84a | 150 | |
a287a306 | 151 | time_t file_mtime(const std::string& path); |
6a93d84a | 152 | |
a287a306 TJ |
153 | bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false ); |
154 | std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false ); | |
6a93d84a TJ |
155 | |
156 | bool unlink(const std::string& path); | |
157 | ||
158 | bool symlink(const std::string& target, const std::string& link_name, bool force= false); | |
159 | ||
a287a306 | 160 | std::string read_link(const std::string& path); |
6a93d84a | 161 | |
a287a306 | 162 | std::string read_file(const std::string& path); |
6a93d84a | 163 | |
71f7bfb8 | 164 | bool write_file(const std::string& path, const std::string& data, bool trunc=true); |
6a93d84a | 165 | |
1a2fc4e8 | 166 | bool copy_file(const std::string& src, const std::string& dest); |
a1e7d2a7 | 167 | bool copy_stream(std::istream& is, std::ostream& os); |
1a2fc4e8 | 168 | |
6a93d84a TJ |
169 | std::string basename(const std::string& path); |
170 | ||
171 | std::string dirname(const std::string& path); | |
172 | ||
a287a306 | 173 | std::string normalize_path(const std::string& path); |
6a93d84a | 174 | |
f002679a GE |
175 | bool dirsync(const std::string& path); |
176 | ||
6a93d84a | 177 | bool chmod(const std::string& path, int mode); |
a287a306 | 178 | bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group()); |
e93545dd | 179 | |
0a654ec0 TJ |
180 | bool recursive_delete(const std::string &path, std::string *error=NULL); |
181 | ||
44725532 TJ |
182 | std::string mkdtemp(const std::string &path_template, std::string *error=NULL); |
183 | bool mkdir(const std::string &path, const mode_t &mode=0700, std::string *error=NULL); | |
184 | bool rmdir(const std::string &path, std::string *error=NULL); | |
185 | ||
186 | std::string getcwd(); | |
187 | bool chdir(const std::string &path, std::string *error=NULL); | |
faf8475b | 188 | |
901e2943 TJ |
189 | mode_t umask(mode_t mask); |
190 | ||
deab8f59 CH |
191 | |
192 | /* | |
193 | ** more specialized tool function(s) | |
194 | */ | |
195 | bool remove_unlisted_files(const std::string &directory, const std::set<std::string> &keep_files, | |
196 | const std::string &prefix=""); | |
197 | ||
6a93d84a TJ |
198 | } |
199 | ||
e93545dd | 200 | #endif |