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

tools.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
25
26
27 #ifndef TOOLS_HPP
28 #define TOOLS_HPP
29
30 #include "../my_config.h"
31
32 extern "C"
33 {
34 #if STDC_HEADERS
35 #include <stdarg.h>
36 #endif
37 #if HAVE_SIGNAL_H
38 #include <signal.h>
39 #endif
40 #if HAVE_GPGME_H
41 #include <gpgme.h>
42 #endif
43 }
44
45 #include <string>
46 #include <vector>
47 #include <deque>
48 #include <map>
49 #include <set>
50 #include "path.hpp"
51 #include "infinint.hpp"
52 #include "generic_file.hpp"
53 #include "integers.hpp"
54 #include "tlv_list.hpp"
55 #include "datetime.hpp"
56 #include "entrepot.hpp"
57
58 namespace libdar
59 {
60
63
64
66 extern void tools_init();
68 extern void tools_end();
69
71
74 extern void tools_write_string(generic_file & f, const std::string & s);
75
77
80 extern void tools_read_string(generic_file & f, std::string & s);
81
83
86 extern void tools_write_string_all(generic_file & f, const std::string & s);
87
89
93 extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
94
96
99 extern infinint tools_get_filesize(const path &p);
100
106 extern std::string tools_display_integer_in_metric_system(infinint number, const std::string & unit, bool binary);
107
109
115 extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
116
118
121 extern void tools_blocking_read(int fd, bool mode);
122
124
127 extern std::string tools_name_of_uid(const infinint & uid);
128
130
133 extern std::string tools_name_of_gid(const infinint & gid);
134
136
139 extern std::string tools_uword2str(U_16 x);
140
142
145 extern std::string tools_int2str(S_I x);
146 extern std::string tools_uint2str(U_I x);
147
149
153 extern std::string tools_addspacebefore(std::string s, U_I expected_size);
154
156
159 extern std::string tools_display_date(const datetime & date);
160
162
167 extern char *tools_str2charptr(const std::string &x);
168
170
173 extern U_I tools_str2int(const std::string & x);
174
176
179 extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
180
182
188 extern void tools_system_with_pipe(const std::shared_ptr<user_interaction> & dialog,
189 const std::string & dar_cmd,
190 const std::vector<std::string> & argvpipe);
191
193
196 extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
197
199
202 extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
203
205
209 extern std::string tools_concat_vector(const std::string & separator,
210 const std::vector<std::string> & x);
211
213
217 std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
218
219
221
226 extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const datetime & date1, const datetime & date2);
227
229
230 template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
231 {
232 a = a + b;
233 return a;
234 }
235
236
237
239
243 extern std::string tools_readlink(const char *root);
244
246
252 extern void tools_noexcept_make_date(const std::string & chem, bool symlink, const datetime & last_acc, const datetime & last_mod, const datetime & birth);
253
255
262 extern void tools_make_date(const std::string & chemin, bool symlink, const datetime & access, const datetime & modif, const datetime & birth);
263
265
269 extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
270
277 extern void tools_to_upper(const std::string & r, std::string & uppered);
278
279 #if HAVE_WCTYPE_H
280 extern void tools_to_wupper(std::wstring & r);
287 #endif
288
290
300 extern std::string tools_printf(const char *format, ...);
301
303
312 extern std::string tools_vprintf(const char *format, va_list ap);
313
315
319 extern bool tools_do_some_files_match_mask_regex(const entrepot & ent,
320 const std::string & file_mask);
321
322
324
330 extern void tools_unlink_file_mask_regex(user_interaction & dialog,
331 const entrepot & ent,
332 const std::string & file_mask,
333 bool info_details);
334
335
337
347 extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
348 const entrepot & ent,
349 const std::string & basename,
350 const std::string & extension,
351 bool info_details,
352 bool allow_overwriting,
353 bool warn_overwriting,
354 bool dry_run);
355
357
363 extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
364
366
370 extern path tools_relative2absolute_path(const path & src, const path & cwd);
371
373
376 extern void tools_block_all_signals(sigset_t &old_mask);
377
379
382 extern void tools_set_back_blocked_signals(sigset_t old_mask);
383
385
389 extern U_I tools_count_in_string(const std::string & s, const char a);
390
392
401 extern datetime tools_get_mtime(user_interaction & dialog,
402 const std::string & s,
403 bool auto_zeroing,
404 bool silent,
405 const std::set<std::string> & ignored_as_symlink = std::set<std::string>());
406
408
411 extern infinint tools_get_size(const std::string & s);
412
414
418 extern infinint tools_get_extended_size(std::string s, U_I base);
419
421
425 extern std::string tools_substitute(const std::string & hook,
426 const std::map<char, std::string> & corres);
427
428
430
441 extern std::string tools_hook_substitute(const std::string & hook,
442 const std::string & path,
443 const std::string & basename,
444 const std::string & num,
445 const std::string & padded_num,
446 const std::string & ext,
447 const std::string & context,
448 const std::string & base_url);
449
450
452
455 extern void tools_hook_execute(user_interaction & ui,
456 const std::string & cmd_line);
457
458
460
470 extern void tools_hook_substitute_and_execute(user_interaction & ui,
471 const std::string & hook,
472 const std::string & path,
473 const std::string & basename,
474 const std::string & num,
475 const std::string & padded_num,
476 const std::string & ext,
477 const std::string & context,
478 const std::string & base_url);
479
480
482
485 extern std::string tools_output2xml(const std::string & src);
486
488
491 extern U_I tools_octal2int(const std::string & perm);
492
493
495
498 extern std::string tools_int2octal(const U_I & perm);
499
501
502 extern char tools_cast_type_to_unix_type(char type);
503
505
506 extern std::string tools_get_permission_string(char type, U_32 perm, bool hard);
507
509
512 extern void tools_set_permission(S_I fd, U_I perm);
513
515
519 extern U_I tools_get_permission(S_I fd);
520
522
524
527 extern uid_t tools_ownership2uid(const std::string & user);
528
530
533 extern uid_t tools_ownership2gid(const std::string & group);
534
536
541 extern void tools_set_ownership(S_I filedesc, const std::string & slice_user, const std::string & slice_group);
542
544
549 extern void tools_memxor(void *dest, const void *src, U_I n);
550
552
557 extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
558
559
560
562
565 extern U_I tools_pseudo_random(U_I max);
566
567
569
576
577 template <class N, class B> std::deque<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
578 {
579 std::deque<B> ret;
580
581 if(base <= 0)
582 throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
583
584 while(number != 0)
585 {
586 ret.push_back(number % base);
587 number /= base;
588 }
589
590 return ret;
591 }
592
594
597 extern std::string tools_unsigned_char_to_hexa(unsigned char x);
598
600
603
604 extern std::string tools_string_to_hexa(const std::string & input);
605
607
610 extern infinint tools_file_size_to_crc_size(const infinint & size);
611
613 extern std::string tools_getcwd();
614
616 extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size, bool compressed);
617
619 extern std::string tools_strerror_r(int errnum);
620
621 #ifdef GPGME_SUPPORT
622 extern std::string tools_gpgme_strerror_r(gpgme_error_t err);
624 #endif
625
626 #if HAVE_WCHAR_H
627 extern std::wstring tools_string_to_wstring(const std::string & val);
629
631 extern std::string tools_wstring_to_string(const std::wstring & val);
632 #endif
633
642 struct dirent *tools_allocate_struct_dirent(const std::string & path_name, U_64 & max_name_length);
643
647 extern void tools_release_struct_dirent(struct dirent *ptr);
648
650 extern void tools_secu_string_show(user_interaction & dialog, const std::string & msg, const secu_string & key);
651
652 template <class T> T tools_max(T a, T b) { return a > b ? a : b; }
653 template <class T> T tools_min(T a, T b) { return a > b ? b : a; }
654
656 void tools_unlink(const std::string & filename);
657
659 extern std::string tools_escape_chars_in_string(const std::string & val, const char *to_escape);
660
662
664 extern bool tools_infinint2U_64(infinint val, U_64 & res);
665
667
673 extern bool tools_my_atoi(const char *a, U_I & val);
674
676 extern infinint tools_double2infinint(double arg);
677
679
686 template <class T> void tools_check_negative_date(T & val,
687 user_interaction & ui,
688 const char *inode_path,
689 const char *nature,
690 bool ask_before,
691 bool silent)
692 {
693 if(val < 0)
694 {
695 if(!silent)
696 {
697 std::string msg = tools_printf(gettext("Found negative date (%s) for inode %s ."),
698 nature,
699 inode_path);
700 if(ask_before)
701 ui.pause(tools_printf(gettext("%S Can we read it as if it was zero (1st January 1970 at 00:00:00 UTC)?"),
702 &msg));
703 else // just warn
704 ui.message(msg + gettext("Considering date as if it was zero (Jan 1970)"));
705 }
706
707 val = 0;
708 }
709 }
710
711
713
714 extern infinint tools_upper_rounded_log2(const infinint & ref);
715
716
718
719 extern infinint tools_lower_rounded_exp2(const infinint & ref);
720
721
723
724 extern infinint tools_rounded_square_root(const infinint & ref);
725
726
728
729 extern infinint tools_rounded_cube_root(const infinint & ref);
730
732
733 }
734
735 #endif
libdar::tools_getcwd
std::string tools_getcwd()
get current working directory
libdar::tools_double2infinint
infinint tools_double2infinint(double arg)
convert a double (float) to infinint (integer) taking care of rounding it to the closest value
libdar::tools_ownership2uid
uid_t tools_ownership2uid(const std::string &user)
change ownership of the file which descriptor is given
libdar::tools_string2tlv_list
tlv_list tools_string2tlv_list(user_interaction &dialog, const U_16 &type, const std::vector< std::string > &data)
Produces a list of TLV from a constant type and a list of string.
libdar::tools_block_all_signals
void tools_block_all_signals(sigset_t &old_mask)
block all signals (based on POSIX sigprocmask)
libdar::user_interaction
This is a pure virtual class that is used by libdar when interaction with the user is required.
Definition: user_interaction.hpp:47
libdar::tools_end
void tools_end()
libdar internal use only: it is launched from close_and_clean() and releases tools internal variables
libdar::tools_escape_chars_in_string
std::string tools_escape_chars_in_string(const std::string &val, const char *to_escape)
escape with a anti-slash character a set of chars found in the given string
libdar::tools_hook_execute
void tools_hook_execute(user_interaction &ui, const std::string &cmd_line)
execute and retries at user will a given command line
libdar::tools_system
void tools_system(user_interaction &dialog, const std::vector< std::string > &argvector)
wrapper to the "system" system call.
libdar::tools_get_filesize
infinint tools_get_filesize(const path &p)
retrieve the size in byte of a file
libdar::tools_concat_vector
std::string tools_concat_vector(const std::string &separator, const std::vector< std::string > &x)
concatenate a vectors of strings in a single string
integers.hpp
are defined here basic integer types that tend to be portable
libdar::tools_read_vector
void tools_read_vector(generic_file &f, std::vector< std::string > &x)
read a list of string from a file
libdar::tools_init
void tools_init()
libdar internal use only: it is launched from get_version() and initializes tools internal variables
libdar::tools_is_case_insensitive_equal
bool tools_is_case_insensitive_equal(const std::string &a, const std::string &b)
compare two string in case insensitive manner
libdar::tools_rounded_cube_root
infinint tools_rounded_cube_root(const infinint &ref)
compute an approximate cube root of the argument
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
libdar::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:62
libdar::tools_output2xml
std::string tools_output2xml(const std::string &src)
convert string for xml output
libdar::tools_is_equal_with_hourshift
bool tools_is_equal_with_hourshift(const infinint &hourshift, const datetime &date1, const datetime &date2)
test if two dates are equal taking care of a integer hour of difference
libdar::tools_find_last_char_of
std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v)
give a pointer to the last character of the given value in the given string
datetime.hpp
this file contains the definition of class datetime that stores unix times in a portable way
libdar::tools_rounded_square_root
infinint tools_rounded_square_root(const infinint &ref)
compute an approximate square root of the argument
libdar::tools_set_back_blocked_signals
void tools_set_back_blocked_signals(sigset_t old_mask)
unblock signals according to given mask
libdar::tools_get_extended_size
infinint tools_get_extended_size(std::string s, U_I base)
convert the given string to infinint taking care of multiplication suffixes like k,...
libdar::tools_are_on_same_filesystem
bool tools_are_on_same_filesystem(const std::string &file1, const std::string &file2)
tells whether two files are on the same mounted filesystem
libdar::tools_file_size_to_crc_size
infinint tools_file_size_to_crc_size(const infinint &size)
Defines the CRC size to use for a given filesize.
libdar::tools_printf
std::string tools_printf(const char *format,...)
make printf-like formating to a std::string
libdar::tools_uword2str
std::string tools_uword2str(U_16 x)
convert unsigned word to string
libdar::tools_unlink_file_mask_regex
void tools_unlink_file_mask_regex(user_interaction &dialog, const entrepot &ent, const std::string &file_mask, bool info_details)
remove files from a given directory
libdar::tools_name_of_gid
std::string tools_name_of_gid(const infinint &gid)
convert gid to name in regards of the current system's configuration
libdar::tools_get_permission
U_I tools_get_permission(S_I fd)
obtain the permission of the file which descriptor is given
libdar::tools_cast_type_to_unix_type
char tools_cast_type_to_unix_type(char type)
change the libdar internal type representation to the usual unix char type
libdar::tools_check_negative_date
void tools_check_negative_date(T &val, user_interaction &ui, const char *inode_path, const char *nature, bool ask_before, bool silent)
check the value is not negative, and if asked set it to zero
Definition: tools.hpp:686
libdar::tools_get_permission_string
std::string tools_get_permission_string(char type, U_32 perm, bool hard)
convert a permission number into its string representation (rwxrwxrwx)
libdar::tools_int2octal
std::string tools_int2octal(const U_I &perm)
convert a number to a string corresponding to its octal representation
libdar::tools_hook_substitute
std::string tools_hook_substitute(const std::string &hook, const std::string &path, const std::string &basename, const std::string &num, const std::string &padded_num, const std::string &ext, const std::string &context, const std::string &base_url)
produces the string resulting from the substitution of %... macro
entrepot.hpp
defines the entrepot interface. Entrepot interface defines a generic way to interact with files (slic...
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::tools_memxor
void tools_memxor(void *dest, const void *src, U_I n)
Produces in "dest" the XORed value of "dest" and "src".
libdar::tools_octal2int
U_I tools_octal2int(const std::string &perm)
convert octal string to integer
libdar::tools_allocate_struct_dirent
struct dirent * tools_allocate_struct_dirent(const std::string &path_name, U_64 &max_name_length)
libdar::tools_my_atoi
bool tools_my_atoi(const char *a, U_I &val)
ascii to integer conversion
libdar::tools_write_vector
void tools_write_vector(generic_file &f, const std::vector< std::string > &x)
write a list of string to file
libdar::tools_set_permission
void tools_set_permission(S_I fd, U_I perm)
change the permission of the file which descriptor is given
libdar::path
the class path is here to manipulate paths in the Unix notation: using'/'
Definition: path.hpp:51
libdar::tools_relative2absolute_path
path tools_relative2absolute_path(const path &src, const path &cwd)
transform a relative path to an absolute one given the current directory value
libdar::tools_read_string_size
void tools_read_string_size(generic_file &f, std::string &s, infinint taille)
read a string if given size from a file '\0' has no special meaning
libdar::datetime
stores time information
Definition: datetime.hpp:59
libdar::tools_blocking_read
void tools_blocking_read(int fd, bool mode)
set blocking/not blocking mode for reading on a file descriptor
libdar::tools_name_of_uid
std::string tools_name_of_uid(const infinint &uid)
convert uid to name in regards to the current system's configuration
libdar::tools_get_compression_ratio
std::string tools_get_compression_ratio(const infinint &storage_size, const infinint &file_size, bool compressed)
return the string about compression ratio
libdar::tools_display_date
std::string tools_display_date(const datetime &date)
convert a date in second to its human readable representation
libdar::tools_hook_substitute_and_execute
void tools_hook_substitute_and_execute(user_interaction &ui, const std::string &hook, const std::string &path, const std::string &basename, const std::string &num, const std::string &padded_num, const std::string &ext, const std::string &context, const std::string &base_url)
subsititue and execute command line
libdar::tools_vprintf
std::string tools_vprintf(const char *format, va_list ap)
make printf-like formating to a std::string
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:77
libdar::Erange
exception used to signal range error
Definition: erreurs.hpp:220
libdar::tools_str2charptr
char * tools_str2charptr(const std::string &x)
convert a string to a char *
libdar::tools_str2int
U_I tools_str2int(const std::string &x)
convert an integer written in decimal notation to the corresponding value
libdar::tools_int2str
std::string tools_int2str(S_I x)
convert integer to string
libdar::tools_secu_string_show
void tools_secu_string_show(user_interaction &dialog, const std::string &msg, const secu_string &key)
display the content of a secu_string, this function is only for trouble shooting!
libdar::entrepot
the Entrepot interface
Definition: entrepot.hpp:57
libdar::tools_do_some_files_match_mask_regex
bool tools_do_some_files_match_mask_regex(const entrepot &ent, const std::string &file_mask)
test the presence of files corresponding to a given mask in a directory (regex mask)
libdar::tools_make_date
void tools_make_date(const std::string &chemin, bool symlink, const datetime &access, const datetime &modif, const datetime &birth)
set dates of a given file, may throw exception
libdar::tools_infinint2U_64
bool tools_infinint2U_64(infinint val, U_64 &res)
convert an infinint to U_64 (aka "uint64_t" or yet "unsigned long long")
libdar::tools_upper_rounded_log2
infinint tools_upper_rounded_log2(const infinint &ref)
compute an approximate log2 of the argument
libdar::tools_addspacebefore
std::string tools_addspacebefore(std::string s, U_I expected_size)
prepend spaces before the given string
tlv_list.hpp
List of Generic Type Length Value data structures.
libdar::tools_count_in_string
U_I tools_count_in_string(const std::string &s, const char a)
counts the number of a given char in a given string
libdar::tools_number_base_decomposition_in_big_endian
std::deque< B > tools_number_base_decomposition_in_big_endian(N number, const B &base)
Template for the decomposition of any number in any base (decimal, octal, hexa, etc....
Definition: tools.hpp:577
libdar::secu_string
class secu_string
Definition: secu_string.hpp:58
libdar::tools_lower_rounded_exp2
infinint tools_lower_rounded_exp2(const infinint &ref)
compute an approximate exp2 of the argument
libdar::tools_pseudo_random
U_I tools_pseudo_random(U_I max)
Produces a pseudo random number x, where 0 <= x < max.
libdar::tools_write_string
void tools_write_string(generic_file &f, const std::string &s)
write a string to a file with a '\0' at then end
libdar::tools_read_string
void tools_read_string(generic_file &f, std::string &s)
read a string from a file expecting it to terminate by '\0'
libdar::tools_to_upper
void tools_to_upper(const std::string &r, std::string &uppered)
convert a string to upper case
libdar::tools_strerror_r
std::string tools_strerror_r(int errnum)
wrapper routine to strerror_r
libdar::tools_noexcept_make_date
void tools_noexcept_make_date(const std::string &chem, bool symlink, const datetime &last_acc, const datetime &last_mod, const datetime &birth)
set dates of a given file, no exception thrown
libdar::tools_string_to_hexa
std::string tools_string_to_hexa(const std::string &input)
convert a string into its hexadecima representation
libdar::tools_get_mtime
datetime tools_get_mtime(user_interaction &dialog, const std::string &s, bool auto_zeroing, bool silent, const std::set< std::string > &ignored_as_symlink=std::set< std::string >())
returns the last modification date of the given file
libdar::tools_avoid_slice_overwriting_regex
void tools_avoid_slice_overwriting_regex(user_interaction &dialog, const entrepot &ent, const std::string &basename, const std::string &extension, bool info_details, bool allow_overwriting, bool warn_overwriting, bool dry_run)
prevents slice overwriting: check the presence of slice and if necessary ask the user if they can be ...
path.hpp
here is the definition of the path class
libdar::operator+=
std::vector< T > operator+=(std::vector< T > &a, const std::vector< T > &b)
template function to add two vectors
Definition: tools.hpp:230
libdar::tools_release_struct_dirent
void tools_release_struct_dirent(struct dirent *ptr)
libdar::tools_system_with_pipe
void tools_system_with_pipe(const std::shared_ptr< user_interaction > &dialog, const std::string &dar_cmd, const std::vector< std::string > &argvpipe)
wrapper to the "system" system call using anonymous pipe to tranmit arguments to the child process
libdar::tools_ownership2gid
uid_t tools_ownership2gid(const std::string &group)
convert string group name or gid to numeric gid value
libdar::tools_set_ownership
void tools_set_ownership(S_I filedesc, const std::string &slice_user, const std::string &slice_group)
change ownership of the file which descriptor is given
libdar::tools_display_integer_in_metric_system
std::string tools_display_integer_in_metric_system(infinint number, const std::string &unit, bool binary)
libdar::tools_unlink
void tools_unlink(const std::string &filename)
remove from filesystem the file which path is given
libdar::tools_write_string_all
void tools_write_string_all(generic_file &f, const std::string &s)
write a string to a file, '\0' has no special meaning nor is added at the end
libdar::tools_unsigned_char_to_hexa
std::string tools_unsigned_char_to_hexa(unsigned char x)
convert a unsigned char into its hexa decima representation
libdar::tools_get_size
infinint tools_get_size(const std::string &s)
returns the size of the given plain file
libdar::tools_readlink
std::string tools_readlink(const char *root)
returns the file pointed to by a symbolic link (or transparent if the file is not a symlink).
libdar::tools_substitute
std::string tools_substitute(const std::string &hook, const std::map< char, std::string > &corres)
produce the string resulting from the substition of % macro defined in the map
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47