From 9280ff2c747a5a59f0a6e0cc6335b24de62cacc9 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sat, 28 Dec 2024 12:41:18 +0100 Subject: Rename in file to diff file for restore --- src/restore.cpp | 85 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) (limited to 'src/restore.cpp') 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 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(&out_offset), sizeof(out_offset)); + diff_file.read(reinterpret_cast(&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 in_buffer; + const size_t diff_buffer_size = sizeof(uint64_t) + opts.getSectorSize(); + std::unique_ptr diff_buffer; try { - in_buffer = std::make_unique(in_buffer_size); + diff_buffer = std::make_unique(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(in_buffer.get())); + le64toh(*reinterpret_cast(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(in_buffer.get()) + + if (!out_file.write(reinterpret_cast(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"); } } -- cgit v1.2.3