Disk ARchive 2.8.0
Full featured and portable backup and archiving tool
cat_file.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2025 Denis Corbin
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 2
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18//
19// to contact the author, see the AUTHOR file
20/*********************************************************************/
21
25
26#ifndef CAT_FILE_HPP
27#define CAT_FILE_HPP
28
29#include "../my_config.h"
30
31extern "C"
32{
33} // end extern "C"
34
35#include "cat_inode.hpp"
36#include "memory_file.hpp"
38
39namespace libdar
40{
41
44
45
47
48 class cat_file : public cat_inode
49 {
50 public :
51
54 {
58 plain
59 };
60
61 static constexpr U_8 FILE_DATA_WITH_HOLE = 0x01;
62 static constexpr U_8 FILE_DATA_IS_DIRTY = 0x02;
63 static constexpr U_8 FILE_DATA_HAS_DELTA_SIG = 0x04;
64
65 cat_file(const infinint & xuid,
66 const infinint & xgid,
67 U_16 xperm,
68 const datetime & last_access,
69 const datetime & last_modif,
70 const datetime & last_change,
71 const std::string & src,
72 const path & che,
73 const infinint & taille,
74 const infinint & fs_device,
75 bool x_furtive_read_mode);
76 cat_file(const std::shared_ptr<user_interaction> & dialog,
78 const archive_version & reading_ver,
79 saved_status saved,
80 compression default_algo,
81 bool small);
82 cat_file(const cat_file & ref);
83 cat_file(cat_file && ref) = delete;
84 cat_file & operator = (const cat_file & ref) = delete;
85 cat_file & operator = (cat_file && ref) = delete;
86 ~cat_file() { detruit(); };
87
88 virtual bool has_changed_since(const cat_inode & ref,
89 const infinint & hourshift,
90 comparison_fields what_to_check) const override;
91 infinint get_size() const { return status == from_patch ? (*in_place).get_size() : *size; };
92 void change_size(const infinint & s) const { *size = s; };
93 infinint get_storage_size() const { return *storage_size; };
94 void set_storage_size(const infinint & s) { *storage_size = s; };
95
97 bool can_get_data() const { return get_saved_status() == saved_status::saved || get_saved_status() == saved_status::delta || status == from_path; };
98
100
114 std::shared_ptr<memory_file> delta_sig_mem,
115 U_I signature_block_size,
116 std::shared_ptr<memory_file> delta_ref,
117 const crc **checksum = nullptr) const;
118
119 void clean_data() const; // partially free memory (but get_data() becomes disabled)
120
122
130
132 bool applying_binary_patch() const { return status == from_patch && in_place != nullptr; };
133
134 void set_offset(const infinint & r);
135 const infinint & get_offset() const;
136 virtual unsigned char signature() const override { return 'f'; };
137 virtual std::string get_description() const override { return "file"; };
138
139 void set_crc(const crc &c);
140 bool get_crc(const crc * & c) const;
141 bool has_crc() const { return check != nullptr; };
142 bool get_crc_size(infinint & val) const;
143 void drop_crc() { if(check != nullptr) { delete check; check = nullptr; } };
144
145 // whether the plain file has to detect sparse file
146 void set_sparse_file_detection_read(bool val) { if(status == from_cat) throw SRC_BUG; if(val) file_data_status_read |= FILE_DATA_WITH_HOLE; else file_data_status_read &= ~FILE_DATA_WITH_HOLE; };
147
148 void set_sparse_file_detection_write(bool val) { if(val) file_data_status_write |= FILE_DATA_WITH_HOLE; else file_data_status_write &= ~FILE_DATA_WITH_HOLE; };
149
150 // whether the plain file is stored with a sparse_file datastructure in the archive
151 bool get_sparse_file_detection_read() const { return (file_data_status_read & FILE_DATA_WITH_HOLE) != 0; };
152 bool get_sparse_file_detection_write() const { return (file_data_status_write & FILE_DATA_WITH_HOLE) != 0; };
153
154 virtual cat_entree *clone() const override { return new (std::nothrow) cat_file(*this); };
155
156 compression get_compression_algo_read() const { return algo_read; };
157
158 compression get_compression_algo_write() const { return algo_write; };
159
160 // object migration methods (merging)
161 void change_compression_algo_write(compression x) { algo_write = x; };
162
163 // dirtiness
164
165 bool is_dirty() const { return dirty; };
166 void set_dirty(bool value) { dirty = value; };
167
168
170 bool has_delta_signature_structure() const { return delta_sig != nullptr; };
171
173
177 bool has_delta_signature_available() const { return delta_sig != nullptr && delta_sig->can_obtain_sig(); };
178
179
181 bool has_patch_base_crc() const;
182
184 bool get_patch_base_crc(const crc * & c) const;
185
187 void set_patch_base_crc(const crc & c);
188
189
190
193
195 bool get_patch_result_crc(const crc * & c) const;
196
198 void set_patch_result_crc(const crc & c);
199
202
204
209
211
216 void dump_delta_signature(std::shared_ptr<memory_file> & sig, U_I sign_block_size, generic_file & where, bool small) const;
217
219 void dump_delta_signature(generic_file & where, bool small) const;
220
221
223
226
228
233 void read_delta_signature(std::shared_ptr<memory_file> & delta_sig,
234 U_I & block_len) const;
235
238
240 bool has_same_delta_signature(const cat_file & ref) const;
241
244
247
249 virtual bool operator == (const cat_entree & ref) const override { return true; };
250
252 bool same_data_as(const cat_file & other,
253 bool check_data,
254 const infinint & hourshift,
255 bool me_or_other_read_in_seq_mode);
256
258 const archive_version & get_archive_version() const { return read_ver; };
259
260 protected:
261 virtual void sub_compare(const cat_inode & other,
262 bool isolated_mode,
263 bool seq_read_mode) const override;
264 virtual void inherited_dump(const pile_descriptor & pdesc, bool small) const override;
265 virtual void post_constructor(const pile_descriptor & pdesc) override;
266
267 enum { from_path, from_cat, from_patch } status;
268
269 private:
270 std::string chemin;
275 bool dirty;
283 mutable bool delta_sig_read;
285 std::unique_ptr<cat_file> in_place;
286
287 void sub_compare_internal(const cat_inode & other,
288 bool can_read_my_data,
289 bool can_read_other_data,
290 const infinint & hourshift,
291 bool seq_read_mode) const;
292
293 void clean_patch_base_crc();
294
295 void detruit();
296
297 cat_file* clone_as_file() const;
298
299 };
300
302
303} // end of namespace
304
305#endif
class used to manage binary delta signature in catalogue and archive
base object for all inode types, managed EA and FSA, dates, permissions, ownership,...
class archive_version manages the version of the archive format
the cat_delta_signature file class
bool can_obtain_sig() const
the cat_delta_signature structure can only hold CRC without delta_signature, this call gives the situ...
the root class from all other inherite for any entry in the catalogue
Definition: cat_entree.hpp:60
saved_status get_saved_status() const
obtain the saved status of the object
Definition: cat_entree.hpp:164
the plain file class
Definition: cat_file.hpp:49
infinint * size
size of the data (uncompressed)
Definition: cat_file.hpp:272
bool get_crc(const crc *&c) const
the argument is set to an allocated crc object owned by the "cat_file" object, its stay valid while t...
bool has_same_delta_signature(const cat_file &ref) const
return true if ref and "this" have both equal delta signatures
void set_patch_base_crc(const crc &c)
set the reference CRC of the file to base the patch on, for s_detla objects
bool get_patch_base_crc(const crc *&c) const
returns the CRC of the file to base the patch on, for s_delta objects
void dump_delta_signature(std::shared_ptr< memory_file > &sig, U_I sign_block_size, generic_file &where, bool small) const
write down to archive the given delta signature
const archive_version & get_archive_version() const
expose the archive format the object of the backup this object comes from
Definition: cat_file.hpp:258
static constexpr U_8 FILE_DATA_HAS_DELTA_SIG
delta signature is present
Definition: cat_file.hpp:63
bool same_data_as(const cat_file &other, bool check_data, const infinint &hourshift, bool me_or_other_read_in_seq_mode)
compare just data not inode information EA nor FSA
void read_delta_signature(std::shared_ptr< memory_file > &delta_sig, U_I &block_len) const
fetch the delta signature from the archive
void drop_delta_signature_data() const
drop the delta signature from memory (will not more be posible to be read, using read_delta_signature...
virtual void post_constructor(const pile_descriptor &pdesc) override
let inherited classes build object's data after CRC has been read from file in small read mode
bool dirty
true when a file has been modified at the time it was saved
Definition: cat_file.hpp:275
bool get_patch_result_crc(const crc *&c) const
returns the CRC the file will have once restored or patched (for s_saved, s_delta,...
compression algo_write
which compression algorithm to use to write down (merging) the file's data
Definition: cat_file.hpp:277
bool delta_sig_read
whether delta sig has been read/initialized from filesystem
Definition: cat_file.hpp:283
static constexpr U_8 FILE_DATA_IS_DIRTY
data modified while being saved
Definition: cat_file.hpp:62
get_data_mode
how to get data from archive
Definition: cat_file.hpp:54
@ keep_compressed
provide access to compressed data
Definition: cat_file.hpp:55
@ normal
provide access to full data (uncompressed, uses skip() to restore holes)
Definition: cat_file.hpp:57
@ keep_hole
provide access to uncompressed data but sparse_file datastructure
Definition: cat_file.hpp:56
@ plain
provide access to plain data, no skip to restore holes, provide instead zeroed bytes
Definition: cat_file.hpp:58
cat_delta_signature * delta_sig
delta signature and associated CRC
Definition: cat_file.hpp:282
crc * patch_base_check
when data contains a delta patch, moved from delta_sig since format 10.2
Definition: cat_file.hpp:281
void will_have_delta_signature_available()
prepare the object to receive a delta signature structure including delta signature
void clear_delta_signature_structure()
remove any information about delta signature
bool get_crc_size(infinint &val) const
returns true if crc is know and puts its width in argument
void clear_delta_signature_only()
remove information about delta signature also associated CRCs if status is not s_delta
bool has_delta_signature_structure() const
return whether the object has an associated delta signature structure
Definition: cat_file.hpp:170
char file_data_status_read
defines the datastructure to use when reading the data
Definition: cat_file.hpp:279
infinint * storage_size
how much data used in archive (after compression)
Definition: cat_file.hpp:273
bool has_patch_result_crc() const
returns whether the object has a CRC corresponding to data (for s_saved, s_delta, and when delta sign...
crc * check
crc computed on the data
Definition: cat_file.hpp:274
static constexpr U_8 FILE_DATA_WITH_HOLE
file's data contains hole datastructure
Definition: cat_file.hpp:61
virtual void inherited_dump(const pile_descriptor &pdesc, bool small) const override
true if object has been created by sequential reading of an archive
virtual bool operator==(const cat_entree &ref) const override
not used
Definition: cat_file.hpp:249
bool has_delta_signature_available() const
return whether the object has an associated delta signature structure including a delta signature dat...
Definition: cat_file.hpp:177
std::unique_ptr< cat_file > in_place
when data is build from patch (merging context only) this is the object to apply the patch on (while ...
Definition: cat_file.hpp:285
virtual cat_entree * clone() const override
a way to copy the exact type of an object even if pointed to by a parent class pointer
Definition: cat_file.hpp:154
bool furtive_read_mode
used only when status equals "from_path"
Definition: cat_file.hpp:278
char file_data_status_write
defines the datastructure to apply when writing down the data
Definition: cat_file.hpp:280
void will_have_delta_signature_structure()
prepare the object to receive a delta signature structure
bool applying_binary_patch() const
return the save value as what set_data_from_binary_patch has provided
Definition: cat_file.hpp:132
archive_version read_ver
archive format used/to use
Definition: cat_file.hpp:284
void read_delta_signature_metadata() const
load metadata (and delta signature when in sequential mode) into memory
bool set_data_from_binary_patch(cat_file *in_place_addr)
used while merging, chages the behavior of our get_data() to provide the patched version of the provi...
virtual std::string get_description() const override
inherited class designation
Definition: cat_file.hpp:137
void set_patch_result_crc(const crc &c)
set the CRC the file will have once restored or patched (for s_saved, s_delta, and when delta signatu...
bool can_get_data() const
check whether the object will be able to provide a object using get_data() method
Definition: cat_file.hpp:97
void dump_delta_signature(generic_file &where, bool small) const
variant of dump_delta_signature when just CRC have to be dumped
bool has_patch_base_crc() const
returns whether the object has a base patch CRC (s_delta status objects)
std::string chemin
path to the data (when read from filesystem)
Definition: cat_file.hpp:270
virtual unsigned char signature() const override
inherited class signature
Definition: cat_file.hpp:136
infinint * offset
start location of the data in 'loc'
Definition: cat_file.hpp:271
virtual generic_file * get_data(get_data_mode mode, std::shared_ptr< memory_file > delta_sig_mem, U_I signature_block_size, std::shared_ptr< memory_file > delta_ref, const crc **checksum=nullptr) const
returns a newly allocated object in read_only mode
compression algo_read
which compression algorithm to use to read the file's data
Definition: cat_file.hpp:276
the root class for all cat_inode
Definition: cat_inode.hpp:53
pure virtual class defining interface of a CRC object
Definition: crc.hpp:47
stores time information
Definition: datetime.hpp:59
this is the interface class from which all other data transfer classes inherit
the arbitrary large positive integer class
the class path is here to manipulate paths in the Unix notation: using'/'
Definition: path.hpp:51
comparison_fields
how to consider file change during comparison and incremental backup
Definition: archive_aux.hpp:53
compression
the different compression algorithm available
Definition: compression.hpp:46
saved_status
data saved status for an entry
Definition: cat_status.hpp:44
@ saved
inode is saved in the archive
@ delta
inode is saved but as delta binary from the content (delta signature) of what was found in the archiv...
Memory_file is a generic_file class that only uses virtual memory.
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47