Pārlūkot izejas kodu

Simplifying file list on server

Jonas Pflanzer 5 gadi atpakaļ
vecāks
revīzija
7d4a0e968f
2 mainītis faili ar 15 papildinājumiem un 26 dzēšanām
  1. 0 8
      daemon/include/FileManager.h
  2. 15 18
      daemon/src/FileManager.cpp

+ 0 - 8
daemon/include/FileManager.h

@@ -51,14 +51,6 @@ private:
    */
   std::vector<std::vector<std::string>> list;
 
-  /**
-   * Get vector of all files in current directory.
-   * Ignores directories
-   *
-   * @return vector of all files in the current directory
-   */
-  std::vector<std::string> listFiles();
-
 public:
   enum { max_data_length = 512 };
 

+ 15 - 18
daemon/src/FileManager.cpp

@@ -94,31 +94,23 @@ std::vector<char> FileManager::readGet() {
   return data;
 }
 
-std::vector<std::string> FileManager::listFiles() {
-  std::vector<std::string> res;
-  for (const auto &entry :
-       boost::filesystem::directory_iterator(this->fileDirectory)) {
-    if (boost::filesystem::is_directory(entry.path()))
-      continue;
-    res.push_back(entry.path().filename().string());
-  }
-  return res;
-}
-
 int FileManager::openList() {
-  std::vector<std::string> fullList = listFiles();
-
-  if (fullList.size() == 0) {
-    return 0;
-  }
-
   // add empty chunk vector
   this->list.push_back(std::vector<std::string>());
 
   int cursize = 0;
-  for (const std::string &s : fullList) {
+
+  for (const auto &entry :
+       boost::filesystem::directory_iterator(this->fileDirectory)) {
+    // getting filename from
+    if (boost::filesystem::is_directory(entry.path()))
+      continue;
+    const std::string s = entry.path().filename().string();
+
+    // check if the size is too big
     if (s.length() > max_data_length)
       return -1;
+
     cursize += s.length() + 3;
     if (cursize > max_data_length) {
       this->list.push_back(std::vector<std::string>());
@@ -129,6 +121,11 @@ int FileManager::openList() {
     this->list.back().push_back(s);
   }
 
+  // empty list if no file was read
+  if (this->list.size() == 1 && this->list.back().size() == 0) {
+    this->list.clear();
+  }
+
   return this->list.size();
 }