diff --git a/HISTORY b/HISTORY index dc871da..5571151 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-02-27 +Version 1.44 2020-03-03 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -13,6 +13,7 @@ Version 1.44 2020-02-27 * shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime * fast_mpool.[hc]: change function fast_mpool_strdup * fast_allocator.[hc]: add function fast_allocator_strdup + * shared_func.[hc]: add functions getFileSize Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/shared_func.c b/src/shared_func.c index 23ba2aa..8c43b6a 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -323,7 +323,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \ sprintf(fullpath, "%s/%s", path, dirp->d_name); memset(&statbuf, 0, sizeof(statbuf)); - if (lstat(fullpath, &statbuf)<0) + if (lstat(fullpath, &statbuf) < 0) { continue; } @@ -1240,6 +1240,22 @@ int getFileContentEx(const char *filename, char *buff, \ return 0; } +int getFileSize(const char *filename, int64_t *file_size) +{ + struct stat buf; + if (stat(filename, &buf) != 0) + { + logError("file: "__FILE__", line: %d, " + "stat file %s fail, " + "errno: %d, error info: %s", __LINE__, + filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EIO; + } + + *file_size = buf.st_size; + return 0; +} + int writeToFile(const char *filename, const char *buff, const int file_size) { int fd; diff --git a/src/shared_func.h b/src/shared_func.h index c5f7d6d..448bd30 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -546,6 +546,14 @@ int getFileContent(const char *filename, char **buff, int64_t *file_size); int getFileContentEx(const char *filename, char *buff, \ int64_t offset, int64_t *size); +/** get file size + * parameters: + * filename: the filename + * file_size: store the file size + * return: error no , 0 success, != 0 fail +*/ +int getFileSize(const char *filename, int64_t *file_size); + /** write to file * parameters: * filename: the filename to write