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