From 893e88294ba64fae582aca032c173b004a50b0f1 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 24 Sep 2014 00:06:02 -0400 Subject: [PATCH] more succinct way to get file sizes --- src/chat_commands.c | 14 +------------- src/friendlist.c | 12 +----------- src/log.c | 9 +-------- src/misc_tools.c | 11 +++++++++++ src/misc_tools.h | 3 +++ src/toxic.c | 12 +----------- 6 files changed, 18 insertions(+), 43 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 6652833..00d576f 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -224,13 +224,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv return; } - if (fseek(file_to_send, 0, SEEK_END) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); - fclose(file_to_send); - return; - } - - uint64_t filesize = ftell(file_to_send); + uint64_t filesize = file_size(path); if (filesize == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); @@ -238,12 +232,6 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv return; } - if (fseek(file_to_send, 0, SEEK_SET) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); - fclose(file_to_send); - return; - } - char filename[MAX_STR_SIZE] = {0}; get_file_name(filename, sizeof(filename), path); int namelen = strlen(filename); diff --git a/src/friendlist.c b/src/friendlist.c index 0b63257..67fcd85 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -181,23 +181,13 @@ int load_blocklist(char *path) if (fp == NULL) return -1; - if (fseek(fp, 0L, SEEK_END) == -1) { - fclose(fp); - return -1; - } - - int len = ftell(fp); + uint64_t len = file_size(path); if (len == -1) { fclose(fp); return -1; } - if (fseek(fp, 0L, SEEK_SET) == -1) { - fclose(fp); - return -1; - } - char *data = malloc(len); if (data == NULL) { diff --git a/src/log.c b/src/log.c index 0942b4b..d727870 100644 --- a/src/log.c +++ b/src/log.c @@ -156,14 +156,7 @@ void load_chat_history(ToxWindow *self, struct chatlog *log) if (log->file == NULL) return; - struct stat st; - - if (stat(log->path, &st) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to stat log file"); - return; - } - - int sz = st.st_size; + uint64_t sz = file_size(log->path); if (sz <= 0) return; diff --git a/src/misc_tools.c b/src/misc_tools.c index 8f01739..8b928c4 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -311,3 +311,14 @@ bool file_exists(const char *path) struct stat s; return stat(path, &s) == 0; } + +/* returns file size or -1 on error */ +uint64_t file_size(const char *path) +{ + struct stat st; + + if (stat(path, &st) == -1) + return -1; + + return (uint64_t) st.st_size; +} diff --git a/src/misc_tools.h b/src/misc_tools.h index e4966d1..e6f66a2 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -112,4 +112,7 @@ void bytes_convert_str(char *buf, int size, uint64_t bytes); /* checks if a file exists. Returns true or false */ bool file_exists(const char *path); +/* returns file size or -1 on error */ +uint64_t file_size(const char *path); + #endif /* #define MISC_TOOLS_H */ diff --git a/src/toxic.c b/src/toxic.c index 0ffd8be..bc20bea 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -629,23 +629,13 @@ static void load_data(Tox *m, char *path) FILE *fd; if ((fd = fopen(path, "rb")) != NULL) { - if (fseek(fd, 0L, SEEK_END) == -1) { - fclose(fd); - exit_toxic_err("failed in load_data", FATALERR_FILEOP); - } - - int len = ftell(fd); + uint64_t len = file_size(path); if (len == -1) { fclose(fd); exit_toxic_err("failed in load_data", FATALERR_FILEOP); } - if (fseek(fd, 0L, SEEK_SET)) { - fclose(fd); - exit_toxic_err("failed in load_data", FATALERR_FILEOP); - } - char *buf = malloc(len); if (buf == NULL) {