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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2