Disk ARchive  2.7.0
Full featured and portable backup and archiving tool
sar.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2021 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 SAR_HPP
27 #define SAR_HPP
28 
29 #include "../my_config.h"
30 
31 #include <string>
32 #include "infinint.hpp"
33 #include "generic_file.hpp"
34 #include "header.hpp"
35 #include "integers.hpp"
36 #include "entrepot.hpp"
37 #include "slice_layout.hpp"
38 #include "contextual.hpp"
39 #include "mem_ui.hpp"
40 
41 namespace libdar
42 {
43  // contextual is defined in generic_file module
44 
47 
49 
54  class sar : public generic_file, public contextual, protected mem_ui
55  {
56  public:
57 
59 
81  sar(const std::shared_ptr<user_interaction> & dialog,
82  const std::string & base_name,
83  const std::string & extension,
84  const std::shared_ptr<entrepot> & where,
85  bool by_the_end,
86  const infinint & x_min_digits,
87  bool lax = false,
88  const std::string & execute = "");
89 
90 
92 
113  sar(const std::shared_ptr<user_interaction> & dialog,
114  gf_mode open_mode,
115  const std::string & base_name,
116  const std::string & extension,
117  const infinint & file_size,
118  const infinint & first_file_size,
119  bool x_warn_overwrite,
120  bool x_allow_overwrite,
121  const infinint & pause,
122  const std::shared_ptr<entrepot> & where,
123  const label & internal_name,
124  const label & data_name,
125  bool force_permission,
126  U_I permission,
127  hash_algo x_hash,
128  const infinint & x_min_digits,
129  bool format_07_compatible,
130  const std::string & execute = "");
131 
133  sar(const sar & ref) = delete;
134 
136  sar(sar && ref) noexcept = delete;
137 
139  sar & operator = (const sar & ref) = delete;
140 
142  sar & operator = (sar && ref) noexcept = delete;
143 
145  ~sar();
146 
147  // inherited from generic_file
148  virtual bool skippable(skippability direction, const infinint & amount) override;
149  virtual bool skip(const infinint &pos) override;
150  virtual bool skip_to_eof() override;
151  virtual bool skip_relative(S_I x) override;
152  virtual bool truncatable(const infinint & pos) const override;
153  virtual infinint get_position() const override;
154 
155  // informational routines
156  const slice_layout & get_slicing() const { return slicing; };
157  bool get_total_file_number(infinint &num) const { num = of_last_file_num; return of_last_file_known; };
158  bool get_last_file_size(infinint &num) const { num = of_last_file_size; return of_last_file_known; };
159 
160  // disable execution of user command when destroying the current object
161  void disable_natural_destruction() { natural_destruction = false; };
162 
163  // enable back execution of user command when destroying the current object
164  void enable_natural_destruction() { natural_destruction = true; };
165 
166  // true if sar's header is from an old archive format (<= "07")
167  virtual bool is_an_old_start_end_archive() const override { return slicing.older_sar_than_v8; };
168 
169  // return the internal_name used to link slices toghether
170  const label & get_internal_name_used() const { return of_internal_name; };
171 
172  // return the data_name used to link slices toghether
173  virtual const label & get_data_name() const override { return of_data_name; };
174 
175  const std::shared_ptr<entrepot> & get_entrepot() const { return entr; };
176 
178  const infinint & get_first_slice_header_size() const { return slicing.first_slice_header; };
179 
181  const infinint & get_non_first_slice_header_size() const { return slicing.other_slice_header; };
182 
183  protected :
184  virtual void inherited_read_ahead(const infinint & amount) override;
185  virtual U_I inherited_read(char *a, U_I size) override;
186  virtual void inherited_write(const char *a, U_I size) override;
187  virtual void inherited_truncate(const infinint & pos) override;
188  virtual void inherited_sync_write() override {}; // nothing to do
189  virtual void inherited_flush_read() override {}; // nothing to do
190  virtual void inherited_terminate() override;
191 
192  private :
193  std::shared_ptr<entrepot> entr;
194  std::string base;
195  std::string ext;
196  std::string hook;
197  slice_layout slicing;
202  // these following variables are modified by open_file / open_file_init
203  // else the are used only for reading
212  bool force_perm;
213  U_I perm;
215  char of_flag;
216  bool initial;
217  // these are the option flags
220  //
222  bool lax;
224 
225  bool skip_forward(U_I x);
226  bool skip_backward(U_I x);
227  void close_file(bool terminal);
228  void open_readonly(const std::string & fic,
229  const infinint &num,
230  bool bytheend
231  );
232  void open_writeonly(const std::string & fic,
233  const infinint &num,
234  bool bytheend
235  );
236  void open_file_init();
237  void open_file(infinint num, bool bytheend);
238  void set_offset(infinint offset);
239  void open_last_file(bool bytheend);
242  header make_write_header(const infinint &num, char flag);
243 
244  // function to lauch the eventually existing command to execute after/before each slice
245  void hook_execute(const infinint &num);
246  };
247 
249 
250 } // end of namespace
251 
252 #endif
the contextual class adds the information of phases in the generic_file
Definition: contextual.hpp:74
abstraction of filesystem files for entrepot
this is the interface class from which all other data transfer classes inherit
this class manages the header of each slice
Definition: header.hpp:66
the arbitrary large positive integer class
manage label data structure used in archive slice headers
Definition: label.hpp:43
class mem_ui to keep a copy of a user_interaction object
Definition: mem_ui.hpp:55
Sar class stands for Segmentation And Reassembly class.
Definition: sar.hpp:55
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
infinint size_of_current
size of the current slice (used in reading mode only)
Definition: sar.hpp:205
bool skip_backward(U_I x)
skip backward in sar global contents
std::string hook
command line to execute between slices
Definition: sar.hpp:196
void open_file(infinint num, bool bytheend)
close current slice and open the slice 'num'
label of_data_name
internal name linked to data (transparent to dar_xform and used by isolated catalogue as reference)
Definition: sar.hpp:211
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
infinint to_read_ahead
amount of data to read ahead for next slices
Definition: sar.hpp:223
bool opt_warn_overwrite
a warning must be issued before overwriting a slice
Definition: sar.hpp:218
const infinint & get_first_slice_header_size() const
get the first slice header
Definition: sar.hpp:178
infinint of_last_file_num
number of the last slice (if met)
Definition: sar.hpp:208
fichier_global * of_fd
file object currently openned
Definition: sar.hpp:214
virtual const label & get_data_name() const override
obtain the data_name of the archive (label associated with the archive's data)
Definition: sar.hpp:173
infinint pause
do we pause between slices
Definition: sar.hpp:221
bool is_current_eof_a_normal_end_of_slice() const
return true if current reading position is at end of slice
bool initial
do not launch hook command-line during sar initialization
Definition: sar.hpp:216
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
std::shared_ptr< entrepot > entr
where are stored slices
Definition: sar.hpp:193
virtual bool is_an_old_start_end_archive() const override
returns whether the archive is a old archive (format < 8)
Definition: sar.hpp:167
bool opt_allow_overwrite
is slice overwriting allowed
Definition: sar.hpp:219
sar(const std::shared_ptr< user_interaction > &dialog, gf_mode open_mode, const std::string &base_name, const std::string &extension, const infinint &file_size, const infinint &first_file_size, bool x_warn_overwrite, bool x_allow_overwrite, const infinint &pause, const std::shared_ptr< entrepot > &where, const label &internal_name, const label &data_name, bool force_permission, U_I permission, hash_algo x_hash, const infinint &x_min_digits, bool format_07_compatible, const std::string &execute="")
this constructor creates a new set of slices
virtual void inherited_read_ahead(const infinint &amount) override
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
void open_writeonly(const std::string &fic, const infinint &num, bool bytheend)
void set_offset(infinint offset)
skip to current slice relative offset
bool lax
whether to try to go further reading problems
Definition: sar.hpp:222
~sar()
destructor
virtual bool truncatable(const infinint &pos) const override
whether the implementation is able to truncate to the given position
std::string base
archive base name
Definition: sar.hpp:194
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
bool force_perm
true if any future slice has its permission to be set explicitely
Definition: sar.hpp:212
label of_internal_name
internal name shared in all slice header
Definition: sar.hpp:210
char of_flag
flags of the open file
Definition: sar.hpp:215
bool of_last_file_known
whether the T terminal slice has been met
Definition: sar.hpp:207
infinint of_current
number of the open slice
Definition: sar.hpp:204
hash_algo hash
whether to build a hashing when creating slices, and if so, which algorithm to use
Definition: sar.hpp:199
sar & operator=(const sar &ref)=delete
assignment operator
void close_file(bool terminal)
close current openned file, adding (in write mode only) a terminal mark (last slice) or not
bool skip_forward(U_I x)
skip forward in sar global contents
virtual bool skip(const infinint &pos) override
skip at the absolute position
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
Definition: sar.hpp:189
sar(sar &&ref) noexcept=delete
move constructor
sar(const std::shared_ptr< user_interaction > &dialog, const std::string &base_name, const std::string &extension, const std::shared_ptr< entrepot > &where, bool by_the_end, const infinint &x_min_digits, bool lax=false, const std::string &execute="")
this constructor reads data from a set of slices
infinint bytes_still_to_read_in_slice() const
returns the number of bytes expected before the end of slice
infinint min_digits
minimum number of digits the slices number is stored with in the filename
Definition: sar.hpp:200
virtual bool skip_relative(S_I x) override
skip relatively to the current position
void open_readonly(const std::string &fic, const infinint &num, bool bytheend)
U_I perm
if force_perm is true, value to use for slice permission
Definition: sar.hpp:213
sar(const sar &ref)=delete
the copy constructor
virtual bool skip_to_eof() override
skip to the end of file
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
infinint of_max_seen
highest slice number seen so far
Definition: sar.hpp:206
virtual infinint get_position() const override
get the current read/write position
void open_file_init()
initialize some of_* fields
virtual void inherited_sync_write() override
write down any pending data
Definition: sar.hpp:188
infinint of_last_file_size
size of the last slice (if met)
Definition: sar.hpp:209
infinint file_offset
current reading/writing position in the current slice (relative to the whole slice file,...
Definition: sar.hpp:198
const infinint & get_non_first_slice_header_size() const
get the non first slice header
Definition: sar.hpp:181
bool natural_destruction
whether to execute commands between slices on object destruction
Definition: sar.hpp:201
void open_last_file(bool bytheend)
open the last slice, ask the user, test, until last slice available
std::string ext
archive extension
Definition: sar.hpp:195
slice_layout slicing
slice layout
Definition: sar.hpp:197
class contextual adds the information of phases in the generic_file
defines the entrepot interface. Entrepot interface defines a generic way to interact with files (slic...
class generic_file is defined here as well as class fichier
hash_algo
hashing algorithm available
Definition: archive_aux.hpp:63
gf_mode
generic_file openning modes
Definition: gf_mode.hpp:44
slice header structure is defined here
switch module to limitint (32 ou 64 bits integers) or infinint
are defined here basic integer types that tend to be portable
class mem_ui definition. This class is to be used as parent class to handle user_interaction object m...
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
object describing the slicing of an archive