Add new option to recursive_delete: keep_parent_dir
[libi2ncommon] / src / filefunc.hxx
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 */
20 /***************************************************************************
21  *   Copyright (C) 2004-2008 by Intra2net AG                                    *
22  *                                                                         *
23  ***************************************************************************/
24
25 #ifndef __FILEFUNC_HXX
26 #define __FILEFUNC_HXX
27
28 #include "userfunc.hpp"
29 #include <set>
30
31 namespace I2n
32 {
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 {
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;
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);
149 bool file_content_differs(const std::string &old_filename, const std::string &new_filename);
150
151 time_t file_mtime(const std::string& path);
152
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 );
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
160 std::string read_link(const std::string& path);
161
162 std::string read_file(const std::string& path);
163
164 bool write_file(const std::string& path, const std::string& data, bool trunc=true);
165
166 bool copy_file(const std::string& src, const std::string& dest);
167 bool copy_stream(std::istream& is, std::ostream& os);
168
169 std::string basename(const std::string& path);
170
171 std::string dirname(const std::string& path);
172
173 std::string normalize_path(const std::string& path);
174
175 bool dirsync(const std::string& path);
176
177 bool chmod(const std::string& path, int mode);
178 bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
179
180 bool recursive_delete(const std::string &path,
181                       bool keep_parent_dir=false,
182                       std::string *error=NULL);
183
184 std::string mkdtemp(const std::string &path_template, std::string *error=NULL);
185 bool mkdir(const std::string &path, const mode_t &mode=0700, std::string *error=NULL);
186 bool rmdir(const std::string &path, std::string *error=NULL);
187
188 std::string getcwd();
189 bool chdir(const std::string &path, std::string *error=NULL);
190
191 mode_t umask(mode_t mask);
192
193
194 /*
195 ** more specialized tool function(s)
196 */
197 bool remove_unlisted_files(const std::string &directory,
198                            const std::set<std::string> &keep_files,
199                            const std::string &prefix="");
200
201 }
202
203 #endif