(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.3 and 1.21

version 1.3, 2001/02/13 02:06:40 version 1.21, 2001/05/06 00:09:12
Line 1 
Line 1 
 //BEGIN_LICENSE  //%/////////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
 // //
Line 17 
Line 17 
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 // DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
 // //
 //END_LICENSE  //==============================================================================
 //BEGIN_HISTORY  
 // //
 // Author:  // Author: Mike Brasher (mbrasher@bmc.com)
 // //
 // $Log$  // Modified By:
 // 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  
 // Pegasus import  
 //  
 //  
 //END_HISTORY  
   
 #include <Pegasus/Common/Config.h>  
   
 #ifdef PEGASUS_OS_TYPE_WINDOWS  
 # include <io.h>  
 # include <direct.h>  
 #else  
 # include <unistd.h>  
 # include <dirent.h>  
 #endif  
  
 #include <sys/stat.h>  #include <iostream>
 #include <sys/types.h>  
 #include <cstdio> #include <cstdio>
   #include <Pegasus/Common/Config.h>
   #include <Pegasus/Common/System.h>
 #include "Destroyer.h" #include "Destroyer.h"
 #include "FileSystem.h" #include "FileSystem.h"
   #include "System.h"
 #include "Dir.h" #include "Dir.h"
  
 // ATTN-B: porting!  using namespace std;
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS  // Clone the path as a C String but discard trailing slash if any:
 static const int ACCESS_EXISTS = 0;  
 static const int ACCESS_WRITE = 2;  
 static const int ACCESS_READ = 4;  
 static const int ACCESS_READ_AND_WRITE = 6;  
 #endif  
   
 // Clone the path but discard trailing slash if any:  
  
 static char* _clonePath(const String& path) static char* _clonePath(const String& path)
 { {
Line 72 
Line 47 
     if (!*p)     if (!*p)
         return p;         return p;
  
     char* last = p + path.getLength() - 1;      char* last = p + path.size() - 1;
  
     if (*last == '/')     if (*last == '/')
         *last = '\0';         *last = '\0';
Line 82 
Line 57 
  
 Boolean FileSystem::exists(const String& path) Boolean FileSystem::exists(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
       return System::exists(p.getPointer());
   }
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS  Boolean FileSystem::getCurrentDirectory(String& path)
     return _access(p.getPointer(), ACCESS_EXISTS) == 0;  {
 #else      path.clear();
     return access(p.getPointer(), F_OK) == 0;      char tmp[4096];
 #endif  
       if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
           return false;
   
       path.append(tmp);
       return true;
 } }
  
 Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath) Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath)
 { {
     realPath.clear();     realPath.clear();
     Destroyer<char> destroyer(_clonePath(path));      ArrayDestroyer<char> destroyer(_clonePath(path));
     char* p = destroyer.getPointer();     char* p = destroyer.getPointer();
  
     char* dirPath;     char* dirPath;
Line 106 
Line 88 
         *slash = '\0';         *slash = '\0';
         fileName = slash + 1;         fileName = slash + 1;
         dirPath = p;         dirPath = p;
   
         if (*fileName == '\0')         if (*fileName == '\0')
             return false;             return false;
     }     }
Line 115 
Line 98 
         dirPath = ".";         dirPath = ".";
     }     }
  
   
     for (Dir dir(dirPath); dir.more(); dir.next())     for (Dir dir(dirPath); dir.more(); dir.next())
     {     {
 #ifdef PEGASUS_OS_TYPE_WINDOWS          if (CompareIgnoreCase(fileName, dir.getName()) == 0)
         if (stricmp(fileName, dir.getName()) == 0)  
 #else  
         if (strcasecmp(fileName, dir.getName()) == 0)  
 #endif  
         {         {
             if (strcmp(dirPath, ".") == 0)             if (strcmp(dirPath, ".") == 0)
                 realPath = dir.getName();                 realPath = dir.getName();
Line 140 
Line 120 
  
 Boolean FileSystem::canRead(const String& path) Boolean FileSystem::canRead(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
       return System::canRead(p.getPointer());
 #ifdef PEGASUS_OS_TYPE_WINDOWS  
     return _access(p.getPointer(), ACCESS_READ) == 0;  
 #else  
     return access(p.getPointer(), R_OK) == 0;  
 #endif  
 } }
  
 Boolean FileSystem::canWrite(const String& path) Boolean FileSystem::canWrite(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
       return System::canWrite(p.getPointer());
 #ifdef PEGASUS_OS_TYPE_WINDOWS  
     return _access(p.getPointer(), ACCESS_WRITE) == 0;  
 #else  
     return access(p.getPointer(), W_OK) == 0;  
 #endif  
 } }
  
 #if 0  Boolean FileSystem::isDirectory(const String& path)
 // ATTN: not implemented for NT. But not used by Pegasus.  
 Boolean FileSystem::canExecute(const String& path)  
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
     return access(p.getPointer(), X_OK) == 0;      return System::isDirectory(p.getPointer());
 } }
 #endif  
  
 Boolean FileSystem::isDirectory(const String& path)  Boolean FileSystem::changeDirectory(const String& path)
 { {
     Destroyer<char> p(_clonePath(path));      ArrayDestroyer<char> p(_clonePath(path));
       return System::changeDirectory(p.getPointer());
   }
  
     struct stat st;  Boolean FileSystem::makeDirectory(const String& path)
   {
       ArrayDestroyer<char> p(_clonePath(path));
       return System::makeDirectory(p.getPointer());
   }
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS  Boolean FileSystem::getFileSize(const String& path, Uint32& size)
   {
       ArrayDestroyer<char> p(_clonePath(path));
       return System::getFileSize(p.getPointer(), size);
   }
  
     if (stat(p.getPointer(), &st) != 0)  Boolean FileSystem::removeDirectory(const String& path)
         return false;  {
       ArrayDestroyer<char> p(_clonePath(path));
       return System::removeDirectory(p.getPointer());
   }
  
     Boolean result = (st.st_mode & _S_IFDIR) != 0;  Boolean FileSystem::removeDirectoryHier(const String& path)
     return result;  {
       Array<String> fileList;
  
 #else      // Get contents of current directory
  
     if (stat(p.getPointer(), &st) != 0)      if (!FileSystem::getDirectoryContents(path,fileList))
         return false;         return false;
  
     Boolean result = S_ISDIR(st.st_mode);      // for files-in-directory, delete or recall removedir
     return result;  
  
 #endif      for (Uint32 i = 0, n = fileList.size(); i < n; i++)
 }  
   
 Boolean FileSystem::changeDirectory(const String& path)  
 { {
     Destroyer<char> p(_clonePath(path));          String newPath = path;   // extend path to subdir
     return chdir(p.getPointer()) == 0;          newPath.append("/");
 }          newPath.append(fileList[i]);
  
 Boolean FileSystem::makeDirectory(const String& path)          if (FileSystem::isDirectory(newPath))
 { {
     Destroyer<char> p(_clonePath(path));              // Recall ourselves with extended path
 #ifdef PEGASUS_OS_TYPE_WINDOWS              if (!FileSystem::removeDirectoryHier(newPath))
     return _mkdir(p.getPointer()) == 0;                  return false;
 #else  
     return mkdir(p.getPointer(), 0777) == 0;  
 #endif  
 } }
  
 Boolean FileSystem::getFileSize(const String& path, Uint32& size)          else
 { {
     struct stat st;            if (!FileSystem::removeFile(newPath))
   
     Destroyer<char> p(_clonePath(path));  
   
     if (stat(p.getPointer(), &st) != 0)  
         return false;         return false;
           }
     size = st.st_size;  
     return true;  
 } }
  
 Boolean FileSystem::removeDirectory(const String& path)      return removeDirectory(path);
 {  
     Destroyer<char> p(_clonePath(path));  
     return rmdir(p.getPointer()) == 0;  
 } }
  
 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 System::removeFile(p.getPointer());
 } }
  
 void FileSystem::loadFileToMemory( void FileSystem::loadFileToMemory(
Line 313 
Line 278 
     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)
 { {
   #if 0
       // This may be just extra fluff but added anyway
       if (!FileSystem::isDirectory(path))
           return false;
   #endif
   
     paths.clear();     paths.clear();
  
       try
       {
     for (Dir dir(path); dir.more(); dir.next())     for (Dir dir(path); dir.more(); dir.next())
     {     {
         String name = dir.getName();         String name = dir.getName();
  
         if (name == "." || name ==  "..")              if (String::equal(name, ".") || String::equal(name, ".."))
             continue;             continue;
  
         paths.append(name);         paths.append(name);
     }     }
   
     return true;     return true;
 } }
  
       // Catch the Dir exception
       catch(CannotOpenDirectory&)
       {
           return false;
       }
   }
   
 Boolean FileSystem::renameFile( Boolean FileSystem::renameFile(
     const String& oldFileName,     const String& oldFileName,
     const String& newFileName)     const String& newFileName)
 { {
     Destroyer<char> p(oldFileName.allocateCString());      ArrayDestroyer<char> p(oldFileName.allocateCString());
     Destroyer<char> q(newFileName.allocateCString());      ArrayDestroyer<char> q(newFileName.allocateCString());
       return System::renameFile(p.getPointer(), q.getPointer());
   }
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS  void FileSystem::translateSlashes(String& path)
     return rename(p.getPointer(), q.getPointer()) == 0;  {
 #else      for (Uint32 i = 0, n = path.size(); i < n; i++)
     return link(p.getPointer(), q.getPointer()) == 0;      {
 #endif          if (path[i] == '\\')
               path[i] = '/';
       }
   }
   
   Boolean FileSystem::isDirectoryEmpty(const String& path)
   {
       for (Dir dir(path); dir.more(); dir.next())
       {
           const char* name = dir.getName();
   
           if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
               return false;
       }
   
       return true;
 } }
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.3  
changed lines
  Added in v.1.21

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2