2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 /***************************************************************************
21 * Copyright (C) 2004-2008 by Intra2net AG *
23 ***************************************************************************/
25 #ifndef __FILEFUNC_HXX
26 #define __FILEFUNC_HXX
28 #include "userfunc.hpp"
31 // make sure we have proper large file support
40 * @brief helper class representing a file state.
42 * This basically wraps struct stat and provides some nicer access to the values.
48 Stat(const std::string& path, bool follow_links= true);
53 bool is_valid() const { return Valid; }
55 bool exists() const { return Valid; }
56 std::string path() const { return Path; }
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; }
70 // bytes used on disk, calculated from allocated blocks
71 long long bytes_on_disk() const { return BytesOnDisk; }
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; }
79 ** unix style like type queries:
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; }
91 ** readable style type queries:
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; }
102 bool is_device() const { return IsCharacterDevice or IsBlockDevice; }
106 ** "high level" queries
109 bool is_same_as(const Stat& rhs);
110 bool is_same_device_as(const Stat& rhs);
113 ** convenience methods
116 operator bool() const { return Valid; }
120 void stat(const std::string& path, bool follow_links= true);
137 long long BytesOnDisk;
144 bool IsDirectory : 1;
145 bool IsCharacterDevice : 1;
146 bool IsBlockDevice : 1;
152 ** misc tool functions
153 ** (basically wrappers around system functions)
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);
161 time_t file_mtime(const std::string& path);
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 );
167 bool unlink(const std::string& path);
169 bool symlink(const std::string& target, const std::string& link_name, bool force= false);
171 std::string read_link(const std::string& path);
173 std::string read_file(const std::string& path);
175 bool write_file(const std::string& path, const std::string& data, bool trunc=true);
177 bool copy_file(const std::string& src, const std::string& dest);
178 bool copy_stream(std::istream& is, std::ostream& os);
180 std::string basename(const std::string& path);
182 std::string dirname(const std::string& path);
184 std::string normalize_path(const std::string& path);
186 bool dirsync(const std::string& path);
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());
191 bool recursive_delete(const std::string &path,
192 bool keep_parent_dir=false,
193 std::string *error=NULL);
195 std::string mkdtemp(const std::string &path_template, std::string *error=NULL);
196 // mkstemp: see tmpfstream
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);
201 std::string getcwd();
202 bool chdir(const std::string &path, std::string *error=NULL);
204 mode_t umask(mode_t mask);
206 long long get_free_diskspace(const std::string& path);
207 long long du(const std::string &path, std::string *error=NULL);
210 ** more specialized tool function(s)
212 bool remove_unlisted_files(const std::string &directory,
213 const std::set<std::string> &keep_files,
214 const std::string &prefix="");