1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 16:03:27 +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; return;
} }
if (fseek(file_to_send, 0, SEEK_END) == -1) { uint64_t filesize = file_size(path);
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);
if (filesize == -1) { if (filesize == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); 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; 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}; char filename[MAX_STR_SIZE] = {0};
get_file_name(filename, sizeof(filename), path); get_file_name(filename, sizeof(filename), path);
int namelen = strlen(filename); int namelen = strlen(filename);

View File

@ -181,23 +181,13 @@ int load_blocklist(char *path)
if (fp == NULL) if (fp == NULL)
return -1; return -1;
if (fseek(fp, 0L, SEEK_END) == -1) { uint64_t len = file_size(path);
fclose(fp);
return -1;
}
int len = ftell(fp);
if (len == -1) { if (len == -1) {
fclose(fp); fclose(fp);
return -1; return -1;
} }
if (fseek(fp, 0L, SEEK_SET) == -1) {
fclose(fp);
return -1;
}
char *data = malloc(len); char *data = malloc(len);
if (data == NULL) { if (data == NULL) {

View File

@ -156,14 +156,7 @@ void load_chat_history(ToxWindow *self, struct chatlog *log)
if (log->file == NULL) if (log->file == NULL)
return; return;
struct stat st; uint64_t sz = file_size(log->path);
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;
if (sz <= 0) if (sz <= 0)
return; return;

View File

@ -311,3 +311,14 @@ bool file_exists(const char *path)
struct stat s; struct stat s;
return stat(path, &s) == 0; 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 */ /* checks if a file exists. Returns true or false */
bool file_exists(const char *path); 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 */ #endif /* #define MISC_TOOLS_H */

View File

@ -629,23 +629,13 @@ static void load_data(Tox *m, char *path)
FILE *fd; FILE *fd;
if ((fd = fopen(path, "rb")) != NULL) { if ((fd = fopen(path, "rb")) != NULL) {
if (fseek(fd, 0L, SEEK_END) == -1) { uint64_t len = file_size(path);
fclose(fd);
exit_toxic_err("failed in load_data", FATALERR_FILEOP);
}
int len = ftell(fd);
if (len == -1) { if (len == -1) {
fclose(fd); fclose(fd);
exit_toxic_err("failed in load_data", FATALERR_FILEOP); 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); char *buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {