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
39namespace Pt {
40
41namespace System {
42
103class PT_SYSTEM_API FileInfo
104{
105 public:
107 enum Type
108 {
111 File = 2,
112 Link = 4
113 };
114
116 enum Perms
117 {
119 OwnerRead = 0400,
120 OwnerWrite = 0200,
121 OwnerExec = 0100,
122 OwnerAll = 0700,
123 GroupRead = 040,
125 GroupExec = 010,
126 GroupAll = 070,
131 AllPerms = 0777,
132 SetUid = 04000,
133 SetGid = 02000,
134 StickyBit = 01000,
135 PermMask = 07777,
136 UnknownPerms = 0xFFFF
137 };
138
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
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
331inline bool operator<(const FileInfo& a, const FileInfo& b)
332{
333 return a.path() < b.path();
334}
335
340inline bool operator==(const FileInfo& a, const FileInfo& b)
341{
342 return a.path() == b.path();
343}
344
349inline 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
Combined Date and Time value.
Definition DateTime.h:66
Unicode capable basic_string.
Definition Api-String.h:67
static void resize(const Path &path, Pt::uint64_t n)
Resizes a file.
static Perms permissions(const Path &path)
Returns the permissions of the file at the path.
FileInfo(const char *path)
Constructs from the path.
Pt::uint64_t size() const
Returns the file size.
Definition FileInfo.h:181
Type
File-node type.
Definition FileInfo.h:108
@ Directory
Directory.
Definition FileInfo.h:110
@ File
Regular file.
Definition FileInfo.h:111
@ Link
Symbolic link.
Definition FileInfo.h:112
@ Invalid
Invalid file type.
Definition FileInfo.h:109
FileInfo()
Default constructor.
Definition FileInfo.h:150
static DateTime lastModified(const Path &path)
Returns the time when last modified.
FileInfo(const Pt::String &path)
Constructs from the path.
FileInfo::Perms operator&(FileInfo::Perms a, FileInfo::Perms b)
Bitwise AND for FileInfo permission flags.
Definition FileInfo.h:274
static void createHardlink(const Path &target, const Path &link)
Creates a hard link at link pointing to target.
static void createDirectory(const Path &path)
Creates a new directory.
const Path & path() const
Returns the full path of node in the file-system.
Definition FileInfo.h:177
static Pt::uint64_t size(const Path &path)
Returns the size of the file in bytes.
bool operator!=(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition FileInfo.h:349
Perms permissions() const
Returns the file permissions.
Definition FileInfo.h:192
static void createFile(const Path &path)
Creates a new file.
static Type type(const Path &path)
Returns the type of file at the path.
static void createDirectories(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
FileInfo::Perms & operator&=(FileInfo::Perms &a, FileInfo::Perms b)
Bitwise AND assignment for FileInfo permission flags.
Definition FileInfo.h:311
static void createSymlink(const Path &target, const Path &link)
Creates a symbolic link at link pointing to target.
PermOptions
Options for modifying permissions.
Definition FileInfo.h:141
@ PermReplace
Replace permissions entirely.
Definition FileInfo.h:142
@ PermAdd
Add permission bits.
Definition FileInfo.h:143
@ PermNoFollow
Do not follow symbolic links.
Definition FileInfo.h:145
@ PermRemove
Remove permission bits.
Definition FileInfo.h:144
FileInfo::PermOptions operator|(FileInfo::PermOptions a, FileInfo::PermOptions b)
Bitwise OR for FileInfo permission options.
Definition FileInfo.h:321
FileInfo::Perms & operator|=(FileInfo::Perms &a, FileInfo::Perms b)
Bitwise OR assignment for FileInfo permission flags.
Definition FileInfo.h:301
static void permissions(const Path &path, Perms prms, PermOptions opts=PermReplace)
Sets the permissions of the file at the path.
FileInfo(const Path &path)
Constructs from the path.
FileInfo::Perms operator|(FileInfo::Perms a, FileInfo::Perms b)
Bitwise OR for FileInfo permission flags.
Definition FileInfo.h:265
bool operator<(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition FileInfo.h:331
FileInfo::Perms operator~(FileInfo::Perms a)
Bitwise NOT for FileInfo permission flags.
Definition FileInfo.h:292
void clear()
Clears the state.
static void move(const Path &path, const Path &to)
Moves a file or directory.
static void removeAll(const Pt::System::Path &path)
Removes a directory and its contents.
Perms
File permission flags.
Definition FileInfo.h:117
@ SetUid
Set-user-ID on execute.
Definition FileInfo.h:132
@ OthersExec
Others execute permission.
Definition FileInfo.h:129
@ PermMask
All valid permission bits.
Definition FileInfo.h:135
@ OwnerExec
Owner execute permission.
Definition FileInfo.h:121
@ GroupRead
Group read permission.
Definition FileInfo.h:123
@ AllPerms
All basic permissions.
Definition FileInfo.h:131
@ GroupAll
All group permissions.
Definition FileInfo.h:126
@ OwnerAll
All owner permissions.
Definition FileInfo.h:122
@ OthersWrite
Others write permission.
Definition FileInfo.h:128
@ SetGid
Set-group-ID on execute.
Definition FileInfo.h:133
@ OwnerRead
Owner read permission.
Definition FileInfo.h:119
@ NoPerms
No permissions.
Definition FileInfo.h:118
@ UnknownPerms
Permissions not known.
Definition FileInfo.h:136
@ OwnerWrite
Owner write permission.
Definition FileInfo.h:120
@ GroupWrite
Group write permission.
Definition FileInfo.h:124
@ GroupExec
Group execute permission.
Definition FileInfo.h:125
@ StickyBit
Sticky bit.
Definition FileInfo.h:134
@ OthersRead
Others read permission.
Definition FileInfo.h:127
@ OthersAll
All others permissions.
Definition FileInfo.h:130
Type type() const
Returns the file type.
Definition FileInfo.h:185
~FileInfo()
Destructor.
Definition FileInfo.h:166
bool operator==(const FileInfo &a, const FileInfo &b)
Compare two FileInfo objects.
Definition FileInfo.h:340
static void remove(const Path &path)
Removes a file or directory.
static bool exists(const Path &path)
Returns true if a file or directory exists at the path.
Definition FileInfo.h:221
Represents a path in the file-system.
Definition Path.h:48
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33