version 1.3, 2001/02/13 02:06:40
|
version 1.11, 2001/04/08 20:29:41
|
|
|
// Author: | // Author: |
// | // |
// $Log$ | // $Log$ |
|
// Revision 1.11 2001/04/08 20:29:41 mike |
|
// Added std:: before cout and endl |
|
// |
|
// 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 | // Revision 1.3 2001/02/13 02:06:40 mike |
// Added renameFile() method. | // Added renameFile() method. |
// | // |
|
|
#include "Destroyer.h" | #include "Destroyer.h" |
#include "FileSystem.h" | #include "FileSystem.h" |
#include "Dir.h" | #include "Dir.h" |
|
//DEBUG ONLY |
|
#include <iostream.h> |
| |
// ATTN-B: porting! | // ATTN-B: porting! |
| |
|
|
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(); |
|
|
| |
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; |
|
|
#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) | 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; |
|
|
| |
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; |
|
|
| |
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; |
|
|
// 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; |
| |
|
|
| |
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 |
|
|
{ | { |
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; |
|
|
| |
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. |
|
std::cout << "DEBUG RMFIL " << q.getPointer() <<std::endl; |
|
std::cout << "DEBUG RMFIL " << fileList[i] <<std::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; |
} | } |
| |
|
|
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)) |
|
return false; |
| |
|
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 (String::equal(name, ".") || String::equal(name, "..")) |
if (name == "." || 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()); |
| |
#ifdef PEGASUS_OS_TYPE_WINDOWS | #ifdef PEGASUS_OS_TYPE_WINDOWS |
return rename(p.getPointer(), q.getPointer()) == 0; | return rename(p.getPointer(), q.getPointer()) == 0; |
#else | #else |
return link(p.getPointer(), q.getPointer()) == 0; |
if (link(p.getPointer(), q.getPointer()) != 0) |
|
return false; |
|
|
|
return unlink(p.getPointer()) == 0; |
#endif | #endif |
} | } |
| |