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

  1 karl  1.54 //%2005////////////////////////////////////////////////////////////////////////
  2 mike  1.28 //
  3 karl  1.52 // 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.46 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.52 // 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.54 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 mike  1.28 //
 12            // Permission is hereby granted, free of charge, to any person obtaining a copy
 13 kumpf 1.34 // of this software and associated documentation files (the "Software"), to
 14            // deal in the Software without restriction, including without limitation the
 15            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16 mike  1.28 // sell copies of the Software, and to permit persons to whom the Software is
 17            // furnished to do so, subject to the following conditions:
 18            // 
 19 kumpf 1.34 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20 mike  1.28 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 kumpf 1.34 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25 mike  1.28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27            //
 28            //==============================================================================
 29            //
 30            // Author: Mike Brasher (mbrasher@bmc.com)
 31            //
 32            // Modified By:
 33 ramnath 1.29 //         Ramnath Ravindran(Ramnath.Ravindran@compaq.com)
 34 a.arora 1.50 //         Amit K Arora, IBM (amita@in.ibm.com)
 35 kumpf   1.51 //         Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 36 david.dillard 1.53 //         David Dillard, VERITAS Software Corp.
 37                    //             (david.dillard@veritas.com)
 38 mike          1.28 //
 39                    //%/////////////////////////////////////////////////////////////////////////////
 40                    
 41                    #include <iostream>
 42 konrad.r      1.48 //#include <cstdio>
 43 mike          1.28 #include <Pegasus/Common/Config.h>
 44                    #include <Pegasus/Common/System.h>
 45 a.arora       1.50 #include <Pegasus/Common/AutoPtr.h>
 46 mike          1.28 #include "FileSystem.h"
 47                    #include "Dir.h"
 48                    
 49                    PEGASUS_NAMESPACE_BEGIN
 50                    
 51                    // Clone the path as a C String but discard trailing slash if any:
 52                    
 53 kumpf         1.38 static CString _clonePath(const String& path)
 54 mike          1.28 {
 55 kumpf         1.38     String clone = path;
 56 mike          1.28 
 57 kumpf         1.38     if (clone.size() && clone[clone.size()-1] == '/')
 58                            clone.remove(clone.size()-1);
 59 mike          1.28 
 60 kumpf         1.38     return clone.getCString();
 61 mike          1.28 }
 62                    
 63                    Boolean FileSystem::exists(const String& path)
 64                    {
 65 kumpf         1.38     return System::exists(_clonePath(path));
 66 mike          1.28 }
 67                    
 68                    Boolean FileSystem::getCurrentDirectory(String& path)
 69                    {
 70                        path.clear();
 71                        char tmp[4096];
 72                    
 73                        if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
 74                    	return false;
 75                    
 76                        path.append(tmp);
 77                        return true;
 78                    }
 79                    
 80                    Boolean FileSystem::existsNoCase(const String& path, String& realPath)
 81                    {
 82 chuck         1.41 #ifdef PEGASUS_OS_OS400
 83                        // The OS/400 file system is case insensitive, so just call exists( ).
 84                        // This is faster, but the main reason to do this is to
 85                        // avoid multi-threading problems with the IFS directory APIs
 86                        // (even though they claim to be threadsafe).
 87                        realPath = path;
 88                        return exists(path);
 89                    #else
 90 mike          1.28     realPath.clear();
 91 kumpf         1.38     CString cpath = _clonePath(path);
 92                        const char* p = cpath;
 93 mike          1.28 
 94                        const char* dirPath;
 95 kumpf         1.38     const char* fileName;
 96 kumpf         1.39     char* slash = (char *) strrchr(p, '/');
 97 mike          1.28 
 98                        if (slash)
 99                        {
100                    	*slash = '\0';
101                    	fileName = slash + 1;
102                    	dirPath = p;
103                    
104                    	if (*fileName == '\0')
105                    	    return false;
106                        }
107                        else
108                        {
109                    	fileName = p;
110                    	dirPath = ".";
111                        }
112                    
113                    
114                        for (Dir dir(dirPath); dir.more(); dir.next())
115                        {
116 kumpf         1.40 	if (System::strcasecmp(fileName, dir.getName()) == 0)
117 mike          1.28 	{
118                    	    if (strcmp(dirPath, ".") == 0)
119                    		realPath = dir.getName();
120                    	    else
121                    	    {
122                    		realPath = dirPath;
123 kumpf         1.37 		realPath.append('/');
124                    		realPath.append(dir.getName());
125 mike          1.28 	    }
126                    	    return true;
127                    	}
128                        }
129                    
130                        return false;
131 chuck         1.41 #endif
132 mike          1.28 }
133                    
134                    Boolean FileSystem::canRead(const String& path)
135                    {
136 kumpf         1.38     return System::canRead(_clonePath(path));
137 mike          1.28 }
138                    
139                    Boolean FileSystem::canWrite(const String& path)
140                    {
141 kumpf         1.38     return System::canWrite(_clonePath(path));
142 mike          1.28 }
143                    
144                    Boolean FileSystem::getFileSize(const String& path, Uint32& size)
145                    {
146 kumpf         1.38     return System::getFileSize(_clonePath(path), size);
147 mike          1.28 }
148                    
149                    Boolean FileSystem::removeFile(const String& path)
150                    {
151 kumpf         1.38     return System::removeFile(_clonePath(path));
152 mike          1.28 }
153                    
154                    void FileSystem::loadFileToMemory(
155 david.dillard 1.53     Array<char>& array,
156 mike          1.28     const String& fileName)
157                    {
158                        Uint32 fileSize;
159                    
160                        if (!getFileSize(fileName, fileSize))
161                    	throw CannotOpenFile(fileName);
162                    
163 kumpf         1.38     FILE* fp = fopen(fileName.getCString(), "rb");
164 mike          1.28 
165                        if (fp == NULL)
166                    	throw CannotOpenFile(fileName);
167                    
168 kumpf         1.36     array.reserveCapacity(fileSize);
169 mike          1.28     char buffer[4096];
170                        size_t n;
171                    
172                        while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
173                            array.append(buffer, n);
174                    
175                        fclose(fp);
176                    }
177                    
178                    Boolean FileSystem::compareFiles(
179                        const String& path1,
180                        const String& path2)
181                    {
182                        Uint32 fileSize1;
183                    
184                        if (!getFileSize(path1, fileSize1))
185                    	throw CannotOpenFile(path1);
186                    
187                        Uint32 fileSize2;
188                    
189                        if (!getFileSize(path2, fileSize2))
190 mike          1.28 	throw CannotOpenFile(path2);
191                    
192                        if (fileSize1 != fileSize2)
193                    	return false;
194                    
195 kumpf         1.38     FILE* fp1 = fopen(path1.getCString(), "rb");
196 mike          1.28 
197                        if (fp1 == NULL)
198                    	throw CannotOpenFile(path1);
199                    
200 kumpf         1.38     FILE* fp2 = fopen(path2.getCString(), "rb");
201 mike          1.28 
202                        if (fp2 == NULL)
203                        {
204                    	fclose(fp1);
205                    	throw CannotOpenFile(path2);
206                        }
207                    
208                        int c1;
209                        int c2;
210                    
211                        while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
212                        {
213                    	if (c1 != c2)
214                    	{
215                    	    fclose(fp1);
216                    	    fclose(fp2);
217                    	    return false;
218                    	}
219                        }
220                    
221                        fclose(fp1);
222 mike          1.28     fclose(fp2);
223                        return true;
224                    }
225                    
226                    Boolean FileSystem::renameFile(
227                        const String& oldPath,
228                        const String& newPath)
229                    {
230 kumpf         1.38     return System::renameFile(oldPath.getCString(), newPath.getCString());
231 mike          1.28 }
232                    
233 mike          1.31 Boolean FileSystem::copyFile(
234                        const String& fromPath,
235                        const String& toPath)
236                    {
237 kumpf         1.38     return System::copyFile(fromPath.getCString(), toPath.getCString());
238 mike          1.31 }
239                    
240 mike          1.28 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
241                    {
242                        String realPath;
243                    
244                        if (!existsNoCase(path, realPath))
245                    	return false;
246                    
247 kumpf         1.38     is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
248 david         1.44 
249 mike          1.31     return !!is;
250                    }
251                    
252                    Boolean FileSystem::openNoCase(
253                        PEGASUS_STD(fstream)& fs, 
254                        const String& path,
255                        int mode)
256                    {
257                        String realPath;
258 ramnath       1.30 
259 mike          1.31     if (!existsNoCase(path, realPath))
260                    	return false;
261 mday          1.43 #if defined(__GNUC__) && GCC_VERSION >= 30200
262                        fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
263                    #else
264 david         1.44 #if defined(PEGASUS_OS_OS400)
265 david         1.45     fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
266 david         1.44 #else
267 kumpf         1.38     fs.open(_clonePath(realPath), mode);
268 david         1.44 #endif
269 mday          1.43 #endif
270 mike          1.31     return !!fs;
271 mike          1.28 }
272                    
273                    Boolean FileSystem::isDirectory(const String& path)
274                    {
275 kumpf         1.38     return System::isDirectory(_clonePath(path));
276 mike          1.28 }
277                    
278                    Boolean FileSystem::changeDirectory(const String& path)
279                    {
280 kumpf         1.38     return System::changeDirectory(_clonePath(path));
281 mike          1.28 }
282                    
283                    Boolean FileSystem::makeDirectory(const String& path)
284                    {
285 kumpf         1.38     return System::makeDirectory(_clonePath(path));
286 mike          1.28 }
287                    
288                    Boolean FileSystem::removeDirectory(const String& path)
289                    {
290 kumpf         1.38     return System::removeDirectory(_clonePath(path));
291 mike          1.28 }
292                    
293                    Boolean FileSystem::removeDirectoryHier(const String& path)
294                    {
295                        Array<String> fileList;
296                    
297                        // Get contents of current directory
298                    
299                        if (!FileSystem::getDirectoryContents(path,fileList))
300                    	return false;
301                    
302                        // for files-in-directory, delete or recall removedir
303                    
304                        for (Uint32 i = 0, n = fileList.size(); i < n; i++)
305                        {   
306                    	String newPath = path;	 // extend path	to subdir
307                    	newPath.append("/");
308                    	newPath.append(fileList[i]);
309                    	
310                    	if (FileSystem::isDirectory(newPath))
311                    	{
312 mike          1.28 	    // Recall ourselves with extended path
313                    	    if (!FileSystem::removeDirectoryHier(newPath))
314                    		return false; 
315                    	}
316                    
317                    	else
318                    	{
319                              if (!FileSystem::removeFile(newPath))
320                    		return false;
321                    	}
322                        }
323                    
324                        return removeDirectory(path);	
325                    }
326                    
327                    //
328                    //  Get the file list in the directory into the
329                    //  array of strings provided
330                    //  @return The function should return false under these circumstances:
331                    //
332                    //
333 mike          1.28 //  1. The directory does not exist.
334                    //  2. The file exists but is not a directory.
335                    //  3. The directory is inaccessible.
336                    //
337                    //
338                    Boolean FileSystem::getDirectoryContents(
339                        const String& path,
340                        Array<String>& paths)
341                    {
342                        paths.clear();
343                    
344                        try
345                        { 
346                    	for (Dir dir(path); dir.more(); dir.next())
347                    	{
348                    	    String name = dir.getName();
349                    
350                    	    if (String::equal(name, ".") || String::equal(name, ".."))
351                    		continue;
352                    
353                    	    paths.append(name);
354 mike          1.28 	}
355                    	return true;
356                        }
357                    
358                        // Catch the Dir exception
359                        catch(CannotOpenDirectory&)
360                        {
361                        	return false;
362                        }
363                    }
364                    
365                    Boolean FileSystem::isDirectoryEmpty(const String& path)
366                    {
367                        for (Dir dir(path); dir.more(); dir.next())
368                        {
369                            const char* name = dir.getName();
370                    
371                            if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
372                                return false;
373                        }
374                    
375 mike          1.28     return true;
376                    }
377                    
378                    void FileSystem::translateSlashes(String& path)
379                    {
380 kumpf         1.35     for (Uint32 i = 0; i < path.size(); i++)
381 mike          1.28     {
382 kumpf         1.35 	if (path[i] == '\\')
383                    	    path[i] = '/';
384 mike          1.28     }
385                    }
386                    
387 tony          1.42 // Return the just the base name from the path.
388                    String  FileSystem::extractFileName(const String& path)
389                    {
390 a.arora       1.50   AutoArrayPtr<char> p_path(new char[path.size() + 1]);
391                      String basename = System::extract_file_name((const char *)path.getCString(), p_path.get());
392 tony          1.42   
393                      return basename;
394                    }
395                    
396                    // Return just the path to the file or directory into path
397                    String FileSystem::extractFilePath(const String& path)
398                    {
399 a.arora       1.50   AutoArrayPtr<char> p_path(new char[path.size() + 1]);
400                      String newpath = System::extract_file_path((const char *)path.getCString(), p_path.get());
401 tony          1.42   
402                      return newpath;
403                    }
404                    
405 kumpf         1.47 // Changes file permissions on the given file.
406                    Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
407                    {
408                    #if defined(PEGASUS_OS_OS400)
409                        // ATTN: If getCString() is modified to return UTF8, then handle the 
410                        //       EBCDIC coversion in SystemUnix.cpp
411                        CString tempPath = path.getCString();
412                    #else
413 david         1.49     CString tempPath = path.getCString();
414 kumpf         1.47 #endif
415                    
416                        return System::changeFilePermissions(tempPath, mode);
417 konrad.r      1.48 }
418                    
419                    String FileSystem::getAbsoluteFileName(const String &paths, const String &filename) {
420                    
421                      Uint32 pos =0;
422                      Uint32 token=0;
423                      String path = String::EMPTY;
424                      String root = String::EMPTY;
425                      String tempPath = paths;
426                      do {
427                        if (( pos = tempPath.find(FileSystem::getPathDelimiter())) == PEG_NOT_FOUND) {
428                                    pos = tempPath.size();
429                                    token = 0;
430                            }
431                            else {
432                                    token = 1;
433                            }
434                            path = tempPath.subString(0, pos);
435                            tempPath.remove(0,pos+token);
436                            if (FileSystem::exists( path + "/" + filename ) == true) {
437                    	  root = path + "/" + filename;
438 konrad.r      1.48 	  break;
439                            } else
440                    	  {
441                    	  //  cout << "File does not exist.\n";
442                    	  }
443                      } while (tempPath.size() > 0);
444                      return root;
445 kumpf         1.47 }
446                    
447 kumpf         1.51 String FileSystem::buildLibraryFileName(const String &libraryName)
448                    {
449                        String fileName;
450                    
451                        //
452                        // Add the necessary prefix and suffix to convert the library name to its
453                        // corresponding file name.
454                        //
455                    #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
456                        fileName = libraryName + String(".dll");
457                    #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
458                        fileName = String("lib") + libraryName + String(".sl");
459                    #elif defined(PEGASUS_OS_OS400)
460                        fileName = libraryName;
461                    #elif defined(PEGASUS_OS_DARWIN)
462                        fileName = String("lib") + libraryName + String(".dylib");
463                    #else
464                        fileName = String("lib") + libraryName + String(".so");
465                    #endif
466                    
467                        return fileName;
468 kumpf         1.51 }
469                    
470 tony          1.42 
471 kumpf         1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
472                    {
473                        line.clear();
474                    
475                        Boolean gotChar = false;
476                        char c;
477                    
478                        while (is.get(c))
479                        {
480                            gotChar = true;
481                    
482                            if (c == '\n')
483                                break;
484                    
485                            line.append(c);
486                        }
487                    
488                        return gotChar;
489                    }
490                    
491 mike          1.28 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2