1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-23 03:13:25 +01:00

more succinct way to get file sizes

This commit is contained in:
Jfreegman 2014-09-24 00:06:02 -04:00
parent b071a9e992
commit 893e88294b
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
6 changed files with 18 additions and 43 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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 */

View File

@ -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) {