git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@60 d34b734f-a00e-4b39-a726-e4eeb87269abadeniz_1
@@ -0,0 +1,143 @@ | |||
// \file filesystem.cpp | |||
// | |||
// Copyright (C) 2014 MicroNeil Research Corporation. | |||
// | |||
// This program is part of the MicroNeil Research Open Library Project. For | |||
// more information go to http://www.microneil.com/OpenLibrary/index.html | |||
// | |||
// This program is free software; you can redistribute it and/or modify it | |||
// under the terms of the GNU General Public License as published by the | |||
// Free Software Foundation; either version 2 of the License, or (at your | |||
// option) any later version. | |||
// | |||
// This program is distributed in the hope that it will be useful, but WITHOUT | |||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |||
// more details. | |||
// | |||
// You should have received a copy of the GNU General Public License along with | |||
// this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |||
// Place, Suite 330, Boston, MA 02111-1307 USA | |||
//============================================================================== | |||
#include <stdexcept> | |||
#include "filesystem.hpp" | |||
namespace CodeDweller { | |||
FileReference::FileReference(std::string fileName) : | |||
name(fileName), | |||
timestamp(0), | |||
size_bytes(0), | |||
fileExists(false), | |||
fileIsDirectory(false) { | |||
refresh(); | |||
} | |||
void FileReference::refresh() { | |||
// Load path if necessary. | |||
if (path.empty()) { | |||
char *realPath = realpath(name.c_str(), NULL); | |||
if (NULL == path) { | |||
// Nothing to do if the file doesn't exist. | |||
if (ENOENT == errno) { | |||
return; | |||
} | |||
// Something went wrong. | |||
throw std::runtime_error("Error checking file \"" + name "\": " + | |||
getErrorText()); | |||
} | |||
path = *realPath; | |||
free(realPath); | |||
} | |||
// Load info. | |||
struct stat statBuffer; | |||
int status = stat(path.c_str(), &statBuffer); | |||
if (-1 == status) { | |||
// File no longer exists. | |||
if (ENOENT == errno) { | |||
reset(); | |||
return; | |||
} | |||
// Something went wrong. | |||
throw std::runtime_error("Error updating status of file \"" + | |||
path "\": " + getErrorText()); | |||
} | |||
modTimestamp = statBuffer.st_mtime; | |||
size_bytes = statBuffer.st_size; | |||
fileExists = true; | |||
fileIsDirectory = S_ISDIR(statBuffer.st_mode); | |||
} | |||
void FileReference:: reset() { | |||
modTimestamp = 0; | |||
size_bytes = 0; | |||
fileExists = false; | |||
fileIsDirectory = false; | |||
} | |||
time_t FileReference::ModTimestamp() const { | |||
return modTimestamp; | |||
} | |||
long FileReference::Size() const { | |||
return size_bytes; | |||
} | |||
std::string FileReference::FullPath() const { | |||
return path; | |||
} | |||
time_t FileReference::exists() const { | |||
return fileExists; | |||
} | |||
time_t FileReference::isDirectory() const { | |||
return fileIsDirectory; | |||
} | |||
std::string FileReference::getErrorText() { | |||
#ifdef _WIN32 | |||
LPVOID winMsgBuf; | |||
DWORD lastError = GetLastError(); | |||
FormatMessage( | |||
FORMAT_MESSAGE_ALLOCATE_BUFFER | | |||
FORMAT_MESSAGE_FROM_SYSTEM | | |||
FORMAT_MESSAGE_IGNORE_INSERTS, | |||
NULL, | |||
lastError, | |||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |||
(char *) &winMsgBuf, | |||
0, NULL ); | |||
std::string errMsg((char *) winMsgBuf); | |||
LocalFree(winMsgBuf); | |||
return errMsg; | |||
#else | |||
return strerror(errno); | |||
#endif | |||
} | |||
} |
@@ -0,0 +1,124 @@ | |||
// \file filesystem.hpp | |||
// | |||
// Copyright (C) 2015 MicroNeil Research Corporation. | |||
// | |||
// This program is part of the MicroNeil Research Open Library Project. For | |||
// more information go to http://www.microneil.com/OpenLibrary/index.html | |||
// | |||
// This program is free software; you can redistribute it and/or modify it | |||
// under the terms of the GNU General Public License as published by the | |||
// Free Software Foundation; either version 2 of the License, or (at your | |||
// option) any later version. | |||
// | |||
// This program is distributed in the hope that it will be useful, but WITHOUT | |||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |||
// more details. | |||
// | |||
// You should have received a copy of the GNU General Public License along with | |||
// this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |||
// Place, Suite 330, Boston, MA 02111-1307 USA | |||
//============================================================================== | |||
/* | |||
\brief The filesystem module provides classes to access the filesystem. | |||
*/ | |||
#ifndef FILESYSTEM_HPP | |||
#define FILESYSTEM_HPP | |||
#include <ctime> | |||
#include <string> | |||
namespace CodeDweller { | |||
/** Abstracts OS specifics for identifying a file and provides | |||
access to meta data. | |||
*/ | |||
class FileReference { | |||
public: | |||
/** Initialize with the name of the file. | |||
@param[in] fileName is the name of the file. | |||
*/ | |||
FileReference(std::string fileName); | |||
/** Return timestamp. | |||
@returns the epoch modification time for file when | |||
FileReference was created or last refreshed, or zero if the | |||
file doesn't exist. | |||
*/ | |||
time_t ModTimestamp() const; | |||
/** Get the file size. | |||
@returns the size of file when FileReference was created or | |||
last refreshed, or 0 if the file doesn't exist. | |||
*/ | |||
long Size() const; | |||
/** Get full path. | |||
@returns a fully referenced path to the file. | |||
*/ | |||
std:: string FullPath() const; | |||
/** Refreshes the FileReference object with the current data on the file | |||
system. | |||
*/ | |||
void refresh(); | |||
/** Check if the file exists. | |||
@returns true if the file reference name exists in the file | |||
system, false otherwise. | |||
@throws std::runtime_error if an error occurs. | |||
*/ | |||
bool exists() const; | |||
/** Check if the file is a directory. | |||
@returns true if the name provided on construction resulted in | |||
finding a directory not a file, and false otherwise or if the | |||
file doesn't exist. | |||
*/ | |||
bool isDirectory() const; | |||
private: | |||
/// Return text for the most recent error. | |||
// | |||
// \returns Human-readable description of the most recent error. | |||
// | |||
static std::string getErrorText(); | |||
/** Name provided to constructor. */ | |||
std::string name; | |||
/** Full path. */ | |||
std::string path; | |||
/** Timestamp. */ | |||
time_t modTimestamp; | |||
/** Size in bytes. */ | |||
long size_bytes; | |||
/** True if the file exists, false otherwise. */ | |||
bool fileExists; | |||
/** True if the file is a directory, false otherwise. */ | |||
bool fileIsDirectory; | |||
}; | |||
} | |||
#endif // FILESYSTEM_HPP |