diff options
| author | Jan Sucan <jan@jansucan.com> | 2024-12-28 12:41:18 +0100 |
|---|---|---|
| committer | Ján Sučan <jan@jansucan.com> | 2024-12-28 12:47:08 +0100 |
| commit | 9280ff2c747a5a59f0a6e0cc6335b24de62cacc9 (patch) | |
| tree | 1d1b33ba93e2926124cfecf890c8bb2a4236a12e | |
| parent | 217a6b905d46ee355373b2de60f0f36be828202c (diff) | |
Rename in file to diff file for restore
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | src/options.cpp | 8 | ||||
| -rw-r--r-- | src/options.h | 4 | ||||
| -rw-r--r-- | src/restore.cpp | 85 | ||||
| -rw-r--r-- | tests/004-unknown_option.sh | 2 | ||||
| -rw-r--r-- | tests/006-incorrect_buffer_size.sh | 6 | ||||
| -rw-r--r-- | tests/007-incorrect_sector_size.sh | 6 | ||||
| -rw-r--r-- | tests/300-incorrect_reference_file.sh | 32 |
8 files changed, 75 insertions, 74 deletions
@@ -20,7 +20,7 @@ is read twice when restoring it. Because of that, it is slower. > diff-dd backup [-s SECTOR_SIZE] [-b BUFFER_SIZE] INFILE BASEFILE OUTFILE -> diff-dd restore [-s SECTOR_SIZE] [-b BUFFER_SIZE] INFILE OUTFILE +> diff-dd restore [-s SECTOR_SIZE] [-b BUFFER_SIZE] DIFFFILE OUTFILE ## Backup @@ -37,9 +37,9 @@ which only the changed sectors of the ```INFILE```, compared to the ## Restore The restoration means application of the changed sectors saved in the -```INFILE```, which is the differential image, to the ```OUTFILE```: +```DIFFFILE```, which is the differential image, to the ```OUTFILE```: -> diff-dd restore INFILE OUTFILE +> diff-dd restore DIFFFILE OUTFILE ## Options diff --git a/src/options.cpp b/src/options.cpp index ed6eec8..280d5c6 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -72,9 +72,9 @@ OptionsBackup::getOutFilePath() const } std::filesystem::path -OptionsRestore::getInFilePath() const +OptionsRestore::getDiffFilePath() const { - return in_file_path; + return diff_file_path; } std::filesystem::path @@ -90,7 +90,7 @@ OptionParser::printUsage() std::cout << " [-b BUFFER_SIZE] INFILE BASEFILE OUTFILE" << std::endl; std::cout << " Or: " << PROGRAM_NAME_STR << " restore [-s SECTOR_SIZE]"; - std::cout << "[-b BUFFER_SIZE] REFFILE OUTFILE" << std::endl; + std::cout << "[-b BUFFER_SIZE] DIFFFILE OUTFILE" << std::endl; std::cout << " Or: " << PROGRAM_NAME_STR << " help" << std::endl; } @@ -145,7 +145,7 @@ OptionParser::parseRestore(int argc, char **argv) } else if (argc > 2) { throw OptionError("too many arguments"); } else { - opts.in_file_path = next_arg(&argv); + opts.diff_file_path = next_arg(&argv); opts.out_file_path = next_arg(&argv); } diff --git a/src/options.h b/src/options.h index e22b3e3..08559e2 100644 --- a/src/options.h +++ b/src/options.h @@ -80,11 +80,11 @@ class OptionsRestore : public Options public: virtual ~OptionsRestore() override = default; - std::filesystem::path getInFilePath() const; + std::filesystem::path getDiffFilePath() const; std::filesystem::path getOutFilePath() const; private: - std::filesystem::path in_file_path; + std::filesystem::path diff_file_path; std::filesystem::path out_file_path; }; diff --git a/src/restore.cpp b/src/restore.cpp index 813d9ce..8a5b29f 100644 --- a/src/restore.cpp +++ b/src/restore.cpp @@ -31,24 +31,24 @@ #include <fstream> static void -check_input_file(const OptionsRestore &opts) +check_diff_file(const OptionsRestore &opts) { - size_t in_size{0}; + size_t diff_size{0}; try { - in_size = std::filesystem::file_size(opts.getInFilePath()); + diff_size = std::filesystem::file_size(opts.getDiffFilePath()); } catch (const std::exception &e) { - throw RestoreError("cannot get size of input file: " + + throw RestoreError("cannot get size of diff file: " + std::string(e.what())); } - if (in_size == 0) { - throw RestoreError("input file is empty"); - } else if ((in_size % (sizeof(uint64_t) + opts.getSectorSize())) != 0) { - /* The input file must hold equally sized sectors and the + if (diff_size == 0) { + throw RestoreError("diff file is empty"); + } else if ((diff_size % (sizeof(uint64_t) + opts.getSectorSize())) != 0) { + /* The diff file must hold equally sized sectors and the * offset of each of them */ throw RestoreError( - "input file has size that cannot contain valid diff data"); + "diff file has size that cannot contain valid diff data"); } size_t out_size{0}; @@ -59,24 +59,25 @@ check_input_file(const OptionsRestore &opts) std::string(e.what())); } - std::ifstream in_file; - in_file.open(opts.getInFilePath(), std::ios::in | std::ios::binary); - if (!in_file) { - throw RestoreError("cannot open input file"); + std::ifstream diff_file; + diff_file.open(opts.getDiffFilePath(), std::ios::in | std::ios::binary); + if (!diff_file) { + throw RestoreError("cannot open diff file"); } uint64_t prev_out_offset = 0; bool is_first_reading = true; - /* Scan the input file and check */ + /* Scan the diff file and check */ for (;;) { uint64_t out_offset; /* Read the next offset */ - in_file.read(reinterpret_cast<char *>(&out_offset), sizeof(out_offset)); + diff_file.read(reinterpret_cast<char *>(&out_offset), + sizeof(out_offset)); - if (in_file.eof() && in_file.fail() && !in_file.bad()) { + if (diff_file.eof() && diff_file.fail() && !diff_file.bad()) { break; - } else if (!in_file.good() && !in_file.eof()) { + } else if (!diff_file.good() && !diff_file.eof()) { throw RestoreError("cannot read from file"); } out_offset = le64toh(out_offset); @@ -87,31 +88,31 @@ check_input_file(const OptionsRestore &opts) } else if ((out_offset + opts.getSectorSize()) > out_size) { throw RestoreError( "a sector offset points past the end of the output file"); - } else if (!in_file.seekg(opts.getSectorSize(), std::ios_base::cur)) { - throw RestoreError("cannot seek in input file"); + } else if (!diff_file.seekg(opts.getSectorSize(), std::ios_base::cur)) { + throw RestoreError("cannot seek in diff file"); } is_first_reading = false; prev_out_offset = out_offset; } - /* The input file must be read completely */ + /* The diff file must be read completely */ char c; - in_file.read(&c, 1); - if (in_file.gcount() != 0) { - throw RestoreError("input file is not valid"); + diff_file.read(&c, 1); + if (diff_file.gcount() != 0) { + throw RestoreError("diff file is not valid"); } - in_file.clear(); + diff_file.clear(); - in_file.close(); + diff_file.close(); } void restore(const OptionsRestore &opts) { - check_input_file(opts); + check_diff_file(opts); - BufferedFileReader in_file(opts.getInFilePath(), opts.getBufferSize()); + BufferedFileReader diff_file(opts.getDiffFilePath(), opts.getBufferSize()); std::fstream out_file; out_file.open(opts.getOutFilePath(), @@ -120,33 +121,33 @@ restore(const OptionsRestore &opts) throw RestoreError("cannot open output file"); } - const size_t in_buffer_size = sizeof(uint64_t) + opts.getSectorSize(); - std::unique_ptr<char[]> in_buffer; + const size_t diff_buffer_size = sizeof(uint64_t) + opts.getSectorSize(); + std::unique_ptr<char[]> diff_buffer; try { - in_buffer = std::make_unique<char[]>(in_buffer_size); + diff_buffer = std::make_unique<char[]>(diff_buffer_size); } catch (const std::bad_alloc &e) { - throw RestoreError("cannot allocate sector buffer for input file data"); + throw RestoreError("cannot allocate sector buffer for diff file data"); } /* Restore data from the differential image */ - size_t in_read_size = {0}; + size_t diff_read_size = {0}; for (;;) { - in_read_size = in_file.read(in_buffer.get(), in_buffer_size); + diff_read_size = diff_file.read(diff_buffer.get(), diff_buffer_size); - if (in_read_size == 0) { + if (diff_read_size == 0) { break; - } else if (in_read_size != in_buffer_size) { - throw RestoreError("cannot read from input file"); + } else if (diff_read_size != diff_buffer_size) { + throw RestoreError("cannot read from diff file"); } const uint64_t out_offset = - le64toh(*reinterpret_cast<uint64_t *>(in_buffer.get())); + le64toh(*reinterpret_cast<uint64_t *>(diff_buffer.get())); if (!out_file.seekp(out_offset, std::ios_base::beg)) { throw RestoreError("cannot seek in output file"); } - if (!out_file.write(reinterpret_cast<char *>(in_buffer.get()) + + if (!out_file.write(reinterpret_cast<char *>(diff_buffer.get()) + sizeof(uint64_t), opts.getSectorSize())) { throw RestoreError("cannot write to output file"); @@ -155,10 +156,10 @@ restore(const OptionsRestore &opts) out_file.close(); - /* The input file must be read completely */ + /* The diff file must be read completely */ char c; - in_read_size = in_file.read(&c, 1); - if (in_read_size != 0) { - throw RestoreError("input file is not valid"); + diff_read_size = diff_file.read(&c, 1); + if (diff_read_size != 0) { + throw RestoreError("diff file is not valid"); } } diff --git a/tests/004-unknown_option.sh b/tests/004-unknown_option.sh index 583f1ca..4a03a37 100644 --- a/tests/004-unknown_option.sh +++ b/tests/004-unknown_option.sh @@ -5,6 +5,6 @@ source ./assert.sh PROGRAM_EXEC="$1" assert "Usage" "unknown option '-x'" 1 $PROGRAM_EXEC backup -x in base out -assert "Usage" "unknown option '-x'" 1 $PROGRAM_EXEC restore -x ref out +assert "Usage" "unknown option '-x'" 1 $PROGRAM_EXEC restore -x diff out exit 0 diff --git a/tests/006-incorrect_buffer_size.sh b/tests/006-incorrect_buffer_size.sh index a579b26..5e59952 100644 --- a/tests/006-incorrect_buffer_size.sh +++ b/tests/006-incorrect_buffer_size.sh @@ -8,8 +8,8 @@ assert "Usage" "incorrect sector size" 1 $PROGRAM_EXEC backup -s abc123 in base assert "Usage" "sector size cannot be 0" 1 $PROGRAM_EXEC backup -s 0 in base out assert "Usage" "sector size cannot larger than buffer size" 1 $PROGRAM_EXEC backup -s 2 -b 1 in base out -assert "Usage" "incorrect sector size" 1 $PROGRAM_EXEC restore -s abc123 in out -assert "Usage" "sector size cannot be 0" 1 $PROGRAM_EXEC restore -s 0 in out -assert "Usage" "sector size cannot larger than buffer size" 1 $PROGRAM_EXEC restore -s 2 -b 1 in out +assert "Usage" "incorrect sector size" 1 $PROGRAM_EXEC restore -s abc123 diff out +assert "Usage" "sector size cannot be 0" 1 $PROGRAM_EXEC restore -s 0 diff out +assert "Usage" "sector size cannot larger than buffer size" 1 $PROGRAM_EXEC restore -s 2 -b 1 diff out exit 0 diff --git a/tests/007-incorrect_sector_size.sh b/tests/007-incorrect_sector_size.sh index 89f7051..a8dc956 100644 --- a/tests/007-incorrect_sector_size.sh +++ b/tests/007-incorrect_sector_size.sh @@ -8,8 +8,8 @@ assert "Usage" "incorrect buffer size" 1 $PROGRAM_EXEC backup -b abc123 in base assert "Usage" "buffer size cannot be 0" 1 $PROGRAM_EXEC backup -b 0 in base out assert "Usage" "buffer size is not multiple of sector size" 1 $PROGRAM_EXEC backup -b 3 -s 2 in base out -assert "Usage" "incorrect buffer size" 1 $PROGRAM_EXEC restore -b abc123 in out -assert "Usage" "buffer size cannot be 0" 1 $PROGRAM_EXEC restore -b 0 in out -assert "Usage" "buffer size is not multiple of sector size" 1 $PROGRAM_EXEC restore -b 3 -s 2 in out +assert "Usage" "incorrect buffer size" 1 $PROGRAM_EXEC restore -b abc123 diff out +assert "Usage" "buffer size cannot be 0" 1 $PROGRAM_EXEC restore -b 0 diff out +assert "Usage" "buffer size is not multiple of sector size" 1 $PROGRAM_EXEC restore -b 3 -s 2 diff out exit 0 diff --git a/tests/300-incorrect_reference_file.sh b/tests/300-incorrect_reference_file.sh index ea52ab2..b15ee12 100644 --- a/tests/300-incorrect_reference_file.sh +++ b/tests/300-incorrect_reference_file.sh @@ -4,34 +4,34 @@ source ./assert.sh PROGRAM_EXEC="$1" -rm -f ref out -touch ref out -assert "" "input file is empty" 1 $PROGRAM_EXEC restore ref out +rm -f diff out +touch diff out +assert "" "diff file is empty" 1 $PROGRAM_EXEC restore diff out -dd if=/dev/zero of=ref bs=513 count=1 1>/dev/null 2>&1 -assert "" "input file has size that cannot contain valid diff data" \ - 1 $PROGRAM_EXEC restore -s 512 ref out +dd if=/dev/zero of=diff bs=513 count=1 1>/dev/null 2>&1 +assert "" "diff file has size that cannot contain valid diff data" \ + 1 $PROGRAM_EXEC restore -s 512 diff out -rm -f ref out +rm -f diff out dd if=/dev/zero of=out bs=512 count=2 1>/dev/null 2>&1 # Create a two-sector backup file -dd if=/dev/zero of=ref bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1 +dd if=/dev/zero of=diff bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1 # The first offset will be 2 -printf '\x02' | dd of=ref bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1 +printf '\x02' | dd of=diff bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1 # The second offset will be 1 -printf '\x01' | dd of=ref bs=1 count=1 seek=520 conv=notrunc 1>/dev/null 2>&1 +printf '\x01' | dd of=diff bs=1 count=1 seek=520 conv=notrunc 1>/dev/null 2>&1 assert "" "a sector offset points behind the previous offset" \ - 1 $PROGRAM_EXEC restore -s 512 ref out + 1 $PROGRAM_EXEC restore -s 512 diff out -rm -f ref out +rm -f diff out dd if=/dev/zero of=out bs=512 count=1 1>/dev/null 2>&1 # Create a one-sector backup file -dd if=/dev/zero of=ref bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1 +dd if=/dev/zero of=diff bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1 # The first offset will be 1 -printf '\x01' | dd of=ref bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1 +printf '\x01' | dd of=diff bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1 assert "" "a sector offset points past the end of the output file" \ - 1 $PROGRAM_EXEC restore -s 512 ref out + 1 $PROGRAM_EXEC restore -s 512 diff out -rm -f ref out +rm -f diff out exit 0 |
