File.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_File_hxx
00024 #define __ccxx_File_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/FileName.h++>
00028 #include <commonc++/Permissions.h++>
00029 #include <commonc++/Stream.h++>
00030 #include <commonc++/String.h++>
00031 
00032 namespace ccxx {
00033 
00034 enum FileType { TypeFile, TypeDir, TypeSymLink, TypePipe, TypeDevice,
00035                 TypeOther, TypeUnknown };
00036 
00037 enum FileOpenMode { FileOpenElseCreate, FileOpen, FileCreate, FileTruncate,
00038                     FileTruncateElseCreate };
00039 
00045 struct COMMONCPP_API FileAttributes
00046 {
00049   FileAttributes()
00050     : type(TypeUnknown),
00051       created(INT64_CONST(0)),
00052       modified(INT64_CONST(0)),
00053       accessed(INT64_CONST(0)),
00054       size(UINT64_CONST(0))
00055   { }
00056 
00058   FileType type;
00060   time_ms_t created;
00062   time_ms_t modified;
00064   time_ms_t accessed;
00066   uint64_t size;
00068   Permissions permissions;
00069 
00071   inline bool isDirectory() const throw()
00072   { return(type == TypeDir); }
00073 
00075   inline bool isFile() const throw()
00076   { return(type == TypeFile); }
00077 };
00078 
00079 enum LockType { LockRead, LockWrite };
00080 
00087 class COMMONCPP_API File : public Stream
00088 {
00089   public:
00090 
00095   File(const String &path);
00096 
00098   virtual ~File() throw();
00099 
00107   void open(IOMode mode = IOReadWrite,
00108             FileOpenMode openMode = FileOpenElseCreate,
00109             const Permissions &perm = Permissions::USER_READ_WRITE)
00110     throw(IOException);
00111 
00113   bool exists() const throw();
00114 
00116   bool isDirectory() const throw();
00117 
00119   bool isFile() const throw();
00120 
00122   bool isPipe() const throw();
00123 
00129   bool isSymLink() const throw();
00130 
00136   bool touch() throw();
00137 
00143   bool remove() throw();
00144 
00150   bool setPermissions(const Permissions &perm) throw();
00151 
00159   bool getPermissions(Permissions &perm) throw();
00160 
00172   bool lockRange(uint64_t start, uint64_t length, LockType type,
00173                  bool wait = true) throw();
00174 
00183   bool unlockRange(uint64_t start, uint64_t length) throw();
00184 
00195   void truncate(uint64_t size = 0) throw(IOException);
00196 
00198   inline String getPath() const
00199   { return(_name); }
00200 
00206   int64_t getSize() throw(IOException);
00207 
00217   static bool makeDirectory(
00218     const String &path, bool createAll = false,
00219     const Permissions &perm = Permissions::defaultDirPerms) throw();
00220 
00227   static bool remove(const String &path) throw();
00228 
00236   static bool removeDirectory(const String &path) throw();
00237 
00245   static bool removeDirectoryTree(const String &path);
00246 
00252   static bool exists(const String &path) throw();
00253 
00260   static bool isDirectory(const String &path) throw();
00261 
00268   static bool isFile(const String &path) throw();
00269 
00276   static bool isPipe(const String &path) throw();
00277 
00285   static bool isSymLink(const String &path) throw();
00286 
00295   static bool readSymLink(String &path) throw();
00296 
00304   static bool resolveSymLink(String &path) throw();
00305 
00315   static bool createLink(const String &oldPath, const String &newPath,
00316                          bool hardLink = false) throw();
00317 
00324   static bool setPermissions(const String &path, const Permissions &perm)
00325     throw();
00326 
00334   static bool getPermissions(const String &path, Permissions &perm)
00335     throw();
00336 
00344   static bool getAttributes(const String &path, FileAttributes &attrs)
00345     throw();
00346 
00353   static int64_t getSize(const String &path) throw();
00354 
00361   static bool rename(const String &oldName, const String &newName) throw();
00362 
00369   static bool copy(const String &oldFile, const String &newFile) throw();
00370 
00378   static bool move(const String &oldFile, const String &newFile) throw();
00379 
00386   static bool touch(const String &path) throw();
00387 
00397   static void getFileSystemRoots(StringVec &roots, bool localOnly = true)
00398     throw();
00399 
00406   static void setCreationMask(const Permissions &perm) throw();
00407 
00417   static uint64_t getDiskFreeSpace(const String &path) throw();
00418 
00428   static size_t getDiskBlockSize(const String &path) throw();
00429 
00438   static int64_t roundSizeToBlockSize(const int64_t &fileSize,
00439                                       size_t blockSize);
00440 
00446   static String removeTrailingSeparators(const String &path) throw();
00447 
00449   static const char separator;
00450 
00455   static const char validSeparators[];
00456 
00460   static bool isValidSeparator(char c) throw();
00461 
00463   static const char separatorString[];
00464 
00466   static const char *eol;
00467 
00468   protected:
00469 
00470   String _name;
00471 
00472   private:
00473 
00474   CCXX_COPY_DECLS(File);
00475 };
00476 
00477 }; // namespace ccxx
00478 
00479 #endif // __ccxx_File_hxx
00480 
00481 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3