From 13d50c229624b508b7eb1d49a3cbf39640ff65a6 Mon Sep 17 00:00:00 2001 From: "Liu, Xinwu" Date: Fri, 19 Apr 2019 10:25:10 +0800 Subject: [PATCH] tools:acrn-crashlog: check blocks size instead of file size What acrnprobe really care about is the storage capacity taken up from emmc. This patch checks all the logs' blocks size instead of the file size before collecting new logs. Tracked-On: #1024 Signed-off-by: Liu, Xinwu Reviewed-by: Liu, Xiaojing Acked-by: Chen, Gang --- tools/acrn-crashlog/acrnprobe/sender.c | 3 ++- tools/acrn-crashlog/common/fsutils.c | 15 ++++++++------- tools/acrn-crashlog/common/include/fsutils.h | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tools/acrn-crashlog/acrnprobe/sender.c b/tools/acrn-crashlog/acrnprobe/sender.c index dd60756cf..53b983dcc 100644 --- a/tools/acrn-crashlog/acrnprobe/sender.c +++ b/tools/acrn-crashlog/acrnprobe/sender.c @@ -57,7 +57,8 @@ static int crashlog_check_space(void) if (!space_available(crashlog->outdir, quota)) return -1; - if (dir_size(crashlog->outdir, crashlog->outdir_len, &dsize) == -1) { + if (dir_blocks_size(crashlog->outdir, crashlog->outdir_len, + &dsize) == -1) { LOGE("failed to check outdir size\n"); return -1; } diff --git a/tools/acrn-crashlog/common/fsutils.c b/tools/acrn-crashlog/common/fsutils.c index 1e4998d2e..70fc203e0 100644 --- a/tools/acrn-crashlog/common/fsutils.c +++ b/tools/acrn-crashlog/common/fsutils.c @@ -1085,11 +1085,12 @@ int find_file(const char *dir, size_t dlen, const char *target_file, return -1; } -static int _count_file_size(const char *pdir, struct dirent *dirp, void *arg) +static int _count_file_blocks_size(const char *pdir, struct dirent *dirp, + void *arg) { char file[PATH_MAX]; int res; - ssize_t fsize; + ssize_t fbsize; if (dirp->d_type != DT_REG && dirp->d_type != DT_DIR) return DIR_SUCCESS; @@ -1098,22 +1099,22 @@ static int _count_file_size(const char *pdir, struct dirent *dirp, void *arg) if (s_not_expect(res, sizeof(file))) return DIR_ERROR; - fsize = get_file_size(file); - if (fsize < 0) + fbsize = get_file_blocks_size(file); + if (fbsize < 0) return DIR_ERROR; - *(size_t *)arg += fsize; + *(size_t *)arg += fbsize; return DIR_SUCCESS; } -int dir_size(const char *dir, size_t dlen, size_t *size) +int dir_blocks_size(const char *dir, size_t dlen, size_t *size) { if (!dir || !dlen || !size) return -1; *size = 0; - if (dir_recursive(dir, dlen, -1, _count_file_size, + if (dir_recursive(dir, dlen, -1, _count_file_blocks_size, (void *)size) != DIR_SUCCESS) { LOGE("failed to recursive dir (%s)\n", dir); return -1; diff --git a/tools/acrn-crashlog/common/include/fsutils.h b/tools/acrn-crashlog/common/include/fsutils.h index b5ae6a387..a54c634ab 100644 --- a/tools/acrn-crashlog/common/include/fsutils.h +++ b/tools/acrn-crashlog/common/include/fsutils.h @@ -75,6 +75,19 @@ static inline ssize_t get_file_size(const char *filepath) return info.st_size; } +static inline ssize_t get_file_blocks_size(const char *filepath) +{ + struct stat info; + + if (filepath == NULL) + return -ENOENT; + + if (stat(filepath, &info) < 0) + return -errno; + + return info.st_blocks * 512; +} + char *mm_get_line(struct mm_file_t *mfile, int line); int mkdir_p(const char *path); int remove_r(const char *dir); @@ -114,7 +127,7 @@ int dir_contains(const char *dir, const char *filename, size_t flen, int exact); int lsdir(const char *dir, char *fullname[], int limit); int find_file(const char *dir, size_t dlen, const char *target_file, size_t tflen, int depth, char *path[], int limit); -int dir_size(const char *dir, size_t dlen, size_t *size); +int dir_blocks_size(const char *dir, size_t dlen, size_t *size); int read_file(const char *path, unsigned long *size, void **data); int is_ac_filefmt(const char *file_fmt); int config_fmt_to_files(const char *file_fmt, char ***out);