| Disk ARchive 2.8.3 Full featured and portable backup and archiving tool

Loading...
Searching...
No Matches
escape.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2026 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
37
38
39#ifndef ESCAPE_HPP
40#define ESCAPE_HPP
41
42#include "../my_config.h"
43
44 extern "C"
45{
46#if HAVE_LIMITS_H
47#include <limits.h>
48#endif
49}
50
51#include <set>
52
53#include "generic_file.hpp"
54
55#define ESCAPE_FIXED_SEQUENCE_NORMAL 0xAD
56#define ESCAPE_FIXED_SEQUENCE_SPARSE_FILE 0xAE
57
58#define MAX_BUFFER_SIZE 102400
59#ifdef SSIZE_MAX
60#if SSIZE_MAX < MAX_BUFFER_SIZE
61#undef MAX_BUFFER_SIZE
62#define MAX_BUFFER_SIZE SSIZE_MAX
63#endif
64#endif
65
66 namespace libdar
67{
68
71
72 class escape : public generic_file
73 {
74 public:
75 enum sequence_type
76 {
77 seqt_undefined,
78 seqt_not_a_sequence,
79 seqt_file,
80 seqt_ea,
81 seqt_catalogue,
82 seqt_data_name,
83 seqt_file_crc,
84 seqt_ea_crc,
85 seqt_changed,
86 seqt_dirty,
87 seqt_failed_backup,
88 seqt_fsa,
89 seqt_fsa_crc,
90 seqt_delta_sig,
91 seqt_in_place
92 };
93
94 // the archive layout of marks is for each entry:
95 // #seqt_file# <inode> [<file data> [#seqt_changed# <new copy of data> [...] ] #seqt_file_crc# <CRC>[#seqt_dirty#]] [#seqt_ea# <EA> #seqt_ea_crc# <CRC>]
96 // this previous sequence that we will call <SEQ> is repeated for each file, then on the overall archive we have :
97 // #seqt_data_name# <data_name> <SEQ> ... <SEQ> #seqt_catalogue# <catalogue> <terminator>
98
99 // the provided "below" object must exist during the whole live of the escape object,
100 // the escape object does not own this "below" object
101 // it must be destroyed by the caller/creator of the escape object.
102
103
104 // constructors & destructors
105
106 escape(generic_file *below,
107 const std::set<sequence_type> & x_unjumpable
108 );
109 escape(const escape & ref) : generic_file(ref) { copy_from(ref); };
110 escape(escape && ref) noexcept : generic_file(std::move(ref)) { nullifyptr(); move_from(std::move(ref)); };
111 escape & operator = (const escape & ref);
112 escape & operator = (escape && ref) noexcept { generic_file::operator = (std::move(ref)); move_from(std::move(ref)); return *this; };
113 ~escape();
114
115 // escape specific routines
116
117 void add_mark_at_current_position(sequence_type t);
118
120
125 bool skip_to_next_mark(sequence_type t, bool jump);
126 bool next_to_read_is_mark(sequence_type t);
127 bool next_to_read_is_which_mark(sequence_type & t);
128
129 void add_unjumpable_mark(sequence_type t) { if(is_terminated()) throw SRC_BUG; unjumpable.insert(t); };
130 void remove_unjumpable_mark(sequence_type t);
131 bool is_unjumpable_mark(sequence_type t) const { return unjumpable.find(t) != unjumpable.end(); };
132 void clear_all_unjumpable_marks() { unjumpable.clear(); };
133
134
135 // generic_file inherited routines
136 // NOTA: Nothing is done to prevent skip* operation to put the read cursor in the middle of an escape sequence and
137 // thus incorrectly consider it as normal data. Such event should only occure upon archive corruption and will be detected
138 // by checksum mechanisms.
139
140 virtual bool skippable(skippability direction, const infinint & amount) override;
141 virtual bool skip(const infinint & pos) override;
142 virtual bool skip_to_eof() override;
143 virtual bool skip_relative(S_I x) override;
144 virtual bool truncatable(const infinint & pos) const override { return x_below->truncatable(pos); };
145 virtual infinint get_position() const override;
146
147 protected:
148 virtual void inherited_read_ahead(const infinint & amount) override;
149 virtual U_I inherited_read(char *a, U_I size) override;
150 virtual void inherited_write(const char *a, U_I size) override;
151 virtual void inherited_truncate(const infinint & pos) override;
152 virtual void inherited_sync_write() override { flush_write(); };
153 virtual void inherited_flush_read() override { flush_write(); clean_read(); };
154 virtual void inherited_terminate() override { flush_or_clean(); };
155
156 void change_fixed_escape_sequence(unsigned char value) { fixed_sequence[0] = value; };
157 bool has_escaped_data_since_last_skip() const { return escaped_data_count_since_last_skip > 0; };
158
159 private:
160
161 //-- constants
162
164 static constexpr U_I ESCAPE_SEQUENCE_LENGTH = 6;
165 static constexpr U_I INITIAL_WRITE_BUFFER_SIZE = 2*ESCAPE_SEQUENCE_LENGTH;
166 static constexpr U_I INITIAL_READ_BUFFER_SIZE = MAX_BUFFER_SIZE;
167 static const infinint READ_BUFFER_SIZE_INFININT;
168
170
176 static const unsigned char usual_fixed_sequence[ESCAPE_SEQUENCE_LENGTH];
177
178 //-- variables
179
180 generic_file *x_below;
181 U_I write_buffer_size;
182 char write_buffer[INITIAL_WRITE_BUFFER_SIZE];
184 U_I already_read;
185 bool read_eof;
186 U_I escape_seq_offset_in_buffer;
187 char* read_buffer;
188 U_I read_buffer_size;
189 U_I read_buffer_alloc;
190 std::set<sequence_type> unjumpable;
191 unsigned char fixed_sequence[ESCAPE_SEQUENCE_LENGTH];
192 infinint escaped_data_count_since_last_skip;
193 infinint below_position;
194
195 //-- routines
196
197 void set_fixed_sequence_for(sequence_type t) { fixed_sequence[ESCAPE_SEQUENCE_LENGTH - 1] = type2char(t); };
198 void check_below() const { if(x_below == nullptr) throw SRC_BUG; };
199 void clean_read();
200 void flush_write();
201 void flush_or_clean()
202 {
203 switch(get_mode())
204 {
205 case gf_read_only:
206 clean_read();
207 break;
208 case gf_write_only:
209 case gf_read_write:
210 flush_write();
211 break;
212 default:
213 throw SRC_BUG;
214 }
215 };
216 void nullifyptr() noexcept { x_below = nullptr; };
217 void copy_from(const escape & ref);
218 void move_from(escape && ref) noexcept;
219 bool mini_read_buffer();
220
221 //-- static routine(s)
222
223 // some convertion routines
224 static char type2char(sequence_type x);
225 static sequence_type char2type(char x);
226
228
230
233 static U_I trouve_amorce(const char *a, U_I size, const unsigned char escape_sequence[ESCAPE_SEQUENCE_LENGTH]);
234
236
243 static U_I remove_data_marks_and_stop_at_first_real_mark(char *a, U_I size, U_I & delta, const unsigned char escape_sequence[ESCAPE_SEQUENCE_LENGTH]);
244 };
245
247
248} // end of namespace
249
250#endif
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition dar64-2.8.3-win64/include/dar/generic_file.hpp:77
libdar::generic_file::inherited_sync_write
virtual void inherited_sync_write()=0
write down any pending data
libdar::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition dar64-2.8.3-win64/include/dar/generic_file.hpp:80
libdar::generic_file::operator=
generic_file & operator=(const generic_file &ref)
assignment operator
Definition dar64-2.8.3-win64/include/dar/generic_file.hpp:89
libdar::generic_file::get_position
virtual infinint get_position() const =0
get the current read/write position
libdar::generic_file::skip_to_eof
virtual bool skip_to_eof()=0
skip to the end of file
libdar::generic_file::is_terminated
bool is_terminated() const
Definition dar64-2.8.3-win64/include/dar/generic_file.hpp:328
libdar::generic_file::get_mode
gf_mode get_mode() const
retreive the openning mode for this object
Definition dar64-2.8.3-win64/include/dar/generic_file.hpp:109
libdar::generic_file::inherited_terminate
virtual void inherited_terminate()=0
destructor-like call, except that it is allowed to throw exceptions
libdar::generic_file::inherited_flush_read
virtual void inherited_flush_read()=0
reset internal engine, flush caches in order to read the data at current position
libdar::saved_status::delta
@ delta
inode is saved but as delta binary from the content (delta signature) of what was found in the archiv...
Definition dar64-2.8.3-win64/include/dar/cat_status.hpp:49
libdar::gf_write_only
@ gf_write_only
write only access
Definition dar64-2.8.3-win64/include/dar/gf_mode.hpp:46
libdar::gf_read_write
@ gf_read_write
read and write access
Definition dar64-2.8.3-win64/include/dar/gf_mode.hpp:47
libdar::gf_read_only
@ gf_read_only
read only access
Definition dar64-2.8.3-win64/include/dar/gf_mode.hpp:45
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition dar64-2.8.3-win64/include/dar/archive.hpp:47
generic_file.hpp
class generic_file is defined here as well as class fichier