File: test_libdar.cpp

package info (click to toggle)
dar 2.6.13-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 10,364 kB
  • sloc: cpp: 77,385; sh: 6,192; ansic: 776; makefile: 435; python: 242; csh: 95; perl: 43; sed: 16
file content (273 lines) | stat: -rw-r--r-- 7,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2020 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 : http://dar.linux.free.fr/email.html
/*********************************************************************/

#include "../my_config.h"

extern "C"
{
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if MUTEX_WORKS
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#endif
} // end extern "C"

#include "libdar.hpp"
#include "tools.hpp"
#include "null_file.hpp"
#include "shell_interaction.hpp"

using namespace std;
using namespace libdar;

void f1();
void warning(const string &x, void *context);
bool question(const string &x, void *context);
string getstring(const string &x, bool echo, void *context);
secu_string getsecustring(const string &x, bool echo, void *context);
void f2();
void f3();
void f4();
void f5();

static shared_ptr<user_interaction> ui(new user_interaction_callback(warning, question, getstring, getsecustring,(void *)1000));

int main()
{
    try
    {
	f1();
	f2();
	f3();
	f4();
	f5();
    }
    catch(Egeneric & e)
    {
	cout << e.dump_str() << endl;
    }
}

#define BOOL2STR(val) ( val ? "yes" : "no" )

void f1()
{
    U_I maj, med, min;
    bool ea, large, nodump, special, thread, libz, libbz2, liblzo2, libcrypto, furtive;
    U_I bits;
    libdar::compile_time::endian endy;

    get_version(maj, med, min);
    ui->printf("version %u.%u.%u\n", maj, med, min);
    ea = libdar::compile_time::ea();
    large = libdar::compile_time::largefile();
    nodump = libdar::compile_time::nodump();
    special = libdar::compile_time::special_alloc();
    bits = libdar::compile_time::bits();
    thread = libdar::compile_time::thread_safe();
    libz = libdar::compile_time::libz();
    libbz2 = libdar::compile_time::libbz2();
    liblzo2 = libdar::compile_time::liblzo();
    libcrypto = libdar::compile_time::libgcrypt();
    furtive = libdar::compile_time::furtive_read();
    endy = libdar::compile_time::system_endian();
    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",
	      BOOL2STR(ea),
	      BOOL2STR(large),
	      BOOL2STR(nodump),
	      BOOL2STR(special),
	      bits,
	      BOOL2STR(thread),
	      BOOL2STR(libz),
	      BOOL2STR(libbz2),
	      BOOL2STR(liblzo2),
	      BOOL2STR(libcrypto),
	      BOOL2STR(furtive),
	      endy);
}

void warning(const string &x, void *context)
{
    cout << "[" << context << "]" << x.c_str() << endl;
}

bool question(const string & x, void *context)
{
    bool rep = false;
    char r;

    printf("[%p]%s\n", context, x.c_str());
    scanf("%c", &r);
    rep = r == 'y';

    return rep;
}

string getstring(const string &x, bool echo, void *context)
{
    throw SRC_BUG;
}

void listing_callback(const string & the_path, const list_entry & entry, void *context)
{
    cout << the_path << " type = " << entry.get_type() << " context = " << (U_I)context << endl;
}

secu_string getsecustring(const string &x, bool echo, void *context)
{
    throw SRC_BUG;
}

void 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)
{
    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");
}

void f2()
{
    statistics st;
    archive_options_read read_options;
    archive_options_create create_options;

    create_options.set_subtree(simple_path_mask(path("/etc"), true));
    create_options.set_warn_over(false);
    archive *toto = nullptr;

    try
    {
	toto = new (nothrow) archive(ui,
				     path("/"),
				     path("."),
				     "toto",
				     "dar",
				     create_options,
				     &st);
	if(toto != nullptr)
	{
	    archive_options_listing_shell options;

	    options.clear();
	    options.set_info_details(true);
	    options.set_list_mode(archive_options_listing_shell::normal);
	    options.set_selection(bool_mask(true));
	    options.set_filter_unsaved(false);
	    toto->op_listing(listing_callback, (void*)1, options);
	}
    }
    catch(...)
    {
	if(toto != nullptr)
	    delete toto;
	throw;
    }
    if(toto != nullptr)
    {
	delete toto;
	toto = nullptr;
    }

    read_options.clear();
    read_options.set_info_details(true);
    archive *arch = new (nothrow) archive(ui, path("."), "toto", "dar", read_options);
    if(arch != nullptr)
    {
	vector<list_entry> liste = arch->get_children_in_table("");
	delete arch;
	arch = nullptr;
    }
}

void f3()
{
    archive_options_read read_options;

	// need to create an archive named "titi" with file recorded as removed since reference backup
    read_options.clear();
    read_options.set_info_details(true);
    archive *arch = new (nothrow) archive(ui, path("."), "toto", "dar", read_options);
    if(arch != nullptr)
    {
	arch->init_catalogue();
	vector<list_entry> contents = arch->get_children_in_table("etc");
	vector<list_entry>::iterator it = contents.begin();
	while(it != contents.end())
	{
	    string line = it->get_name() + " " + (it->has_data_present_in_the_archive() ? "SAVED" : "not saved") + "\n";
	    cout << line;
	    ++it;
	}
	delete arch;
	arch = nullptr;
    }
}

void f4()
{
#if MUTEX_WORKS
    try
    {
	pthread_t tid = pthread_self();
	pthread_t tod = 0;
	bool ret = cancel_status(tod);
	cancel_clear(tid);
	cancel_thread(tid);
 	ret = cancel_status(tod);
	cancel_clear(tod);
	ret = cancel_status(tod);
	null_file fake = null_file(gf_read_write);
	fake.write("coucouc les amsi", 10);
	cancel_thread(tid);
	fake.write("coucouc les amsi", 10);
	ui->printf("this statement should never be reached\n");
	ret = ret+1; // avoid warning of unused variable
    }
    catch(Egeneric & e)
    {
	cout << "Exception caught: " << e.get_message() << endl;
    }
    catch(...)
    {
	ui->printf("unknown expcetion caught\n");
    }
#endif
}

void f5()
{
    string ret;

    ret = ui->get_string("Mot de passe svp :", false);
    cout << "---[" << ret << "]---" << endl;
    ret = ui->get_string("Mot de passe svp :", true);
    cout << "---[" << ret << "]---" << endl;
}