Disk ARchive  2.4.2
 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 2011/04/19 16:24:29 edrusb Rel $
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 
76 
81  extern char *tools_str2charptr(const std::string &x);
82 
84 
87  extern void tools_write_string(generic_file & f, const std::string & s);
88 
90 
93  extern void tools_read_string(generic_file & f, std::string & s);
94 
96 
99  extern void tools_write_string_all(generic_file & f, const std::string & s);
100 
102 
106  extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
107 
109 
112  extern infinint tools_get_filesize(const path &p);
113 
115 
119  extern infinint tools_get_extended_size(std::string s, U_I base);
120 
122 
126  extern void tools_extract_basename(const char *command_name, std::string & basename);
127 
128 
130 
136  extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
137 
139 
145  extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v);
146 
148 
153  extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base);
154 
156 
161  extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base);
162 
164 
171  extern void tools_open_pipes(user_interaction & dialog, const std::string &input, const std::string & output,
172  tuyau *&in, tuyau *&out);
173 
175 
178  extern void tools_blocking_read(int fd, bool mode);
179 
181 
184  extern std::string tools_name_of_uid(const infinint & uid);
185 
187 
190  extern std::string tools_name_of_gid(const infinint & gid);
191 
193 
196  extern std::string tools_uword2str(U_16 x);
197 
199 
202  extern std::string tools_int2str(S_I x);
203 
205 
208  extern U_I tools_str2int(const std::string & x);
209 
211 
214  extern S_I tools_str2signed_int(const std::string & x);
215 
217 
223  extern bool tools_my_atoi(const char *a, U_I & val);
224 
226 
230  extern std::string tools_addspacebefore(std::string s, U_I expected_size);
231 
233 
236  extern std::string tools_display_date(infinint date);
237 
239 
243  extern infinint tools_convert_date(const std::string & repres);
244 
246 
249  extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
250 
252 
258  extern void tools_system_with_pipe(user_interaction & dialog, const std::string & dar_cmd, const std::vector<std::string> & argvpipe);
259 
261 
264  extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
265 
267 
270  extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
271 
273 
277  extern std::string tools_concat_vector(const std::string & separator,
278  const std::vector<std::string> & x);
279 
281 
285  std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
286 
288 
292  extern bool tools_is_member(const std::string & val, const std::vector<std::string> & liste);
293 
295 
311  extern void tools_display_features(user_interaction & dialog,
312  bool ea,
313  bool largefile,
314  bool nodump,
315  bool special_alloc,
316  U_I bits,
317  bool thread_safe,
318  bool libz,
319  bool libbz2,
320  bool liblzo2,
321  bool libcrypto,
322  bool furtive_read);
323 
325 
329  extern void tools_display_features(user_interaction & dialog);
330 
331 
333 
338  extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const infinint & date1, const infinint & date2);
339 
341 
342  template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
343  {
344  a = a + b;
345  return a;
346  }
347 
348 
350 
355  extern const char *tools_get_from_env(const char **env, const char *clef);
356 
358 
364  extern void tools_check_basename(user_interaction & dialog,
365  const path & loc, std::string & base, const std::string & extension);
366 
368 
369  extern std::string tools_getcwd();
370 
372 
376  extern std::string tools_readlink(const char *root);
377 
379 
384  extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
385 
386 
388 
392  extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod);
393 
395 
399  extern void tools_make_date(const std::string & chemin, infinint access, infinint modif);
400 
402 
406  extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
407 
411  extern void tools_to_upper(char *nts);
412 
416  extern void tools_to_upper(std::string & r);
417 
419 
422  extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
423 
425 
433  extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
434 
435 
437 
446  extern std::string tools_printf(const char *format, ...);
447 
449 
458  extern std::string tools_vprintf(const char *format, va_list ap);
459 
461 
466  extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
467 
468 
470 
476  extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
477 
478 
480 
489  extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
490  const path & chemin,
491  const std::string & x_file_mask,
492  bool info_details,
493  bool allow_overwriting,
494  bool warn_overwriting,
495  bool dry_run);
496 
498 
501  extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size);
502 
503 
505 
511  extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
512 
513 
515 
519  extern path tools_relative2absolute_path(const path & src, const path & cwd);
520 
522 
525  extern void tools_block_all_signals(sigset_t &old_mask);
526 
528 
531  extern void tools_set_back_blocked_signals(sigset_t old_mask);
532 
534 
538  extern U_I tools_count_in_string(const std::string & s, const char a);
539 
541 
544  extern infinint tools_get_mtime(const std::string & s);
545 
547 
550  extern infinint tools_get_ctime(const std::string & s);
551 
553 
557  extern std::vector<std::string> tools_split_in_words(generic_file & f);
558 
560 
569  extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found);
570 
571 
573 
577  extern std::string tools_substitute(const std::string & hook,
578  const std::map<char, std::string> & corres);
579 
580 
582 
592  extern std::string tools_hook_substitute(const std::string & hook,
593  const std::string & path,
594  const std::string & basename,
595  const std::string & num,
596  const std::string & padded_num,
597  const std::string & ext,
598  const std::string & context);
599 
600 
602 
605  extern void tools_hook_execute(user_interaction & ui,
606  const std::string & cmd_line);
607 
608 
610 
619  extern void tools_hook_substitute_and_execute(user_interaction & ui,
620  const std::string & hook,
621  const std::string & path,
622  const std::string & basename,
623  const std::string & num,
624  const std::string & padded_num,
625  const std::string & ext,
626  const std::string & context);
627 
629 
630 
634  extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
635  const std::string & relative_part);
636 
638 
641  extern std::string tools_output2xml(const std::string & src);
642 
644 
647  extern U_I tools_octal2int(const std::string & perm);
648 
649 
651 
654  extern std::string tools_int2octal(const U_I & perm);
655 
657 
660  extern void tools_set_permission(S_I fd, U_I perm);
661 
663 
668  extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group);
669 
671 
676  extern void tools_memxor(void *dest, const void *src, U_I n);
677 
679 
684  extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
685 
686 
687 
689 
693  extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
694 
695 
696 
698 
702  extern U_I tools_pseudo_random(U_I max);
703 
704 
706 
713 
714  template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
715  {
716  std::vector<B> ret;
717 
718  if(base <= 0)
719  throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
720 
721  while(number != 0)
722  {
723  ret.push_back(number % base);
724  number /= base;
725  }
726 
727  return ret;
728  }
729 
731 
734  std::string tools_unsigned_char_to_hexa(unsigned char x);
735 
737 
740 
741  std::string tools_string_to_hexa(const std::string & input);
742 
744 
747  extern infinint tools_file_size_to_crc_size(const infinint & size);
748 
750 
751  extern std::string tools_get_euid();
752 
753 
755 
756  extern std::string tools_get_egid();
757 
759 
760  extern std::string tools_get_hostname();
761 
763 
764  extern std::string tools_get_date_utc();
765 
766 }
767 
768 #endif