Switch license from Intranator license to GPLv2 + linking exception (ACKed by Steffen)
[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"
0107a75b 29
a287a306 30namespace I2n
6a93d84a 31{
6a93d84a
TJ
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 */
37class Stat
38{
a287a306
TJ
39public:
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
108protected:
109
110 void stat(const std::string& path, bool follow_links= true);
111 void clear();
112
113protected:
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;
6a93d84a
TJ
138}; // eo class Stat
139
140/*
141** misc tool functions
142** (basically wrappers around system functions)
143*/
144
145bool path_exists(const std::string& path);
146bool file_exists(const std::string& path);
147long file_size (const std::string &name);
148
a287a306 149time_t file_mtime(const std::string& path);
6a93d84a 150
a287a306
TJ
151bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
152std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false );
6a93d84a
TJ
153
154bool unlink(const std::string& path);
155
156bool symlink(const std::string& target, const std::string& link_name, bool force= false);
157
a287a306 158std::string read_link(const std::string& path);
6a93d84a 159
a287a306 160std::string read_file(const std::string& path);
6a93d84a 161
a287a306 162bool write_file(const std::string& path, const std::string& data);
6a93d84a 163
1a2fc4e8 164bool copy_file(const std::string& src, const std::string& dest);
a1e7d2a7 165bool copy_stream(std::istream& is, std::ostream& os);
1a2fc4e8 166
6a93d84a
TJ
167std::string basename(const std::string& path);
168
169std::string dirname(const std::string& path);
170
a287a306 171std::string normalize_path(const std::string& path);
6a93d84a 172
f002679a
GE
173bool dirsync(const std::string& path);
174
6a93d84a 175bool chmod(const std::string& path, int mode);
a287a306 176bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
e93545dd 177
0a654ec0
TJ
178bool recursive_delete(const std::string &path, std::string *error=NULL);
179
6a93d84a
TJ
180}
181
e93545dd 182#endif