casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
File.h
Go to the documentation of this file.
00001 //# File.h: Class to get file information and a base for other file classes
00002 //# Copyright (C) 1993,1994,1995,1996,2000,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: File.h 21051 2011-04-20 11:46:29Z gervandiepen $
00027 
00028 #ifndef CASA_FILE_H
00029 #define CASA_FILE_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/OS/Path.h>
00034 #include <casa/OS/Mutex.h>
00035 #include <casa/BasicSL/String.h>
00036 
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary> 
00041 // Class to get file information and a base for other file classes.
00042 // </summary>
00043 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00044 // </reviewed>
00045 
00046 // <use visibility=export>
00047 
00048 // <prerequisite> 
00049 //    <li> Basic knowledge of the UNIX file system 
00050 //    <li> <linkto class=Path>Path</linkto> 
00051 // </prerequisite>
00052 
00053 // <etymology> 
00054 // 'File' is used in a traditional sense.
00055 // </etymology>
00056 
00057 // <synopsis> 
00058 // The File class provides the primary functions needed by all kinds of 
00059 // files (directories, regular files, symbolic links, named pipes etc.).
00060 // These shared functions serve mostly to return information about a 
00061 // particular file -- for instance, its type, its ownership, read, write
00062 // and execute permissions, date of latest access and the path on secundary 
00063 // storage associated with this file. Every file object has, by definition, 
00064 // a <linkto class=Path>Path</linkto> object associated with it which
00065 // defines the file name.
00066 // <p>
00067 // See also the derived classes
00068 // <linkto class=RegularFile>RegularFile</linkto>,
00069 // <linkto class=Directory>Directory</linkto>, and
00070 // <linkto class=SymLink>SymLink</linkto>.
00071 // <br>
00072 // This class does not contain virtual functions, because a lot of functions 
00073 // have different parameters, e.g. 'create' for RegularFile has one parameter
00074 // and 'create' for SymLink has two parameters. 
00075 //
00076 // It handles large files correctly.
00077 // </synopsis>
00078 
00079 // <example>
00080 // <srcblock>
00081 //    File myFile("someFileName");
00082 //    if (myFile.exists()) {
00083 //      myFile.setPermissions(0644);
00084 //      if (myFile.isRegular()) {
00085 //          cout << "this file is a regular file" << endl;
00086 //      }
00087 //    }
00088 //    else if (!myFile.exists()) {
00089 //        if (!myFile.canCreate()){
00090 //            cout << "cannot create this file" << endl;
00091 //        } 
00092 //    }
00093 // </srcblock>
00094 // </example>
00095 
00096 // <motivation> 
00097 // File systems operations are a notorious source of porting problems.
00098 // The file class provides a standard interface for programmers to use.
00099 // </motivation>
00100 
00101 
00102 class File
00103 {
00104 public: 
00105 
00106         enum FileWriteStatus {
00107                 // file exists and can be overwritten
00108                 OVERWRITABLE,
00109                 // file exists but cannot be overwritten
00110                 NOT_OVERWRITABLE,
00111                 // file does not exist and is creatable
00112                 CREATABLE,
00113                 // file does not exist but cannot be created
00114                 NOT_CREATABLE
00115         };
00116 
00117 
00118     // Construct a File object whose Path is set to the current working 
00119     // directory. 
00120     File();
00121     
00122     // Construct a File object whose Path is set to the given Path.
00123     // <group>
00124     File (const Path& path);
00125     File (const String& path);
00126     // </group>
00127 
00128     // Copy constructor (copy semantics).
00129     File (const File& that);
00130 
00131     virtual ~File();
00132     
00133     // Assignment (copy semantics).
00134     File& operator= (const File& that);
00135 
00136     // Returns the pathname of the file.
00137     const Path& path() const;
00138 
00139     // Check if the file is a regular file. If the boolean followSymLink is
00140     // False a symbolic link will not be followed.
00141     Bool isRegular (Bool followSymLink = True) const;
00142 
00143     // Check if the file is a directory. If the boolean followSymLink is
00144     // False a symbolic link will not be followed.
00145     Bool isDirectory (Bool followSymLink = True) const;
00146 
00147     // Check if the file is a symbolic link.
00148     Bool isSymLink() const;
00149 
00150     // Check if the file is a pipe.
00151     Bool isPipe() const;
00152 
00153     // Check if the file is a character special file.
00154     Bool isCharacterSpecial() const;
00155 
00156     // Check if the file is a block special file.
00157     Bool isBlockSpecial() const;
00158 
00159     // Check if the file is a socket.
00160     Bool isSocket() const;
00161 
00162     // Check if the file exists.
00163     Bool exists() const;
00164 
00165     // Check if the file is readable.
00166     Bool isReadable() const;
00167 
00168     // Check if the file is writable.
00169     Bool isWritable() const;
00170 
00171     // Check if the file is executable.
00172     Bool isExecutable() const;
00173 
00174     // Check if a file can be created.
00175     Bool canCreate() const;
00176     
00177     // Return the userID of the file.
00178     long userID() const; 
00179 
00180     // Return the groupID of the file.
00181     long groupID() const;
00182     
00183     // Return the size of the file. If the file
00184     // does not exist, an exception will be thrown.
00185     virtual Int64 size() const;
00186 
00187     // Return the permissions as a decimal value.
00188     uInt readPermissions() const;
00189 
00190     // Set permission with perm. Perm is an octal value.
00191     void setPermissions (uInt permissions);
00192 
00193     // Update access time and modification time of a file.
00194     void touch (uInt time);
00195 
00196     // Update access time and modification time of a file. This function
00197     // updates the file with the current time.
00198     void touch();
00199 
00200     // Time related fucnctions:
00201     // Return the time when the file was last accessed in seconds since
00202     // 00:00:00 GMT Jan 1, 1970.
00203     uInt accessTime() const;
00204 
00205     // Return the time when the file was last accessed
00206     // as a 26-characters String of the form:
00207     // Thu Feb  3 13:40:11 1994
00208     String accessTimeString() const;
00209 
00210     // Return the time when the file was last modified in seconds since
00211     // 00:00:00 GMT Jan 1, 1970.
00212     uInt modifyTime() const;
00213 
00214     // Return the time when the file was last modified
00215     // as a 26-characters String of the form:
00216     // Thu Feb  3 13:40:11 1994
00217     String modifyTimeString() const;
00218 
00219     // Return the time when the file status was last changed in seconds since
00220     // 00:00:00 GMT Jan 1, 1970.
00221     // It is set both by writing and changing the file status information,
00222     // such as changes of owner, group, link count, or mode.
00223     uInt statusChangeTime() const;
00224 
00225     // return the time when the file status was last changed
00226     // as a 26-characters String of the form:
00227     // Thu Feb  3 13:40:11 1994
00228     String statusChangeTimeString() const;
00229 
00230     // Create a new unique path name in the specified directory, with
00231     // the specified prefix and random trailing characters:
00232     // <srcblock>
00233     //    p.newUniqueName ("./", "temp")  -->  "./tempAAA00xx32"
00234     //    p.newUniqueName ("/home/me", "diary")  -->  "/home/me/diaryAAA00xxb0"
00235     // </srcblock>
00236     static Path newUniqueName (const String& directory, const String& prefix);
00237 
00238     // Create a new unique filename without a prefix.
00239     // As above, but all the characters in the filename are random:
00240     // <srcblock>
00241     //    p.newUniqueName ("./")  -->  "./AAA00xx32"
00242     //    p.newUniqueName ("/home/me")  -->  "/home/me/AAA00xxb0"
00243     // </srcblock>
00244     static Path newUniqueName (const String& directory); 
00245 
00246 
00247     // get write status of the file.
00248     // OVERWRITABLE - file exists and can be overwritten
00249     // NOT_OVERWRITABLE - file exists but cannot be overwritten
00250     // CREATABLE - File does not exist and can be created
00251     // NOT_CREATABLE - file does not exist and cannot be created.
00252     FileWriteStatus getWriteStatus() const;
00253 
00254     // Return the filesystem type.
00255     // If the file doesn't exsist crawl up the directory tree to
00256     // find one that does.
00257     String getFSType() const; 
00258 
00259 protected:
00260     // This function is used by <linkto class=RegularFile>RegularFile</linkto> 
00261     // and <linkto class=Directory>Directory</linkto> to remove all the links
00262     // which, when followed, ultimately resolve to a Directory or a 
00263     // RegularFile.
00264     // For example, A->B, B->C, C->D and D points to a regular file.
00265     // When remove() is called for a regular file A,
00266     // that function uses removeLinks() to remove A, B, C and D.
00267     void removeSymLinks();
00268 
00269     // Check if the new path for a copy or move is valid.
00270     // An exception is thrown if:
00271     // <br>- the target directory is not writable
00272     // <br>- or the target file already exists and overwrite==False
00273     // <br>- or the target file already exists and is not writable
00274     // <br>When the targetName represents a directory, the basename
00275     // of the file is appended to it. This is done to cover the
00276     // case where the source is a symlink to a file. In that case
00277     // the target will get the basename of the symlink and not the
00278     // the basename of the file pointed to. This is not done when
00279     // forDirectory==True (which is used by class Directory).
00280     void checkTarget (Path& targetName, Bool overwrite,
00281                       Bool forDirectory = False) const;
00282 
00283 private:
00284     // Define a function for lstat.
00285     // This is necessary since SunOS4.1.x prototypes lstat() with a first
00286     // argument of type (char*), while Solaris (and presumably all other
00287     // reasonable OS's) prototype it with a first argument of type
00288     // (const char*).  Since lstat() does not change its first argument,
00289     // it is safe to convert our const variable to a non-const one so that
00290     // we can call lstat() successfully.
00291     // <br>It is also useful to be able to pass the buffer as void*. In that
00292     // way the 32-bit or 64-bit file details are only needed in the cc file.
00293     int mylstat (const char* path, void* buf) const;
00294 
00295     // Get the lstat of this file.
00296     // Throw an exception when it fails.
00297     void getstat (void* buf) const;
00298 
00299     // Get the lstat of a file.
00300     // Throw an exception when it fails.
00301     void getstat (const File& file, void* buf) const;
00302 
00303 
00304     // Full pathname of the file.
00305     Path itsPath;
00306     // A sequence number to generate unique file names.
00307     static uInt uniqueSeqnr_p;
00308     static Mutex theirMutex;
00309 };
00310 
00311 
00312 inline const Path& File::path() const
00313 {
00314     return itsPath;
00315 }
00316 
00317 inline void File::getstat (void* buf) const
00318 {
00319     getstat (*this, buf);
00320 }
00321 
00322 
00323 
00324 
00325 } //# NAMESPACE CASA - END
00326 
00327 #endif