Loading...
Searching...
No Matches
sparse_file.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2026 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef SPARSE_FILE_HPP
#define SPARSE_FILE_HPP
#include "../my_config.h"
extern "C"
{
#if HAVE_LIMITS_H
#include <limits.h>
#endif
}
#include "generic_file.hpp"
#include "escape.hpp"
#define SPARSE_FIXED_ZEROED_BLOCK 40960
#ifdef SSIZE_MAX
#if SSIZE_MAX < MAX_BUFFER_SIZE
#undef MAX_BUFFER_SIZE
#define SPARSE_FIXED_ZEROED_BLOCK SSIZE_MAX
#endif
#endif
namespace libdar
{
class sparse_file : public escape
{
public:
sparse_file(generic_file *below, const infinint & hole_size = 15);
sparse_file(const sparse_file & ref) = default;
sparse_file(sparse_file && ref) noexcept = default;
sparse_file & operator = (const sparse_file & ref) = default;
sparse_file & operator = (sparse_file && ref) noexcept = default;
~sparse_file() = default;
void write_as_escape(bool mode) { escape_write = mode; };
void read_as_escape(bool mode) { escape_read = mode; };
void copy_to_without_skip(bool mode) { copy_to_no_skip = mode; };
bool has_seen_hole() const { return seen_hole; };
bool has_escaped_data() const { return data_escaped; };
virtual void copy_to(generic_file & ref) override { crc *tmp = nullptr; copy_to(ref, 0, tmp); if(tmp != nullptr) throw SRC_BUG; };
virtual void copy_to(generic_file & ref, const infinint & crc_size, crc * & value) override;
// indirectly inherited from generic_file
virtual bool skippable(skippability direction, const infinint & amount) override { return false; };
virtual bool skip(const infinint & pos) override { if(pos != offset) throw Efeature("skip in sparse_file"); else return true; };
virtual bool skip_to_eof() override { throw Efeature("skip in sparse_file"); };
virtual bool skip_relative(S_I x) override { if(x != 0) throw Efeature("skip in sparse_file"); return true; };
virtual infinint get_position() const override;
protected:
// methods from the escape class we hide from the (public) class interface
void add_mark_at_current_position(sequence_type t) { escape::add_mark_at_current_position(t); };
bool skip_to_next_mark(sequence_type t, bool jump) { return escape::skip_to_next_mark(t, jump); };
bool next_to_read_is_mark(sequence_type t) { return escape::next_to_read_is_mark(t); };
void add_unjumpable_mark(sequence_type t) { escape::add_unjumpable_mark(t); };
// methods from generic_file redefined as protected
virtual U_I inherited_read(char *a, U_I size) override;
virtual void inherited_write(const char *a, U_I size) override;
virtual void inherited_sync_write() override;
// inherited_flush_read() kept as is from the escape class
// inherited_terminate() kept as is from the escape class
private:
static bool initialized;
static unsigned char zeroed_field[SPARSE_FIXED_ZEROED_BLOCK];
enum { normal, hole } mode;
infinint zero_count;
infinint offset;
infinint min_hole_size;
U_I UI_min_hole_size;
bool escape_write;
bool escape_read;
bool copy_to_no_skip;
bool seen_hole;
bool data_escaped;
void dump_pending_zeros();
void write_hole(const infinint & length);
void reset();
static bool look_for_hole(const char *a, U_I size, U_I min_hole_size, U_I & start, U_I & length);
static U_I count_initial_zeros(const char *a, U_I size);
};
} // end of namespace
#endif
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 generic_file.hpp:80
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
escape.hpp
class escape definition, used for sequential reading of archives
generic_file.hpp
class generic_file is defined here as well as class fichier
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47