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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2