Disk ARchive  2.4.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 
386  extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
387 
388 
390 
394  extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod);
395 
397 
401  extern void tools_make_date(const std::string & chemin, infinint access, infinint modif);
402 
404 
408  extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
409 
413  extern void tools_to_upper(char *nts);
414 
418  extern void tools_to_upper(std::string & r);
419 
421 
424  extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
425 
427 
435  extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
436 
437 
439 
448  extern std::string tools_printf(const char *format, ...);
449 
451 
460  extern std::string tools_vprintf(const char *format, va_list ap);
461 
463 
468  extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
469 
470 
472 
478  extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
479 
480 
482 
491  extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
492  const path & chemin,
493  const std::string & x_file_mask,
494  bool info_details,
495  bool allow_overwriting,
496  bool warn_overwriting,
497  bool dry_run);
498 
500 
503  extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size);
504 
505 
507 
513  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
514 
515 
517 
521  extern path tools_relative2absolute_path(const path & src, const path & cwd);
522 
524 
527  extern void tools_block_all_signals(sigset_t &old_mask);
528 
530 
533  extern void tools_set_back_blocked_signals(sigset_t old_mask);
534 
536 
540  extern U_I tools_count_in_string(const std::string & s, const char a);
541 
543 
546  extern infinint tools_get_mtime(const std::string & s);
547 
549 
552  extern infinint tools_get_size(const std::string & s);
553 
555 
558  extern infinint tools_get_ctime(const std::string & s);
559 
561 
565  extern std::vector<std::string> tools_split_in_words(generic_file & f);
566 
568 
577  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
578 
579 
581 
585  extern std::string tools_substitute(const std::string & hook,
586  const std::map<char, std::string> & corres);
587 
588 
590 
600  extern std::string tools_hook_substitute(const std::string & hook,
601  const std::string & path,
602  const std::string & basename,
603  const std::string & num,
604  const std::string & padded_num,
605  const std::string & ext,
606  const std::string & context);
607 
608 
610 
613  extern void tools_hook_execute(user_interaction & ui,
614  const std::string & cmd_line);
615 
616 
618 
627  extern void tools_hook_substitute_and_execute(user_interaction & ui,
628  const std::string & hook,
629  const std::string & path,
630  const std::string & basename,
631  const std::string & num,
632  const std::string & padded_num,
633  const std::string & ext,
634  const std::string & context);
635 
637 
638 
642  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
643  const std::string & relative_part);
644 
646 
649  extern std::string tools_output2xml(const std::string & src);
650 
652 
655  extern U_I tools_octal2int(const std::string & perm);
656 
657 
659 
662  extern std::string tools_int2octal(const U_I & perm);
663 
665 
668  extern void tools_set_permission(S_I fd, U_I perm);
669 
671 
676  extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group);
677 
679 
684  extern void tools_memxor(void *dest, const void *src, U_I n);
685 
687 
692  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
693 
694 
695 
697 
701  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
702 
703 
704 
706 
710  extern U_I tools_pseudo_random(U_I max);
711 
712 
714 
721 
722  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
723  {
724  std::vector<B> ret;
725 
726  if(base <= 0)
727  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
728 
729  while(number != 0)
730  {
731  ret.push_back(number % base);
732  number /= base;
733  }
734 
735  return ret;
736  }
737 
739 
742  std::string tools_unsigned_char_to_hexa(unsigned char x);
743 
745 
748 
749  std::string tools_string_to_hexa(const std::string & input);
750 
752 
755  extern infinint tools_file_size_to_crc_size(const infinint & size);
756 
758 
759  extern std::string tools_get_euid();
760 
761 
763 
764  extern std::string tools_get_egid();
765 
767 
768  extern std::string tools_get_hostname();
769 
771 
772  extern std::string tools_get_date_utc();
773 
774 }
775 
776 #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, '\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
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 ...
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:722
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_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'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's configuration
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
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
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
void tools_to_upper(char *nts)
convert a string to upper case
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 '\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.
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:171
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
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.
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
bool libbz2()
returns whether libdar is dependent on libbz2 and if so has bzip2 compression/decompression available...
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
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