b9f2d4489aff24b46b73cdf05306c6830b03bba1
[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 // make sure we have proper large file support
32 #include <features.h>
33 #ifndef _GNU_SOURCE
34     #define _GNU_SOURCE
35 #endif
36
37 namespace I2n
38 {
39 /**
40  * @brief helper class representing a file state.
41  *
42  * This basically wraps struct stat and provides some nicer access to the values.
43  */
44 class Stat
45 {
46 public:
47       Stat();
48       Stat(const std::string& path, bool follow_links= true);
49       ~Stat();
50
51    void recheck();
52
53    bool is_valid() const { return Valid; }
54
55    bool exists() const { return Valid; }
56    std::string path() const { return Path; }
57
58    dev_t   device() const { return Device; }
59    ino_t   inode() const { return Inode; }
60    mode_t  mode() const { return Mode; }
61    nlink_t nlink() const { return NumLinks; }
62    uid_t   uid() const { return Uid; }
63    gid_t   gid() const { return Gid; }
64    dev_t   rdev() const { return DeviceType; }
65    off_t   size() const { return Size; }
66    time_t  atime() const { return Atime; }
67    time_t  mtime() const { return Mtime; }
68    time_t  ctime() const { return Ctime; }
69
70    // bytes used on disk, calculated from allocated blocks
71    long long bytes_on_disk() const { return BytesOnDisk; }
72
73    nlink_t num_hard_links() const { return NumLinks; }
74    dev_t   device_type() const { return DeviceType; }
75    time_t  last_modified_time() const { return Mtime; }
76    time_t  created_time() const { return Ctime; }
77
78    /*
79    ** unix style like type queries:
80    */
81
82    bool is_lnk() const { return IsLink; }
83    bool is_reg() const { return IsRegular; }
84    bool is_dir() const { return IsDirectory; }
85    bool is_chr() const { return IsCharacterDevice; }
86    bool is_blk() const { return IsBlockDevice; }
87    bool is_fifo() const { return IsFifo; }
88    bool is_sock() const { return IsSocket; }
89
90    /*
91    **  readable style type queries:
92    */
93
94    bool is_link () const { return IsLink; }
95    bool is_regular() const { return IsRegular; }
96    bool is_regular_file() const { return IsRegular; }
97    bool is_directory() const { return IsDirectory; }
98    bool is_character_device() const {return IsCharacterDevice; }
99    bool is_block_device() const { return IsBlockDevice; }
100    bool is_socket() const { return IsSocket; }
101
102    bool is_device() const { return IsCharacterDevice or IsBlockDevice; }
103
104
105    /*
106    ** "high level" queries
107    */
108
109    bool is_same_as(const Stat& rhs);
110    bool is_same_device_as(const Stat& rhs);
111
112    /*
113    ** convenience methods
114    */
115
116    operator bool() const { return Valid; }
117
118 protected:
119
120    void stat(const std::string& path, bool follow_links= true);
121    void clear();
122
123 protected:
124    std::string Path;
125    bool FollowLinks;
126
127    bool Valid;
128
129    dev_t   Device;
130    ino_t   Inode;
131    mode_t  Mode;
132    nlink_t NumLinks;
133    uid_t   Uid;
134    gid_t   Gid;
135    dev_t   DeviceType;
136    off_t   Size;
137    long long BytesOnDisk;
138    time_t  Atime;
139    time_t  Mtime;
140    time_t  Ctime;
141
142    bool IsLink : 1;
143    bool IsRegular : 1;
144    bool IsDirectory : 1;
145    bool IsCharacterDevice : 1;
146    bool IsBlockDevice : 1;
147    bool IsFifo : 1;
148    bool IsSocket : 1;
149 }; // eo class Stat
150
151 /*
152 ** misc tool functions
153 ** (basically wrappers around system functions)
154 */
155
156 bool path_exists(const std::string& path);
157 bool file_exists(const std::string& path);
158 long file_size (const std::string &name);
159 bool file_content_differs(const std::string &old_filename, const std::string &new_filename);
160
161 time_t file_mtime(const std::string& path);
162
163 bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
164 std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false );
165 int get_dir_size(const std::string& path, bool include_dot_names= false );
166
167 bool unlink(const std::string& path);
168
169 bool symlink(const std::string& target, const std::string& link_name, bool force= false);
170
171 std::string read_link(const std::string& path);
172
173 std::string read_file(const std::string& path);
174
175 bool write_file(const std::string& path, const std::string& data, bool trunc=true);
176
177 bool copy_file(const std::string& src, const std::string& dest);
178 bool copy_stream(std::istream& is, std::ostream& os);
179
180 std::string basename(const std::string& path);
181
182 std::string dirname(const std::string& path);
183
184 std::string normalize_path(const std::string& path);
185
186 bool dirsync(const std::string& path);
187
188 bool chmod(const std::string& path, int mode);
189 bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
190
191 bool recursive_delete(const std::string &path,
192                       bool keep_parent_dir=false,
193                       std::string *error=NULL);
194
195 std::string mkdtemp(const std::string &path_template, std::string *error=NULL);
196 // mkstemp: see tmpfstream
197
198 bool mkdir(const std::string &path, const mode_t &mode=0700, std::string *error=NULL);
199 bool rmdir(const std::string &path, std::string *error=NULL);
200
201 std::string getcwd();
202 bool chdir(const std::string &path, std::string *error=NULL);
203
204 mode_t umask(mode_t mask);
205
206 long long get_free_diskspace(const std::string& path);
207 long long du(const std::string &path, std::string *error=NULL);
208
209 /*
210 ** more specialized tool function(s)
211 */
212 bool remove_unlisted_files(const std::string &directory,
213                            const std::set<std::string> &keep_files,
214                            const std::string &prefix="");
215
216 }
217
218 #endif