瀏覽代碼

Head fixed

Jonas Pflanzer 5 年之前
父節點
當前提交
8180626fc3
共有 3 個文件被更改,包括 35 次插入18 次删除
  1. 14 2
      daemon/include/FileManager.h
  2. 8 9
      daemon/src/FileManager.cpp
  3. 13 7
      daemon/src/JsonCommander.cpp

+ 14 - 2
daemon/include/FileManager.h

@@ -119,15 +119,27 @@ public:
    */
   virtual std::vector<char> readGet();
 
+  /**
+   * head error
+   *
+   * errors which the head command returns
+   *
+   * - no error
+   * - no such file
+   * - file is smaller than specified size
+   */
+  enum headError { no_error, no_such_file, file_too_small };
+
   /**
    * Get the first n bytes of a file as string.
    * @param filename name of the files
    * @param numOfBytes the number of bytes you want
    *
    * @return first: the array of chars containing the data if no error occured
-   *         second: an error string. Empty when no error occured
+   *         second: an error code
    */
-   std::pair<std::vector<char>,std::string> getBytesFromFile(std::string filename, int numOfBytes);
+  virtual std::pair<std::vector<char>, headError>
+  getBytesFromFile(const std::string &filename, int numOfBytes);
 };
 
 #endif

+ 8 - 9
daemon/src/FileManager.cpp

@@ -84,7 +84,8 @@ std::vector<char> FileManager::readGet() {
   return data;
 }
 
-std::pair<std::vector<char>,std::string> FileManager::getBytesFromFile(std::string filename, int numOfBytes) {
+std::pair<std::vector<char>, FileManager::headError>
+FileManager::getBytesFromFile(const std::string &filename, int numOfBytes) {
   std::ifstream file;
   std::string fname = this->fileDirectory;
   fname.append(filename);
@@ -93,22 +94,20 @@ std::pair<std::vector<char>,std::string> FileManager::getBytesFromFile(std::stri
   std::vector<char> bytes;
 
   if (!file.is_open()) {
-    return {bytes, "no such file"};
+    return std::make_pair(bytes, FileManager::headError::no_such_file);
   }
 
   auto size = file.tellg();
   if (size < numOfBytes) {
-    return {bytes, "file is smaller than specified size"};
+    return std::make_pair(bytes, FileManager::headError::file_too_small);
   }
 
-  char buff[8];
+  bytes.resize(numOfBytes);
+
   file.seekg(0, std::ios::beg);
-  for (int i = 0; i < numOfBytes; i++) {
-    file.read(buff, 8);
-    bytes.insert(bytes.begin(), std::begin(buff), std::end(buff));
-  }
+  file.read(bytes.data(), numOfBytes);
 
   file.close();
 
-  return {bytes, ""};
+  return std::make_pair(bytes, FileManager::headError::no_error);
 }

+ 13 - 7
daemon/src/JsonCommander.cpp

@@ -316,23 +316,29 @@ JsonCommander::Response JsonCommander::executeHead(const Json::Value &message) {
   response.action = send;
   response.json["command"] = "head";
 
-  if(!message["file"].isString()) {
+  if (!message["file"].isString()) {
     response.json["accept"] = false;
     response.json["file"] = "";
     response.json["data"] = "";
     response.json["error"] = "incorrect head command request";
   } else {
-    std::pair<std::vector<char>,std::string>res = fileManager.getBytesFromFile(message["file"].asString(), 4);
-    if (res.second == "") {
+    std::pair<std::vector<char>, FileManager::headError> res =
+        fileManager.getBytesFromFile(message["file"].asString(), 4);
+    if (res.second == FileManager::headError::no_error) {
       response.json["accept"] = true;
-      response.json["file"] = message["file"];
+      response.json["file"] = message["file"].asString();
       response.json["data"] = base64::encodeVector(res.first);
       response.json["error"] = "";
+    } else if (res.second == FileManager::headError::no_such_file) {
+      response.json["accept"] = true;
+      response.json["file"] = message["file"].asString();
+      response.json["data"] = "";
+      response.json["error"] = "no such file";
     } else {
       response.json["accept"] = true;
-      response.json["file"] = message["file"];
-      response.json["data"] ="";
-      response.json["error"] = res.second;
+      response.json["file"] = message["file"].asString();
+      response.json["data"] = "";
+      response.json["error"] = "file is smaller than specified size";
     }
   }