Rename FileOps to FileUtils for consistency with the ProcessUtils and StringUtils classes.

This commit is contained in:
Robert Knight 2011-08-29 22:27:55 +01:00
parent b7d999b2c8
commit d7e3cc8d63
6 changed files with 53 additions and 53 deletions

View File

@ -1,6 +1,6 @@
#include "AppInfo.h" #include "AppInfo.h"
#include "FileOps.h" #include "FileUtils.h"
#include "Platform.h" #include "Platform.h"
#include "StringUtils.h" #include "StringUtils.h"

View File

@ -26,7 +26,7 @@ add_definitions(-DTIXML_USE_STL)
set (SOURCES set (SOURCES
AppInfo.cpp AppInfo.cpp
DirIterator.cpp DirIterator.cpp
FileOps.cpp FileUtils.cpp
Log.cpp Log.cpp
ProcessUtils.cpp ProcessUtils.cpp
UpdateInstaller.cpp UpdateInstaller.cpp
@ -44,7 +44,7 @@ endif()
set (HEADERS set (HEADERS
AppInfo.h AppInfo.h
DirIterator.h DirIterator.h
FileOps.h FileUtils.h
Log.h Log.h
ProcessUtils.h ProcessUtils.h
UpdateInstaller.h UpdateInstaller.h

View File

@ -1,4 +1,4 @@
#include "FileOps.h" #include "FileUtils.h"
#include "DirIterator.h" #include "DirIterator.h"
#include "Log.h" #include "Log.h"
@ -20,7 +20,7 @@
#include <libgen.h> #include <libgen.h>
#endif #endif
FileOps::IOException::IOException(const std::string& error) FileUtils::IOException::IOException(const std::string& error)
: m_errno(0) : m_errno(0)
{ {
m_error = error; m_error = error;
@ -39,11 +39,11 @@ FileOps::IOException::IOException(const std::string& error)
#endif #endif
} }
FileOps::IOException::~IOException() throw () FileUtils::IOException::~IOException() throw ()
{ {
} }
bool FileOps::fileExists(const char* path) throw (IOException) bool FileUtils::fileExists(const char* path) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
struct stat fileInfo; struct stat fileInfo;
@ -69,7 +69,7 @@ bool FileOps::fileExists(const char* path) throw (IOException)
#endif #endif
} }
void FileOps::setQtPermissions(const char* path, int qtPermissions) throw (IOException) void FileUtils::setQtPermissions(const char* path, int qtPermissions) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
int mode = toUnixPermissions(qtPermissions); int mode = toUnixPermissions(qtPermissions);
@ -83,7 +83,7 @@ void FileOps::setQtPermissions(const char* path, int qtPermissions) throw (IOExc
#endif #endif
} }
void FileOps::moveFile(const char* src, const char* dest) throw (IOException) void FileUtils::moveFile(const char* src, const char* dest) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (rename(src,dest) != 0) if (rename(src,dest) != 0)
@ -98,7 +98,7 @@ void FileOps::moveFile(const char* src, const char* dest) throw (IOException)
#endif #endif
} }
void FileOps::extractFromZip(const char* zipFilePath, const char* src, const char* dest) throw (IOException) void FileUtils::extractFromZip(const char* zipFilePath, const char* src, const char* dest) throw (IOException)
{ {
unzFile zipFile = unzOpen(zipFilePath); unzFile zipFile = unzOpen(zipFilePath);
int result = unzLocateFile(zipFile,src,0); int result = unzLocateFile(zipFile,src,0);
@ -138,7 +138,7 @@ void FileOps::extractFromZip(const char* zipFilePath, const char* src, const cha
unzClose(zipFile); unzClose(zipFile);
} }
void FileOps::mkdir(const char* dir) throw (IOException) void FileUtils::mkdir(const char* dir) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (::mkdir(dir,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) if (::mkdir(dir,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
@ -153,7 +153,7 @@ void FileOps::mkdir(const char* dir) throw (IOException)
#endif #endif
} }
void FileOps::rmdir(const char* dir) throw (IOException) void FileUtils::rmdir(const char* dir) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (::rmdir(dir) != 0) if (::rmdir(dir) != 0)
@ -168,7 +168,7 @@ void FileOps::rmdir(const char* dir) throw (IOException)
#endif #endif
} }
void FileOps::createSymLink(const char* link, const char* target) throw (IOException) void FileUtils::createSymLink(const char* link, const char* target) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (symlink(target,link) != 0) if (symlink(target,link) != 0)
@ -182,7 +182,7 @@ void FileOps::createSymLink(const char* link, const char* target) throw (IOExcep
#endif #endif
} }
void FileOps::removeFile(const char* src) throw (IOException) void FileUtils::removeFile(const char* src) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (unlink(src) != 0) if (unlink(src) != 0)
@ -225,7 +225,7 @@ void FileOps::removeFile(const char* src) throw (IOException)
#endif #endif
} }
std::string FileOps::fileName(const char* path) std::string FileUtils::fileName(const char* path)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
char* pathCopy = strdup(path); char* pathCopy = strdup(path);
@ -240,7 +240,7 @@ std::string FileOps::fileName(const char* path)
#endif #endif
} }
std::string FileOps::dirname(const char* path) std::string FileUtils::dirname(const char* path)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
char* pathCopy = strdup(path); char* pathCopy = strdup(path);
@ -254,7 +254,7 @@ std::string FileOps::dirname(const char* path)
#endif #endif
} }
void FileOps::touch(const char* path) throw (IOException) void FileUtils::touch(const char* path) throw (IOException)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
// see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html // see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
@ -285,7 +285,7 @@ void FileOps::touch(const char* path) throw (IOException)
#endif #endif
} }
void FileOps::rmdirRecursive(const char* path) throw (IOException) void FileUtils::rmdirRecursive(const char* path) throw (IOException)
{ {
// remove dir contents // remove dir contents
DirIterator dir(path); DirIterator dir(path);
@ -309,7 +309,7 @@ void FileOps::rmdirRecursive(const char* path) throw (IOException)
rmdir(path); rmdir(path);
} }
std::string FileOps::canonicalPath(const char* path) std::string FileUtils::canonicalPath(const char* path)
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
// on Linux and Mac OS 10.6, realpath() can allocate the required // on Linux and Mac OS 10.6, realpath() can allocate the required
@ -339,7 +339,7 @@ void addFlag(InFlags inFlags, int testBit, OutFlags& outFlags, int setBit)
} }
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
int FileOps::toUnixPermissions(int qtPermissions) int FileUtils::toUnixPermissions(int qtPermissions)
{ {
mode_t result = 0; mode_t result = 0;
addFlag(qtPermissions,ReadUser,result,S_IRUSR); addFlag(qtPermissions,ReadUser,result,S_IRUSR);
@ -355,7 +355,7 @@ int FileOps::toUnixPermissions(int qtPermissions)
} }
#endif #endif
std::string FileOps::toUnixPathSeparators(const std::string& str) std::string FileUtils::toUnixPathSeparators(const std::string& str)
{ {
std::string result = str; std::string result = str;
for (size_t i=0; i < result.size(); i++) for (size_t i=0; i < result.size(); i++)
@ -368,7 +368,7 @@ std::string FileOps::toUnixPathSeparators(const std::string& str)
return result; return result;
} }
std::string FileOps::tempPath() std::string FileUtils::tempPath()
{ {
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
return "/tmp"; return "/tmp";

View File

@ -8,11 +8,11 @@
/** A set of functions for performing common operations /** A set of functions for performing common operations
* on files, throwing exceptions if an operation fails. * on files, throwing exceptions if an operation fails.
*/ */
class FileOps class FileUtils
{ {
public: public:
/** Base class for exceptions reported by /** Base class for exceptions reported by
* FileOps methods if an operation fails. * FileUtils methods if an operation fails.
*/ */
class IOException : public std::exception class IOException : public std::exception
{ {

View File

@ -1,6 +1,6 @@
#include "ProcessUtils.h" #include "ProcessUtils.h"
#include "FileOps.h" #include "FileUtils.h"
#include "Platform.h" #include "Platform.h"
#include "StringUtils.h" #include "StringUtils.h"
#include "Log.h" #include "Log.h"
@ -108,7 +108,7 @@ bool ProcessUtils::waitForProcess(PLATFORM_PID pid)
int ProcessUtils::runElevatedLinux(const std::string& executable, int ProcessUtils::runElevatedLinux(const std::string& executable,
const std::list<std::string>& args) const std::list<std::string>& args)
{ {
std::string sudoMessage = FileOps::fileName(executable.c_str()) + " needs administrative privileges. Please enter your password."; std::string sudoMessage = FileUtils::fileName(executable.c_str()) + " needs administrative privileges. Please enter your password.";
std::vector<std::string> sudos; std::vector<std::string> sudos;
sudos.push_back("kdesudo"); sudos.push_back("kdesudo");
@ -421,7 +421,7 @@ int ProcessUtils::runWindows(const std::string& executable,
std::string ProcessUtils::currentProcessPath() std::string ProcessUtils::currentProcessPath()
{ {
#ifdef PLATFORM_LINUX #ifdef PLATFORM_LINUX
std::string path = FileOps::canonicalPath("/proc/self/exe"); std::string path = FileUtils::canonicalPath("/proc/self/exe");
LOG(Info,"Current process path " + path); LOG(Info,"Current process path " + path);
return path; return path;
#elif defined(PLATFORM_MAC) #elif defined(PLATFORM_MAC)

View File

@ -1,6 +1,6 @@
#include "UpdateInstaller.h" #include "UpdateInstaller.h"
#include "FileOps.h" #include "FileUtils.h"
#include "Log.h" #include "Log.h"
#include "ProcessUtils.h" #include "ProcessUtils.h"
#include "UpdateObserver.h" #include "UpdateObserver.h"
@ -81,7 +81,7 @@ void UpdateInstaller::run() throw ()
{ {
updaterPath = ProcessUtils::currentProcessPath(); updaterPath = ProcessUtils::currentProcessPath();
} }
catch (const FileOps::IOException& ex) catch (const FileUtils::IOException& ex)
{ {
LOG(Error,"error reading process path with mode " + intToStr(m_mode)); LOG(Error,"error reading process path with mode " + intToStr(m_mode));
return; return;
@ -147,7 +147,7 @@ void UpdateInstaller::run() throw ()
LOG(Info,"Removing backups"); LOG(Info,"Removing backups");
removeBackups(); removeBackups();
} }
catch (const FileOps::IOException& exception) catch (const FileUtils::IOException& exception)
{ {
error = exception.what(); error = exception.what();
} }
@ -177,9 +177,9 @@ void UpdateInstaller::cleanup()
{ {
try try
{ {
FileOps::rmdirRecursive(m_packageDir.c_str()); FileUtils::rmdirRecursive(m_packageDir.c_str());
} }
catch (const FileOps::IOException& ex) catch (const FileUtils::IOException& ex)
{ {
LOG(Error,"Error cleaning up updater " + std::string(ex.what())); LOG(Error,"Error cleaning up updater " + std::string(ex.what()));
} }
@ -194,11 +194,11 @@ void UpdateInstaller::revert()
const std::string& installedFile = iter->first; const std::string& installedFile = iter->first;
const std::string& backupFile = iter->second; const std::string& backupFile = iter->second;
if (FileOps::fileExists(installedFile.c_str())) if (FileUtils::fileExists(installedFile.c_str()))
{ {
FileOps::removeFile(installedFile.c_str()); FileUtils::removeFile(installedFile.c_str());
} }
FileOps::moveFile(backupFile.c_str(),installedFile.c_str()); FileUtils::moveFile(backupFile.c_str(),installedFile.c_str());
} }
} }
@ -211,32 +211,32 @@ void UpdateInstaller::installFile(const UpdateScriptFile& file)
backupFile(destPath); backupFile(destPath);
// create the target directory if it does not exist // create the target directory if it does not exist
std::string destDir = FileOps::dirname(destPath.c_str()); std::string destDir = FileUtils::dirname(destPath.c_str());
if (!FileOps::fileExists(destDir.c_str())) if (!FileUtils::fileExists(destDir.c_str()))
{ {
FileOps::mkdir(destDir.c_str()); FileUtils::mkdir(destDir.c_str());
} }
if (target.empty()) if (target.empty())
{ {
// locate the package containing the file // locate the package containing the file
std::string packageFile = m_packageDir + '/' + file.package + ".zip"; std::string packageFile = m_packageDir + '/' + file.package + ".zip";
if (!FileOps::fileExists(packageFile.c_str())) if (!FileUtils::fileExists(packageFile.c_str()))
{ {
throw "Package file does not exist: " + packageFile; throw "Package file does not exist: " + packageFile;
} }
// extract the file from the package and copy it to // extract the file from the package and copy it to
// the destination // the destination
FileOps::extractFromZip(packageFile.c_str(),file.path.c_str(),destPath.c_str()); FileUtils::extractFromZip(packageFile.c_str(),file.path.c_str(),destPath.c_str());
// set the permissions on the newly extracted file // set the permissions on the newly extracted file
FileOps::setQtPermissions(destPath.c_str(),file.permissions); FileUtils::setQtPermissions(destPath.c_str(),file.permissions);
} }
else else
{ {
// create the symlink // create the symlink
FileOps::createSymLink(destPath.c_str(),target.c_str()); FileUtils::createSymLink(destPath.c_str(),target.c_str());
} }
} }
@ -262,9 +262,9 @@ void UpdateInstaller::uninstallFiles()
for (;iter != m_script->filesToUninstall().end();iter++) for (;iter != m_script->filesToUninstall().end();iter++)
{ {
std::string path = m_installDir + '/' + iter->c_str(); std::string path = m_installDir + '/' + iter->c_str();
if (FileOps::fileExists(path.c_str())) if (FileUtils::fileExists(path.c_str()))
{ {
FileOps::removeFile(path.c_str()); FileUtils::removeFile(path.c_str());
} }
else else
{ {
@ -275,15 +275,15 @@ void UpdateInstaller::uninstallFiles()
void UpdateInstaller::backupFile(const std::string& path) void UpdateInstaller::backupFile(const std::string& path)
{ {
if (!FileOps::fileExists(path.c_str())) if (!FileUtils::fileExists(path.c_str()))
{ {
// no existing file to backup // no existing file to backup
return; return;
} }
std::string backupPath = path + ".bak"; std::string backupPath = path + ".bak";
FileOps::removeFile(backupPath.c_str()); FileUtils::removeFile(backupPath.c_str());
FileOps::moveFile(path.c_str(), backupPath.c_str()); FileUtils::moveFile(path.c_str(), backupPath.c_str());
m_backups[path] = backupPath; m_backups[path] = backupPath;
} }
@ -293,7 +293,7 @@ void UpdateInstaller::removeBackups()
for (;iter != m_backups.end();iter++) for (;iter != m_backups.end();iter++)
{ {
const std::string& backupFile = iter->second; const std::string& backupFile = iter->second;
FileOps::removeFile(backupFile.c_str()); FileUtils::removeFile(backupFile.c_str());
} }
} }
@ -303,20 +303,20 @@ bool UpdateInstaller::checkAccess()
try try
{ {
FileOps::removeFile(testFile.c_str()); FileUtils::removeFile(testFile.c_str());
} }
catch (const FileOps::IOException& error) catch (const FileUtils::IOException& error)
{ {
LOG(Info,"Removing existing access check file failed " + std::string(error.what())); LOG(Info,"Removing existing access check file failed " + std::string(error.what()));
} }
try try
{ {
FileOps::touch(testFile.c_str()); FileUtils::touch(testFile.c_str());
FileOps::removeFile(testFile.c_str()); FileUtils::removeFile(testFile.c_str());
return true; return true;
} }
catch (const FileOps::IOException& error) catch (const FileUtils::IOException& error)
{ {
LOG(Info,"checkAccess() failed " + std::string(error.what())); LOG(Info,"checkAccess() failed " + std::string(error.what()));
return false; return false;