Skip to content

Commit

Permalink
Added code to prepend path to files placed in FileList in listFiles().
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Oct 17, 2007
1 parent a258e5f commit 70ca38a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
92 changes: 46 additions & 46 deletions io/cpp/db/io/File.cpp
Expand Up @@ -53,12 +53,7 @@ bool File::operator==(const File& rhs)
// compare names and types for equality
if(strcmp(mName, file->getName()) == 0)
{
if(isFile() == file->isFile() &&
isDirectory() == file->isDirectory() &&
isSymbolicLink() == file->isSymbolicLink())
{
rval = true;
}
rval = (getType() == file->getType());
}

return rval;
Expand All @@ -76,6 +71,7 @@ bool File::exists()
}
else
{
// FIXME: ENOENT (2) is errno for file not found
// FIXME: add error handling
}

Expand Down Expand Up @@ -116,64 +112,50 @@ off_t File::getLength()
return s.st_size;
}

bool File::isFile()
File::Type File::getType()
{
bool rval = false;
Type rval = Unknown;

// use lstat so symbolic links aren't followed
struct stat s;
int rc = lstat(mName, &s);
int rc = stat(mName, &s);
if(rc != 0)
{
// FIXME: add error handling
}
else
{
// check for regular file
rval = ((s.st_mode & S_IFMT) == S_IFREG);
switch(s.st_mode & S_IFMT)
{
case S_IFREG:
rval = RegularFile;
break;
case S_IFDIR:
rval = Directory;
break;
case S_IFLNK:
rval = SymbolicLink;
break;
default:
break;
}
}

return rval;
}

bool File::isFile()
{
return getType() == RegularFile;
}

bool File::isDirectory()
{
bool rval = false;

// use lstat so symbolic links aren't followed
struct stat s;
int rc = lstat(mName, &s);
if(rc != 0)
{
// FIXME: add error handling
}
else
{
// check for directory
rval = ((s.st_mode & S_IFMT) == S_IFDIR);
}

return rval;
return getType() == Directory;
}

bool File::isSymbolicLink()
{
bool rval = false;

// use lstat so symbolic links aren't followed
struct stat s;
int rc = lstat(mName, &s);
if(rc != 0)
{
// FIXME: add error handling
}
else
{
// check for symbolic link
rval = ((s.st_mode & S_IFMT) == S_IFLNK);
}

return rval;
return getType() == SymbolicLink;
}

void File::listFiles(FileList* files)
Expand All @@ -190,10 +172,28 @@ void File::listFiles(FileList* files)
{
// read each directory entry
struct dirent* entry;
unsigned int len1 = strlen(mName);
bool separator = mName[len1 - 1] != '/';
while((entry = readdir(dir)) != NULL)
{
// d_name is null-terminated name for file, add new File to list
File* file = new File(entry->d_name);
// d_name is null-terminated name for file, without path name
// so copy file name before d_name to get full path
unsigned int len2 = strlen(entry->d_name);
char path[len1 + len2 + 2];
memcpy(path, mName, len1);
if(separator)
{
// add path separator as appropriate
memset(path + len1, '/', 1);
memcpy(path + len1 + 1, entry->d_name, len2 + 1);
}
else
{
memcpy(path + len1, entry->d_name, len2 + 1);
}

// add new File to list
File* file = new File(path);
files->add(file);
}

Expand Down
16 changes: 16 additions & 0 deletions io/cpp/db/io/File.h
Expand Up @@ -26,6 +26,15 @@ class FileList;
*/
class File : public virtual db::rt::Object
{
public:
/**
* The types of files.
*/
typedef enum Type
{
RegularFile, Directory, SymbolicLink, Socket, Unknown
};

protected:
/**
* Stores the name of this file.
Expand Down Expand Up @@ -90,6 +99,13 @@ class File : public virtual db::rt::Object
*/
virtual off_t getLength();

/**
* Gets the Type of File.
*
* @return the Type of File this File is.
*/
virtual Type getType();

/**
* Returns true if this File is a regular file, false if it is not. If it
* is not, then it may be a directory or a symbolic link.
Expand Down
28 changes: 13 additions & 15 deletions test/cpp/main.cpp
Expand Up @@ -4362,7 +4362,6 @@ void runLoggerTest()
cout << endl << "Logger test complete." << endl;
}


void runUniqueListTest()
{
cout << "Starting UniqueList test." << endl << endl;
Expand Down Expand Up @@ -4411,21 +4410,20 @@ void runFileTest()
{
File* file = i->next();
const char* type;
if(file->isFile())
{
type = "Regular File";
}
else if(file->isDirectory())
{
type = "Directory";
}
else if(file->isSymbolicLink())
{
type = "Symbolic Link";
}
else
switch(file->getType())
{
type = "Unknown";
case File::RegularFile:
type = "Regular File";
break;
case File::Directory:
type = "Directory";
break;
case File::SymbolicLink:
type = "Symbolic Link";
break;
default:
type = "Unknown";
break;
}

cout << "Name: '" << file->getName() << "', Type: " << type << endl;
Expand Down

0 comments on commit 70ca38a

Please sign in to comment.