libi2ncommon: (tomj) added copy_file function
[libi2ncommon] / src / filefunc.hxx
1 /***************************************************************************
2  *   Copyright (C) 2004-2008 by Intra2net AG                                    *
3  *   info@intra2net.com                                                    *
4  *                                                                         *
5  ***************************************************************************/
6
7 #ifndef __FILEFUNC_HXX
8 #define __FILEFUNC_HXX
9
10 #include "userfunc.hpp"
11
12 namespace I2n
13 {
14 /**
15  * @brief helper class representing a file state.
16  *
17  * This basically wraps struct stat and provides some nicer access to the values.
18  */
19 class Stat
20 {
21 public:
22       Stat();
23       Stat(const std::string& path, bool follow_links= true);
24       ~Stat();
25
26    void recheck();
27
28    bool is_valid() const { return Valid; }
29
30    bool exists() const { return Valid; }
31    std::string path() const { return Path; }
32
33    dev_t   device() const { return Device; }
34    ino_t   inode() const { return Inode; }
35    mode_t  mode() const { return Mode; }
36    nlink_t nlink() const { return NumLinks; }
37    uid_t   uid() const { return Uid; }
38    gid_t   gid() const { return Gid; }
39    dev_t   rdev() const { return DeviceType; }
40    off_t   size() const { return Size; }
41    time_t  atime() const { return Atime; }
42    time_t  mtime() const { return Mtime; }
43    time_t  ctime() const { return Ctime; }
44
45    nlink_t num_hard_links() const { return NumLinks; }
46    dev_t   device_type() const { return DeviceType; }
47    time_t  last_modified_time() const { return Mtime; }
48    time_t  created_time() const { return Ctime; }
49
50    /*
51    ** unix style like type queries:
52    */
53
54    bool is_lnk() const { return IsLink; }
55    bool is_reg() const { return IsRegular; }
56    bool is_dir() const { return IsDirectory; }
57    bool is_chr() const { return IsCharacterDevice; }
58    bool is_blk() const { return IsBlockDevice; }
59    bool is_fifo() const { return IsFifo; }
60    bool is_sock() const { return IsSocket; }
61
62    /*
63    **  readable style type queries:
64    */
65
66    bool is_link () const { return IsLink; }
67    bool is_regular() const { return IsRegular; }
68    bool is_regular_file() const { return IsRegular; }
69    bool is_directory() const { return IsDirectory; }
70    bool is_character_device() const {return IsCharacterDevice; }
71    bool is_block_device() const { return IsBlockDevice; }
72    bool is_socket() const { return IsSocket; }
73
74    bool is_device() const { return IsCharacterDevice or IsBlockDevice; }
75
76
77    /*
78    ** "high level" queries
79    */
80
81    bool is_same_as(const Stat& rhs);
82    bool is_same_device_as(const Stat& rhs);
83
84    /*
85    ** convenience methods
86    */
87
88    operator bool() const { return Valid; }
89
90 protected:
91
92    void stat(const std::string& path, bool follow_links= true);
93    void clear();
94
95 protected:
96    std::string Path;
97    bool FollowLinks;
98
99    bool Valid;
100
101    dev_t   Device;
102    ino_t   Inode;
103    mode_t  Mode;
104    nlink_t NumLinks;
105    uid_t   Uid;
106    gid_t   Gid;
107    dev_t   DeviceType;
108    off_t   Size;
109    time_t  Atime;
110    time_t  Mtime;
111    time_t  Ctime;
112
113    bool IsLink : 1;
114    bool IsRegular : 1;
115    bool IsDirectory : 1;
116    bool IsCharacterDevice : 1;
117    bool IsBlockDevice : 1;
118    bool IsFifo : 1;
119    bool IsSocket : 1;
120 }; // eo class Stat
121
122 /*
123 ** misc tool functions
124 ** (basically wrappers around system functions)
125 */
126
127 bool path_exists(const std::string& path);
128 bool file_exists(const std::string& path);
129 long file_size (const std::string &name);
130
131 time_t file_mtime(const std::string& path);
132
133 bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
134 std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false );
135
136 bool unlink(const std::string& path);
137
138 bool symlink(const std::string& target, const std::string& link_name, bool force= false);
139
140 std::string read_link(const std::string& path);
141
142 std::string read_file(const std::string& path);
143
144 bool write_file(const std::string& path, const std::string& data);
145
146 bool copy_file(const std::string& src, const std::string& dest);
147
148 std::string basename(const std::string& path);
149
150 std::string dirname(const std::string& path);
151
152 std::string normalize_path(const std::string& path);
153
154 bool chmod(const std::string& path, int mode);
155 bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
156
157 bool recursive_delete(const std::string &path, std::string *error=NULL);
158
159 }
160
161 #endif