version 1.10, 2001/04/08 20:28:27
|
version 1.83, 2008/12/01 17:49:51
|
|
|
//BEGIN_LICENSE |
//%LICENSE//////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM |
// Licensed to The Open Group (TOG) under one or more contributor license |
|
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
|
// this work for additional information regarding copyright ownership. |
|
// Each contributor licenses this file to you under the OpenPegasus Open |
|
// Source License; you may not use this file except in compliance with the |
|
// License. |
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a | // Permission is hereby granted, free of charge, to any person obtaining a |
// copy of this software and associated documentation files (the "Software"), | // copy of this software and associated documentation files (the "Software"), |
|
|
// and/or sell copies of the Software, and to permit persons to whom the | // and/or sell copies of the Software, and to permit persons to whom the |
// Software is furnished to do so, subject to the following conditions: | // Software is furnished to do so, subject to the following conditions: |
// | // |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
// The above copyright notice and this permission notice shall be included |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
// in all copies or substantial portions of the Software. |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
// DEALINGS IN THE SOFTWARE. |
|
// | // |
//END_LICENSE |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
//BEGIN_HISTORY |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// | // |
// Author: |
////////////////////////////////////////////////////////////////////////// |
// | // |
// $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 |
|
// Pegasus import |
|
// |
|
// |
|
//END_HISTORY |
|
| |
|
#include <iostream> |
|
//#include <cstdio> |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
|
#include <Pegasus/Common/System.h> |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
#include <Pegasus/Common/AutoPtr.h> |
# include <io.h> |
#include <Executor/Match.h> |
# include <direct.h> |
|
#else |
|
# include <unistd.h> |
|
# include <dirent.h> |
|
#endif |
|
|
|
#include <sys/stat.h> |
|
#include <sys/types.h> |
|
#include <cstdio> |
|
#include "Destroyer.h" |
|
#include "FileSystem.h" | #include "FileSystem.h" |
#include "Dir.h" | #include "Dir.h" |
//DEBUG ONLY |
#ifndef PEGASUS_OS_TYPE_WINDOWS |
#include <iostream.h> |
#include <pwd.h> |
|
#endif |
// ATTN-B: porting! |
#include <Pegasus/Common/Tracer.h> |
|
|
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 |
static CString _clonePath(const String& path) |
trailing slash if any: |
|
*/ |
|
static char* _clonePath(const String& path) |
|
{ | { |
char* p = path.allocateCString(); |
String clone = path; |
|
|
if (!*p) |
|
return p; |
|
| |
char* last = p + path.getLength() - 1; |
if (clone.size() && clone[clone.size()-1] == '/') |
|
clone.remove(clone.size()-1); |
| |
if (*last == '/') |
return clone.getCString(); |
*last = '\0'; |
|
|
|
return p; |
|
} | } |
| |
Boolean FileSystem::exists(const String& path) | Boolean FileSystem::exists(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
return System::exists(_clonePath(path)); |
|
|
#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; |
} | } |
| |
Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath) |
Boolean FileSystem::existsNoCase(const String& path, String& realPath) |
{ | { |
|
#if !defined(PEGASUS_OS_VMS) && \ |
|
!defined(PEGASUS_OS_TYPE_WINDOWS) && \ |
|
!defined(PEGASUS_OS_DARWIN) |
|
|
|
// If a file exists that has the same case as the path parmater, |
|
// then we can bypass the expensive directory scanning below. |
|
|
|
if (FileSystem::exists(path)) |
|
{ |
|
realPath = path; |
|
return true; |
|
} |
|
|
|
#endif |
|
|
realPath.clear(); | realPath.clear(); |
ArrayDestroyer<char> destroyer(_clonePath(path)); |
CString cpath = _clonePath(path); |
char* p = destroyer.getPointer(); |
const char* p = cpath; |
| |
char* dirPath; |
const char* dirPath; |
char* fileName; |
const char* fileName; |
char* slash = strrchr(p, '/'); |
char* slash = (char *) strrchr(p, '/'); |
| |
if (slash) | if (slash) |
{ | { |
*slash = '\0'; | *slash = '\0'; |
fileName = slash + 1; | fileName = slash + 1; |
dirPath = p; | dirPath = p; |
|
|
if (*fileName == '\0') | if (*fileName == '\0') |
return false; | return false; |
} | } |
|
|
dirPath = "."; | dirPath = "."; |
} | } |
| |
|
|
for (Dir dir(dirPath); dir.more(); dir.next()) | for (Dir dir(dirPath); dir.more(); dir.next()) |
{ | { |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
if (System::strcasecmp(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(); |
else | else |
{ | { |
realPath = dirPath; | realPath = dirPath; |
realPath += '/'; |
realPath.append('/'); |
realPath += dir.getName(); |
realPath.append(dir.getName()); |
} | } |
return true; | return true; |
} | } |
|
|
| |
Boolean FileSystem::canRead(const String& path) | Boolean FileSystem::canRead(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
return System::canRead(_clonePath(path)); |
|
|
#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)); |
return System::canWrite(_clonePath(path)); |
|
} |
| |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
Boolean FileSystem::getFileSize(const String& path, Uint32& size) |
return _access(p.getPointer(), ACCESS_WRITE) == 0; |
{ |
#else |
return System::getFileSize(_clonePath(path), size); |
return access(p.getPointer(), W_OK) == 0; |
|
#endif |
|
} | } |
| |
#if 0 |
Boolean FileSystem::removeFile(const String& path) |
// ATTN: not implemented for NT. But not used by Pegasus. |
|
Boolean FileSystem::canExecute(const String& path) |
|
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
return System::removeFile(_clonePath(path)); |
return access(p.getPointer(), X_OK) == 0; |
|
} | } |
#endif |
|
| |
Boolean FileSystem::isDirectory(const String& path) |
void FileSystem::loadFileToMemory( |
|
Buffer& array, |
|
const String& fileName) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
Uint32 fileSize; |
| |
struct stat st; |
if (!getFileSize(fileName, fileSize)) |
|
throw CannotOpenFile(fileName); |
|
|
|
FILE* fp = fopen(fileName.getCString(), "rb"); |
|
|
|
if (fp == NULL) |
|
throw CannotOpenFile(fileName); |
|
|
|
array.reserveCapacity(fileSize); |
|
char buffer[4096]; |
|
size_t n; |
| |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) |
|
array.append(buffer, static_cast<Uint32>(n)); |
| |
if (stat(p.getPointer(), &st) != 0) |
fclose(fp); |
|
} |
|
|
|
Boolean FileSystem::compareFiles( |
|
const String& path1, |
|
const String& path2) |
|
{ |
|
Uint32 fileSize1; |
|
|
|
if (!getFileSize(path1, fileSize1)) |
|
throw CannotOpenFile(path1); |
|
|
|
Uint32 fileSize2; |
|
|
|
if (!getFileSize(path2, fileSize2)) |
|
throw CannotOpenFile(path2); |
|
|
|
if (fileSize1 != fileSize2) |
return false; | return false; |
| |
Boolean result = (st.st_mode & _S_IFDIR) != 0; |
FILE* fp1 = fopen(path1.getCString(), "rb"); |
return result; |
|
| |
#else |
if (fp1 == NULL) |
|
throw CannotOpenFile(path1); |
|
|
|
FILE* fp2 = fopen(path2.getCString(), "rb"); |
|
|
|
if (fp2 == NULL) |
|
{ |
|
fclose(fp1); |
|
throw CannotOpenFile(path2); |
|
} |
|
|
|
int c1; |
|
int c2; |
| |
if (stat(p.getPointer(), &st) != 0) |
while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF) |
|
{ |
|
if (c1 != c2) |
|
{ |
|
fclose(fp1); |
|
fclose(fp2); |
return false; | return false; |
|
} |
|
} |
| |
Boolean result = S_ISDIR(st.st_mode); |
fclose(fp1); |
return result; |
fclose(fp2); |
|
return true; |
|
} |
| |
#endif |
Boolean FileSystem::renameFile( |
|
const String& oldPath, |
|
const String& newPath) |
|
{ |
|
return System::renameFile(oldPath.getCString(), newPath.getCString()); |
} | } |
| |
Boolean FileSystem::changeDirectory(const String& path) |
Boolean FileSystem::copyFile( |
|
const String& fromPath, |
|
const String& toPath) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
return System::copyFile(fromPath.getCString(), toPath.getCString()); |
return chdir(p.getPointer()) == 0; |
|
} | } |
| |
Boolean FileSystem::makeDirectory(const String& path) |
Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
String realPath; |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
|
return _mkdir(p.getPointer()) == 0; |
if (!existsNoCase(path, realPath)) |
|
return false; |
|
|
|
is.open(_clonePath(realPath) PEGASUS_IOS_BINARY); |
|
|
|
return !!is; |
|
} |
|
|
|
Boolean FileSystem::openNoCase( |
|
PEGASUS_STD(fstream)& fs, |
|
const String& path, |
|
int mode) |
|
{ |
|
String realPath; |
|
|
|
if (!existsNoCase(path, realPath)) |
|
return false; |
|
#if defined(__GNUC__) && GCC_VERSION >= 30200 |
|
fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode)); |
#else | #else |
return mkdir(p.getPointer(), 0777) == 0; |
fs.open(_clonePath(realPath), mode); |
#endif | #endif |
|
return !!fs; |
} | } |
| |
Boolean FileSystem::getFileSize(const String& path, Uint32& size) |
Boolean FileSystem::isDirectory(const String& path) |
{ | { |
struct stat st; |
return System::isDirectory(_clonePath(path)); |
|
} |
ArrayDestroyer<char> p(_clonePath(path)); |
|
| |
if (stat(p.getPointer(), &st) != 0) |
Boolean FileSystem::changeDirectory(const String& path) |
return false; |
{ |
|
return System::changeDirectory(_clonePath(path)); |
|
} |
| |
size = st.st_size; |
Boolean FileSystem::makeDirectory(const String& path) |
return true; |
{ |
|
return System::makeDirectory(_clonePath(path)); |
} | } |
| |
Boolean FileSystem::removeDirectory(const String& path) | Boolean FileSystem::removeDirectory(const String& path) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
return System::removeDirectory(_clonePath(path)); |
return rmdir(p.getPointer()) == 0; |
|
} | } |
| |
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.size(); 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. |
{ |
cout << "DEBUG RMFIL " << q.getPointer() <<endl; |
if (!FileSystem::removeFile(newPath)) |
cout << "DEBUG RMFIL " << fileList[i] <<endl; |
return false; |
removeFile(fileList[i]); |
|
} | } |
} | } |
| |
FileSystem::changeDirectory(saveCwd); |
|
return removeDirectory(path); | return removeDirectory(path); |
} | } |
| |
Boolean FileSystem::removeFile(const String& path) |
// |
|
// 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( |
|
const String& path, |
|
Array<String>& paths) |
{ | { |
ArrayDestroyer<char> p(_clonePath(path)); |
paths.clear(); |
return unlink(p.getPointer()) == 0; |
|
|
try |
|
{ |
|
for (Dir dir(path); dir.more(); dir.next()) |
|
{ |
|
String name = dir.getName(); |
|
|
|
if (String::equal(name, ".") || String::equal(name, "..")) |
|
continue; |
|
|
|
paths.append(name); |
|
} |
|
return true; |
} | } |
| |
void FileSystem::loadFileToMemory( |
// Catch the Dir exception |
Array<Sint8>& array, |
catch (CannotOpenDirectory&) |
const String& fileName) |
|
{ | { |
Uint32 fileSize; |
return false; |
|
} |
|
} |
| |
if (!getFileSize(fileName, fileSize)) |
Boolean FileSystem::isDirectoryEmpty(const String& path) |
throw CannotOpenFile(fileName); |
{ |
|
for (Dir dir(path); dir.more(); dir.next()) |
|
{ |
|
const char* name = dir.getName(); |
| |
char* tmp = fileName.allocateCString(); |
if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) |
FILE* fp = fopen(tmp, "rb"); |
return false; |
delete [] tmp; |
} |
| |
if (fp == NULL) |
return true; |
throw CannotOpenFile(fileName); |
} |
| |
array.reserve(fileSize); |
void FileSystem::translateSlashes(String& path) |
char buffer[4096]; |
{ |
size_t n; |
for (Uint32 i = 0; i < path.size(); i++) |
|
{ |
|
if (path[i] == '\\') |
|
path[i] = '/'; |
|
} |
|
} |
| |
while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) |
// Return the just the base name from the path. |
array.append(buffer, n); |
String FileSystem::extractFileName(const String& path) |
|
{ |
|
AutoArrayPtr<char> p_path(new char[path.size() + 1]); |
|
String basename = System::extract_file_name( |
|
(const char*)path.getCString(), p_path.get()); |
| |
fclose(fp); |
return basename; |
} | } |
| |
Boolean FileSystem::compare( |
// Return just the path to the file or directory into path |
const String& fileName1, |
String FileSystem::extractFilePath(const String& path) |
const String& fileName2) |
|
{ | { |
Uint32 fileSize1; |
AutoArrayPtr<char> p_path(new char[path.size() + 1]); |
|
String newpath = System::extract_file_path( |
|
(const char*)path.getCString(), p_path.get()); |
| |
if (!getFileSize(fileName1, fileSize1)) |
return newpath; |
throw CannotOpenFile(fileName1); |
} |
| |
Uint32 fileSize2; |
// Changes file permissions on the given file. |
|
Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode) |
|
{ |
|
CString tempPath = path.getCString(); |
| |
if (!getFileSize(fileName2, fileSize2)) |
return System::changeFilePermissions(tempPath, mode); |
throw CannotOpenFile(fileName2); |
} |
| |
if (fileSize1 != fileSize2) |
String FileSystem::getAbsoluteFileName( |
return false; |
const String& paths, |
|
const String& filename) |
|
{ |
|
Uint32 pos = 0; |
|
Uint32 token = 0; |
|
String path; |
|
String root; |
|
String tempPath = paths; |
| |
char* tmp1 = fileName1.allocateCString(); |
do |
FILE* fp1 = fopen(tmp1, "rb"); |
{ |
delete [] tmp1; |
if ((pos = tempPath.find(FileSystem::getPathDelimiter())) == |
|
PEG_NOT_FOUND) |
|
{ |
|
pos = tempPath.size(); |
|
token = 0; |
|
} |
|
else |
|
{ |
|
token = 1; |
|
} |
|
path = tempPath.subString(0, pos); |
|
tempPath.remove(0,pos+token); |
|
if (FileSystem::exists(path + "/" + filename) == true) |
|
{ |
|
root = path + "/" + filename; |
|
break; |
|
} |
|
else |
|
{ |
|
// cout << "File does not exist.\n"; |
|
} |
|
} while (tempPath.size() > 0); |
| |
if (fp1 == NULL) |
return root; |
throw CannotOpenFile(fileName1); |
} |
| |
char* tmp2 = fileName2.allocateCString(); |
String FileSystem::buildLibraryFileName(const String &libraryName) |
FILE* fp2 = fopen(tmp2, "rb"); |
{ |
delete [] tmp2; |
String fileName; |
| |
if (fp2 == NULL) |
// |
|
// Add the necessary prefix and suffix to convert the library name to its |
|
// corresponding file name. |
|
// |
|
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
|
fileName = libraryName + String(".dll"); |
|
#else |
|
fileName = String("lib") + libraryName + getDynamicLibraryExtension(); |
|
#endif |
|
return fileName; |
|
} |
|
|
|
String FileSystem::getDynamicLibraryExtension() |
{ | { |
fclose(fp1); |
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
throw CannotOpenFile(fileName2); |
return String(".dll"); |
|
#elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) |
|
return String(".sl"); |
|
#elif defined(PEGASUS_OS_DARWIN) |
|
return String(".dylib"); |
|
#elif defined(PEGASUS_OS_VMS) |
|
return String(".exe"); |
|
#elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES64_IBM) |
|
return String("64.so"); |
|
#else |
|
return String(".so"); |
|
#endif |
} | } |
| |
int c1; |
Boolean GetLine(PEGASUS_STD(istream)& is, Buffer& line) |
int c2; |
{ |
|
const Uint32 buffersize = 1024; |
|
Uint32 gcount = 0; |
| |
while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF) |
line.clear(); |
|
|
|
// Read the input line in chunks. A non-full buffer indicates the end of |
|
// the line has been reached. |
|
do |
{ | { |
if (c1 != c2) |
char input[buffersize]; |
|
|
|
// This reads up to buffersize-1 char, but stops before consuming |
|
// a newline character ('\n'). |
|
is.get(input, buffersize); |
|
|
|
gcount = (Uint32)is.gcount(); |
|
line.append(input, gcount); |
|
|
|
if (is.rdstate() & PEGASUS_STD(istream)::failbit) |
{ | { |
fclose(fp1); |
// It is okay if we encounter the newline character without reading |
fclose(fp2); |
// data. |
return false; |
is.clear(); |
|
break; |
} | } |
|
} while (gcount == buffersize-1); |
|
|
|
if (!is.eof()) |
|
{ |
|
// we need to consume the '\n', because get() doesn't |
|
char c = 0; |
|
is.get(c); |
} | } |
| |
fclose(fp1); |
return !!is; |
fclose(fp2); |
} |
|
|
|
// |
|
// changes the file owner to one specified |
|
// |
|
Boolean FileSystem::changeFileOwner( |
|
const String& fileName, |
|
const String& userName) |
|
{ |
|
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
|
|
return true; | return true; |
|
|
|
#else |
|
|
|
PEG_METHOD_ENTER(TRC_AUTHENTICATION, "FileSystem::changeFileOwner()"); |
|
|
|
struct passwd* userPasswd; |
|
#if defined(PEGASUS_OS_SOLARIS) || \ |
|
defined(PEGASUS_OS_HPUX) || \ |
|
defined(PEGASUS_OS_LINUX) || \ |
|
defined (PEGASUS_OS_VMS) || \ |
|
defined (PEGASUS_OS_AIX) |
|
|
|
const unsigned int PWD_BUFF_SIZE = 1024; |
|
struct passwd pwd; |
|
char pwdBuffer[PWD_BUFF_SIZE]; |
|
|
|
if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE, |
|
&userPasswd) != 0) |
|
{ |
|
userPasswd = (struct passwd*)NULL; |
} | } |
/* Get the file list in the directory into the |
|
array of strings provided |
|
@return The function should return false under these circumstances: |
|
| |
|
#else |
| |
1. The directory does not exist. |
userPasswd = getpwnam(userName.getCString()); |
2. The file exists but is not a directory. |
#endif |
3. The directory is inaccessible. |
|
| |
*/ |
if (userPasswd == NULL) |
Boolean FileSystem::getDirectoryContents( |
|
const String& path, |
|
Array<String>& paths) |
|
{ | { |
// This may be just extra fluff but added anyway |
PEG_METHOD_EXIT(); |
if (!FileSystem::isDirectory(path)) |
|
return false; | return false; |
|
} |
| |
paths.clear(); |
Sint32 ret = chown( |
try |
fileName.getCString(), userPasswd->pw_uid, userPasswd->pw_gid); |
{ |
|
for (Dir dir(path); dir.more(); dir.next()) |
if (ret == -1) |
{ | { |
String name = dir.getName(); |
PEG_METHOD_EXIT(); |
if (String::equal(name, ".") || String::equal(name, "..")) |
return false; |
continue; |
|
paths.append(name); |
|
} | } |
|
|
|
PEG_METHOD_EXIT(); |
|
|
return true; | return true; |
|
#endif |
} | } |
| |
// Catch the Dir exception |
void FileSystem::syncWithDirectoryUpdates(PEGASUS_STD(fstream)& fs) |
catch(CannotOpenDirectory&) |
|
{ | { |
return false; |
#if defined(PEGASUS_OS_HPUX) |
|
// Writes the data from the iostream buffers to the OS buffers |
|
fs.flush(); |
|
// Writes the data from the OS buffers to the disk |
|
fsync(fs.rdbuf()->fd()); |
|
#endif |
} | } |
| |
} |
Boolean FileSystem::glob( |
|
const String& path, |
|
const String& pattern_, |
|
Array<String>& filenames) |
|
{ |
|
filenames.clear(); |
| |
Boolean FileSystem::renameFile( |
try |
const String& oldFileName, |
|
const String& newFileName) |
|
{ | { |
ArrayDestroyer<char> p(oldFileName.allocateCString()); |
CString pattern(pattern_.getCString()); |
ArrayDestroyer<char> q(newFileName.allocateCString()); |
|
| |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
for (Dir dir(path); dir.more(); dir.next()) |
return rename(p.getPointer(), q.getPointer()) == 0; |
{ |
#else |
const char* name = dir.getName(); |
if (link(p.getPointer(), q.getPointer()) != 0) |
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) |
|
continue; |
|
|
|
if (Match(pattern, name) == 0) |
|
filenames.append(name); |
|
} |
|
} |
|
catch (CannotOpenDirectory&) |
|
{ |
return false; | return false; |
|
} |
| |
return unlink(p.getPointer()) == 0; |
return true; |
#endif |
|
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |