添加tar保存文件通用接口 258/1
authorpenglei <1624952103@qq.com>
Tue, 11 Jun 2024 09:45:54 +0000 (17:45 +0800)
committerpenglei <1624952103@qq.com>
Tue, 11 Jun 2024 09:45:54 +0000 (17:45 +0800)
code/sdk/drivers/peripheral/utils/custom_utils.c

index c6aab10..2fce0a2 100644 (file)
@@ -298,12 +298,6 @@ void startup_post_init_task(void)
 
     uapi_calendar_recover_date();
 
-    int ret = uapi_save_exc();
-    if(ret != 0)
-        PRINT("exc file save failed \r\n");
-    else
-        PRINT("exc file save succed \r\n");
-
     if(!custom_ble_service_on_flag())
     {
         stop_custom_ble_service();
@@ -320,6 +314,16 @@ void startup_post_init_task(void)
     //initializeMotionRecognition();
     
     LOS_Msleep(5000);
+
+    int ret = uapi_save_exc();
+    if(ret != 0)
+        PRINT("exc file save failed \r\n");
+    else
+    {
+        PRINT("exc file save succed \r\n");
+        removeFilesInFolder("/user/exc");
+    }
+
     addressbookToRead(NULL);
     up_report_timer();
     if ( poweron_cat1_on_bootup_flag() )
@@ -1613,12 +1617,9 @@ void uapi_delete_files(void)
     }
 }
 
-int uapi_save_exc(void)
+int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
 {
-    const char *folderPath = "/user/exc";
-    const char *tarPath = "/user/tar";
-    const char *tarfilePath = "/user/tar/exc_%d.tar";
-
+    //const char *tarfilePath = "%s/exc_%d.tar";
     DIR *dir;
     struct dirent *entry;
     mtar_t tar;
@@ -1626,44 +1627,40 @@ int uapi_save_exc(void)
     size_t bytesRead = 0;
     FILE *fp;
     struct stat file_inf;
+    long tar_size = 0;
     int ret = 0;
 
-    if(access(folderPath, F_OK) == -1)
+    if(access(src_path, F_OK) == -1)
     {
         if(ret == -1)
         {
-            PRINT("%s does not exist!!!\r\n", folderPath);
+            PRINT("%s does not exist!!!\r\n", src_path);
             return -1;
         }
     }
 
-    dir = opendir(folderPath);
+    dir = opendir(src_path);
     if (dir == NULL) 
     {
-        PRINT("unable to open %s\r\n",folderPath);
+        PRINT("unable to open %s\r\n",src_path);
         return -1;
     }
 
-    if((entry = readdir(dir)) == NULL) 
+    if(access(tar_path, F_OK) == -1)
     {
-        PRINT("%s is empty\r\n",folderPath);
-        return -1;
-    }
-
-    if(access(tarPath, F_OK) == -1)
-    {
-        ret = mkdir(tarPath, 0777); 
+        ret = mkdir(tar_path, 0777); 
         if(ret == -1)
         {
-            PRINT("mkdir %s failed \r\n",tarPath);
+            PRINT("mkdir %s failed \r\n",tar_path);
             return -1;
         }
     }
 
-    uint32_t timestamp = get_local_timestamp();
-    char *str = (char *)malloc(200);
-    memset_s(str, 200, 0, 200);
-    sprintf(str, tarfilePath, timestamp);
+    char str[200] = {0};
+    //snprintf(str, sizeof(str), tar_path,tarfilePath,timestamp);
+    sprintf(str, "%s/%s",tar_path,tarfilename);
+    PRINT("str %s \r\n",str);
+    //sprintf(str, tarfilePath, timestamp);
 
     ret = mtar_open(&tar, str, "w");
     if (ret != MTAR_ESUCCESS) 
@@ -1677,7 +1674,7 @@ int uapi_save_exc(void)
         if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) 
         {
             char filePath[64];
-            snprintf(filePath, sizeof(filePath), "%s/%s", folderPath, entry->d_name);
+            snprintf(filePath, sizeof(filePath), "%s/%s", src_path, entry->d_name);
 
             fp = fopen(filePath, "r");
             if(fp == NULL) 
@@ -1688,7 +1685,7 @@ int uapi_save_exc(void)
 
             memset(&file_inf, 0, sizeof(file_inf));
             stat(filePath, &file_inf);
-            
+            tar_size += file_inf.st_size;
             mtar_write_file_header(&tar, entry->d_name, file_inf.st_size);
 
             while((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0)
@@ -1708,17 +1705,33 @@ int uapi_save_exc(void)
             }
         }
     }
-
+    
     /* Finalize -- this needs to be the last thing done before closing */
     mtar_finalize(&tar);
 
     /* Close archive */
     mtar_close(&tar);
     closedir(dir);
-    removeFilesInFolder(folderPath);
+    if(tar_size == 0)
+    {
+        PRINT("%s is empty\r\n",src_path);
+        remove(str);
+        return -1;
+    }
+    //free(str);
     return ret;
 }
 
+
+int uapi_save_exc(void)
+{
+    uint32_t timestamp = get_local_timestamp();
+    char *tarfilePath = "exc_%d.tar";
+    char str[200] = {0};
+    sprintf(str, tarfilePath, timestamp);
+    return uapi_tar_file("/user/exc","/user/tar",str);
+}
+
 #include "driver/calendar.h"
 
 uint32_t uapi_calendar_store_date(void)