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

fichier_libcurl.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2020 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 : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21
26
27 #ifndef FICHIER_LIBCURL_HPP
28 #define FICHIER_LIBCURL_HPP
29
30
31 #include "../my_config.h"
32
33 extern "C"
34 {
35 #if LIBCURL_AVAILABLE
36 #if HAVE_CURL_CURL_H
37 #include <curl/curl.h>
38 #endif
39 #endif
40 } // end extern "C"
41
42 #include <string>
43 #ifdef LIBTHREADAR_AVAILABLE
44 #include <libthreadar/libthreadar.hpp>
45 #endif
46 #include "integers.hpp"
47 #include "user_interaction.hpp"
48 #include "fichier_global.hpp"
49 #include "mycurl_protocol.hpp"
50 #include "mycurl_shared_handle.hpp"
51
52 namespace libdar
53 {
54
57
58 #if defined ( LIBCURL_AVAILABLE ) && defined ( LIBTHREADAR_AVAILABLE )
59
61
62 class fichier_libcurl : public fichier_global, protected libthreadar::thread
63 {
64 public:
65
67 fichier_libcurl(const std::shared_ptr<user_interaction> & dialog, //< for user interaction requested by fichier_global
68 const std::string & chemin, //< full path of the file to open
69 mycurl_protocol proto, //< to workaround some libcurl strange behavior for some protocols
70 mycurl_shared_handle && handle, //< the easy handle wrapper object
71 gf_mode m, //< open mode
72 U_I waiting, //< retry timeout in case of network error
73 bool force_permission, //< whether file permission should be modified
74 U_I permission, //< file permission to enforce if force_permission is set
75 bool erase); //< whether to erase the file before writing to it
76
78
80 fichier_libcurl(const fichier_libcurl & ref) = delete;
81
83 fichier_libcurl(fichier_libcurl && ref) = delete;
84
87 fichier_libcurl & operator = (const fichier_libcurl & ref) = delete;
88
90 fichier_libcurl & operator = (fichier_libcurl && ref) = delete;
91
93 ~fichier_libcurl() noexcept { detruit(); };
94
96 virtual void change_permission(U_I perm) override;
97
99 virtual void change_ownership(const std::string & user, const std::string & group) override
100 { throw Efeature(gettext("user/group ownership not supported for this repository")); }; // not supported
101
103 virtual infinint get_size() const override;
104
106 virtual void fadvise(advise adv) const override {}; // not supported and ignored
107
108 // inherited from generic_file
109 virtual bool skippable(skippability direction, const infinint & amount) override;
110 virtual bool skip(const infinint & pos) override;
111 virtual bool skip_to_eof() override;
112 virtual bool skip_relative(S_I x) override;
113 virtual infinint get_position() const override { return current_offset; };
114
115 protected:
116 // inherited from generic_file grand-parent class
117 virtual void inherited_read_ahead(const infinint & amount) override;
118 virtual void inherited_sync_write() override;
119 virtual void inherited_flush_read() override;
120 virtual void inherited_terminate() override;
121
122 // inherited from fichier_global parent class
123 virtual U_I fichier_global_inherited_write(const char *a, U_I size) override;
124 virtual bool fichier_global_inherited_read(char *a, U_I size, U_I & read, std::string & message) override;
125
126 // inherited from thread
127 virtual void inherited_run() override;
128
129 private:
130 static const U_I tampon_size = CURL_MAX_WRITE_SIZE;
131
133 //
134 // implementation internals
135 //
137 // the object has two modes:
138 // - meta data mode (skip, get_position() and other non read/write operations)
139 // - data mode (read or write operations)
140 //
141 // in metadata mode each method is a simple code execution (no subthread, no callback)
142 //
143 // in data mode, a subthread is used to interact with libcurl. It sends or receives
144 // data through the interthread pipe. A callback is occasionally run by libcurl in this
145 // subthread.
146 // in read mode, the subthread is run only if the interthread is empty. the subthread may
147 // survive the inherited_read call and may suspend on writing data to interthread being full
148 // - "subthread_net_offset" is updated by the callback and read by the subthread when
149 // libcurl has returned it keeps trace of the amount of data sent to interthread.
150 // - "network_block" is set by the main thread to define the amount of data to be fetched. It
151 // it used to setup libcurl and is read by the subthread for control/validation purposes
152
153 bool end_data_mode;
154 bool sub_is_dying;
155 mycurl_shared_handle ehandle;
156 bool metadatamode;
157 infinint current_offset;
158 bool has_maxpos;
159 infinint maxpos;
160 bool append_write;
161 char meta_tampon[tampon_size];
162 U_I meta_inbuf;
163 U_I wait_delay;
164 infinint network_block;
165 infinint subthread_net_offset;
166 infinint subthread_cur_offset;
167 libthreadar::fast_tampon<char> interthread;
168 libthreadar::barrier synchronize;
169 mycurl_protocol x_proto;
170
171 void set_range(const infinint & begin, const infinint & range_size);
172 void unset_range();
173 void switch_to_metadata(bool mode);
174 void detruit();
175 void run_thread();
176 void stop_thread();
177 void relaunch_thread(const infinint & block_size);
178 void initialize_subthread();
179 void finalize_subthread();
180 void set_subthread(U_I & needed_bytes);
181
182 static size_t write_data_callback(char *buffer, size_t size, size_t nmemb, void *userp);
183 static size_t read_data_callback(char *bufptr, size_t size, size_t nitems, void *userp);
184 static size_t write_meta_callback(char *buffer, size_t size, size_t nmemb, void *userp);
185 static size_t read_meta_callback(char *bufptr, size_t size, size_t nitems, void *userp);
186 };
187
190
195 extern void fichier_libcurl_check_wait_or_throw(const std::shared_ptr<user_interaction> & dialog,
196 CURLcode err,
197 U_I wait_seconds,
198 const std::string & err_context);
199
200 #endif
201
203 } // end of namespace
204
205 #endif
user_interaction.hpp
defines the interaction interface between libdar and users.
integers.hpp
are defined here basic integer types that tend to be portable
libdar::gf_mode
gf_mode
generic_file openning modes
Definition: gf_mode.hpp:44
mycurl_protocol.hpp
datastructure defining the network protocols available for entrepot_libcurl class
mycurl_shared_handle.hpp
structure used to optimize network session creation done by libcurl
fichier_global.hpp
class fichier_global definition. This class is a pure virtual class class fichier_global is an abstra...
libdar::mycurl_protocol
mycurl_protocol
libcurl protocols supported by libdar
Definition: mycurl_protocol.hpp:49
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47