1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21
22 #include "../my_config.h"
23
24 extern "C"
25 {
26 #if HAVE_STDIO_H
27 #include <stdio.h>
28 #endif
29 #if MUTEX_WORKS
30 #if HAVE_PTHREAD_H
31 #include <pthread.h>
32 #endif
33 #endif
34 } // end extern "C"
35
36 #include "libdar.hpp"
37 #include "tools.hpp"
38 #include "null_file.hpp"
39 #include "shell_interaction.hpp"
40
41 using namespace std;
42 using namespace libdar;
43
44 void f1();
45 void warning(const string &x, void *context);
46 bool question(const string &x, void *context);
47 string getstring(const string &x, bool echo, void *context);
48 secu_string getsecustring(const string &x, bool echo, void *context);
49 void f2();
50 void f3();
51 void f4();
52 void f5();
53
54 static user_interaction_callback ui = user_interaction_callback(warning, question, getstring, getsecustring,(void *)1000);
55
main()56 int main()
57 {
58 f1();
59 f2();
60 f3();
61 f4();
62 f5();
63 }
64
65 #define BOOL2STR(val) ( val ? "yes" : "no" )
66
f1()67 void f1()
68 {
69 U_I maj, med, min;
70 bool ea, large, nodump, special, thread, libz, libbz2, liblzo2, libcrypto, furtive;
71 U_I bits;
72 libdar::compile_time::endian endy;
73
74 get_version(maj, med, min);
75 ui.printf("version %u.%u.%u\n", maj, med, min);
76 ea = libdar::compile_time::ea();
77 large = libdar::compile_time::largefile();
78 nodump = libdar::compile_time::nodump();
79 special = libdar::compile_time::special_alloc();
80 bits = libdar::compile_time::bits();
81 thread = libdar::compile_time::thread_safe();
82 libz = libdar::compile_time::libz();
83 libbz2 = libdar::compile_time::libbz2();
84 liblzo2 = libdar::compile_time::liblzo();
85 libcrypto = libdar::compile_time::libgcrypt();
86 furtive = libdar::compile_time::furtive_read();
87 endy = libdar::compile_time::system_endian();
88 ui.printf("features:\nEA = %s\nLARGE = %s\nNODUMP = %s\nSPECIAL = %s\nbits = %u\nthread = %s\nlibz =%s\nlibbz2 = %s\nliblzo = %s\nlibcrypto = %s\nfurtive = %s\nendian = %c\n",
89 BOOL2STR(ea),
90 BOOL2STR(large),
91 BOOL2STR(nodump),
92 BOOL2STR(special),
93 bits,
94 BOOL2STR(thread),
95 BOOL2STR(libz),
96 BOOL2STR(libbz2),
97 BOOL2STR(liblzo2),
98 BOOL2STR(libcrypto),
99 BOOL2STR(furtive),
100 endy);
101 }
102
warning(const string & x,void * context)103 void warning(const string &x, void *context)
104 {
105 cout << "[" << context << "]" << x.c_str() << endl;
106 }
107
question(const string & x,void * context)108 bool question(const string & x, void *context)
109 {
110 bool rep = false;
111 char r;
112
113 printf("[%p]%s\n", context, x.c_str());
114 scanf("%c", &r);
115 rep = r == 'y';
116
117 return rep;
118 }
119
getstring(const string & x,bool echo,void * context)120 string getstring(const string &x, bool echo, void *context)
121 {
122 throw SRC_BUG;
123 }
124
getsecustring(const string & x,bool echo,void * context)125 secu_string getsecustring(const string &x, bool echo, void *context)
126 {
127 throw SRC_BUG;
128 }
129
listing(const std::string & flag,const std::string & perm,const std::string & uid,const std::string & gid,const std::string & size,const std::string & date,const std::string & filename,bool is_dir,bool has_children,void * context)130 void listing(const std::string & flag,
131 const std::string & perm,
132 const std::string & uid,
133 const std::string & gid,
134 const std::string & size,
135 const std::string & date,
136 const std::string & filename,
137 bool is_dir,
138 bool has_children,
139 void *context)
140 {
141 ui.printf("[[%p]][%S][%S][%S][%S][%S][%S][%S][%s][%s]\n", context, &flag, &perm, &uid, &gid, &size, &date, &filename, is_dir ? "dir" : "not_dir", has_children ? "has children" : "no children");
142 }
143
f2()144 void f2()
145 {
146 U_16 code;
147 string msg;
148 statistics st;
149 archive_options_read read_options;
150 archive_options_create create_options;
151
152 create_options.set_subtree(simple_path_mask("/etc", true));
153 archive *toto = create_archive_noexcept(ui,
154 "/",
155 ".",
156 "toto",
157 "dar",
158 create_options,
159 &st,
160 code,
161 msg);
162 if(code != LIBDAR_NOEXCEPT && code != LIBDAR_EUSER_ABORT)
163 {
164 ui.printf("exception creating archive: %S\n", &msg);
165 return;
166 }
167 if(toto != nullptr)
168 {
169 archive_options_listing options;
170
171 options.clear();
172 options.set_info_details(true);
173 options.set_list_mode(archive_options_listing::normal);
174 options.set_selection(bool_mask(true));
175 options.set_filter_unsaved(false);
176 op_listing_noexcept(ui, toto, options, code, msg);
177 if(code != LIBDAR_NOEXCEPT && code != LIBDAR_EUSER_ABORT)
178 {
179 ui.printf("exception creating archive: %S\n", &msg);
180 }
181 close_archive_noexcept(toto, code, msg);
182 }
183
184 read_options.clear();
185 read_options.set_info_details(true);
186 archive *arch = open_archive_noexcept(ui, ".", "toto", "dar", read_options, code, msg);
187 if(code != LIBDAR_NOEXCEPT)
188 {
189 ui.printf("exception openning archive: %S\n", &msg);
190 return;
191 }
192
193 ui.set_listing_callback(&listing);
194 bool ret = get_children_of_noexcept(ui, arch, "etc/rc.d", code, msg);
195 if(code != LIBDAR_NOEXCEPT)
196 {
197 ui.printf("exception looking for children: %S\n", &msg);
198 return;
199 }
200 if(ret)
201 ui.printf("found children\n");
202 else
203 ui.printf("no found children\n");
204
205 ret = get_children_of_noexcept(ui, arch, "", code, msg);
206 if(code != LIBDAR_NOEXCEPT)
207 {
208 ui.printf("exception looking for children of root: %S\n", &msg);
209 return;
210 }
211 if(ret)
212 ui.printf("found children\n");
213 else
214 ui.printf("no found children\n");
215
216
217 close_archive_noexcept(arch, code, msg);
218 if(code != LIBDAR_NOEXCEPT)
219 {
220 ui.printf("exception closing: %S\n", &msg);
221 return;
222 }
223 }
224
f3()225 void f3()
226 {
227 archive_options_read read_options;
228
229 // need to create an archive named "titi" with file recorded as removed since reference backup
230 U_16 code;
231 string msg;
232 read_options.clear();
233 read_options.set_info_details(true);
234 archive *arch = open_archive_noexcept(ui, ".", "toto", "dar", read_options, code, msg);
235 if(code != LIBDAR_NOEXCEPT)
236 {
237 ui.printf("exception openning archive: %S\n", &msg);
238 return;
239 }
240
241 ui.set_listing_callback(&listing);
242 bool ret = get_children_of_noexcept(ui, arch, "etc/rc.d", code, msg);
243 if(code != LIBDAR_NOEXCEPT)
244 {
245 ui.printf("exception looking for children: %S\n", &msg);
246 return;
247 }
248 if(ret)
249 ui.printf("found children\n");
250 else
251 ui.printf("no found children\n");
252
253 arch->init_catalogue(ui);
254 vector<list_entry> contents = arch->get_children_in_table("etc");
255 vector<list_entry>::iterator it = contents.begin();
256 while(it != contents.end())
257 {
258 string line = it->get_name() + " " + (it->has_data_present_in_the_archive() ? "SAVED" : "not saved") + "\n";
259 cout << line;
260 ++it;
261 }
262
263 close_archive_noexcept(arch, code, msg);
264 if(code != LIBDAR_NOEXCEPT)
265 {
266 ui.printf("exception closing: %S\n", &msg);
267 return;
268 }
269
270
271 }
272
f4()273 void f4()
274 {
275 #if MUTEX_WORKS
276 try
277 {
278 pthread_t tid = pthread_self();
279 pthread_t tod = 0;
280 bool ret = cancel_status(tod);
281 cancel_clear(tid);
282 cancel_thread(tid);
283 ret = cancel_status(tod);
284 cancel_clear(tod);
285 ret = cancel_status(tod);
286 null_file fake = null_file(gf_read_write);
287 fake.write("coucouc les amsi", 10);
288 cancel_thread(tid);
289 fake.write("coucouc les amsi", 10);
290 ui.printf("this statement should never be reached\n");
291 ret = ret+1; // avoid warning of unused variable
292 }
293 catch(Egeneric & e)
294 {
295 cout << "Exception caught: " << e.get_message() << endl;
296 }
297 catch(...)
298 {
299 ui.printf("unknown expcetion caught\n");
300 }
301 #endif
302 }
303
f5()304 void f5()
305 {
306 string ret;
307
308 ret = ui.get_string("Mot de passe svp :", false);
309 cout << "---[" << ret << "]---" << endl;
310 ret = ui.get_string("Mot de passe svp :", true);
311 cout << "---[" << ret << "]---" << endl;
312 }
313