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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2