Disk ARchive
2.4.5
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id$ 00022 // 00023 /*********************************************************************/ 00024 00025 00035 00039 00040 00041 #ifndef TOOLS_HPP 00042 #define TOOLS_HPP 00043 00044 #include "../my_config.h" 00045 00046 extern "C" 00047 { 00048 #if STDC_HEADERS 00049 #include <stdarg.h> 00050 #endif 00051 #if HAVE_SIGNAL_H 00052 #include <signal.h> 00053 #endif 00054 } 00055 00056 #include <string> 00057 #include <vector> 00058 #include <map> 00059 #include "path.hpp" 00060 #include "infinint.hpp" 00061 #include "generic_file.hpp" 00062 #include "tuyau.hpp" 00063 #include "integers.hpp" 00064 #include "tlv_list.hpp" 00065 00066 #define TOOLS_SI_SUFFIX 1000 00067 #define TOOLS_BIN_SUFFIX 1024 00068 00069 namespace libdar 00070 { 00071 00074 00075 00077 extern void tools_init(); 00079 extern void tools_end(); 00080 00082 00087 extern char *tools_str2charptr(const std::string &x); 00088 00090 00093 extern void tools_write_string(generic_file & f, const std::string & s); 00094 00096 00099 extern void tools_read_string(generic_file & f, std::string & s); 00100 00102 00105 extern void tools_write_string_all(generic_file & f, const std::string & s); 00106 00108 00112 extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille); 00113 00115 00118 extern infinint tools_get_filesize(const path &p); 00119 00121 00125 extern infinint tools_get_extended_size(std::string s, U_I base); 00126 00128 00132 extern void tools_extract_basename(const char *command_name, std::string & basename); 00133 00134 00136 00142 extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v); 00143 00145 00151 extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v); 00152 00154 00159 extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base); 00160 00162 00167 extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base); 00168 00170 00177 extern void tools_open_pipes(user_interaction & dialog, const std::string &input, const std::string & output, 00178 tuyau *&in, tuyau *&out); 00179 00181 00184 extern void tools_blocking_read(int fd, bool mode); 00185 00187 00190 extern std::string tools_name_of_uid(const infinint & uid); 00191 00193 00196 extern std::string tools_name_of_gid(const infinint & gid); 00197 00199 00202 extern std::string tools_uword2str(U_16 x); 00203 00205 00208 extern std::string tools_int2str(S_I x); 00209 00211 00214 extern U_I tools_str2int(const std::string & x); 00215 00217 00220 extern S_I tools_str2signed_int(const std::string & x); 00221 00223 00229 extern bool tools_my_atoi(const char *a, U_I & val); 00230 00232 00236 extern std::string tools_addspacebefore(std::string s, U_I expected_size); 00237 00239 00242 extern std::string tools_display_date(infinint date); 00243 00245 00249 extern infinint tools_convert_date(const std::string & repres); 00250 00252 00255 extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector); 00256 00258 00264 extern void tools_system_with_pipe(user_interaction & dialog, const std::string & dar_cmd, const std::vector<std::string> & argvpipe); 00265 00267 00270 extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x); 00271 00273 00276 extern void tools_read_vector(generic_file & f, std::vector<std::string> & x); 00277 00279 00283 extern std::string tools_concat_vector(const std::string & separator, 00284 const std::vector<std::string> & x); 00285 00287 00291 std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b); 00292 00294 00298 extern bool tools_is_member(const std::string & val, const std::vector<std::string> & liste); 00299 00301 00317 extern void tools_display_features(user_interaction & dialog, 00318 bool ea, 00319 bool largefile, 00320 bool nodump, 00321 bool special_alloc, 00322 U_I bits, 00323 bool thread_safe, 00324 bool libz, 00325 bool libbz2, 00326 bool liblzo2, 00327 bool libcrypto, 00328 bool furtive_read); 00329 00331 00335 extern void tools_display_features(user_interaction & dialog); 00336 00337 00339 00344 extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const infinint & date1, const infinint & date2); 00345 00347 00348 template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b) 00349 { 00350 a = a + b; 00351 return a; 00352 } 00353 00354 00356 00361 extern const char *tools_get_from_env(const char **env, const char *clef); 00362 00364 00370 extern void tools_check_basename(user_interaction & dialog, 00371 const path & loc, std::string & base, const std::string & extension); 00372 00374 00375 extern std::string tools_getcwd(); 00376 00378 00382 extern std::string tools_readlink(const char *root); 00383 00385 00390 extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]); 00391 00392 00394 00398 extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod); 00399 00401 00405 extern void tools_make_date(const std::string & chemin, infinint access, infinint modif); 00406 00408 00412 extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b); 00413 00417 extern void tools_to_upper(char *nts); 00418 00422 extern void tools_to_upper(std::string & r); 00423 00425 00428 extern void tools_remove_last_char_if_equal_to(char c, std::string & s); 00429 00431 00439 extern void tools_read_range(const std::string & s, S_I & min, U_I & max); 00440 00441 00443 00452 extern std::string tools_printf(const char *format, ...); 00453 00455 00464 extern std::string tools_vprintf(const char *format, va_list ap); 00465 00467 00472 extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask); 00473 00474 00476 00482 extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details); 00483 00484 00486 00495 extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog, 00496 const path & chemin, 00497 const std::string & x_file_mask, 00498 bool info_details, 00499 bool allow_overwriting, 00500 bool warn_overwriting, 00501 bool dry_run); 00502 00504 00507 extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size); 00508 00509 00511 00517 extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2); 00518 00519 00521 00525 extern path tools_relative2absolute_path(const path & src, const path & cwd); 00526 00528 00531 extern void tools_block_all_signals(sigset_t &old_mask); 00532 00534 00537 extern void tools_set_back_blocked_signals(sigset_t old_mask); 00538 00540 00544 extern U_I tools_count_in_string(const std::string & s, const char a); 00545 00547 00550 extern infinint tools_get_mtime(const std::string & s); 00551 00553 00556 extern infinint tools_get_ctime(const std::string & s); 00557 00559 00563 extern std::vector<std::string> tools_split_in_words(generic_file & f); 00564 00566 00575 extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found); 00576 00577 00579 00583 extern std::string tools_substitute(const std::string & hook, 00584 const std::map<char, std::string> & corres); 00585 00586 00588 00598 extern std::string tools_hook_substitute(const std::string & hook, 00599 const std::string & path, 00600 const std::string & basename, 00601 const std::string & num, 00602 const std::string & padded_num, 00603 const std::string & ext, 00604 const std::string & context); 00605 00606 00608 00611 extern void tools_hook_execute(user_interaction & ui, 00612 const std::string & cmd_line); 00613 00614 00616 00625 extern void tools_hook_substitute_and_execute(user_interaction & ui, 00626 const std::string & hook, 00627 const std::string & path, 00628 const std::string & basename, 00629 const std::string & num, 00630 const std::string & padded_num, 00631 const std::string & ext, 00632 const std::string & context); 00633 00635 00636 00640 extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix, 00641 const std::string & relative_part); 00642 00644 00647 extern std::string tools_output2xml(const std::string & src); 00648 00650 00653 extern U_I tools_octal2int(const std::string & perm); 00654 00655 00657 00660 extern std::string tools_int2octal(const U_I & perm); 00661 00663 00666 extern void tools_set_permission(S_I fd, U_I perm); 00667 00669 00674 extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group); 00675 00677 00682 extern void tools_memxor(void *dest, const void *src, U_I n); 00683 00685 00690 extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data); 00691 00692 00693 00695 00699 extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result); 00700 00701 00702 00704 00708 extern U_I tools_pseudo_random(U_I max); 00709 00710 00712 00719 00720 template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base) 00721 { 00722 std::vector<B> ret; 00723 00724 if(base <= 0) 00725 throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive"); 00726 00727 while(number != 0) 00728 { 00729 ret.push_back(number % base); 00730 number /= base; 00731 } 00732 00733 return ret; 00734 } 00735 00737 00740 std::string tools_unsigned_char_to_hexa(unsigned char x); 00741 00743 00746 00747 std::string tools_string_to_hexa(const std::string & input); 00748 00750 00753 extern infinint tools_file_size_to_crc_size(const infinint & size); 00754 00756 00757 extern std::string tools_get_euid(); 00758 00759 00761 00762 extern std::string tools_get_egid(); 00763 00765 00766 extern std::string tools_get_hostname(); 00767 00769 00770 extern std::string tools_get_date_utc(); 00771 00772 } 00773 00774 #endif