diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-11-12 15:15:31 +0100 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-11-12 15:15:38 +0100 |
| commit | bdcce656c964c6d94d85639e3f064fc8aef0c124 (patch) | |
| tree | 226ee7572421645d6308eb405ccbb254ad9a5837 | |
| parent | 28ec9477c63342f05c6d7b0d39581c6611f76ab4 (diff) | |
Refactor the file size and tell operations
This was done in order to avoid C++ compiler warnings about comparing types with
different signedness.
| -rw-r--r-- | src/backup.c | 10 | ||||
| -rw-r--r-- | src/file.c | 27 | ||||
| -rw-r--r-- | src/file.h | 4 | ||||
| -rw-r--r-- | src/restore.c | 31 |
4 files changed, 54 insertions, 18 deletions
diff --git a/src/backup.c b/src/backup.c index 4c9dfbd..0b558b6 100644 --- a/src/backup.c +++ b/src/backup.c @@ -43,16 +43,18 @@ static int check_files(const options_backup_t *const opts, const resources_backup_t *const res) { - const long in_size = file_size(res->in_file); + bool in_size_ok = false; + const size_t in_size = file_size(res->in_file, &in_size_ok); - if (in_size < 0) { + if (!in_size_ok) { print_error("cannot get size of input file: %s", strerror(errno)); return 1; } - const long ref_size = file_size(res->ref_file); + bool ref_size_ok = false; + const size_t ref_size = file_size(res->ref_file, &ref_size_ok); - if (ref_size < 0) { + if (!ref_size_ok) { print_error("cannot get size of reference file: %s", strerror(errno)); return 1; } @@ -31,22 +31,39 @@ #include <errno.h> #include <string.h> -long -file_size(FILE *const file) +size_t +file_size(FILE *const file, bool *const return_is_ok) { fpos_t p; if ((fgetpos(file, &p) != 0) || (fseek(file, 0L, SEEK_END) != 0)) { - return -1; + *return_is_ok = false; + return 0; } const long size = ftell(file); if (fsetpos(file, &p) != 0) { - return -1; + *return_is_ok = false; + return 0; } - return size; + *return_is_ok = true; + return (size_t)size; +} + +size_t +file_tell(FILE *const file, bool *const return_is_ok) +{ + const long pos = ftell(file); + + if (pos < 0) { + *return_is_ok = false; + return 0; + } else { + *return_is_ok = true; + return (size_t)pos; + } } size_t @@ -27,10 +27,12 @@ #ifndef FILE_H #define FILE_H +#include <stdbool.h> #include <stdint.h> #include <stdio.h> -long file_size(FILE *const file); +size_t file_size(FILE *const file, bool *const return_is_ok); +size_t file_tell(FILE *const file, bool *const return_is_ok); size_t file_read_sectors(FILE *const file, char *const buffer, uint32_t buffer_size, uint32_t sector_size); diff --git a/src/restore.c b/src/restore.c index df58ab8..e73e42e 100644 --- a/src/restore.c +++ b/src/restore.c @@ -39,9 +39,10 @@ static bool is_input_file_valid(const resources_restore_t *const res, uint32_t sector_size) { - const long in_size = file_size(res->in_file); + bool in_size_ok = false; + const size_t in_size = file_size(res->in_file, &in_size_ok); - if (in_size < 0) { + if (!in_size_ok) { print_error("cannot get size of input file: %s", strerror(errno)); return false; } else if (in_size == 0) { @@ -55,9 +56,10 @@ is_input_file_valid(const resources_restore_t *const res, uint32_t sector_size) return false; } - const long out_size = file_size(res->out_file); + bool out_size_ok = false; + const size_t out_size = file_size(res->out_file, &out_size_ok); - if (out_size < 0) { + if (!out_size_ok) { print_error("cannot get size of output file: %s", strerror(errno)); return 1; } @@ -94,7 +96,13 @@ is_input_file_valid(const resources_restore_t *const res, uint32_t sector_size) prev_out_offset = out_offset; } - if (ftell(res->in_file) != in_size) { + bool pos_ok = false; + const size_t pos = file_tell(res->in_file, &pos_ok); + + if (!pos_ok) { + print_error("cannot get position in the input file"); + return false; + } else if (pos != in_size) { /* The input file must be read completely */ print_error("input file is not valid"); return false; @@ -116,9 +124,10 @@ restore(const options_restore_t *const opts, return 1; } - const long in_size = file_size(res->in_file); + bool in_size_ok = false; + const size_t in_size = file_size(res->in_file, &in_size_ok); - if (in_size < 0) { + if (!in_size_ok) { print_error("cannot get size of input file: %s", strerror(errno)); return 1; } @@ -158,7 +167,13 @@ restore(const options_restore_t *const opts, } /* The input file must be read completely */ - if (ftell(res->in_file) != in_size) { + bool pos_ok = false; + const size_t pos = file_tell(res->in_file, &pos_ok); + + if (!pos_ok) { + print_error("cannot get position in the input file"); + return 1; + } else if (pos != in_size) { print_error("input file is not valid"); return 1; } |
