Disk ARchive  2.5.3
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-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 
498  extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
499  const path & chemin,
500  const std::string & x_file_mask,
501  bool info_details,
502  bool allow_overwriting,
503  bool warn_overwriting,
504  bool dry_run);
505 
507 
526  extern void tools_add_elastic_buffer(generic_file & f,
527  U_32 max_size,
528  U_32 modulo,
529  U_32 offset);
530 
531 
533 
539  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
540 
541 
543 
547  extern path tools_relative2absolute_path(const path & src, const path & cwd);
548 
550 
553  extern void tools_block_all_signals(sigset_t &old_mask);
554 
556 
559  extern void tools_set_back_blocked_signals(sigset_t old_mask);
560 
562 
566  extern U_I tools_count_in_string(const std::string & s, const char a);
567 
569 
572  extern datetime tools_get_mtime(const std::string & s);
573 
575 
578  extern infinint tools_get_size(const std::string & s);
579 
581 
584  extern datetime tools_get_ctime(const std::string & s);
585 
587 
591  extern std::vector<std::string> tools_split_in_words(generic_file & f);
592 
593 
595 
599  extern std::vector<std::string> tools_split_in_words(const std::string & arg);
600 
601 
603 
612  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
613 
614 
616 
620  extern std::string tools_substitute(const std::string & hook,
621  const std::map<char, std::string> & corres);
622 
623 
625 
635  extern std::string tools_hook_substitute(const std::string & hook,
636  const std::string & path,
637  const std::string & basename,
638  const std::string & num,
639  const std::string & padded_num,
640  const std::string & ext,
641  const std::string & context);
642 
643 
645 
648  extern void tools_hook_execute(user_interaction & ui,
649  const std::string & cmd_line);
650 
651 
653 
662  extern void tools_hook_substitute_and_execute(user_interaction & ui,
663  const std::string & hook,
664  const std::string & path,
665  const std::string & basename,
666  const std::string & num,
667  const std::string & padded_num,
668  const std::string & ext,
669  const std::string & context);
670 
672 
673 
677  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
678  const std::string & relative_part);
679 
681 
684  extern std::string tools_output2xml(const std::string & src);
685 
687 
690  extern U_I tools_octal2int(const std::string & perm);
691 
692 
694 
697  extern std::string tools_int2octal(const U_I & perm);
698 
700 
701  extern std::string tools_get_permission_string(char type, U_32 perm, bool hard);
702 
704 
707  extern void tools_set_permission(S_I fd, U_I perm);
708 
710 
714  extern U_I tools_get_permission(S_I fd);
715 
717 
719 
722  extern uid_t tools_ownership2uid(const std::string & user);
723 
725 
728  extern uid_t tools_ownership2gid(const std::string & group);
729 
731 
736  extern void tools_set_ownership(S_I filedesc, const std::string & slice_user, const std::string & slice_group);
737 
739 
744  extern void tools_memxor(void *dest, const void *src, U_I n);
745 
747 
752  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
753 
754 
755 
757 
761  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
762 
763 
764 
766 
769  extern U_I tools_pseudo_random(U_I max);
770 
771 
773 
780 
781  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
782  {
783  std::vector<B> ret;
784 
785  if(base <= 0)
786  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
787 
788  while(number != 0)
789  {
790  ret.push_back(number % base);
791  number /= base;
792  }
793 
794  return ret;
795  }
796 
798 
801  std::string tools_unsigned_char_to_hexa(unsigned char x);
802 
804 
807 
808  std::string tools_string_to_hexa(const std::string & input);
809 
811 
814  extern infinint tools_file_size_to_crc_size(const infinint & size);
815 
817  extern std::string tools_get_euid();
818 
820  extern std::string tools_get_egid();
821 
823  extern std::string tools_get_hostname();
824 
826  extern std::string tools_get_date_utc();
827 
829  extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size, bool compressed);
830 
832  extern std::string tools_strerror_r(int errnum);
833 
834  #ifdef GPGME_SUPPORT
835  extern std::string tools_gpgme_strerror_r(gpgme_error_t err);
837  #endif
838 
839  #if HAVE_WCHAR_H
840  extern std::wstring tools_string_to_wstring(const std::string & val);
842 
844  extern std::string tools_wstring_to_string(const std::wstring & val);
845  #endif
846 
848  extern void tools_merge_to_vector(std::vector<std::string> & a, const std::vector<std::string> & b);
849 
851  extern std::vector<std::string> tools_substract_from_vector(const std::vector<std::string> & a, const std::vector<std::string> & b);
852 
862  struct dirent *tools_allocate_struct_dirent(const std::string & path_name, memory_pool *pool = nullptr);
863 
864 
868  extern void tools_release_struct_dirent(struct dirent *ptr);
869 
871  extern void tools_secu_string_show(user_interaction & dialog, const std::string & msg, const secu_string & key);
872 
873  template <class T> T tools_max(T a, T b) { return a > b ? a : b; }
874  template <class T> T tools_min(T a, T b) { return a > b ? b : a; }
875 
876 
877  }
878 
879  #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 ...
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:781
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
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_avoid_slice_overwriting_regex(user_interaction &dialog, const path &chemin, const std::string &x_file_mask, 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 ...
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_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_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)
struct dirent * tools_allocate_struct_dirent(const std::string &path_name, memory_pool *pool=nullptr)
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)
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