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

Diff for /pegasus/src/Pegasus/Common/FileSystem.cpp between version 1.1.1.1 and 1.10

version 1.1.1.1, 2001/01/14 19:51:35 version 1.10, 2001/04/08 20:28:27
Line 23 
Line 23 
 // Author: // Author:
 // //
 // $Log$ // $Log$
   // Revision 1.10  2001/04/08 20:28:27  karl
   // test
   //
   // Revision 1.9  2001/04/08 19:56:38  karl
   // Test version
   //
   // Revision 1.8  2001/04/08 19:20:04  mike
   // more TCP work
   //
   // Revision 1.7  2001/04/08 01:13:21  mike
   // Changed "ConstCIM" to "CIMConst"
   //
   // Revision 1.5  2001/03/11 23:35:32  mike
   // Ports to Linux
   //
   // Revision 1.3  2001/02/13 02:06:40  mike
   // Added renameFile() method.
   //
   // Revision 1.2  2001/02/11 05:42:33  mike
   // new
   //
 // Revision 1.1.1.1  2001/01/14 19:51:35  mike // Revision 1.1.1.1  2001/01/14 19:51:35  mike
 // Pegasus import // Pegasus import
 // //
Line 44 
Line 65 
 #include <cstdio> #include <cstdio>
 #include "Destroyer.h" #include "Destroyer.h"
 #include "FileSystem.h" #include "FileSystem.h"
   #include "Dir.h"
   //DEBUG ONLY
   #include <iostream.h>
  
 // ATTN-B: porting! // ATTN-B: porting!
  
Line 56 
Line 80 
 static const int ACCESS_READ_AND_WRITE = 6; static const int ACCESS_READ_AND_WRITE = 6;
 #endif #endif
  
 // Clone the path but discard trailing slash if any:  /** Clone the path as a C String but discard
       trailing slash if any:
   */
 static char* _clonePath(const String& path) static char* _clonePath(const String& path)
 { {
     char* p = path.allocateCString();     char* p = path.allocateCString();
Line 75 
Line 100 
  
 Boolean FileSystem::exists(const String& path) Boolean FileSystem::exists(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS #ifdef PEGASUS_OS_TYPE_WINDOWS
     return _access(p.getPointer(), ACCESS_EXISTS) == 0;     return _access(p.getPointer(), ACCESS_EXISTS) == 0;
Line 84 
Line 109 
 #endif #endif
 } }
  
   Boolean FileSystem::getCurrentDirectory(String& path)
   {
   #ifdef PEGASUS_OS_TYPE_WINDOWS
       char* tmp = _getcwd(NULL, 0);
   
       if (!tmp)
           return false;
   
       path.append(tmp);
       delete [] tmp;
   #else
       char tmp[4096];
   
       getcwd(tmp, sizeof(tmp));
       path.append(tmp);
   #endif
       return true;
   }
   
   Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath)
   {
       realPath.clear();
       ArrayDestroyer<char> destroyer(_clonePath(path));
       char* p = destroyer.getPointer();
   
       char* dirPath;
       char* fileName;
       char* slash = strrchr(p, '/');
   
       if (slash)
       {
           *slash = '\0';
           fileName = slash + 1;
           dirPath = p;
           if (*fileName == '\0')
               return false;
       }
       else
       {
           fileName = p;
           dirPath = ".";
       }
   
       for (Dir dir(dirPath); dir.more(); dir.next())
       {
   #ifdef PEGASUS_OS_TYPE_WINDOWS
           if (stricmp(fileName, dir.getName()) == 0)
   #else
           if (strcasecmp(fileName, dir.getName()) == 0)
   #endif
           {
               if (strcmp(dirPath, ".") == 0)
                   realPath = dir.getName();
               else
               {
                   realPath = dirPath;
                   realPath += '/';
                   realPath += dir.getName();
               }
               return true;
           }
       }
   
       return false;
   }
   
 Boolean FileSystem::canRead(const String& path) Boolean FileSystem::canRead(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS #ifdef PEGASUS_OS_TYPE_WINDOWS
     return _access(p.getPointer(), ACCESS_READ) == 0;     return _access(p.getPointer(), ACCESS_READ) == 0;
Line 97 
Line 188 
  
 Boolean FileSystem::canWrite(const String& path) Boolean FileSystem::canWrite(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS #ifdef PEGASUS_OS_TYPE_WINDOWS
     return _access(p.getPointer(), ACCESS_WRITE) == 0;     return _access(p.getPointer(), ACCESS_WRITE) == 0;
Line 110 
Line 201 
 // ATTN: not implemented for NT. But not used by Pegasus. // ATTN: not implemented for NT. But not used by Pegasus.
 Boolean FileSystem::canExecute(const String& path) Boolean FileSystem::canExecute(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
     return access(p.getPointer(), X_OK) == 0;     return access(p.getPointer(), X_OK) == 0;
 } }
 #endif #endif
  
 Boolean FileSystem::isDirectory(const String& path) Boolean FileSystem::isDirectory(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
  
     struct stat st;     struct stat st;
  
Line 142 
Line 233 
  
 Boolean FileSystem::changeDirectory(const String& path) Boolean FileSystem::changeDirectory(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
     return chdir(p.getPointer()) == 0;     return chdir(p.getPointer()) == 0;
 } }
  
 Boolean FileSystem::makeDirectory(const String& path) Boolean FileSystem::makeDirectory(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
 #ifdef PEGASUS_OS_TYPE_WINDOWS #ifdef PEGASUS_OS_TYPE_WINDOWS
     return _mkdir(p.getPointer()) == 0;     return _mkdir(p.getPointer()) == 0;
 #else #else
Line 160 
Line 251 
 { {
     struct stat st;     struct stat st;
  
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
  
     if (stat(p.getPointer(), &st) != 0)     if (stat(p.getPointer(), &st) != 0)
         return false;         return false;
Line 171 
Line 262 
  
 Boolean FileSystem::removeDirectory(const String& path) Boolean FileSystem::removeDirectory(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
     return rmdir(p.getPointer()) == 0;     return rmdir(p.getPointer()) == 0;
 } }
  
   Boolean FileSystem::removeDirectoryHier(const String& path)
   {
       String saveCwd;
       FileSystem::getCurrentDirectory(saveCwd);
   
       Array<String> fileList;
   
       if (!FileSystem::getDirectoryContents(path,fileList))
           return false;
   
       FileSystem::changeDirectory(path);
   
       // for files-in-directory, delete or recall removedir
       // Do not yet test for boolean returns on the removes
       for (Uint32 i = 0, n = fileList.getSize(); i < n; i++)
       {
           // ATTN: Debug code here
           ArrayDestroyer<char> q(_clonePath(fileList[i]));
   
           if (FileSystem::isDirectory(fileList[i])){
               FileSystem::removeDirectoryHier(fileList[i]);
           }
   
           else{
               // ATTN: Mike the second is the problem.
               cout << "DEBUG RMFIL " << q.getPointer() <<endl;
               cout << "DEBUG RMFIL " << fileList[i] <<endl;
               removeFile(fileList[i]);
           }
       }
   
       FileSystem::changeDirectory(saveCwd);
       return removeDirectory(path);
   }
   
 Boolean FileSystem::removeFile(const String& path) Boolean FileSystem::removeFile(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
     return unlink(p.getPointer()) == 0;     return unlink(p.getPointer()) == 0;
 } }
  
Line 258 
Line 384 
     fclose(fp2);     fclose(fp2);
     return true;     return true;
 } }
   /*  Get the file list in the directory into the
       array of strings provided
       @return The function should return false under these circumstances:
   
  
       1. The directory does not exist.
       2. The file exists but is not a directory.
       3. The directory is inaccessible.
   
   */
 Boolean FileSystem::getDirectoryContents( Boolean FileSystem::getDirectoryContents(
     const String& path,     const String& path,
     Array<String>& paths)     Array<String>& paths)
 { {
     paths.clear();      // This may be just extra fluff but added anyway
       if (!FileSystem::isDirectory(path))
 #ifdef PEGASUS_OS_TYPE_WINDOWS  
   
     String tmp = path;  
     tmp += "/*";  
     Destroyer<char> p(tmp.allocateCString());  
   
     long file;  
     struct _finddata_t findData;  
   
     if ((file = _findfirst(p.getPointer(), &findData)) == -1L)  
         return false;         return false;
  
     do      paths.clear();
       try
     {     {
         const char* name = findData.name;          for (Dir dir(path); dir.more(); dir.next())
           {
         if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)              String name = dir.getName();
               if (String::equal(name, ".") || String::equal(name, ".."))
             continue;             continue;
   
         paths.append(name);         paths.append(name);
           }
           return true;
       }
  
     } while (_findnext(file, &findData) == 0);      // Catch the Dir exception
       catch(CannotOpenDirectory&)
     _findclose(file);      {
   
 #else  
   
     Destroyer<char> p(_clonePath(path));  
     DIR* dir = opendir(p.getPointer());  
   
     if (!dir)  
         return false;         return false;
       }
  
     struct dirent* entry;  }
  
     while ((entry = readdir(dir)) != NULL)  Boolean FileSystem::renameFile(
       const String& oldFileName,
       const String& newFileName)
     {     {
         const char* name = entry->d_name;      ArrayDestroyer<char> p(oldFileName.allocateCString());
       ArrayDestroyer<char> q(newFileName.allocateCString());
  
         if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)  #ifdef PEGASUS_OS_TYPE_WINDOWS
             continue;      return rename(p.getPointer(), q.getPointer()) == 0;
   #else
         paths.append(name);      if (link(p.getPointer(), q.getPointer()) != 0)
     }          return false;
  
     closedir(dir);      return unlink(p.getPointer()) == 0;
 #endif #endif
   
     return true;  
 } }
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.10

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2