Disk ARchive  2.4.21
tools.hpp
Go to the documentation of this file.
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 
31 
35 
36 
37 #ifndef TOOLS_HPP
38 #define TOOLS_HPP
39 
40 #include "../my_config.h"
41 
42 extern "C"
43 {
44 #if STDC_HEADERS
45 #include <stdarg.h>
46 #endif
47 #if HAVE_SIGNAL_H
48 #include <signal.h>
49 #endif
50 }
51 
52 #include <string>
53 #include <vector>
54 #include <map>
55 #include "path.hpp"
56 #include "infinint.hpp"
57 #include "generic_file.hpp"
58 #include "tuyau.hpp"
59 #include "integers.hpp"
60 #include "tlv_list.hpp"
61 
62 #define TOOLS_SI_SUFFIX 1000
63 #define TOOLS_BIN_SUFFIX 1024
64 
65 namespace libdar
66 {
67 
70 
71 
73  extern void tools_init();
75  extern void tools_end();
76 
78 
83  extern char *tools_str2charptr(const std::string &x);
84 
86 
89  extern void tools_write_string(generic_file & f, const std::string & s);
90 
92 
95  extern void tools_read_string(generic_file & f, std::string & s);
96 
98 
101  extern void tools_write_string_all(generic_file & f, const std::string & s);
102 
104 
108  extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
109 
111 
114  extern infinint tools_get_filesize(const path &p);
115 
117 
121  extern infinint tools_get_extended_size(std::string s, U_I base);
122 
124 
128  extern void tools_extract_basename(const char *command_name, std::string & basename);
129 
130 
132 
138  extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
139 
141 
147  extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v);
148 
150 
155  extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base);
156 
158 
163  extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base);
164 
166 
173  extern void tools_open_pipes(user_interaction & dialog, const std::string &input, const std::string & output,
174  tuyau *&in, tuyau *&out);
175 
177 
180  extern void tools_blocking_read(int fd, bool mode);
181 
183 
186  extern std::string tools_name_of_uid(const infinint & uid);
187 
189 
192  extern std::string tools_name_of_gid(const infinint & gid);
193 
195 
198  extern std::string tools_uword2str(U_16 x);
199 
201 
204  extern std::string tools_int2str(S_I x);
205 
207 
210  extern U_I tools_str2int(const std::string & x);
211 
213 
216  extern S_I tools_str2signed_int(const std::string & x);
217 
219 
225  extern bool tools_my_atoi(const char *a, U_I & val);
226 
228 
232  extern std::string tools_addspacebefore(std::string s, U_I expected_size);
233 
235 
238  extern std::string tools_display_date(infinint date);
239 
241 
245  extern infinint tools_convert_date(const std::string & repres);
246 
248 
251  extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
252 
254 
260  extern void tools_system_with_pipe(user_interaction & dialog, const std::string & dar_cmd, const std::vector<std::string> & argvpipe);
261 
263 
266  extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
267 
269 
272  extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
273 
275 
279  extern std::string tools_concat_vector(const std::string & separator,
280  const std::vector<std::string> & x);
281 
283 
287  std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
288 
290 
294  extern bool tools_is_member(const std::string & val, const std::vector<std::string> & liste);
295 
297 
313  extern void tools_display_features(user_interaction & dialog,
314  bool ea,
315  bool largefile,
316  bool nodump,
317  bool special_alloc,
318  U_I bits,
319  bool thread_safe,
320  bool libz,
321  bool libbz2,
322  bool liblzo2,
323  bool libcrypto,
324  bool furtive_read);
325 
327 
331  extern void tools_display_features(user_interaction & dialog);
332 
333 
335 
340  extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const infinint & date1, const infinint & date2);
341 
343 
344  template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
345  {
346  a = a + b;
347  return a;
348  }
349 
350 
352 
357  extern const char *tools_get_from_env(const char **env, const char *clef);
358 
360 
366  extern void tools_check_basename(user_interaction & dialog,
367  const path & loc, std::string & base, const std::string & extension);
368 
370 
371  extern std::string tools_getcwd();
372 
374 
378  extern std::string tools_readlink(const char *root);
379 
381 
387  extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
388 
389 
391 
395  extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod);
396 
398 
402  extern void tools_make_date(const std::string & chemin, infinint access, infinint modif);
403 
405 
409  extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
410 
417  extern void tools_to_upper(const std::string & r, std::string & uppered);
418 
419 #if HAVE_WCTYPE_H
420  extern void tools_to_wupper(std::wstring & r);
427 #endif
428 
430 
433  extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
434 
436 
444  extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
445 
446 
448 
457  extern std::string tools_printf(const char *format, ...);
458 
460 
469  extern std::string tools_vprintf(const char *format, va_list ap);
470 
472 
477  extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
478 
479 
481 
487  extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
488 
489 
491 
502  const path & chemin,
503  const std::string & basename,
504  const std::string & extension,
505  bool info_details,
506  bool allow_overwriting,
507  bool warn_overwriting,
508  bool dry_run);
509 
511 
514  extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size);
515 
516 
518 
524  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
525 
526 
528 
532  extern path tools_relative2absolute_path(const path & src, const path & cwd);
533 
535 
538  extern void tools_block_all_signals(sigset_t &old_mask);
539 
541 
544  extern void tools_set_back_blocked_signals(sigset_t old_mask);
545 
547 
551  extern U_I tools_count_in_string(const std::string & s, const char a);
552 
554 
557  extern infinint tools_get_mtime(const std::string & s);
558 
560 
563  extern infinint tools_get_size(const std::string & s);
564 
566 
569  extern infinint tools_get_ctime(const std::string & s);
570 
572 
576  extern std::vector<std::string> tools_split_in_words(generic_file & f);
577 
579 
588  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
589 
590 
592 
596  extern std::string tools_substitute(const std::string & hook,
597  const std::map<char, std::string> & corres);
598 
599 
601 
611  extern std::string tools_hook_substitute(const std::string & hook,
612  const std::string & path,
613  const std::string & basename,
614  const std::string & num,
615  const std::string & padded_num,
616  const std::string & ext,
617  const std::string & context);
618 
619 
621 
624  extern void tools_hook_execute(user_interaction & ui,
625  const std::string & cmd_line);
626 
627 
629 
639  const std::string & hook,
640  const std::string & path,
641  const std::string & basename,
642  const std::string & num,
643  const std::string & padded_num,
644  const std::string & ext,
645  const std::string & context);
646 
648 
649 
653  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
654  const std::string & relative_part);
655 
657 
660  extern std::string tools_output2xml(const std::string & src);
661 
663 
666  extern U_I tools_octal2int(const std::string & perm);
667 
668 
670 
673  extern std::string tools_int2octal(const U_I & perm);
674 
676 
679  extern void tools_set_permission(S_I fd, U_I perm);
680 
682 
686  extern U_I tools_get_permission(S_I fd);
687 
689 
694  extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group);
695 
697 
702  extern void tools_memxor(void *dest, const void *src, U_I n);
703 
705 
710  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
711 
712 
713 
715 
719  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
720 
721 
722 
724 
728  extern U_I tools_pseudo_random(U_I max);
729 
730 
732 
739 
740  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
741  {
742  std::vector<B> ret;
743 
744  if(base <= 0)
745  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
746 
747  while(number != 0)
748  {
749  ret.push_back(number % base);
750  number /= base;
751  }
752 
753  return ret;
754  }
755 
757 
760  std::string tools_unsigned_char_to_hexa(unsigned char x);
761 
763 
766 
767  std::string tools_string_to_hexa(const std::string & input);
768 
770 
773  extern infinint tools_file_size_to_crc_size(const infinint & size);
774 
776  extern std::string tools_get_euid();
777 
779  extern std::string tools_get_egid();
780 
782  extern std::string tools_get_hostname();
783 
785  extern std::string tools_get_date_utc();
786 
788  extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size);
789 
790 #if HAVE_WCHAR_H
791  extern std::wstring tools_string_to_wstring(const std::string & val);
793 
795  extern std::string tools_wstring_to_string(const std::wstring & val);
796 #endif
797 
799  extern std::string tools_escape_chars_in_string(const std::string & val, const char *to_escape);
800 
801 
802 }
803 
804 #endif
std::string tools_get_egid()
return a string containing the Effective UID
void tools_check_basename(user_interaction &dialog, const path &loc, std::string &base, const std::string &extension)
does sanity checks on a slice name, check presence and detect whether the given basename is not rathe...
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)
subsititue and execute command line
void tools_write_string_all(generic_file &f, const std::string &s)
write a string to a file, &#39;\0&#39; has no special meaning nor is added at the end
bool tools_look_for(const char *argument, S_I argc, char *const argv[])
test the presence of an argument
void tools_to_upper(const std::string &r, std::string &uppered)
convert a string to upper case
bool furtive_read()
returns whether libdar can support furtive read mode when run by privileged user
are defined here basic integer types that tend to be portable
std::string tools_display_date(infinint date)
convert a date in second to its human readable representation
void tools_read_from_pipe(user_interaction &dialog, S_I fd, tlv_list &result)
Extract from anonymous pipe a tlv_list.
S_I tools_str2signed_int(const std::string &x)
convert a signed integer written in decimal notation to the corresponding value
std::string tools_build_regex_for_exclude_mask(const std::string &prefix, const std::string &relative_part)
builds a regex from root directory and user provided regex to be applied to the relative path ...
void tools_init()
libdar internal use only: it is launched from get_version() and initializes tools internal variables ...
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
bool largefile()
returns whether largefile (>2GiB) support has been activated at compilation time
const char * tools_get_from_env(const char **env, const char *clef)
isolate the value of a given variable from the environment vector
std::vector< 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:740
void tools_extract_basename(const char *command_name, std::string &basename)
extracts the basename of a file (removing path part)
void tools_read_string_size(generic_file &f, std::string &s, infinint taille)
read a string if given size from a file &#39;\0&#39; has no special meaning
bool tools_find_next_char_out_of_parenthesis(const std::string &data, const char what, U_32 start, U_32 &found)
look next char in string out of parenthesis
void tools_write_string(generic_file &f, const std::string &s)
write a string to a file with a &#39;\0&#39; at then end
void tools_read_vector(generic_file &f, std::vector< std::string > &x)
read a list of string from a file
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
void tools_system_with_pipe(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 ...
void tools_make_date(const std::string &chemin, infinint access, infinint modif)
set dates of a given file, may throw exception
void tools_set_back_blocked_signals(sigset_t old_mask)
unblock signals according to given mask
void tools_set_permission(S_I fd, U_I perm)
change the permission of the file which descriptor is given
infinint tools_convert_date(const std::string &repres)
convert a human readable date representation in number of second since the system reference date ...
std::string tools_name_of_uid(const infinint &uid)
convert uid to name in regards to the current system&#39;s configuration
std::string tools_uword2str(U_16 x)
convert unsigned word to string
void tools_read_range(const std::string &s, S_I &min, U_I &max)
from a string with a range notation (min-max) extract the range values
bool ea()
returns whether EA support has been activated at compilation time
std::vector< T > operator+=(std::vector< T > &a, const std::vector< T > &b)
template function to add two vectors
Definition: tools.hpp:344
void tools_end()
libdar internal use only: it is launched from close_and_clean() and releases tools internal variables...
std::string tools_name_of_gid(const infinint &gid)
convert gid to name in regards of the current system&#39;s configuration
This is a pure virtual class that is used by libdar when interaction with the user is required...
bool tools_is_case_insensitive_equal(const std::string &a, const std::string &b)
compare two string in case insensitive manner
void tools_unlink_file_mask_regex(user_interaction &dialog, const std::string &c_chemin, const std::string &file_mask, bool info_details)
remove files from a given directory
infinint tools_file_size_to_crc_size(const infinint &size)
Defines the CRC size to use for a given filesize.
std::string tools_printf(const char *format,...)
make printf-like formating to a std::string
infinint tools_get_mtime(const std::string &s)
returns the last modification date of the given file
std::string tools_string_to_hexa(const std::string &input)
convert a string into its hexadecima representation
void tools_blocking_read(int fd, bool mode)
set blocking/not blocking mode for reading on a file descriptor
void tools_noexcept_make_date(const std::string &chem, const infinint &last_acc, const infinint &last_mod)
set dates of a given file, no exception thrown
void tools_split_path_basename(const char *all, path *&chemin, std::string &base)
split a given full path in path part and basename part
void tools_display_features(user_interaction &dialog, bool ea, bool largefile, bool nodump, bool special_alloc, U_I bits, bool thread_safe, bool libz, bool libbz2, bool liblzo2, bool libcrypto, bool furtive_read)
display the compilation time features of libdar
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
bool libz()
returns whether libdar is dependent on libz and if so has gzip compression/decompression available ...
infinint tools_get_extended_size(std::string s, U_I base)
convert the given string to infinint taking care of multiplication suffixes like k, M, T, etc.
U_I tools_octal2int(const std::string &perm)
convert octal string to integer
bool tools_do_some_files_match_mask_regex(user_interaction &ui, const std::string &c_chemin, const std::string &file_mask)
test the presence of files corresponding to a given mask in a directory (regex mask) ...
here is the definition of the path classthe path class handle path and provide several operation on t...
void tools_read_string(generic_file &f, std::string &s)
read a string from a file expecting it to terminate by &#39;\0&#39;
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.
bool tools_is_member(const std::string &val, const std::vector< std::string > &liste)
test the presence of a value in a list
std::string tools_get_euid()
return a string containing the Effective UID
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)
produces the string resulting from the substitution of %... macro
std::string tools_getcwd()
get current working directory
std::string tools_addspacebefore(std::string s, U_I expected_size)
prepend spaces before the given string
defines the implementation of pipe under the generic_file interface.mainly used between zapette and s...
std::string tools_int2str(S_I x)
convert integer to string
void tools_open_pipes(user_interaction &dialog, const std::string &input, const std::string &output, tuyau *&in, tuyau *&out)
open a pair of tuyau objects encapsulating two named pipes.
void tools_write_vector(generic_file &f, const std::vector< std::string > &x)
write a list of string to file
path tools_relative2absolute_path(const path &src, const path &cwd)
transform a relative path to an absolute one given the current directory value
infinint tools_get_size(const std::string &s)
returns the size of the given plain file
std::vector< std::string > tools_split_in_words(generic_file &f)
read a file and split its contents in words
switch module to limitint (32 ou 64 bits integers) or infinint
std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v)
give a pointer to the last character of the given value in the given string
std::string tools_get_hostname()
return a string containing the hostname of the current host
std::string tools_unsigned_char_to_hexa(unsigned char x)
convert a unsigned char into its hexa decima representation
std::string tools_get_date_utc()
return a string containing the current time (UTC)
bool thread_safe()
returns whether the current libdar is thread safe
std::string tools_output2xml(const std::string &src)
convert string for xml output
exception used to signal range error
Definition: erreurs.hpp:173
U_I bits()
returns the internal integer type used
std::string tools_int2octal(const U_I &perm)
convert a number to a string corresponding to its octal representation
void tools_memxor(void *dest, const void *src, U_I n)
Produces in "dest" the XORed value of "dest" and "src".
bool tools_is_equal_with_hourshift(const infinint &hourshift, const infinint &date1, const infinint &date2)
test if two dates are equal taking care of a integer hour of difference
U_I tools_str2int(const std::string &x)
convert an integer written in decimal notation to the corresponding value
this is the interface class from which all other data transfer classes inherit
bool nodump()
returns whether nodump flag support has been activated at compilation time
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)...
void tools_remove_last_char_if_equal_to(char c, std::string &s)
remove last character of a string is it equal to a given value
infinint tools_get_filesize(const path &p)
retrieve the size in byte of a file
std::string tools_concat_vector(const std::string &separator, const std::vector< std::string > &x)
concatenate a vectors of strings in a single string
U_I tools_pseudo_random(U_I max)
Produces a pseudo random number x, where 0 <= x < max.
std::string tools_vprintf(const char *format, va_list ap)
make printf-like formating to a std::string
List of Generic Type Length Value data structures.
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
char * tools_str2charptr(const std::string &x)
convert a string to a char *
void tools_system(user_interaction &dialog, const std::vector< std::string > &argvector)
wrapper to the "system" system call.
the arbitrary large positive integer class
void tools_add_elastic_buffer(generic_file &f, U_32 max_size)
append an elastic buffer of given size to the file
bool tools_my_atoi(const char *a, U_I &val)
ascii to integer conversion
U_I tools_count_in_string(const std::string &s, const char a)
counts the number of a given char in a given string
void tools_avoid_slice_overwriting_regex(user_interaction &dialog, const path &chemin, 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 ...
bool libbz2()
returns whether libdar is dependent on libbz2 and if so has bzip2 compression/decompression available...
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:43
std::string tools_get_compression_ratio(const infinint &storage_size, const infinint &file_size)
return the string about compression ratio
void tools_block_all_signals(sigset_t &old_mask)
block all signals (based on POSIX sigprocmask)
infinint tools_get_ctime(const std::string &s)
returns the last change date of the given file
void tools_set_ownership(S_I fd, const std::string &slice_user, const std::string &slice_group)
change ownership of the file which descriptor is given
U_I tools_get_permission(S_I fd)
obtain the permission of the file which descriptor is given
the class path is here to manipulate paths in the Unix notation: using&#39;/&#39;
Definition: path.hpp:50
bool tools_are_on_same_filesystem(const std::string &file1, const std::string &file2)
tells whether two files are on the same mounted filesystem
void tools_hook_execute(user_interaction &ui, const std::string &cmd_line)
execute and retries at user will a given command line