Disk ARchive  2.4.5
 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 // $Id: tools.hpp,v 1.85.2.1 2012/04/09 14:28:33 edrusb Exp $
22 //
23 /*********************************************************************/
24 
25 
35 
39 
40 
41 #ifndef TOOLS_HPP
42 #define TOOLS_HPP
43 
44 #include "../my_config.h"
45 
46 extern "C"
47 {
48 #if STDC_HEADERS
49 #include <stdarg.h>
50 #endif
51 #if HAVE_SIGNAL_H
52 #include <signal.h>
53 #endif
54 }
55 
56 #include <string>
57 #include <vector>
58 #include <map>
59 #include "path.hpp"
60 #include "infinint.hpp"
61 #include "generic_file.hpp"
62 #include "tuyau.hpp"
63 #include "integers.hpp"
64 #include "tlv_list.hpp"
65 
66 #define TOOLS_SI_SUFFIX 1000
67 #define TOOLS_BIN_SUFFIX 1024
68 
69 namespace libdar
70 {
71 
74 
75 
77  extern void tools_init();
79  extern void tools_end();
80 
82 
87  extern char *tools_str2charptr(const std::string &x);
88 
90 
93  extern void tools_write_string(generic_file & f, const std::string & s);
94 
96 
99  extern void tools_read_string(generic_file & f, std::string & s);
100 
102 
105  extern void tools_write_string_all(generic_file & f, const std::string & s);
106 
108 
112  extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
113 
115 
118  extern infinint tools_get_filesize(const path &p);
119 
121 
125  extern infinint tools_get_extended_size(std::string s, U_I base);
126 
128 
132  extern void tools_extract_basename(const char *command_name, std::string & basename);
133 
134 
136 
142  extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
143 
145 
151  extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v);
152 
154 
159  extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base);
160 
162 
167  extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base);
168 
170 
177  extern void tools_open_pipes(user_interaction & dialog, const std::string &input, const std::string & output,
178  tuyau *&in, tuyau *&out);
179 
181 
184  extern void tools_blocking_read(int fd, bool mode);
185 
187 
190  extern std::string tools_name_of_uid(const infinint & uid);
191 
193 
196  extern std::string tools_name_of_gid(const infinint & gid);
197 
199 
202  extern std::string tools_uword2str(U_16 x);
203 
205 
208  extern std::string tools_int2str(S_I x);
209 
211 
214  extern U_I tools_str2int(const std::string & x);
215 
217 
220  extern S_I tools_str2signed_int(const std::string & x);
221 
223 
229  extern bool tools_my_atoi(const char *a, U_I & val);
230 
232 
236  extern std::string tools_addspacebefore(std::string s, U_I expected_size);
237 
239 
242  extern std::string tools_display_date(infinint date);
243 
245 
249  extern infinint tools_convert_date(const std::string & repres);
250 
252 
255  extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
256 
258 
264  extern void tools_system_with_pipe(user_interaction & dialog, const std::string & dar_cmd, const std::vector<std::string> & argvpipe);
265 
267 
270  extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
271 
273 
276  extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
277 
279 
283  extern std::string tools_concat_vector(const std::string & separator,
284  const std::vector<std::string> & x);
285 
287 
291  std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
292 
294 
298  extern bool tools_is_member(const std::string & val, const std::vector<std::string> & liste);
299 
301 
317  extern void tools_display_features(user_interaction & dialog,
318  bool ea,
319  bool largefile,
320  bool nodump,
321  bool special_alloc,
322  U_I bits,
323  bool thread_safe,
324  bool libz,
325  bool libbz2,
326  bool liblzo2,
327  bool libcrypto,
328  bool furtive_read);
329 
331 
335  extern void tools_display_features(user_interaction & dialog);
336 
337 
339 
344  extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const infinint & date1, const infinint & date2);
345 
347 
348  template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
349  {
350  a = a + b;
351  return a;
352  }
353 
354 
356 
361  extern const char *tools_get_from_env(const char **env, const char *clef);
362 
364 
370  extern void tools_check_basename(user_interaction & dialog,
371  const path & loc, std::string & base, const std::string & extension);
372 
374 
375  extern std::string tools_getcwd();
376 
378 
382  extern std::string tools_readlink(const char *root);
383 
385 
390  extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
391 
392 
394 
398  extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod);
399 
401 
405  extern void tools_make_date(const std::string & chemin, infinint access, infinint modif);
406 
408 
412  extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
413 
417  extern void tools_to_upper(char *nts);
418 
422  extern void tools_to_upper(std::string & r);
423 
425 
428  extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
429 
431 
439  extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
440 
441 
443 
452  extern std::string tools_printf(const char *format, ...);
453 
455 
464  extern std::string tools_vprintf(const char *format, va_list ap);
465 
467 
472  extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
473 
474 
476 
482  extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
483 
484 
486 
495  extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
496  const path & chemin,
497  const std::string & x_file_mask,
498  bool info_details,
499  bool allow_overwriting,
500  bool warn_overwriting,
501  bool dry_run);
502 
504 
507  extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size);
508 
509 
511 
517  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
518 
519 
521 
525  extern path tools_relative2absolute_path(const path & src, const path & cwd);
526 
528 
531  extern void tools_block_all_signals(sigset_t &old_mask);
532 
534 
537  extern void tools_set_back_blocked_signals(sigset_t old_mask);
538 
540 
544  extern U_I tools_count_in_string(const std::string & s, const char a);
545 
547 
550  extern infinint tools_get_mtime(const std::string & s);
551 
553 
556  extern infinint tools_get_ctime(const std::string & s);
557 
559 
563  extern std::vector<std::string> tools_split_in_words(generic_file & f);
564 
566 
575  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
576 
577 
579 
583  extern std::string tools_substitute(const std::string & hook,
584  const std::map<char, std::string> & corres);
585 
586 
588 
598  extern std::string tools_hook_substitute(const std::string & hook,
599  const std::string & path,
600  const std::string & basename,
601  const std::string & num,
602  const std::string & padded_num,
603  const std::string & ext,
604  const std::string & context);
605 
606 
608 
611  extern void tools_hook_execute(user_interaction & ui,
612  const std::string & cmd_line);
613 
614 
616 
625  extern void tools_hook_substitute_and_execute(user_interaction & ui,
626  const std::string & hook,
627  const std::string & path,
628  const std::string & basename,
629  const std::string & num,
630  const std::string & padded_num,
631  const std::string & ext,
632  const std::string & context);
633 
635 
636 
640  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
641  const std::string & relative_part);
642 
644 
647  extern std::string tools_output2xml(const std::string & src);
648 
650 
653  extern U_I tools_octal2int(const std::string & perm);
654 
655 
657 
660  extern std::string tools_int2octal(const U_I & perm);
661 
663 
666  extern void tools_set_permission(S_I fd, U_I perm);
667 
669 
674  extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group);
675 
677 
682  extern void tools_memxor(void *dest, const void *src, U_I n);
683 
685 
690  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
691 
692 
693 
695 
699  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
700 
701 
702 
704 
708  extern U_I tools_pseudo_random(U_I max);
709 
710 
712 
719 
720  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
721  {
722  std::vector<B> ret;
723 
724  if(base <= 0)
725  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
726 
727  while(number != 0)
728  {
729  ret.push_back(number % base);
730  number /= base;
731  }
732 
733  return ret;
734  }
735 
737 
740  std::string tools_unsigned_char_to_hexa(unsigned char x);
741 
743 
746 
747  std::string tools_string_to_hexa(const std::string & input);
748 
750 
753  extern infinint tools_file_size_to_crc_size(const infinint & size);
754 
756 
757  extern std::string tools_get_euid();
758 
759 
761 
762  extern std::string tools_get_egid();
763 
765 
766  extern std::string tools_get_hostname();
767 
769 
770  extern std::string tools_get_date_utc();
771 
772 }
773 
774 #endif