X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/96f8ba7ed42706e7d875ffd0dac3f62ec4c43ba5..3b2cf3d5caf8a8e7d6761696038c73590434a93c:/projects/stargazer/plugins/store/files/file_store.cpp diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 255f0134..1a16ed91 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -51,6 +51,7 @@ #include "stg/blowfish.h" #include "stg/logger.h" #include "stg/locker.h" +#include "stg/plugin_creator.h" #include "file_store.h" #define DELETED_USERS_DIR "deleted_users" @@ -63,78 +64,15 @@ int GetFileList(vector * fileList, const string & directory, mode_t mode const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- -class BAK_FILE -{ -public: - - //------------------------------------------------------------------------- - BAK_FILE(const string & fileName, bool removeBak) - : f(NULL), - removeBak(false) - { - bakSuccessed = false; - BAK_FILE::removeBak = removeBak; - fileNameBak = fileName + ".bak"; - if (rename(fileName.c_str(), fileNameBak.c_str())) - { - printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno)); - } - else - { - bakSuccessed = true; - } - - } - //------------------------------------------------------------------------- - ~BAK_FILE() - { - if(bakSuccessed && removeBak) - { - if (unlink(fileNameBak.c_str())) - { - printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno)); - } - } - } - //------------------------------------------------------------------------- - -private: - FILE * f; - bool bakSuccessed; - string fileNameBak; - bool removeBak; -}; -//----------------------------------------------------------------------------- -class FILES_STORE_CREATOR -{ -private: - FILES_STORE * fs; - -public: - FILES_STORE_CREATOR() - : fs(new FILES_STORE()) - { - }; - ~FILES_STORE_CREATOR() - { - delete fs; - }; - - FILES_STORE * GetStore() - { - return fs; - }; -}; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -FILES_STORE_CREATOR fsc; +PLUGIN_CREATOR fsc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- STORE * GetStore() { -return fsc.GetStore(); +return fsc.GetPlugin(); } //----------------------------------------------------------------------------- FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() @@ -561,40 +499,62 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE::RemoveDir(const char * path) const { -vector fileList; +DIR * d = opendir(path); -GetFileList(&fileList, path, S_IFREG, ""); +if (!d) + { + errorStr = "failed to open dir. Message: '"; + errorStr += strerror(errno); + errorStr += "'"; + printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno)); + return -1; + } -for (unsigned i = 0; i < fileList.size(); i++) +dirent * entry; +while ((entry = readdir(d))) { - string file = path + string("/") + fileList[i]; - if (unlink(file.c_str())) + if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))) + continue; + + string str = path; + str += "/" + string(entry->d_name); + + struct stat st; + if (stat(str.c_str(), &st)) + continue; + + if ((st.st_mode & S_IFREG)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; - printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno)); - return -1; + if (unlink(str.c_str())) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "unlink failed. Message: '"; + errorStr += strerror(errno); + errorStr += "'"; + printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno)); + return -1; + } } - } -fileList.clear(); -GetFileList(&fileList, path, S_IFDIR, ""); + if (!(st.st_mode & S_IFDIR)) + { + if (RemoveDir(str.c_str())) + { + return -1; + } -for (unsigned i = 0; i < fileList.size(); i++) - { - string dir = string(path) + "/" + fileList[i]; - RemoveDir(dir.c_str()); + } } +closedir(d); + if (rmdir(path)) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = "rmdir failed. Message: '"; errorStr += strerror(errno); errorStr += "'"; - printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno)); + printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -956,8 +916,6 @@ int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) cons string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; -//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); @@ -1015,8 +973,6 @@ char s[22]; string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; -//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - { CONFIGFILE cfstat(fileName, true); int e = cfstat.Error();