From: Maxim Mamontov Date: Fri, 17 Dec 2010 16:30:28 +0000 (+0200) Subject: Рефакторинг метода FILE_STORE::GetFilesList X-Git-Tag: 2.407-rc3~276 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/f5f5ca8105191dc60d2aa39715a407350b57d17c Рефакторинг метода FILE_STORE::GetFilesList Метод переименован в GetFileList и вынесен в функцию (более не является членом класса) --- diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index b926eb6a..21c6cb17 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -60,6 +60,8 @@ using namespace std; +int GetFileList(vector * fileList, const string & directory, mode_t mode, const string & ext); + const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- class BAK_FILE @@ -504,79 +506,69 @@ const string & FILES_STORE::GetVersion() const return version; } //----------------------------------------------------------------------------- -int FILES_STORE::GetFilesList(vector * filesList, const string & directory, mode_t mode, const string & ext) const +int FILES_STORE::GetUsersList(vector * userList) const { -// æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ -DIR * d; -string str; -struct stat st; -dirent * dir; +vector files; -filesList->clear(); - -d = opendir(directory.c_str()); - -if (!d) +if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, "")) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + directory + "\' cannot be opened."; - //printfd(__FILE__, "%s\n", errorStr.c_str()); + errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno)); return -1; } -int d_nameLen; -int extLen = ext.size() ; -while ((dir = readdir(d))) - { - if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")) - { - str = directory + "/" + string(dir->d_name); - if (!stat(str.c_str(), &st)) - { - if (st.st_mode & mode) // ïÔÓÅ× ÆÁÊÌÏ× or directories - { - d_nameLen = strlen(dir->d_name); - if (d_nameLen > extLen) - { - if (strcmp(dir->d_name + (d_nameLen - extLen), ext.c_str()) == 0) - { - dir->d_name[d_nameLen - extLen] = 0; - filesList->push_back(dir->d_name); - } - } - } - } - } - } +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +userList->swap(files); -closedir(d); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetUsersList(vector * usersList) const +int FILES_STORE::GetAdminsList(vector * adminList) const { -return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, ""); -} -//----------------------------------------------------------------------------- -int FILES_STORE::GetAdminsList(vector * adminsList) const -{ -return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm"); +vector files; + +if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +adminList->swap(files); + +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetTariffsList(vector * tariffsList) const +int FILES_STORE::GetTariffsList(vector * tariffList) const { -return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf"); +vector files; + +if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +tariffList->swap(files); + +return 0; } //----------------------------------------------------------------------------- int FILES_STORE::RemoveDir(const char * path) const { -vector filesList; +vector fileList; -GetFilesList(&filesList, path, S_IFREG, ""); +GetFileList(&fileList, path, S_IFREG, ""); -for (unsigned i = 0; i < filesList.size(); i++) +for (unsigned i = 0; i < fileList.size(); i++) { - string file = path + string("/") + filesList[i]; + string file = path + string("/") + fileList[i]; if (unlink(file.c_str())) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -588,11 +580,12 @@ for (unsigned i = 0; i < filesList.size(); i++) } } -GetFilesList(&filesList, path, S_IFDIR, ""); +fileList.clear(); +GetFileList(&fileList, path, S_IFDIR, ""); -for (unsigned i = 0; i < filesList.size(); i++) +for (unsigned i = 0; i < fileList.size(); i++) { - string dir = string(path) + "/" + filesList[i]; + string dir = string(path) + "/" + fileList[i]; RemoveDir(dir.c_str()); } @@ -2021,13 +2014,18 @@ return unlink(fn.c_str()); //----------------------------------------------------------------------------- int FILES_STORE::GetMessageHdrs(vector * hdrsList, const string & login) const { -vector messages; -string dn; -dn = storeSettings.GetUsersDir() + "/" + login + "/messages/"; -GetFilesList(&messages, dn, S_IFREG, ""); +string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/"); //hdrsList->resize(messages.size()); +if (access(dn.c_str(), F_OK) != 0) + { + return 0; + } + +vector messages; +GetFileList(&messages, dn, S_IFREG, ""); + for (unsigned i = 0; i < messages.size(); i++) { unsigned long long id = 0; @@ -2169,3 +2167,54 @@ if (f) return -1; } //----------------------------------------------------------------------------- +int GetFileList(vector * fileList, const string & directory, mode_t mode, const string & ext) +{ +// æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ + +DIR * d = opendir(directory.c_str()); + +if (!d) + { + printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno)); + return -1; + } + +dirent * entry; +while ((entry = readdir(d))) + { + if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))) + continue; + + string str = directory + "/" + string(entry->d_name); + + struct stat st; + if (stat(str.c_str(), &st)) + continue; + + if (!(st.st_mode & mode)) // Filter by mode + continue; + + if (!ext.empty()) + { + // Check extension + size_t d_nameLen = strlen(entry->d_name); + if (d_nameLen <= ext.size()) + continue; + + if (ext == entry->d_name + (d_nameLen - ext.size())) + { + entry->d_name[d_nameLen - ext.size()] = 0; + fileList->push_back(entry->d_name); + } + } + else + { + fileList->push_back(entry->d_name); + } + } + +closedir(d); + +return 0; +} +//----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/store/files/file_store.h b/projects/stargazer/plugins/store/files/file_store.h index 2e55b6e3..dbca0f20 100644 --- a/projects/stargazer/plugins/store/files/file_store.h +++ b/projects/stargazer/plugins/store/files/file_store.h @@ -194,7 +194,6 @@ private: virtual int WriteLogString(const string & str, const string & login) const; virtual int WriteLog2String(const string & str, const string & login) const; int RemoveDir(const char * path) const; - int GetFilesList(vector * filesList, const string & directory, mode_t mode, const string & ext) const; int Touch(const std::string & path) const; mutable string errorStr;