Rename get_dir_size() to get_dir_count()
[libi2ncommon] / src / filefunc.hxx
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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
a9f2cccc
GE
31// make sure we have proper large file support
32#include <features.h>
33#ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35#endif
36
a287a306 37namespace I2n
6a93d84a 38{
6a93d84a
TJ
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 */
44class Stat
45{
a287a306
TJ
46public:
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
a9f2cccc
GE
70 // bytes used on disk, calculated from allocated blocks
71 long long bytes_on_disk() const { return BytesOnDisk; }
72
a287a306
TJ
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
118protected:
119
120 void stat(const std::string& path, bool follow_links= true);
121 void clear();
122
123protected:
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;
a9f2cccc 137 long long BytesOnDisk;
a287a306
TJ
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;
6a93d84a
TJ
149}; // eo class Stat
150
151/*
152** misc tool functions
153** (basically wrappers around system functions)
154*/
155
156bool path_exists(const std::string& path);
157bool file_exists(const std::string& path);
158long file_size (const std::string &name);
9d1eb8c8 159bool file_content_differs(const std::string &old_filename, const std::string &new_filename);
6a93d84a 160
a287a306 161time_t file_mtime(const std::string& path);
6a93d84a 162
a287a306
TJ
163bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
164std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false );
3f8a0632 165int get_dir_count(const std::string& path, bool include_dot_names= false );
6a93d84a
TJ
166
167bool unlink(const std::string& path);
168
169bool symlink(const std::string& target, const std::string& link_name, bool force= false);
170
a287a306 171std::string read_link(const std::string& path);
6a93d84a 172
a287a306 173std::string read_file(const std::string& path);
6a93d84a 174
71f7bfb8 175bool write_file(const std::string& path, const std::string& data, bool trunc=true);
6a93d84a 176
1a2fc4e8 177bool copy_file(const std::string& src, const std::string& dest);
a1e7d2a7 178bool copy_stream(std::istream& is, std::ostream& os);
1a2fc4e8 179
6a93d84a
TJ
180std::string basename(const std::string& path);
181
182std::string dirname(const std::string& path);
183
a287a306 184std::string normalize_path(const std::string& path);
6a93d84a 185
f002679a
GE
186bool dirsync(const std::string& path);
187
6a93d84a 188bool chmod(const std::string& path, int mode);
a287a306 189bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
e93545dd 190
997987a6
TJ
191bool recursive_delete(const std::string &path,
192 bool keep_parent_dir=false,
193 std::string *error=NULL);
0a654ec0 194
44725532 195std::string mkdtemp(const std::string &path_template, std::string *error=NULL);
9513f841
CH
196// mkstemp: see tmpfstream
197
44725532
TJ
198bool mkdir(const std::string &path, const mode_t &mode=0700, std::string *error=NULL);
199bool rmdir(const std::string &path, std::string *error=NULL);
200
201std::string getcwd();
202bool chdir(const std::string &path, std::string *error=NULL);
faf8475b 203
901e2943
TJ
204mode_t umask(mode_t mask);
205
34ed5e5e 206long long get_free_diskspace(const std::string& path);
fcadd7ba 207long long du(const std::string &path, std::string *error=NULL);
deab8f59
CH
208
209/*
210** more specialized tool function(s)
211*/
997987a6
TJ
212bool remove_unlisted_files(const std::string &directory,
213 const std::set<std::string> &keep_files,
214 const std::string &prefix="");
deab8f59 215
6a93d84a
TJ
216}
217
e93545dd 218#endif