(file) Return to FileSystem.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.39 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.15 //
  3 karl  1.34 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.30 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.34 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.36 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.39 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.15 //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 mike  1.16 // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.15 // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20 kumpf 1.19 // 
 21 mike  1.16 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.15 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 mike  1.16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.15 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifndef Pegasus_FileSystem_h
 35            #define Pegasus_FileSystem_h
 36            
 37            #include <Pegasus/Common/Config.h>
 38            #include <Pegasus/Common/String.h>
 39 kumpf 1.23 #include <Pegasus/Common/ArrayInternal.h>
 40 kumpf 1.22 #include <Pegasus/Common/InternalException.h>
 41 kumpf 1.26 #include <Pegasus/Common/System.h>
 42 kumpf 1.21 #include <Pegasus/Common/Linkage.h>
 43 mike  1.16 #include <fstream>
 44 david 1.27 #include <cstdio>
 45 mike  1.37 #include <Pegasus/Common/Buffer.h>
 46 mike  1.15 
 47            PEGASUS_NAMESPACE_BEGIN
 48            
 49            /** The FileSystem class provides methods for manipulating the file system.
 50            
 51                This class provides an methods for:
 52                <ul>
 53 kumpf 1.42         <li>Manipulating directories (create, remove, change).</li>
 54                    <li>Checking files for ability to read and write.</li>
 55                    <li>Removing files.</li>
 56                    <li>Comparing files.</li>
 57                    <li>Loading files into memory.</li>
 58 mike  1.15     </ul>
 59            
 60 mike  1.16     The methods of this class are all static. So there is no need to
 61 mike  1.15     instantiate this class to use it. In fact, instantiation is precluded
 62                by a private default constructor.
 63            
 64                A word about the "NoCase" extensions. Some methods of this class have
 65                a "NoCase" version. For example, there is a canRead() and a canReadNoCase().
 66                The "NoCase" variation ignores the case of the file while it is being
 67                located. For example, suppose there is a file called "File1". Then
 68                canReadNoCase("file1") finds the file called "File1" and returns true (of
 69                course there is a possibility that there really is a file called "file1"
 70 mike  1.16     in the same directory in which case the behavior of this method is
 71                undefined). Notice that Windows does this anyway. These methods were
 72 mike  1.15     developed primarily for Unix which is case sensitive with respect to file
 73                names. It should be noted that the no-case methods are slower (since they
 74                must stat the directory and look at the file names).
 75            
 76                The no-case variations are used by the repository which--according to
 77 mike  1.16     CIM--must treat two classes names with different case characterisits, but
 78 mike  1.15     othererwise similar, as identical. For example, "MyClass", "myclass", and
 79                "MYCLASS" all refer to the same class. Since the default repository
 80                implementation uses disk file names to represent class names (e.g., there
 81                may be a file called "MyClass.#) that there must be a way of opening
 82                a file without regard to its case.
 83            */
 84            class PEGASUS_COMMON_LINKAGE FileSystem
 85            {
 86            public:
 87            
 88                /** Determines whether file exists.
 89 kumpf 1.42         @param path path of the file.
 90                    @return true if the file exists.
 91 mike  1.15     */
 92                static Boolean exists(const String& path);
 93            
 94                /** Determine whether the file exists. Ignores case of the file.
 95 kumpf 1.42         @param path path of the file.
 96                    @param pathOut path of the file with actual case.
 97                    @return true if the file exists; false otherwise.
 98 mike  1.15     */
 99                static Boolean existsNoCase(const String& path, String& pathOut);
100            
101                /** Determine whether the file exists. Ignores the case of the file.
102 kumpf 1.42         @param path path of the file.
103                    @return true if the file exists; false otherwise.
104 mike  1.15     */
105                static Boolean existsNoCase(const String& path);
106            
107                /** Determines whether the file can be read.
108 kumpf 1.42         @param path path of the file.
109                    @return true if the file can be read.
110 mike  1.15     */
111                static Boolean canRead(const String& path);
112            
113                /** Determines whether the file can be read. Ignores case of file.
114 kumpf 1.42         @param path path of the file.
115                    @return true if the file can be read.
116 mike  1.15     */
117                static Boolean canReadNoCase(const String& path);
118            
119                /** Determines whether the file can be written.
120 kumpf 1.42         @param path path of the file.
121                    @return true if the file can be written.
122 mike  1.15     */
123                static Boolean canWrite(const String& path);
124            
125                /** Determines whether the file can be written. Ignores case of file.
126 kumpf 1.42         @param path path of the file.
127                    @return true if the file can be written.
128 mike  1.15     */
129                static Boolean canWriteNoCase(const String& path);
130            
131                /** Get the size of the file in bytes.
132 kumpf 1.42         @param path path of file.
133                    @param size set to size of file.
134                    @return true on success.
135 mike  1.15     */
136                static Boolean getFileSize(const String& path, Uint32& size);
137            
138                /** Get the size of the file in bytes.
139 kumpf 1.42         @param path path of file.
140                    @param size set to size of file.
141                    @return true on success.
142 mike  1.15     */
143                static Boolean getFileSizeNoCase(const String& path, Uint32& size);
144            
145                /** Removes a file.
146 kumpf 1.42         @param path of file to be removed.
147                    @return true on sucess.
148 mike  1.15     */
149                static Boolean removeFile(const String& path);
150            
151                /** Removes a file. Ignores case of file.
152 kumpf 1.42         @param path of file to be removed.
153                    @return true on sucess.
154 mike  1.15     */
155                static Boolean removeFileNoCase(const String& path);
156            
157                /** Loads contents of the file into the array. Note that the file is
158 kumpf 1.42         opened using binary mode (newline sequences are not expanded to
159                    carriage-return-line-feed sequences on Windows).
160                    @param array set to the contents of the file upon return.
161                    @param fileName name of file to be loaded.
162                    @exception CannotOpenFile
163 mike  1.15     */
164                static void loadFileToMemory(
165 kumpf 1.42         Buffer& array,
166                    const String& fileName);
167 mike  1.15 
168                /** Determines whether two files have exactly the same content.
169 kumpf 1.42         @param path1 path of first file.
170                    @param path2 path of second file.
171                    @return true if files are identical.
172                    @exception CannotOpenFile
173 mike  1.15     */
174                static Boolean compareFiles(
175 kumpf 1.42         const String& path1,
176                    const String& path2);
177 mike  1.15 
178                /** Renames a file.
179 kumpf 1.42         @param oldPath old name of file.
180                    @param newPath new name of file.
181                    @return true on success.
182 mike  1.15     */
183                static Boolean renameFile(
184 kumpf 1.42         const String& oldPath,
185                    const String& newPath);
186 mike  1.15 
187 mike  1.17     /** Same as rename file except that the case of the file referred to
188 kumpf 1.42         by oldPath is ignored.
189 mike  1.17     */
190                static Boolean renameFileNoCase(
191 kumpf 1.42         const String& oldPath,
192                    const String& newPath);
193 mike  1.17 
194                /** Copy a file.
195 kumpf 1.42         @param fromPath name of existing file.
196                    @param toPath name of new file.
197                    @return true on success.
198 mike  1.17     */
199                static Boolean copyFile(
200 kumpf 1.42         const String& fromPath,
201                    const String& toPath);
202 mike  1.17 
203 mike  1.15     /** Opens a file and ignores the case of the file. Note that the file
204 kumpf 1.42         will be opend in binary mode (no translation of carriage-return-line-
205                    feed sequences on Windows).
206                    @param os file stream to be opend.
207                    @param path path of file to be opened.
208                    @return true on success.
209 mike  1.15     */
210 mike  1.17     static Boolean openNoCase(PEGASUS_STD(ifstream)& is, const String& path);
211            
212                /** Opens a file and ignores the case of the file. Note that the file
213 kumpf 1.42         open mode of the file must be passed in.
214                    @param os file stream to be opend.
215                    @param path path of file to be opened.
216                    @param mode mode to open the file in.
217                    @return true on success.
218 mike  1.17     */
219                static Boolean openNoCase(
220 kumpf 1.43         PEGASUS_STD(fstream)& fs,
221                    const String& path,
222 kumpf 1.42         int mode);
223 mike  1.15 
224                /** Determines whether the path refers to a directory.
225 kumpf 1.42         @param path path of the directory.
226                    @return true if path refers to a directory.
227 mike  1.15     */
228                static Boolean isDirectory(const String& path);
229            
230                /** Changes the current directory.
231 kumpf 1.42         @param path path of directory to be changed to.
232                    @return true on success.
233 mike  1.15     */
234                static Boolean changeDirectory(const String& path);
235            
236 mike  1.16     /** Creates a directory.
237 kumpf 1.42         @param path path of directory to be created.
238                    @return true on success.
239 mike  1.15     */
240                static Boolean makeDirectory(const String& path);
241            
242 mike  1.16     /** Get the path of the current working Directory.
243 kumpf 1.42         @param path set to current working directory upon return.
244                    @return true on success (operation may fail if the current
245                        working directory becomes stale; this can happen on
246                        Unix if it is removed but is impossible on Windows
247                        due to reference counting).
248 mike  1.15     */
249                static Boolean getCurrentDirectory(String& path);
250            
251                /** Remove the given directory. The directory must be empty
252 kumpf 1.42         to be eligible for removal
253                    @param String path is the relative or ablsolute path to
254                    the directory to remove
255                    @return true if directory removed
256 mike  1.15     */
257                static Boolean removeDirectory(const String& path);
258            
259                /** Remove a directory and all files and directories under it.
260 kumpf 1.42         WARNING: This differs significantly from the <tt>removeDirectory</tt>
261                    function in that it removes both directories and files and
262                    removes a complete hiearchy.  Use with caution.
263                    @param path path of directory to be removed.
264                    @return true on success.
265 mike  1.16     */
266 mike  1.15     static Boolean removeDirectoryHier(const String& path);
267            
268                /** Gets names of all entries (files and directories) of a directory.
269 kumpf 1.42         Note that this function excludes the "." and ".." entries.
270                    @param path path path of directory.
271                    @param paths contains list of entry names upon return. Note that
272                        the entry names only are provided (no path part).
273                    @return true on success.
274 mike  1.15     */
275                static Boolean getDirectoryContents(
276 kumpf 1.42         const String& path,
277                    Array<String>& paths);
278 mike  1.15 
279                /** Determines whether the given directory is empty. A directory is
280 kumpf 1.42         empty if it contains no files or directories.
281                    @param path path of directory.
282                    @return true if directory is empty.
283 mike  1.15     */
284                static Boolean isDirectoryEmpty(const String& path);
285            
286 mike  1.16     /** Translate backward slashes to forward slashes.
287 kumpf 1.42         @param path to be translated.
288 mike  1.15     */
289                static void translateSlashes(String& path);
290            
291 kumpf 1.20     /** Get an absolute path from an absolute directory and a relative or
292                    absolute file name.  If the file name is fully specified, it is
293                    returned unchanged.  Otherwise, the specified directory is prepended
294                    to the file name.
295                */
296                static String getAbsolutePath(const char* path, const String& filename);
297            
298 tony  1.25     /** Return the just the filename to the file name into base.
299                */
300                static String extractFileName(const String& base);
301            
302                /** Return the just the path to the file name into path.
303                */
304                static String extractFilePath(const String& path);
305            
306 kumpf 1.31     /** Changes file permissions on the given file.
307 kumpf 1.42         @param path path of the file.
308                    @param mode the bit-wise inclusive OR of the values for the
309 kumpf 1.31         desired permissions.
310 kumpf 1.42         @return true on success, false on error and errno is set appropriately.
311 kumpf 1.31     */
312                static Boolean changeFilePermissions(const String& path, mode_t mode);
313            
314 konrad.r 1.32     /**
315                      Return OS path specific delimiter.
316 kumpf    1.43 
317 konrad.r 1.32        @return delimiter specific to the platform
318                   */
319                   static String getPathDelimiter();
320               
321                   /**
322                      Returns the absolute pathname for the specified filename.
323 kumpf    1.43 
324 konrad.r 1.32        @param paths directories seperated by an OS specific delimiter to search
325                      @param filename filename to search for in the paths
326               
327                      @return the full absolute pathname to the found filename or an empty
328                      string on failure.
329                   */
330 kumpf    1.42     static String getAbsoluteFileName(
331                       const String& paths,
332                       const String& filename);
333 kumpf    1.43 
334 kumpf    1.33     /**
335                       Convert a library name to its corresponding file name by adding the
336                       appropriate prefix and suffix.
337               
338                       @param libraryName The name of the library for which to build the file
339                                          name.
340                       @return The file name corresponding to the specified library name.
341                   */
342                   static String buildLibraryFileName(const String &libraryName);
343               
344 kumpf    1.42     static Boolean changeFileOwner(
345                       const String& fileName,
346                       const String& userName);
347               
348 mateus.baur 1.47     /**
349                          Flushes the data from the iostream buffers to the OS buffers and
350                          then flushes the data from the OS buffers to the disk.
351                  
352                          This will avoid the possible data loss in case of an OS crash when
353                          OS filesystem commit directory-level changes immediately while
354                          file-level changes remain cached (e.g. HP-UX).
355                  
356                          @param fstream. The iostream that we want to flush data.
357                      */
358                      static void syncWithDirectoryUpdates(PEGASUS_STD(fstream)&);
359                  
360 mike        1.15 private:
361                  
362                      FileSystem() { }
363                  };
364                  
365                  inline Boolean FileSystem::existsNoCase(const String& path)
366                  {
367                      String dummy;
368                      return existsNoCase(path, dummy);
369                  }
370                  
371                  inline Boolean FileSystem::canReadNoCase(const String& path)
372                  {
373                      String realPath;
374                  
375                      if (!existsNoCase(path, realPath))
376 kumpf       1.42         return false;
377 mike        1.15 
378                      return FileSystem::canRead(realPath);
379                  }
380                  
381                  inline Boolean FileSystem::canWriteNoCase(const String& path)
382                  {
383                      String realPath;
384                  
385                      if (!existsNoCase(path, realPath))
386 kumpf       1.42         return false;
387 mike        1.15 
388                      return FileSystem::canWrite(realPath);
389                  }
390                  
391                  inline Boolean FileSystem::removeFileNoCase(const String& path)
392                  {
393                      String realPath;
394                  
395                      if (!existsNoCase(path, realPath))
396 kumpf       1.42         return false;
397 mike        1.15 
398                      return FileSystem::removeFile(realPath);
399 mike        1.17 }
400                  
401                  inline Boolean FileSystem::renameFileNoCase(
402                      const String& oldPath,
403                      const String& newPath)
404                  {
405                      String realPath;
406                  
407                      if (!existsNoCase(oldPath, realPath))
408 kumpf       1.42         return false;
409 mike        1.17 
410                      return FileSystem::renameFile(realPath, newPath);
411 mike        1.15 }
412                  
413                  inline Boolean FileSystem::getFileSizeNoCase(const String& path, Uint32& size)
414                  {
415                      String realPath;
416                  
417                      if (!existsNoCase(path, realPath))
418 kumpf       1.42         return false;
419 mike        1.15 
420                      return FileSystem::getFileSize(realPath, size);
421 kumpf       1.20 }
422                  
423                  inline String FileSystem::getAbsolutePath(
424                      const char* path,
425                      const String& filename)
426                  {
427                      String absolutePath;
428                  
429                      if (filename != String::EMPTY)
430                      {
431 kumpf       1.26         if (!System::is_absolute_path(filename.getCString()) && path && path[0])
432 kumpf       1.20         {
433                              absolutePath.append(path);
434                              absolutePath.append('/');
435                          }
436                          absolutePath.append(filename);
437                      }
438                      translateSlashes(absolutePath);
439                  
440                      return absolutePath;
441 mike        1.15 }
442                  
443 kumpf       1.18 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
444                  {
445 kumpf       1.24     is.open(path.getCString());
446 kumpf       1.18     return !!is;
447                  }
448                  
449                  inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
450                  {
451 kumpf       1.24     os.open(path.getCString());
452 kumpf       1.18     return !!os;
453                  }
454                  
455                  inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
456                  {
457 kumpf       1.24     os.open(path.getCString(), PEGASUS_STD(ios::app));
458 kumpf       1.18     return !!os;
459 konrad.r    1.32 }
460                  
461 kumpf       1.43 inline String FileSystem::getPathDelimiter()
462 konrad.r    1.32 {
463 david.dillard 1.38 #if defined(PEGASUS_OS_TYPE_WINDOWS)
464 kumpf         1.42     return String(";");
465 konrad.r      1.32 #else
466 kumpf         1.42     return String(":");
467 konrad.r      1.32 #endif
468 kumpf         1.18 }
469                    
470                    /** Get the next line from the input file.
471                    */
472                    PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
473                    
474 mike          1.15 PEGASUS_NAMESPACE_END
475                    
476                    #endif /* Pegasus_FileSystem_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2