FileInfo.h
1 /*
2  * Copyright (C) 2006-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_System_FileInfo_h
30 #define Pt_System_FileInfo_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/Path.h>
34 #include <Pt/DateTime.h>
35 #include <Pt/String.h>
36 #include <Pt/Types.h>
37 #include <string>
38 
39 namespace Pt {
40 
41 namespace System {
42 
103 class PT_SYSTEM_API FileInfo
104 {
105  public:
107  enum Type
108  {
109  Invalid = 0,
110  Directory = 1,
111  File = 2,
112  Link = 4
113  };
114 
116  enum Perms
117  {
118  NoPerms = 0,
119  OwnerRead = 0400,
120  OwnerWrite = 0200,
121  OwnerExec = 0100,
122  OwnerAll = 0700,
123  GroupRead = 040,
124  GroupWrite = 020,
125  GroupExec = 010,
126  GroupAll = 070,
127  OthersRead = 04,
128  OthersWrite = 02,
129  OthersExec = 01,
130  OthersAll = 07,
131  AllPerms = 0777,
132  SetUid = 04000,
133  SetGid = 02000,
134  StickyBit = 01000,
135  PermMask = 07777,
136  UnknownPerms = 0xFFFF
137  };
138 
141  {
142  PermReplace = 1,
143  PermAdd = 2,
144  PermRemove = 4,
145  PermNoFollow = 8
146  };
147 
148  public:
151  {}
152 
155  explicit FileInfo(const Path& path);
156 
159  explicit FileInfo(const Pt::String& path);
160 
163  explicit FileInfo(const char* path);
164 
167  {}
168 
170  void clear();
171 
177  const Path& path() const
178  { return _path; }
179 
182  { return FileInfo::size(_path); }
183 
185  Type type() const
186  { return FileInfo::type(_path); }
187 
188  bool isLink() const
189  { return FileInfo::isLink(_path); }
190 
193  { return FileInfo::permissions(_path); }
194 
195  public:
197  static Type type(const Path& path);
198 
199  static bool isLink(const Path& path);
200 
202  static Pt::uint64_t size(const Path& path);
203 
205  static DateTime lastModified(const Path& path);
206 
208  static Perms permissions(const Path& path);
209 
217  static void permissions(const Path& path, Perms prms,
218  PermOptions opts = PermReplace);
219 
221  static bool exists(const Path& path)
222  { return type(path) != Invalid; }
223 
225  static void createFile(const Path& path);
226 
228  static void createSymlink(const Path& target, const Path& link);
229 
231  static void createHardlink(const Path& target, const Path& link);
232 
234  static void createDirectory(const Path& path);
235 
237  static void createDirectories(const Path& path);
238 
240  static void resize(const Path& path, Pt::uint64_t n);
241 
243  static void remove(const Path& path);
244 
246  static void removeAll(const Pt::System::Path& path);
247 
249  static void move(const Path& path, const Path& to);
250 
251  public:
253  Path& path()
254  { return _path; }
255 
256  private:
257  Path _path;
258 };
259 
260 
266 {
267  return static_cast<FileInfo::Perms>(static_cast<int>(a) | static_cast<int>(b));
268 }
269 
275 {
276  return static_cast<FileInfo::Perms>(static_cast<int>(a) & static_cast<int>(b));
277 }
278 
284 {
285  return static_cast<FileInfo::Perms>(static_cast<int>(a) ^ static_cast<int>(b));
286 }
287 
293 {
294  return static_cast<FileInfo::Perms>(~static_cast<int>(a));
295 }
296 
302 {
303  a = a | b;
304  return a;
305 }
306 
312 {
313  a = a & b;
314  return a;
315 }
316 
322 {
323  return static_cast<FileInfo::PermOptions>(static_cast<int>(a) | static_cast<int>(b));
324 }
325 
326 
331 inline bool operator<(const FileInfo& a, const FileInfo& b)
332 {
333  return a.path() < b.path();
334 }
335 
340 inline bool operator==(const FileInfo& a, const FileInfo& b)
341 {
342  return a.path() == b.path();
343 }
344 
349 inline bool operator!=(const FileInfo& a, const FileInfo& b)
350 {
351  return !(a == b);
352 }
353 
354 } // namespace System
355 
356 } // namespace Pt
357 
358 #endif // Pt_System_FileInfo_h
Type
File-node type.
Definition: FileInfo.h:108
Core module.
Definition: Allocator.h:33
static void remove(const Path &path)
Removes a file or directory.
bool operator==(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:340
static Pt::uint64_t size(const Path &path)
Returns the size of the file in bytes.
Provides information about a node in the file-system.
Definition: FileInfo.h:104
static void removeAll(const Pt::System::Path &path)
Removes a directory and its contents.
FileInfo::Perms operator|(FileInfo::Perms a, FileInfo::Perms b)
Bitwise OR for FileInfo permission flags.
Definition: FileInfo.h:265
Pt::uint64_t size() const
Returns the file size.
Definition: FileInfo.h:181
FileInfo::Perms & operator|=(FileInfo::Perms &a, FileInfo::Perms b)
Bitwise OR assignment for FileInfo permission flags.
Definition: FileInfo.h:301
FileInfo(const Pt::String &path)
Constructs from the path.
~FileInfo()
Destructor.
Definition: FileInfo.h:166
Perms permissions() const
Returns the file permissions.
Definition: FileInfo.h:192
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
FileInfo(const Path &path)
Constructs from the path.
static void createDirectory(const Path &path)
Creates a new directory.
FileInfo::Perms operator^(FileInfo::Perms a, FileInfo::Perms b)
Bitwise XOR for FileInfo permission flags.
Definition: FileInfo.h:283
Type type() const
Returns the file type.
Definition: FileInfo.h:185
static Perms permissions(const Path &path)
Returns the permissions of the file at the path.
const Path & path() const
Returns the full path of node in the file-system.
Definition: FileInfo.h:177
FileInfo()
Default constructor.
Definition: FileInfo.h:150
FileInfo::Perms operator~(FileInfo::Perms a)
Bitwise NOT for FileInfo permission flags.
Definition: FileInfo.h:292
FileInfo::PermOptions operator|(FileInfo::PermOptions a, FileInfo::PermOptions b)
Bitwise OR for FileInfo permission options.
Definition: FileInfo.h:321
Perms
File permission flags.
Definition: FileInfo.h:117
static void move(const Path &path, const Path &to)
Moves a file or directory.
FileInfo(const char *path)
Constructs from the path.
static Type type(const Path &path)
Returns the type of file at the path.
static void resize(const Path &path, Pt::uint64_t n)
Resizes a file.
static void permissions(const Path &path, Perms prms, PermOptions opts=PermReplace)
Sets the permissions of the file at the path.
static void createHardlink(const Path &target, const Path &link)
Creates a hard link at link pointing to target.
Represents a path in the file-system.
Definition: Path.h:48
static void createFile(const Path &path)
Creates a new file.
static void createSymlink(const Path &target, const Path &link)
Creates a symbolic link at link pointing to target.
static DateTime lastModified(const Path &path)
Returns the time when last modified.
bool operator!=(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:349
Unicode capable basic_string.
Definition: Api-String.h:44
static bool exists(const Path &path)
Returns true if a file or directory exists at the path.
Definition: FileInfo.h:221
static void createDirectories(const Path &path)
Creates a new directory.
PermOptions
Options for modifying permissions.
Definition: FileInfo.h:141
void clear()
Clears the state.
FileInfo::Perms & operator&=(FileInfo::Perms &a, FileInfo::Perms b)
Bitwise AND assignment for FileInfo permission flags.
Definition: FileInfo.h:311
FileInfo::Perms operator&(FileInfo::Perms a, FileInfo::Perms b)
Bitwise AND for FileInfo permission flags.
Definition: FileInfo.h:274
Combined Date and Time value.
Definition: DateTime.h:66
bool operator<(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition: FileInfo.h:331