]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/files/file_store.cpp
Replace RemoveDir with more efficient implementation
[stg.git] / projects / stargazer / plugins / store / files / file_store.cpp
index c147935dca519308d240ae3040c89cbc236a0693..5fadbd4d1457e54e2cef61a2c0c01bfab16d0d45 100644 (file)
 #include <sstream>
 #include <algorithm>
 
-#include "common.h"
-#include "user_ips.h"
-#include "user_conf.h"
-#include "user_stat.h"
-#include "stg_const.h"
+#include "stg/common.h"
+#include "stg/user_ips.h"
+#include "stg/user_conf.h"
+#include "stg/user_stat.h"
+#include "stg/const.h"
+#include "stg/blowfish.h"
+#include "stg/logger.h"
+#include "stg/locker.h"
 #include "file_store.h"
-#include "blowfish.h"
-#include "stg_logger.h"
-#include "stg_locker.h"
 
 #define DELETED_USERS_DIR   "deleted_users"
 
@@ -132,7 +132,7 @@ FILES_STORE_CREATOR fsc;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-BASE_STORE * GetStore()
+STORE * GetStore()
 {
 return fsc.GetStore();
 }
@@ -561,40 +561,62 @@ return 0;
 //-----------------------------------------------------------------------------
 int FILES_STORE::RemoveDir(const char * path) const
 {
-vector<string> 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;
     }
 
@@ -783,7 +805,7 @@ try
     {
     i = StrToIPS(ipStr);
     }
-catch (string s)
+catch (const string & s)
     {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
@@ -1190,35 +1212,58 @@ return WriteLog2String(logStr.str(), login);
 //-----------------------------------------------------------------------------
 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
 {
-string str;
-int e;
-
-strprintf(&str,"%s/%s/stat.%d.%02d",
+// Classic stats
+string stat1;
+strprintf(&stat1,"%s/%s/stat.%d.%02d",
         storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
 
-CONFIGFILE s(str, true);
-e = s.Error();
+CONFIGFILE s(stat1, true);
 
-if (e)
+if (s.Error())
     {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    errorStr = "Cannot create file " + str;
+    errorStr = "Cannot create file '" + stat1 + "'";
     printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
     return -1;
     }
 
-char dirName[3];
+// New stats
+string stat2;
+strprintf(&stat2,"%s/%s/stat2.%d.%02d",
+        storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
 
-for (int i = 0; i < DIR_NUM; i++)
+CONFIGFILE s2(stat2, true);
+
+if (s2.Error())
     {
+    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+    errorStr = "Cannot create file '" + stat2 + "'";
+    printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
+    return -1;
+    }
+
+for (size_t i = 0; i < DIR_NUM; i++)
+    {
+    char dirName[3];
     snprintf(dirName, 3, "U%d", i);
-    s.WriteInt(dirName, stat.up[i]);
+    s.WriteInt(dirName, stat.up[i]); // Classic
+    s2.WriteInt(dirName, stat.up[i]); // New
     snprintf(dirName, 3, "D%d", i);
-    s.WriteInt(dirName, stat.down[i]);
+    s.WriteInt(dirName, stat.down[i]); // Classic
+    s2.WriteInt(dirName, stat.down[i]); // New
     }
 
+// Classic
 s.WriteDouble("cash", stat.cash);
 
+// New
+s2.WriteDouble("Cash", stat.cash);
+s2.WriteDouble("FreeMb", stat.freeMb);
+s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
+s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
+s2.WriteInt("PassiveTime", stat.passiveTime);
+s2.WriteInt("LastActivityTime", stat.lastActivityTime);
+
 return 0;
 }
 //-----------------------------------------------------------------------------*/
@@ -1339,8 +1384,6 @@ if (cf.ReadString("password", &p, "*"))
 memset(passwordE, 0, sizeof(passwordE));
 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
 
-//printfd(__FILE__, "passwordE %s\n", passwordE);
-
 memset(pass, 0, sizeof(pass));
 
 if (passwordE[0] != 0)