libcommonc++  0.7
File.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_File_hxx
24 #define __ccxx_File_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/FileName.h++>
29 #include <commonc++/Stream.h++>
30 #include <commonc++/String.h++>
31 
32 namespace ccxx {
33 
35 enum FileType {
50 };
51 
64 };
65 
72 {
75  : type(TypeUnknown),
76  created(INT64_CONST(0)),
77  modified(INT64_CONST(0)),
78  accessed(INT64_CONST(0)),
79  size(UINT64_CONST(0))
80  { }
81 
91  uint64_t size;
94 
96  inline bool isDirectory() const
97  { return(type == TypeDir); }
98 
100  inline bool isFile() const
101  { return(type == TypeFile); }
102 };
103 
105 enum LockType {
110 };
111 
118 class COMMONCPP_API File : public Stream
119 {
120  public:
121 
127  File(const String& path);
128 
130  virtual ~File();
131 
141  void open(IOMode mode = IOReadWrite,
142  FileOpenMode openMode = FileOpenElseCreate,
144 
146  bool exists() const;
147 
149  bool isDirectory() const;
150 
152  bool isFile() const;
153 
155  bool isPipe() const;
156 
163  bool isSymLink() const;
164 
171  bool touch();
172 
179  bool remove();
180 
187  bool setPermissions(const Permissions& perm);
188 
197  bool getPermissions(Permissions& perm);
198 
211  bool lockRange(uint64_t start, uint64_t length, LockType type,
212  bool wait = true);
213 
223  bool unlockRange(uint64_t start, uint64_t length);
224 
236  void truncate(uint64_t size = 0);
237 
239  inline String getPath() const
240  { return(_name); }
241 
248  int64_t getSize();
249 
260  static bool makeDirectory(
261  const String& path, bool createAll = false,
263 
271  static bool remove(const String& path);
272 
281  static bool removeDirectory(const String& path);
282 
291  static bool removeDirectoryTree(const String& path);
292 
299  static bool exists(const String& path);
300 
308  static bool isDirectory(const String& path);
309 
317  static bool isFile(const String& path);
318 
326  static bool isPipe(const String& path);
327 
336  static bool isSymLink(const String& path);
337 
347  static bool readSymLink(String& path);
348 
357  static bool resolveSymLink(String& path);
358 
369  static bool createLink(const String& oldPath, const String& newPath,
370  bool hardLink = false);
371 
379  static bool setPermissions(const String& path, const Permissions& perm);
380 
389  static bool getPermissions(const String& path, Permissions& perm);
390 
399  static bool getAttributes(const String& path, FileAttributes& attrs);
400 
408  static int64_t getSize(const String& path);
409 
417  static bool rename(const String& oldName, const String& newName);
418 
426  static bool copy(const String& oldFile, const String& newFile);
427 
436  static bool move(const String& oldFile, const String& newFile);
437 
445  static bool touch(const String& path);
446 
455  static String readToString(const String& path);
456 
467  static void getFileSystemRoots(StringVec& roots, bool localOnly = true);
468 
476  static void setCreationMask(const Permissions& perm);
477 
488  static uint64_t getDiskFreeSpace(const String& path);
489 
500  static size_t getDiskBlockSize(const String& path);
501 
511  static int64_t roundSizeToBlockSize(const int64_t& fileSize,
512  size_t blockSize);
513 
520  static String removeTrailingSeparators(const String& path);
521 
523  static const char separator;
524 
530  static const char validSeparators[];
531 
536  static bool isValidSeparator(char c);
537 
542  static bool isValidSeparator(char16_t c);
543 
545  static const char separatorString[];
546 
548  static const char* eol;
549 
550  protected:
551 
553 
554  private:
555 
557 };
558 
559 } // namespace ccxx
560 
561 #endif // __ccxx_File_hxx
IOMode
Stream I/O modes.
Definition: Stream.h++:36
#define UINT64_CONST(I)
Declare an unsigned 64-bit integer constant I.
Definition: Integers.h++:123
bool isFile() const
Test if the file is a plain file.
Definition: File.h++:100
time_ms_t modified
File last-modified time.
Definition: File.h++:87
A String vector.
Definition: String.h++:1159
int64_t time_ms_t
A time expressed in milliseconds since the epoch (00:00:00, UTC, January 1, 1970).
Definition: Integers.h++:98
An unbuffered I/O stream.
Definition: Stream.h++:60
Open and truncate the file if it exists, otherwise fail.
Definition: File.h++:61
time_ms_t accessed
File last-accessed time.
Definition: File.h++:89
static const char * eol
The host system&#39;s end-of-line character sequence.
Definition: File.h++:548
FileOpenMode
File open modes.
Definition: File.h++:53
Lock for writing.
Definition: File.h++:109
FileType
File types.
Definition: File.h++:35
Permissions permissions
File access permissions.
Definition: File.h++:93
Directory.
Definition: File.h++:39
FileType type
File type.
Definition: File.h++:83
Read/write mode.
Definition: Stream.h++:42
Lock for reading.
Definition: File.h++:107
A representation of a file in the filesystem.
Definition: File.h++:118
LockType
File lock types.
Definition: File.h++:105
Named pipe.
Definition: File.h++:43
Open the file if it exists, otherwise create the file.
Definition: File.h++:55
time_ms_t created
File creation time (not available on all platforms).
Definition: File.h++:85
static const Permissions defaultDirPerms
Default permissions for directories: read, write & execute for user, read & execute for group...
Definition: Permissions.h++:256
static const char separator
The host system&#39;s file separator character.
Definition: File.h++:523
Open the file if it exists, otherwise fail.
Definition: File.h++:57
File attributes.
Definition: File.h++:71
#define COMMONCPP_API
Definition: Common.h++:126
FileAttributes()
Constructor.
Definition: File.h++:74
String getPath() const
Get the pathname for the file.
Definition: File.h++:239
#define INT64_CONST(I)
Declare a signed 64-bit integer constant I.
Definition: Integers.h++:122
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
String _name
Definition: File.h++:552
uint64_t size
File size, in bytes.
Definition: File.h++:91
Symbolic link.
Definition: File.h++:41
bool isDirectory() const
Test if the file is a directory.
Definition: File.h++:96
Create the file if it does not exist, otherwise fail.
Definition: File.h++:59
uint16_t char16_t
A UTF-16 character.
Definition: Common.h++:201
Regular file.
Definition: File.h++:37
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
File permissions.
Definition: Permissions.h++:38
Unknown filetype.
Definition: File.h++:49
Device file.
Definition: File.h++:45
static const int USER_READ_WRITE
User read and write mask.
Definition: Permissions.h++:62
Definition: AllocationMap.c++:25
Other filetype.
Definition: File.h++:47
Open and truncate the file if it exists, otherwise create the file.
Definition: File.h++:63