Przeglądaj źródła

V0.6.5
1、sftp判断文件夹是否存在还有问题

Apple 3 miesięcy temu
rodzic
commit
8be17f200d
1 zmienionych plików z 32 dodań i 19 usunięć
  1. 32 19
      common/CurlFtp/CurlFtp.cpp

+ 32 - 19
common/CurlFtp/CurlFtp.cpp

@@ -47,18 +47,19 @@ static thread_local uint64_t uSpeed;    /* 保存最后的上传速度 */
  */
 static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr) 
 {
-    size_t newLength = size * nmemb;
-    size_t oldLength = userStr->size();
-    try
-    {
-        userStr->resize(oldLength + newLength);
-    }
-    catch(std::bad_alloc &e)
-    {
-        //handle memory problem
-        return 0;
-    }
-    std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
+    // size_t newLength = size * nmemb;
+    // size_t oldLength = userStr->size();
+    // try
+    // {
+    //     userStr->resize(oldLength + newLength);
+    // }
+    // catch(std::bad_alloc &e)
+    // {
+    //     //handle memory problem
+    //     return 0;
+    // }
+    // std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
+    userStr->append((char*)contents, size * nmemb);
     return size * nmemb;
 }
 
@@ -573,6 +574,7 @@ bool CurlFtp::isDirExist(const std::string& dir)
     curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
     /* 获取文件夹是否存在,不需要接收文件 */
     curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
+    curl_easy_setopt(m_curl, CURLOPT_HEADER, 1L);
 
     /* 设置SFTP */
     setSftp(m_curl);
@@ -587,16 +589,27 @@ bool CurlFtp::isDirExist(const std::string& dir)
     /* 这里只需要执行一次 */
     CURLcode res = curl_easy_perform(m_curl);
     bool result = true;
-    if(res != CURLE_OK)
+    if(res == CURLE_REMOTE_FILE_NOT_FOUND)
     {
-        // SPDLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)res, curl_easy_strerror(res));
         result = false;
     }
-    // bool ret = performCurl(m_curl);
-    // if(!ret)
-    // {
-    //     // SPDLOG_ERROR("Failed to create FTP Dir");
-    // }
+    else if(res != CURLE_OK)
+    {
+        // SPDLOG_ERROR("Check remote dir error, error code: {} ,{}", (int)res, curl_easy_strerror(res));
+        result = false;
+    }
+    else 
+    {
+        /* sftp即使远程文件夹不存在也会返回OK,这里使用http进行检查 */
+        // long response_code = -1;
+        // res = curl_easy_getinfo(m_curl, CURLINFO_HTTP_CONNECTCODE, &response_code);
+        // if((res == CURLE_OK) && (response_code == 0))
+        // {
+        //     SPDLOG_ERROR("Remote directory not found: {}", dir);
+        //     result = false;
+        // }
+        // SPDLOG_DEBUG("response_code: {}", response_code);
+    }
 
     return result;
 }