系统:增加6个功能文件夹的内存查询显示
authorCaisking <3449750381@qq.com>
Mon, 21 Oct 2024 08:07:04 +0000 (16:07 +0800)
committerCaisking <3449750381@qq.com>
Mon, 21 Oct 2024 08:07:04 +0000 (16:07 +0800)
code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp

index f3924d5..8b1718a 100644 (file)
 #include <cstring>
 #include <ctime>
 #include <deque>
+#include <dirent.h>
 #include <fstream>
 #include <iomanip>
 #include <iostream>
 #include <sstream>
 #include <stdexcept>
+#include <stdio.h>
 #include <string>
+#include <sys/stat.h>
 #include <sys/statfs.h>
 #include <sys/time.h>
 #include <thread>
 #include <vector>
-#include <stdio.h>
-#include <dirent.h>
-#include <sys/stat.h>
 
-#define SYSTEM_LOG_FILE_PATH "/system/system_log.txt"
-#define SYSTEM_LOG_FILE_PATH_NEXTDAY "/system/system_log_nextday.txt"
-#define SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS "/system/popup_windows_log.txt"
+#define SYSTEM_LOG_FILE_PATH "/user/system/system_log.txt"
+#define SYSTEM_LOG_FILE_PATH_NEXTDAY "/user/system/system_log_nextday.txt"
+#define SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS "/user/system/popup_windows_log.txt"
 
 extern "C" {
 // 通用日志记录功能
@@ -142,6 +142,40 @@ const std::deque<SystemLogData> &SystemLogModel::getLogData() const {
 // 获取日志大小
 size_t SystemLogModel::getLogSize() const { return logData.size(); }
 
+long long SystemLogModel::getFolderSize(const char *folderPath) {
+  DIR *dir;
+  struct dirent *entry;
+  struct stat fileStat;
+  long long totalSize = 0;
+
+  dir = opendir(folderPath);
+  if (dir == NULL) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error opening directory");
+    return 0;
+  }
+
+  while ((entry = readdir(dir)) != NULL) {
+    char filePath[1024];
+    snprintf(filePath, sizeof(filePath), "%s/%s", folderPath, entry->d_name);
+
+    if (stat(filePath, &fileStat) == -1) {
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error getting file status");
+      continue;
+    }
+
+    if (S_ISDIR(fileStat.st_mode)) {
+      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
+        continue;
+      totalSize += getFolderSize(filePath);
+    } else {
+      totalSize += fileStat.st_size;
+    }
+  }
+
+  closedir(dir);
+  return totalSize;
+}
+
 //获取系统内存
 /*
 [FS INFO]total size=12416KB used size=8114KB free size=4302KB
@@ -154,17 +188,11 @@ size_t SystemLogModel::getLogSize() const { return logData.size(); }
 */
 std::string SystemLogModel::get_system_mem_info() {
   struct statfs file_buf = {0};
-  std::stringstream result; // 使用 stringstream 来构建返回的字符串
-  const std::vector<std::string> volume_names = {"/system/", "/update/",
-                                                 "/music/", "/user/"};
-
-  for (const auto &volume : volume_names) {
-    // 获取文件系统状态
-    if (statfs(volume.c_str(), &file_buf) != 0) {
-      result << "Error retrieving info for " << volume << std::endl;
-      continue;
-    }
-
+  std::stringstream result;
+  std::string system_volume_name = "/user/";
+  if (statfs(system_volume_name.c_str(), &file_buf) != 0) {
+    result << "Error retrieving info for " << system_volume_name << std::endl;
+  } else {
     // 计算总大小、已用大小和自由空间
     uint64_t total_size =
         static_cast<uint64_t>(file_buf.f_blocks) * file_buf.f_bsize;
@@ -173,63 +201,47 @@ std::string SystemLogModel::get_system_mem_info() {
     uint64_t used_size = total_size - free_size;
 
     // 按照所需格式输出
-    result << "[" << volume << "]" << std::endl;
-    result << "Total size=" << total_size / 1024 << "KB " << std::endl;
-    result << "Used size=" << used_size / 1024 << "KB " << std::endl;
-    result << "Free size=" << free_size / 1024 << "KB" << std::endl;
+    result << "[" << system_volume_name << "]" << std::endl;
+    result << "Total size = " << total_size / 1024 << " KB" << std::endl;
+    result << "Used size = " << used_size / 1024 << " KB" << std::endl;
+    result << "Free size = " << free_size / 1024 << " KB" << std::endl;
+    result << "====================" << std::endl; // 分隔线
   }
 
-  // 返回所有分区的自由空间信息,如果没有信息,则返回说明信息
-  return result.str().empty() ? "No volumes found or accessible."
-                              : result.str();
-}
-
-
-
-long long SystemLogModel::getFolderSize(const char* folderPath) {
-    DIR* dir;
-    struct dirent* entry;
-    struct stat fileStat;
-    long long totalSize = 0;
-
-    dir = opendir(folderPath);
-    if (dir == NULL) {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error opening directory");
-        return 0;
-    }
+  std::vector<std::string> volume_names = {
+      "/user/dial/", "/user/music/",  "/user/update/",
+      "/user/res/",  "/user/system/", "/user/userdata/",
+  };
 
-    while ((entry = readdir(dir))!= NULL) {
-        char filePath[1024];
-        snprintf(filePath, sizeof(filePath), "%s/%s", folderPath, entry->d_name);
-
-        if (stat(filePath, &fileStat) == -1) {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error getting file status");
-            continue;
-        }
-
-        if (S_ISDIR(fileStat.st_mode)) {
-            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
-                continue;
-            totalSize += getFolderSize(filePath);
-        } else {
-            totalSize += fileStat.st_size;
-        }
+  // 遍历每个目录,获取文件系统状态
+  for (const auto &volume_name : volume_names) {
+    // 获取文件系统状态
+    uint64_t total_size =getFolderSize(volume_name.c_str());
+    if (total_size == 0) {
+      result << "Error retrieving info for " << volume_name << std::endl;
+    } else {
+      // 计算总大小
+      result << "[" << volume_name << "]" << std::endl;
+      result << "Total size = " << total_size / 1024 << " KB" << std::endl;
+      result << "====================" << std::endl; // 分隔线
     }
+  }
 
-    closedir(dir);
-    return totalSize;
+  // 如果没有信息,则返回说明信息
+  return result.str().empty() ? "No volumes found or accessible."
+                              : result.str();
 }
 
 // 检查是否可以同步文件
-bool SystemLogModel::canSyncFile(std::string path,uint32_t path_max_size,uint32_t download_size)
-{
-    // 获取文件夹的大小
-    long long usedSpace = getFolderSize(path.c_str());
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "usedSpace = %lld",usedSpace);
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "path_max_size = %lld",path_max_size);
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "download_size = %lld",download_size);
-
-    return (usedSpace + download_size) <= path_max_size;
+bool SystemLogModel::canSyncFile(std::string path, uint32_t path_max_size,
+                                 uint32_t download_size) {
+  // 获取文件夹的大小
+  long long usedSpace = getFolderSize(path.c_str());
+  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "usedSpace = %lld", usedSpace);
+  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "path_max_size = %lld", path_max_size);
+  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "download_size = %lld", download_size);
+
+  return (usedSpace + download_size) <= path_max_size;
 }
 // nextday相关
 bool SystemLogModel::save_nextday_log(uint32_t timestamp) {
@@ -282,69 +294,68 @@ size_t SystemLogModel::get_nextday_log_size() const {
 }
 
 // 保存popup_windows日志
-bool SystemLogModel::save_popup_windows_log(popup_windows_log_data data)                
-{
-    // 保存新日志
-    popup_windows_log.push_back(data);
-
-    // 将日志写入文件
-    std::ofstream outFile(SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS, std::ios::app);
-    if (!outFile) {
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open log file for writing");
-      return false; // 文件打开失败
-    }
+bool SystemLogModel::save_popup_windows_log(popup_windows_log_data data) {
+  // 保存新日志
+  popup_windows_log.push_back(data);
+
+  // 将日志写入文件
+  std::ofstream outFile(SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS, std::ios::app);
+  if (!outFile) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "Failed to open log file for writing");
+    return false; // 文件打开失败
+  }
 
-    // 将结构体中的数据转换为字符串格式写入文件
-    outFile << static_cast<int>(data.type) << " " << data.timestamp << std::endl;
-    outFile.close();
-    return true;
+  // 将结构体中的数据转换为字符串格式写入文件
+  outFile << static_cast<int>(data.type) << " " << data.timestamp << std::endl;
+  outFile.close();
+  return true;
 }
 
-bool SystemLogModel::load_popup_windows_log()                                           
-{
-    std::ifstream inFile(SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS);
-    if (!inFile) {
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open log file for reading");
-      return false; // 无法打开文件
-    }
+bool SystemLogModel::load_popup_windows_log() {
+  std::ifstream inFile(SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS);
+  if (!inFile) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "Failed to open log file for reading");
+    return false; // 无法打开文件
+  }
 
-    std::string line;
-    while (std::getline(inFile, line)) {
-        std::istringstream ss(line);
-        popup_windows_log_data data;
-        int typeInt;
-        if (ss >> typeInt >> data.timestamp) {
-            data.type = static_cast<popup_windows_type>(typeInt);
-            popup_windows_log.push_back(data);
-        }
+  std::string line;
+  while (std::getline(inFile, line)) {
+    std::istringstream ss(line);
+    popup_windows_log_data data;
+    int typeInt;
+    if (ss >> typeInt >> data.timestamp) {
+      data.type = static_cast<popup_windows_type>(typeInt);
+      popup_windows_log.push_back(data);
     }
+  }
 
-    inFile.close();
-    return true;
+  inFile.close();
+  return true;
 }
 // 获取popup_windows日志记录(常量引用)
-const std::deque<popup_windows_log_data> &SystemLogModel::get_popup_windows_log() const 
-{
+const std::deque<popup_windows_log_data> &
+SystemLogModel::get_popup_windows_log() const {
   return popup_windows_log;
 }
 // 获取日志大小
-size_t SystemLogModel::get_popup_windows_log_size() const                               
-{
+size_t SystemLogModel::get_popup_windows_log_size() const {
   return popup_windows_log.size();
 }
 
-void SystemLogModel::clear_popup_windows_log()
-{
-   // 清空当前的日志
+void SystemLogModel::clear_popup_windows_log() {
+  // 清空当前的日志
   WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "clear_popup_windows_log");
   popup_windows_log.clear();
-  
+
   std::ofstream outFile(SYSTEM_LOG_FILE_PATH_POPUP_WINDOWS, std::ios::trunc);
   if (!outFile) {
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open log file for writing");
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "Failed to open log file for writing");
     return;
   }
-  outFile<<"";
+  outFile << "";
   outFile.close();
 }
 } // namespace OHOS