casa
$Rev:20696$
|
00001 //# RegularFile.h: Manipulate and get information about regular files 00002 //# Copyright (C) 1993,1994,1995,1996,1997,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: RegularFile.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $ 00027 00028 #ifndef CASA_REGULARFILE_H 00029 #define CASA_REGULARFILE_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <casa/OS/Path.h> 00034 #include <casa/OS/File.h> 00035 #include <casa/BasicSL/String.h> 00036 00037 00038 namespace casa { //# NAMESPACE CASA - BEGIN 00039 00040 // <summary> 00041 // Manipulate and get information about regular files 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=File>File</linkto> 00051 // </prerequisite> 00052 00053 // <etymology> 00054 // The class RegularFile provides functions for manipulating and getting 00055 // information about regular files. Regular file are files which hold data. 00056 // </etymology> 00057 00058 // <synopsis> 00059 // This class provides functions to manipulate and to get information about 00060 // regular files. The functions for getting information (like ownership, dates) 00061 // about regular files are inherited from the <linkto class=File>File</linkto> 00062 // class. 00063 // <br> 00064 // The class RegularFile itself provides functions to rename, remove, copy, 00065 // and move regular files. The file name can be a symbolic link resolving 00066 // (eventually) to a regular file. 00067 // </synopsis> 00068 00069 // <example> 00070 // <srcblock> 00071 // // Create the object rFile 00072 // RegularFile rFile ("isFile"); 00073 // 00074 // // Create file; if the file exists it will be overwritten 00075 // rFile.create (True); 00076 // rFile.copy (newPath); 00077 // 00078 // cout << rFile.size() << endl; // Get the size of the file 00079 // cout << rFile.path() << endl; // Show the relative pathname 00080 // 00081 // rFile.remove(); // remove the file 00082 // </srcblock> 00083 // </example> 00084 00085 // <motivation> 00086 // Provide functions for manipulating and getting information 00087 // about regular files. 00088 // </motivation> 00089 00090 00091 class RegularFile: public File 00092 { 00093 public: 00094 00095 // Default constructor sets path to . (working directory). 00096 RegularFile(); 00097 00098 // Create a regular file object for a file with the given path name. 00099 // An exception is thrown if the file is illegal, i.e. if it does 00100 // not exist as a regular file or symbolic link or if cannot be created. 00101 // Note that the file is not created if it does not exist yet. 00102 // This can be done using the function create. 00103 // <br> 00104 // When the given path name is a symbolic link, the symbolic link 00105 // is resolved (recursively) and the resulting file name is used 00106 // instead. 00107 // <group> 00108 RegularFile (const Path& path); 00109 RegularFile (const String& string); 00110 RegularFile (const File& file); 00111 // </group> 00112 00113 // Copy constructor (copy semantics). 00114 RegularFile (const RegularFile& regularFile); 00115 00116 ~RegularFile(); 00117 00118 // Assignment (copy semantics). 00119 RegularFile& operator= (const RegularFile& regularFile); 00120 00121 // Create the regular file. 00122 // <br>If the file exists and is not a regular file, an 00123 // exception is thrown. Otherwise if overwrite is true the regular file 00124 // will be overwritten. If overwrite is false then nothing will be done. 00125 // If the file does not exist, it is created. 00126 void create (Bool overwrite = True); 00127 00128 // Remove the file. 00129 // If it does not exist, an exception will be thrown. 00130 // If a symbolic link is given, the link chain pointing to the file 00131 // will also be removed. 00132 void remove(); 00133 00134 // Copy the file to the target path using the system command cp. 00135 // If the file is a symbolic link, the regular file pointed to 00136 // will be copied. 00137 // The target path can be a directory or a file (as in cp). 00138 // An exception is thrown if: 00139 // <br>- the target directory is not writable 00140 // <br>- or the target file already exists and overwrite==False 00141 // <br>- or the target file already exists and is not writable 00142 // <note role=caution> 00143 // When a readonly file is copied, the resulting 00144 // file is also readonly. Therefore <src>chmod</src> is used to 00145 // set user write permission after the copy. 00146 // The flag <src>setUserWritePermission</src> can be set to False 00147 // when that should not be done. 00148 // </note> 00149 // <group> 00150 void copy (const Path& target, Bool overwrite = True, 00151 Bool setUserWritePermission = True) const; 00152 void copy (const String& target, Bool overwrite = True, 00153 Bool setUserWritePermission = True) const; 00154 // </group> 00155 00156 // Copy the file manually in case the cp command cannot be used. 00157 // (like on the Cray XT3). 00158 static void manualCopy (const String& source, const String& target); 00159 00160 // Move the file to the target path using the system command mv. 00161 // If the file is a symbolic link, the regular file pointed to 00162 // will be moved. 00163 // The target path can be a directory or a file (as in mv). 00164 // An exception is thrown if: 00165 // <br>- the target directory is not writable 00166 // <br>- or the target file already exists and overwrite==False 00167 // <br>- or the target file already exists and is not writable 00168 // <note role=tip> The system command mv is used instead of the 00169 // library function rename to be able to move across file systems. 00170 // </note> 00171 // <group> 00172 void move (const Path& target, Bool overwrite = True); 00173 void move (const String& target, Bool overwrite = True); 00174 // </group> 00175 00176 // Return the size of the file. If the file 00177 // does not exist, an exception will be thrown. 00178 virtual Int64 size() const; 00179 00180 private: 00181 // Check if the path of the file is valid. 00182 // Also resolve possible symlinks. 00183 void checkPath(); 00184 00185 // This variable is used when a symbolic link points to the file. 00186 File itsFile; 00187 }; 00188 00189 00190 inline void RegularFile::copy (const String& target, Bool overwrite, 00191 Bool setUserWritePermission) const 00192 { 00193 copy (Path(target), overwrite, setUserWritePermission); 00194 } 00195 inline void RegularFile::move (const String& target, Bool overwrite) 00196 { 00197 move (Path(target), overwrite); 00198 } 00199 00200 00201 00202 } //# NAMESPACE CASA - END 00203 00204 #endif