Disk ARchive  2.5.9
Full featured and portable backup and archiving tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
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 #if HAVE_GPGME_H
51 #include <gpgme.h>
52 #endif
53 }
54 
55 #include <string>
56 #include <vector>
57 #include <map>
58 #include "path.hpp"
59 #include "infinint.hpp"
60 #include "generic_file.hpp"
61 #include "tuyau.hpp"
62 #include "integers.hpp"
63 #include "tlv_list.hpp"
64 #include "memory_pool.hpp"
65 #include "datetime.hpp"
66 
67 #define TOOLS_SI_SUFFIX 1000
68 #define TOOLS_BIN_SUFFIX 1024
69 
70 namespace libdar
71 {
72 
75 
76 
78  extern void tools_init();
80  extern void tools_end();
81 
83 
88  extern char *tools_str2charptr(const std::string &x);
89 
91 
94  extern void tools_write_string(generic_file & f, const std::string & s);
95 
97 
100  extern void tools_read_string(generic_file & f, std::string & s);
101 
103 
106  extern void tools_write_string_all(generic_file & f, const std::string & s);
107 
109 
113  extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
114 
116 
119  extern infinint tools_get_filesize(const path &p);
120 
122 
126  extern infinint tools_get_extended_size(std::string s, U_I base);
127 
133  extern std::string tools_display_integer_in_metric_system(infinint number, const std::string & unit, bool binary);
134 
136 
140  extern void tools_extract_basename(const char *command_name, std::string & basename);
141 
142 
144 
150  extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
151 
153 
159  extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v);
160 
162 
168  extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base, memory_pool *pool = nullptr);
169 
171 
177  extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base, memory_pool *pool = nullptr);
178 
180 
188  extern void tools_open_pipes(user_interaction & dialog,
189  const std::string &input,
190  const std::string & output,
191  tuyau *&in,
192  tuyau *&out,
193  memory_pool *pool = nullptr);
194 
196 
199  extern void tools_blocking_read(int fd, bool mode);
200 
202 
205  extern std::string tools_name_of_uid(const infinint & uid);
206 
208 
211  extern std::string tools_name_of_gid(const infinint & gid);
212 
214 
217  extern std::string tools_uword2str(U_16 x);
218 
220 
223  extern std::string tools_int2str(S_I x);
224  extern std::string tools_uint2str(U_I x);
225 
227 
230  extern U_I tools_str2int(const std::string & x);
231 
233 
236  extern S_I tools_str2signed_int(const std::string & x);
237 
239 
245  extern bool tools_my_atoi(const char *a, U_I & val);
246 
248 
252  extern std::string tools_addspacebefore(std::string s, U_I expected_size);
253 
255 
258  extern std::string tools_display_date(const datetime & date);
259 
261 
265  extern infinint tools_convert_date(const std::string & repres);
266 
268 
271  extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
272 
274 
281  extern void tools_system_with_pipe(user_interaction & dialog,
282  const std::string & dar_cmd,
283  const std::vector<std::string> & argvpipe,
284  memory_pool *pool = nullptr);
285 
287 
290  extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
291 
293 
296  extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
297 
299 
303  extern std::string tools_concat_vector(const std::string & separator,
304  const std::vector<std::string> & x);
305 
307 
311  std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
312 
313 
315 
319  extern void tools_display_features(user_interaction & dialog);
320 
321 
323 
328  extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const datetime & date1, const datetime & date2);
329 
331 
332  template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
333  {
334  a = a + b;
335  return a;
336  }
337 
338 
340 
345  extern const char *tools_get_from_env(const char **env, const char *clef);
346 
348 
355  extern void tools_check_basename(user_interaction & dialog,
356  const path & loc,
357  std::string & base,
358  const std::string & extension,
359  memory_pool *pool = nullptr);
360 
362 
363  extern std::string tools_getcwd();
364 
366 
370  extern std::string tools_readlink(const char *root);
371 
373 
379  extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
380 
381 
383 
389  extern void tools_noexcept_make_date(const std::string & chem, bool symlink, const datetime & last_acc, const datetime & last_mod, const datetime & birth);
390 
392 
399  extern void tools_make_date(const std::string & chemin, bool symlink, const datetime & access, const datetime & modif, const datetime & birth);
400 
402 
406  extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
407 
414  extern void tools_to_upper(const std::string & r, std::string & uppered);
415 
416 #if HAVE_WCTYPE_H
417  extern void tools_to_wupper(std::wstring & r);
424 #endif
425 
427 
430  extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
431 
433 
441  extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
442 
443 
445 
455  extern std::string tools_printf(const char *format, ...);
456 
458 
467  extern std::string tools_vprintf(const char *format, va_list ap);
468 
470 
475  extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
476 
477 
479 
485  extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
486 
487 
489 
499  extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
500  const path & chemin,
501  const std::string & basename,
502  const std::string & extension,
503  bool info_details,
504  bool allow_overwriting,
505  bool warn_overwriting,
506  bool dry_run);
507 
509 
528  extern void tools_add_elastic_buffer(generic_file & f,
529  U_32 max_size,
530  U_32 modulo,
531  U_32 offset);
532 
533 
541  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
542 
543 
545 
549  extern path tools_relative2absolute_path(const path & src, const path & cwd);
550 
552 
555  extern void tools_block_all_signals(sigset_t &old_mask);
556 
558 
561  extern void tools_set_back_blocked_signals(sigset_t old_mask);
562 
564 
568  extern U_I tools_count_in_string(const std::string & s, const char a);
569 
571 
574  extern datetime tools_get_mtime(const std::string & s);
575 
577 
580  extern infinint tools_get_size(const std::string & s);
581 
583 
586  extern datetime tools_get_ctime(const std::string & s);
587 
589 
593  extern std::vector<std::string> tools_split_in_words(generic_file & f);
594 
595 
597 
601  extern std::vector<std::string> tools_split_in_words(const std::string & arg);
602 
603 
605 
614  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
615 
616 
618 
622  extern std::string tools_substitute(const std::string & hook,
623  const std::map<char, std::string> & corres);
624 
625 
627 
637  extern std::string tools_hook_substitute(const std::string & hook,
638  const std::string & path,
639  const std::string & basename,
640  const std::string & num,
641  const std::string & padded_num,
642  const std::string & ext,
643  const std::string & context);
644 
645 
647 
650  extern void tools_hook_execute(user_interaction & ui,
651  const std::string & cmd_line);
652 
653 
655 
664  extern void tools_hook_substitute_and_execute(user_interaction & ui,
665  const std::string & hook,
666  const std::string & path,
667  const std::string & basename,
668  const std::string & num,
669  const std::string & padded_num,
670  const std::string & ext,
671  const std::string & context);
672 
674 
675 
679  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
680  const std::string & relative_part);
681 
683 
686  extern std::string tools_output2xml(const std::string & src);
687 
689 
692  extern U_I tools_octal2int(const std::string & perm);
693 
694 
696 
699  extern std::string tools_int2octal(const U_I & perm);
700 
702 
703  extern std::string tools_get_permission_string(char type, U_32 perm, bool hard);
704 
706 
709  extern void tools_set_permission(S_I fd, U_I perm);
710 
712 
716  extern U_I tools_get_permission(S_I fd);
717 
719 
721 
724  extern uid_t tools_ownership2uid(const std::string & user);
725 
727 
730  extern uid_t tools_ownership2gid(const std::string & group);
731 
733 
738  extern void tools_set_ownership(S_I filedesc, const std::string & slice_user, const std::string & slice_group);
739 
741 
746  extern void tools_memxor(void *dest, const void *src, U_I n);
747 
749 
754  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
755 
756 
757 
759 
763  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
764 
765 
766 
768 
771  extern U_I tools_pseudo_random(U_I max);
772 
773 
775 
782 
783  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
784  {
785  std::vector<B> ret;
786 
787  if(base <= 0)
788  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
789 
790  while(number != 0)
791  {
792  ret.push_back(number % base);
793  number /= base;
794  }
795 
796  return ret;
797  }
798 
800 
803  std::string tools_unsigned_char_to_hexa(unsigned char x);
804 
806 
809 
810  std::string tools_string_to_hexa(const std::string & input);
811 
813 
816  extern infinint tools_file_size_to_crc_size(const infinint & size);
817 
819  extern std::string tools_get_euid();
820 
822  extern std::string tools_get_egid();
823 
825  extern std::string tools_get_hostname();
826 
828  extern std::string tools_get_date_utc();
829 
831  extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size, bool compressed);
832 
834  extern std::string tools_strerror_r(int errnum);
835 
836 #ifdef GPGME_SUPPORT
837  extern std::string tools_gpgme_strerror_r(gpgme_error_t err);
839 #endif
840 
841 #if HAVE_WCHAR_H
842  extern std::wstring tools_string_to_wstring(const std::string & val);
844 
846  extern std::string tools_wstring_to_string(const std::wstring & val);
847 #endif
848 
850  extern void tools_merge_to_vector(std::vector<std::string> & a, const std::vector<std::string> & b);
851 
853  extern std::vector<std::string> tools_substract_from_vector(const std::vector<std::string> & a, const std::vector<std::string> & b);
854 
865  struct dirent *tools_allocate_struct_dirent(const std::string & path_name, U_64 & max_name_length, memory_pool *pool = nullptr);
866 
867 
871  extern void tools_release_struct_dirent(struct dirent *ptr);
872 
874  extern void tools_secu_string_show(user_interaction & dialog, const std::string & msg, const secu_string & key);
875 
876  template <class T> T tools_max(T a, T b) { return a > b ? a : b; }
877  template <class T> T tools_min(T a, T b) { return a > b ? b : a; }
878 
880  extern std::string tools_escape_chars_in_string(const std::string & val, const char *to_escape);
881 
885  extern bool tools_infinint2U_64(infinint val, U_64 & res);
886 
887 
888 }
889 
890 #endif
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
std::string tools_get_egid()
return a string containing the Effective UID
void tools_open_pipes(user_interaction &dialog, const std::string &input, const std::string &output, tuyau *&in, tuyau *&out, memory_pool *pool=nullptr)
open a pair of tuyau objects encapsulating two named pipes.
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_split_path_basename(const char *all, path *&chemin, std::string &base, memory_pool *pool=nullptr)
split a given full path in path part and basename part
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
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
are defined here basic integer types that tend to be portable
class memory_pool allocates and recycles blocks of memory for better performances it is expected to b...
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
const char * tools_get_from_env(const char **env, const char *clef)
isolate the value of a given variable from the environment vector
datetime tools_get_mtime(const std::string &s)
returns the last modification date of the given file
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:783
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 '\0' 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 '\0' 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_set_back_blocked_signals(sigset_t old_mask)
unblock signals according to given mask
std::vector< std::string > tools_substract_from_vector(const std::vector< std::string > &a, const std::vector< std::string > &b)
remove from 'a' elements found in 'b' and return the resulting vector
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'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
std::vector< T > operator+=(std::vector< T > &a, const std::vector< T > &b)
template function to add two vectors
Definition: tools.hpp:332
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's configuration
void tools_display_features(user_interaction &dialog)
display the compilation time features of libdar
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
struct dirent * tools_allocate_struct_dirent(const std::string &path_name, U_64 &max_name_length, memory_pool *pool=nullptr)
uid_t tools_ownership2gid(const std::string &group)
convert string group name or gid to numeric gid value
datetime tools_get_ctime(const std::string &s)
returns the last change date of the given file
void tools_add_elastic_buffer(generic_file &f, U_32 max_size, U_32 modulo, U_32 offset)
append an elastic buffer of given size to the 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
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
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 '\0'
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.
std::string tools_get_euid()
return a string containing the Effective UID
void tools_release_struct_dirent(struct dirent *ptr)
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
bool tools_infinint2U_64(infinint val, U_64 &res)
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
std::string tools_addspacebefore(std::string s, U_I expected_size)
prepend spaces before the given string
std::string tools_display_date(const datetime &date)
convert a date in second to its human readable representation
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_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::string tools_get_permission_string(char type, U_32 perm, bool hard)
convert a permission number into its string representation (rwxrwxrwx)
std::string tools_strerror_r(int errnum)
wrapper routine to strerror_r
std::vector< std::string > tools_split_in_words(generic_file &f)
read a file and split its contents into 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
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
std::string tools_get_date_utc()
return a string containing the current time (UTC)
std::string tools_output2xml(const std::string &src)
convert string for xml output
exception used to signal range error
Definition: erreurs.hpp:179
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".
U_I tools_str2int(const std::string &x)
convert an integer written in decimal notation to the corresponding value
this file contains the definition of class datetime that stores unix times in a portable way ...
void tools_check_basename(user_interaction &dialog, const path &loc, std::string &base, const std::string &extension, memory_pool *pool=nullptr)
does sanity checks on a slice name, check presence and detect whether the given basename is not rathe...
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
void tools_system_with_pipe(user_interaction &dialog, const std::string &dar_cmd, const std::vector< std::string > &argvpipe, memory_pool *pool=nullptr)
wrapper to the "system" system call using anonymous pipe to tranmit arguments to the child process ...
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.
uid_t tools_ownership2uid(const std::string &user)
change ownership of the file which descriptor is given
std::string tools_vprintf(const char *format, va_list ap)
make printf-like formating to a std::string
std::string tools_get_compression_ratio(const infinint &storage_size, const infinint &file_size, bool compressed)
return the string about compression ratio
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
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! ...
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.
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 ...
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
std::string tools_display_integer_in_metric_system(infinint number, const std::string &unit, bool binary)
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
void tools_block_all_signals(sigset_t &old_mask)
block all signals (based on POSIX sigprocmask)
void tools_merge_to_vector(std::vector< std::string > &a, const std::vector< std::string > &b)
add in 'a', element of 'b' not already found in 'a'
U_I tools_get_permission(S_I fd)
obtain the permission of the file which descriptor is given
bool tools_are_on_same_filesystem(const std::string &file1, const std::string &file2)
void tools_hook_execute(user_interaction &ui, const std::string &cmd_line)
execute and retries at user will a given command line