version 1.11, 2001/04/08 20:29:41
|
version 1.19, 2001/04/29 06:26:23
|
|
|
//BEGIN_LICENSE |
//%///////////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM | // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM |
// | // |
|
|
// 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.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 |
|
// 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 <iostream> |
# include <io.h> |
|
# include <direct.h> |
|
#else |
|
# include <unistd.h> |
|
# include <dirent.h> |
|
#endif |
|
|
|
#include <sys/stat.h> |
|
#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" |
//DEBUG ONLY |
|
#include <iostream.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 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) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
|
return System::exists(p.getPointer()); |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
|
return _access(p.getPointer(), ACCESS_EXISTS) == 0; |
|
#else |
|
return access(p.getPointer(), F_OK) == 0; |
|
#endif |
|
} | } |
| |
Boolean FileSystem::getCurrentDirectory(String& path) | Boolean FileSystem::getCurrentDirectory(String& path) |
{ | { |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
path.clear(); |
char* tmp = _getcwd(NULL, 0); |
char tmp[4096]; |
| |
if (!tmp) |
if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1)) |
return false; | return false; |
| |
path.append(tmp); | path.append(tmp); |
delete [] tmp; |
|
#else |
|
char tmp[4096]; |
|
|
|
getcwd(tmp, sizeof(tmp)); |
|
path.append(tmp); |
|
#endif |
|
return true; | return true; |
} | } |
| |
|
|
*slash = '\0'; | *slash = '\0'; |
fileName = slash + 1; | fileName = slash + 1; |
dirPath = p; | dirPath = p; |
|
|
if (*fileName == '\0') | if (*fileName == '\0') |
return false; | return false; |
} | } |
|
|
| |
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(); |
|
|
Boolean FileSystem::canRead(const String& path) | Boolean FileSystem::canRead(const String& path) |
{ | { |
ArrayDestroyer<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) |
{ | { |
ArrayDestroyer<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 |
|
// ATTN: not implemented for NT. But not used by Pegasus. |
|
Boolean FileSystem::canExecute(const String& path) |
|
{ |
|
ArrayDestroyer<char> p(_clonePath(path)); |
|
return access(p.getPointer(), X_OK) == 0; |
|
} | } |
#endif |
|
| |
Boolean FileSystem::isDirectory(const String& path) | Boolean FileSystem::isDirectory(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
|
return System::isDirectory(p.getPointer()); |
struct stat st; |
|
|
|
#ifdef PEGASUS_OS_TYPE_WINDOWS |
|
|
|
if (stat(p.getPointer(), &st) != 0) |
|
return false; |
|
|
|
Boolean result = (st.st_mode & _S_IFDIR) != 0; |
|
return result; |
|
|
|
#else |
|
|
|
if (stat(p.getPointer(), &st) != 0) |
|
return false; |
|
|
|
Boolean result = S_ISDIR(st.st_mode); |
|
return result; |
|
|
|
#endif |
|
} | } |
| |
Boolean FileSystem::changeDirectory(const String& path) | Boolean FileSystem::changeDirectory(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
return chdir(p.getPointer()) == 0; |
return System::changeDirectory(p.getPointer()); |
} | } |
| |
Boolean FileSystem::makeDirectory(const String& path) | Boolean FileSystem::makeDirectory(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
return System::makeDirectory(p.getPointer()); |
return _mkdir(p.getPointer()) == 0; |
|
#else |
|
return mkdir(p.getPointer(), 0777) == 0; |
|
#endif |
|
} | } |
| |
Boolean FileSystem::getFileSize(const String& path, Uint32& size) | Boolean FileSystem::getFileSize(const String& path, Uint32& size) |
{ | { |
struct stat st; |
|
|
|
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
|
return System::getFileSize(p.getPointer(), size); |
if (stat(p.getPointer(), &st) != 0) |
|
return false; |
|
|
|
size = st.st_size; |
|
return true; |
|
} | } |
| |
Boolean FileSystem::removeDirectory(const String& path) | Boolean FileSystem::removeDirectory(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); | ArrayDestroyer<char> p(_clonePath(path)); |
return rmdir(p.getPointer()) == 0; |
return System::removeDirectory(p.getPointer()); |
} | } |
| |
Boolean FileSystem::removeDirectoryHier(const String& path) | Boolean FileSystem::removeDirectoryHier(const String& path) |
{ | { |
String saveCwd; |
|
FileSystem::getCurrentDirectory(saveCwd); |
|
|
|
Array<String> fileList; | Array<String> fileList; |
| |
|
// Get contents of current directory |
|
|
if (!FileSystem::getDirectoryContents(path,fileList)) | if (!FileSystem::getDirectoryContents(path,fileList)) |
return false; | return false; |
| |
FileSystem::changeDirectory(path); |
|
|
|
// for files-in-directory, delete or recall removedir | // 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++) | for (Uint32 i = 0, n = fileList.getSize(); i < n; i++) |
{ | { |
// ATTN: Debug code here |
String newPath = path; // extend path to subdir |
ArrayDestroyer<char> q(_clonePath(fileList[i])); |
newPath.append("/"); |
|
newPath.append(fileList[i]); |
| |
if (FileSystem::isDirectory(fileList[i])){ |
if (FileSystem::isDirectory(newPath)) |
FileSystem::removeDirectoryHier(fileList[i]); |
{ |
|
// Recall ourselves with extended path |
|
if (!FileSystem::removeDirectoryHier(newPath)) |
|
return false; |
} | } |
| |
else{ |
else |
// ATTN: Mike the second is the problem. |
{ |
std::cout << "DEBUG RMFIL " << q.getPointer() <<std::endl; |
if (!FileSystem::removeFile(newPath)) |
std::cout << "DEBUG RMFIL " << fileList[i] <<std::endl; |
return false; |
removeFile(fileList[i]); |
|
} | } |
} | } |
| |
FileSystem::changeDirectory(saveCwd); |
|
return removeDirectory(path); | return removeDirectory(path); |
} | } |
| |
Boolean FileSystem::removeFile(const String& path) | Boolean FileSystem::removeFile(const String& path) |
{ | { |
ArrayDestroyer<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( |
|
|
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. |
// Get the file list in the directory into the |
2. The file exists but is not a directory. |
// array of strings provided |
3. The directory is inaccessible. |
// @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 | // This may be just extra fluff but added anyway |
if (!FileSystem::isDirectory(path)) | if (!FileSystem::isDirectory(path)) |
return false; | return false; |
|
#endif |
| |
paths.clear(); | paths.clear(); |
|
|
try | 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 (String::equal(name, ".") || String::equal(name, "..")) |
continue; | continue; |
|
|
paths.append(name); | paths.append(name); |
} | } |
return true; | return true; |
|
|
{ | { |
return false; | return false; |
} | } |
|
|
} | } |
| |
Boolean FileSystem::renameFile( | Boolean FileSystem::renameFile( |
|
|
{ | { |
ArrayDestroyer<char> p(oldFileName.allocateCString()); | ArrayDestroyer<char> p(oldFileName.allocateCString()); |
ArrayDestroyer<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.getLength(); i < n; i++) |
if (link(p.getPointer(), q.getPointer()) != 0) |
{ |
|
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 false; |
|
} |
| |
return unlink(p.getPointer()) == 0; |
return true; |
#endif |
|
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |