From 948a65cf39c8ce31c5adc0f24979e0cb55bc33c3 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sat, 18 Jun 2022 08:50:45 +0200 Subject: Refactor the command line interface Select operation by its name instead of by number of the arguments. --- src/backup.c | 41 ++++++----------------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) (limited to 'src/backup.c') diff --git a/src/backup.c b/src/backup.c index 4691bac..4c9dfbd 100644 --- a/src/backup.c +++ b/src/backup.c @@ -34,15 +34,14 @@ #include #include -static int check_files(const options_t *const opts, - const resources_t *const res); -static int allocate_buffers(const options_t *const opts, size_t out_buffer_size, - resources_t *const res); +static int check_files(const options_backup_t *const opts, + const resources_backup_t *const res); static int write_out_buffer(const char *const buffer, size_t size, FILE *const file); static int -check_files(const options_t *const opts, const resources_t *const res) +check_files(const options_backup_t *const opts, + const resources_backup_t *const res) { const long in_size = file_size(res->in_file); @@ -72,25 +71,6 @@ check_files(const options_t *const opts, const resources_t *const res) return 0; } -static int -allocate_buffers(const options_t *const opts, size_t out_buffer_size, - resources_t *const res) -{ - - if ((res->in_buffer = (char *)malloc(opts->buffer_size)) == NULL) { - print_error("cannot allocate buffer for input file data"); - return 1; - } else if ((res->ref_buffer = (char *)malloc(opts->buffer_size)) == NULL) { - print_error("cannot allocate buffer for reference file data"); - return 1; - } else if ((res->out_buffer = (char *)malloc(out_buffer_size)) == NULL) { - print_error("cannot allocate buffer for output file data"); - return 1; - } - - return 0; -} - static int write_out_buffer(const char *const buffer, size_t size, FILE *const file) { @@ -105,21 +85,12 @@ write_out_buffer(const char *const buffer, size_t size, FILE *const file) } int -backup(const options_t *const opts, resources_t *const res) +backup(const options_backup_t *const opts, const resources_backup_t *const res) { if (check_files(opts, res) != 0) { return 1; } - /* The output buffer contains also the offsets */ - const size_t out_buffer_size = - ((opts->buffer_size / opts->sector_size) * sizeof(uint64_t)) + - opts->buffer_size; - - if (allocate_buffers(opts, out_buffer_size, res) != 0) { - return 1; - } - size_t out_buffer_index = 0; uint64_t input_file_offset = 0; @@ -148,7 +119,7 @@ backup(const options_t *const opts, resources_t *const res) res->ref_buffer + buffer_offset, opts->sector_size) != 0) { /* Backup the changed sector */ - if (out_buffer_index >= out_buffer_size) { + if (out_buffer_index >= res->out_buffer_size) { /* The output buffer is full. Write it to the output file */ if (write_out_buffer(res->out_buffer, out_buffer_index, res->out_file) != 0) { -- cgit v1.2.3