Disk ARchive 2.8.0
Full featured and portable backup and archiving tool
hash_fichier.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
29
30#ifndef HASH_FICHIER_HPP
31#define HASH_FICHIER_HPP
32
33#include "../my_config.h"
34
35extern "C"
36{
37#if HAVE_GCRYPT_H
38#ifndef GCRYPT_NO_DEPRECATED
39#define GCRYPT_NO_DEPRECATED
40#endif
41#include <gcrypt.h>
42#endif
43
44#if HAVE_RHASH_H
45#include <rhash.h>
46#endif
47}
48
49#include <string>
50
51#include "fichier_global.hpp"
52#include "integers.hpp"
53#include "archive_aux.hpp"
54
55namespace libdar
56{
57
60
62
64 {
65 public:
66
75
76 hash_fichier(const std::shared_ptr<user_interaction> & dialog,
77 fichier_global *under,
78 const std::string & under_filename,
79 fichier_global *hash_file,
80 hash_algo algo);
81
83 hash_fichier(const hash_fichier & ref) = delete;
84
86 hash_fichier(hash_fichier && ref) noexcept = delete;
87
89 hash_fichier & operator = (const hash_fichier & ref) = delete;
90
92 hash_fichier & operator = (hash_fichier && ref) noexcept = delete;
93
96
97 // inherited from fichier_global
98 virtual void change_ownership(const std::string & user, const std::string & group) override { if(ref == nullptr || hash_ref == nullptr) throw SRC_BUG; ref->change_ownership(user, group); hash_ref->change_ownership(user, group); };
99 virtual void change_permission(U_I perm) override { if(ref == nullptr || hash_ref == nullptr) throw SRC_BUG; ref->change_permission(perm); hash_ref->change_permission(perm); };
100 virtual infinint get_size() const override { if(ref == nullptr) throw SRC_BUG; return ref->get_size(); };
101 virtual void fadvise(advise adv) const override { if(ref == nullptr) throw SRC_BUG; ref->fadvise(adv); };
102
103 // inherited from generic_file
104 virtual bool skippable(skippability direction, const infinint & amount) override { return false; };
105 virtual bool skip(const infinint & pos) override {if(ref == nullptr || pos != ref->get_position()) throw SRC_BUG; else return true; };
106 virtual bool skip_to_eof() override { if(get_mode() == gf_write_only) return true; else throw SRC_BUG; };
107 virtual bool skip_relative(S_I x) override { if(x != 0) throw SRC_BUG; else return true; };
108 virtual bool truncatable(const infinint & pos) const override { return false; };
109 virtual infinint get_position() const override { if(ref == nullptr) throw SRC_BUG; return ref->get_position(); };
110
112 void set_only_hash() { only_hash = true; };
113
114 protected:
115 // inherited from fichier_global
116 virtual void inherited_read_ahead(const infinint & amount) override { ref->read_ahead(amount); };
117 virtual U_I fichier_global_inherited_write(const char *a, U_I size) override;
118 virtual bool fichier_global_inherited_read(char *a, U_I size, U_I & read, std::string & message) override;
119
120 // inherited from generic_file
121 virtual void inherited_truncate(const infinint & pos) override { throw SRC_BUG; }; // truncate not supported on hash files
122 virtual void inherited_sync_write() override {};
123 virtual void inherited_flush_read() override {};
124 virtual void inherited_terminate() override;
125
126 private:
127 fichier_global *ref;
128 fichier_global *hash_ref;
130#if CRYPTO_AVAILABLE
131 gcry_md_hd_t hash_handle;
132#endif
133#if RHASH_AVAILABLE
134 rhash rhash_ctxt;
135#endif
136 std::string ref_filename;
137 U_I hash_gcrypt;
138 bool eof;
139 bool hash_dumped;
140 hash_algo algor;
141
142 void write_hash_in_hexa(void const* digest, U_I digest_size);
143 };
144
146
147} // end of namespace
148
149
150#endif
set of datastructures used to interact with a catalogue object
abstraction of filesystem files for entrepot
virtual infinint get_size() const =0
return the size of the file
virtual void change_permission(U_I perm)=0
change the permission of the file
virtual void fadvise(advise adv) const =0
set posix_fadvise for the whole file
virtual void change_ownership(const std::string &user, const std::string &group)=0
set the ownership of the file
void read_ahead(const infinint &amount)
virtual infinint get_position() const =0
get the current read/write position
gf_mode get_mode() const
retreive the openning mode for this object
virtual U_I read(char *a, U_I size) override
read data from the generic_file inherited from proto_generic_file
manages the generation of a hash
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...
~hash_fichier()
destructor
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
hash_fichier(const hash_fichier &ref)=delete
copy constructor
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
virtual void change_ownership(const std::string &user, const std::string &group) override
set the ownership of the file
virtual infinint get_position() const override
get the current read/write position
virtual infinint get_size() const override
return the size of the file
virtual bool truncatable(const infinint &pos) const override
whether the implementation is able to truncate to the given position
virtual bool skip_to_eof() override
skip to the end of file
virtual bool skip_relative(S_I x) override
skip relatively to the current position
virtual bool skip(const infinint &pos) override
skip at the absolute position
virtual bool fichier_global_inherited_read(char *a, U_I size, U_I &read, std::string &message) override
replaces generic_file::inherited_read() method, to allow the return of partial reading
hash_fichier(const std::shared_ptr< user_interaction > &dialog, fichier_global *under, const std::string &under_filename, fichier_global *hash_file, hash_algo algo)
void set_only_hash()
for debugging purposes only
virtual void change_permission(U_I perm) override
change the permission of the file
virtual void inherited_sync_write() override
write down any pending data
virtual U_I fichier_global_inherited_write(const char *a, U_I size) override
replaces generic_file::inherited_write() method, to allow the return of partial writings
hash_fichier & operator=(const hash_fichier &ref)=delete
assignment operator
virtual void fadvise(advise adv) const override
set posix_fadvise for the whole file
bool only_hash
if set, avoids copying data to file, only compute hash (debugging purpose)
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
hash_fichier(hash_fichier &&ref) noexcept=delete
move constructor
the arbitrary large positive integer class
class fichier_global definition. This class is a pure virtual class class fichier_global is an abstra...
hash_algo
hashing algorithm available
Definition: archive_aux.hpp:63
@ gf_write_only
write only access
Definition: gf_mode.hpp:46
are defined here basic integer types that tend to be portable
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47