![]() |
Disk ARchive
2.5.2
Full featured and portable backup and archiving tool
|
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 00031 00035 00036 00037 #ifndef TOOLS_HPP 00038 #define TOOLS_HPP 00039 00040 #include "../my_config.h" 00041 00042 extern "C" 00043 { 00044 #if STDC_HEADERS 00045 #include <stdarg.h> 00046 #endif 00047 #if HAVE_SIGNAL_H 00048 #include <signal.h> 00049 #endif 00050 #if HAVE_GPGME_H 00051 #include <gpgme.h> 00052 #endif 00053 } 00054 00055 #include <string> 00056 #include <vector> 00057 #include <map> 00058 #include "path.hpp" 00059 #include "infinint.hpp" 00060 #include "generic_file.hpp" 00061 #include "tuyau.hpp" 00062 #include "integers.hpp" 00063 #include "tlv_list.hpp" 00064 #include "memory_pool.hpp" 00065 #include "datetime.hpp" 00066 00067 #define TOOLS_SI_SUFFIX 1000 00068 #define TOOLS_BIN_SUFFIX 1024 00069 00070 namespace libdar 00071 { 00072 00075 00076 00078 extern void tools_init(); 00080 extern void tools_end(); 00081 00083 00088 extern char *tools_str2charptr(const std::string &x); 00089 00091 00094 extern void tools_write_string(generic_file & f, const std::string & s); 00095 00097 00100 extern void tools_read_string(generic_file & f, std::string & s); 00101 00103 00106 extern void tools_write_string_all(generic_file & f, const std::string & s); 00107 00109 00113 extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille); 00114 00116 00119 extern infinint tools_get_filesize(const path &p); 00120 00122 00126 extern infinint tools_get_extended_size(std::string s, U_I base); 00127 00133 extern std::string tools_display_integer_in_metric_system(infinint number, const std::string & unit, bool binary); 00134 00136 00140 extern void tools_extract_basename(const char *command_name, std::string & basename); 00141 00142 00144 00150 extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v); 00151 00153 00159 extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v); 00160 00162 00168 extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base, memory_pool *pool = nullptr); 00169 00171 00177 extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base, memory_pool *pool = nullptr); 00178 00180 00188 extern void tools_open_pipes(user_interaction & dialog, 00189 const std::string &input, 00190 const std::string & output, 00191 tuyau *&in, 00192 tuyau *&out, 00193 memory_pool *pool = nullptr); 00194 00196 00199 extern void tools_blocking_read(int fd, bool mode); 00200 00202 00205 extern std::string tools_name_of_uid(const infinint & uid); 00206 00208 00211 extern std::string tools_name_of_gid(const infinint & gid); 00212 00214 00217 extern std::string tools_uword2str(U_16 x); 00218 00220 00223 extern std::string tools_int2str(S_I x); 00224 extern std::string tools_uint2str(U_I x); 00225 00227 00230 extern U_I tools_str2int(const std::string & x); 00231 00233 00236 extern S_I tools_str2signed_int(const std::string & x); 00237 00239 00245 extern bool tools_my_atoi(const char *a, U_I & val); 00246 00248 00252 extern std::string tools_addspacebefore(std::string s, U_I expected_size); 00253 00255 00258 extern std::string tools_display_date(const datetime & date); 00259 00261 00265 extern infinint tools_convert_date(const std::string & repres); 00266 00268 00271 extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector); 00272 00274 00281 extern void tools_system_with_pipe(user_interaction & dialog, 00282 const std::string & dar_cmd, 00283 const std::vector<std::string> & argvpipe, 00284 memory_pool *pool = nullptr); 00285 00287 00290 extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x); 00291 00293 00296 extern void tools_read_vector(generic_file & f, std::vector<std::string> & x); 00297 00299 00303 extern std::string tools_concat_vector(const std::string & separator, 00304 const std::vector<std::string> & x); 00305 00307 00311 std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b); 00312 00313 00315 00319 extern void tools_display_features(user_interaction & dialog); 00320 00321 00323 00328 extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const datetime & date1, const datetime & date2); 00329 00331 00332 template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b) 00333 { 00334 a = a + b; 00335 return a; 00336 } 00337 00338 00340 00345 extern const char *tools_get_from_env(const char **env, const char *clef); 00346 00348 00355 extern void tools_check_basename(user_interaction & dialog, 00356 const path & loc, 00357 std::string & base, 00358 const std::string & extension, 00359 memory_pool *pool = nullptr); 00360 00362 00363 extern std::string tools_getcwd(); 00364 00366 00370 extern std::string tools_readlink(const char *root); 00371 00373 00379 extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]); 00380 00381 00383 00389 extern void tools_noexcept_make_date(const std::string & chem, bool symlink, const datetime & last_acc, const datetime & last_mod, const datetime & birth); 00390 00392 00399 extern void tools_make_date(const std::string & chemin, bool symlink, const datetime & access, const datetime & modif, const datetime & birth); 00400 00402 00406 extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b); 00407 00414 extern void tools_to_upper(const std::string & r, std::string & uppered); 00415 00416 #if HAVE_WCTYPE_H 00417 00418 00419 00420 00421 00422 00423 extern void tools_to_wupper(std::wstring & r); 00424 #endif 00425 00427 00430 extern void tools_remove_last_char_if_equal_to(char c, std::string & s); 00431 00433 00441 extern void tools_read_range(const std::string & s, S_I & min, U_I & max); 00442 00443 00445 00455 extern std::string tools_printf(const char *format, ...); 00456 00458 00467 extern std::string tools_vprintf(const char *format, va_list ap); 00468 00470 00475 extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask); 00476 00477 00479 00485 extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details); 00486 00487 00489 00498 extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog, 00499 const path & chemin, 00500 const std::string & x_file_mask, 00501 bool info_details, 00502 bool allow_overwriting, 00503 bool warn_overwriting, 00504 bool dry_run); 00505 00507 00526 extern void tools_add_elastic_buffer(generic_file & f, 00527 U_32 max_size, 00528 U_32 modulo, 00529 U_32 offset); 00530 00531 00533 00539 extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2); 00540 00541 00543 00547 extern path tools_relative2absolute_path(const path & src, const path & cwd); 00548 00550 00553 extern void tools_block_all_signals(sigset_t &old_mask); 00554 00556 00559 extern void tools_set_back_blocked_signals(sigset_t old_mask); 00560 00562 00566 extern U_I tools_count_in_string(const std::string & s, const char a); 00567 00569 00572 extern datetime tools_get_mtime(const std::string & s); 00573 00575 00578 extern infinint tools_get_size(const std::string & s); 00579 00581 00584 extern datetime tools_get_ctime(const std::string & s); 00585 00587 00591 extern std::vector<std::string> tools_split_in_words(generic_file & f); 00592 00593 00595 00599 extern std::vector<std::string> tools_split_in_words(const std::string & arg); 00600 00601 00603 00612 extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what, U_32 start, U_32 & found); 00613 00614 00616 00620 extern std::string tools_substitute(const std::string & hook, 00621 const std::map<char, std::string> & corres); 00622 00623 00625 00635 extern std::string tools_hook_substitute(const std::string & hook, 00636 const std::string & path, 00637 const std::string & basename, 00638 const std::string & num, 00639 const std::string & padded_num, 00640 const std::string & ext, 00641 const std::string & context); 00642 00643 00645 00648 extern void tools_hook_execute(user_interaction & ui, 00649 const std::string & cmd_line); 00650 00651 00653 00662 extern void tools_hook_substitute_and_execute(user_interaction & ui, 00663 const std::string & hook, 00664 const std::string & path, 00665 const std::string & basename, 00666 const std::string & num, 00667 const std::string & padded_num, 00668 const std::string & ext, 00669 const std::string & context); 00670 00672 00673 00677 extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix, 00678 const std::string & relative_part); 00679 00681 00684 extern std::string tools_output2xml(const std::string & src); 00685 00687 00690 extern U_I tools_octal2int(const std::string & perm); 00691 00692 00694 00697 extern std::string tools_int2octal(const U_I & perm); 00698 00700 00701 extern std::string tools_get_permission_string(char type, U_32 perm, bool hard); 00702 00704 00707 extern void tools_set_permission(S_I fd, U_I perm); 00708 00710 00714 extern U_I tools_get_permission(S_I fd); 00715 00717 00719 00722 extern uid_t tools_ownership2uid(const std::string & user); 00723 00725 00728 extern uid_t tools_ownership2gid(const std::string & group); 00729 00731 00736 extern void tools_set_ownership(S_I filedesc, const std::string & slice_user, const std::string & slice_group); 00737 00739 00744 extern void tools_memxor(void *dest, const void *src, U_I n); 00745 00747 00752 extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data); 00753 00754 00755 00757 00761 extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result); 00762 00763 00764 00766 00769 extern U_I tools_pseudo_random(U_I max); 00770 00771 00773 00780 00781 template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base) 00782 { 00783 std::vector<B> ret; 00784 00785 if(base <= 0) 00786 throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive"); 00787 00788 while(number != 0) 00789 { 00790 ret.push_back(number % base); 00791 number /= base; 00792 } 00793 00794 return ret; 00795 } 00796 00798 00801 std::string tools_unsigned_char_to_hexa(unsigned char x); 00802 00804 00807 00808 std::string tools_string_to_hexa(const std::string & input); 00809 00811 00814 extern infinint tools_file_size_to_crc_size(const infinint & size); 00815 00817 extern std::string tools_get_euid(); 00818 00820 extern std::string tools_get_egid(); 00821 00823 extern std::string tools_get_hostname(); 00824 00826 extern std::string tools_get_date_utc(); 00827 00829 extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size, bool compressed); 00830 00832 extern std::string tools_strerror_r(int errnum); 00833 00834 #ifdef GPGME_SUPPORT 00835 00836 extern std::string tools_gpgme_strerror_r(gpgme_error_t err); 00837 #endif 00838 00839 #if HAVE_WCHAR_H 00840 00841 extern std::wstring tools_string_to_wstring(const std::string & val); 00842 00844 extern std::string tools_wstring_to_string(const std::wstring & val); 00845 #endif 00846 00848 extern void tools_merge_to_vector(std::vector<std::string> & a, const std::vector<std::string> & b); 00849 00851 extern std::vector<std::string> tools_substract_from_vector(const std::vector<std::string> & a, const std::vector<std::string> & b); 00852 00862 struct dirent *tools_allocate_struct_dirent(const std::string & path_name, memory_pool *pool = nullptr); 00863 00864 00868 extern void tools_release_struct_dirent(struct dirent *ptr); 00869 00871 extern void tools_secu_string_show(user_interaction & dialog, const std::string & msg, const secu_string & key); 00872 00873 template <class T> T tools_max(T a, T b) { return a > b ? a : b; } 00874 template <class T> T tools_min(T a, T b) { return a > b ? b : a; } 00875 00876 00877 } 00878 00879 #endif