Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
tools.hpp
Go to the documentation of this file.
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 }
00051 
00052 #include <string>
00053 #include <vector>
00054 #include <map>
00055 #include "path.hpp"
00056 #include "infinint.hpp"
00057 #include "generic_file.hpp"
00058 #include "tuyau.hpp"
00059 #include "integers.hpp"
00060 #include "tlv_list.hpp"
00061 
00062 #define TOOLS_SI_SUFFIX 1000
00063 #define TOOLS_BIN_SUFFIX 1024
00064 
00065 namespace libdar
00066 {
00067 
00070 
00071 
00073     extern void tools_init();
00075     extern void tools_end();
00076 
00078 
00083     extern char *tools_str2charptr(const std::string &x);
00084 
00086 
00089     extern void tools_write_string(generic_file & f, const std::string & s);
00090 
00092 
00095     extern void tools_read_string(generic_file & f, std::string & s);
00096 
00098 
00101     extern void tools_write_string_all(generic_file & f, const std::string & s);
00102 
00104 
00108     extern void tools_read_string_size(generic_file & f, std::string & s, infinint taille);
00109 
00111 
00114     extern infinint tools_get_filesize(const path &p);
00115 
00117 
00121     extern infinint tools_get_extended_size(std::string s, U_I base);
00122 
00124 
00128     extern void tools_extract_basename(const char *command_name, std::string & basename);
00129 
00130 
00132 
00138     extern std::string::iterator tools_find_last_char_of(std::string &s, unsigned char v);
00139 
00141 
00147     extern std::string::iterator tools_find_first_char_of(std::string &s, unsigned char v);
00148 
00150 
00155     extern void tools_split_path_basename(const char *all, path * &chemin, std::string & base);
00156 
00158 
00163     extern void tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base);
00164 
00166 
00173     extern void tools_open_pipes(user_interaction & dialog, const std::string &input, const std::string & output,
00174                                  tuyau *&in, tuyau *&out);
00175 
00177 
00180     extern void tools_blocking_read(int fd, bool mode);
00181 
00183 
00186     extern std::string tools_name_of_uid(const infinint & uid);
00187 
00189 
00192     extern std::string tools_name_of_gid(const infinint & gid);
00193 
00195 
00198     extern std::string tools_uword2str(U_16 x);
00199 
00201 
00204     extern std::string tools_int2str(S_I x);
00205 
00207 
00210     extern U_I tools_str2int(const std::string & x);
00211 
00213 
00216     extern S_I tools_str2signed_int(const std::string & x);
00217 
00219 
00225     extern bool tools_my_atoi(const char *a, U_I & val);
00226 
00228 
00232     extern std::string tools_addspacebefore(std::string s, U_I expected_size);
00233 
00235 
00238     extern std::string tools_display_date(infinint date);
00239 
00241 
00245     extern infinint tools_convert_date(const std::string & repres);
00246 
00248 
00251     extern void tools_system(user_interaction & dialog, const std::vector<std::string> & argvector);
00252 
00254 
00260     extern void tools_system_with_pipe(user_interaction & dialog, const std::string & dar_cmd, const std::vector<std::string> & argvpipe);
00261 
00263 
00266     extern void tools_write_vector(generic_file & f, const std::vector<std::string> & x);
00267 
00269 
00272     extern void tools_read_vector(generic_file & f, std::vector<std::string> & x);
00273 
00275 
00279     extern std::string tools_concat_vector(const std::string & separator,
00280                                            const std::vector<std::string> & x);
00281 
00283 
00287     std::vector<std::string> operator + (std::vector<std::string> a, std::vector<std::string> b);
00288 
00290 
00294     extern bool tools_is_member(const std::string & val, const std::vector<std::string> & liste);
00295 
00297 
00313     extern void tools_display_features(user_interaction & dialog,
00314                                        bool ea,
00315                                        bool largefile,
00316                                        bool nodump,
00317                                        bool special_alloc,
00318                                        U_I bits,
00319                                        bool thread_safe,
00320                                        bool libz,
00321                                        bool libbz2,
00322                                        bool liblzo2,
00323                                        bool libcrypto,
00324                                        bool furtive_read);
00325 
00327 
00331     extern void tools_display_features(user_interaction & dialog);
00332 
00333 
00335 
00340     extern bool tools_is_equal_with_hourshift(const infinint & hourshift, const infinint & date1, const infinint & date2);
00341 
00343 
00344     template <class T> std::vector<T> operator +=(std::vector<T> & a, const std::vector<T> & b)
00345     {
00346         a = a + b;
00347         return a;
00348     }
00349 
00350 
00352 
00357     extern const char *tools_get_from_env(const char **env, const char *clef);
00358 
00360 
00366     extern void tools_check_basename(user_interaction & dialog,
00367                                      const path & loc, std::string & base, const std::string & extension);
00368 
00370 
00371     extern std::string tools_getcwd();
00372 
00374 
00378     extern std::string tools_readlink(const char *root);
00379 
00381 
00387     extern bool tools_look_for(const char *argument, S_I argc, char *const argv[]);
00388 
00389 
00391 
00395     extern void tools_noexcept_make_date(const std::string & chem, const infinint & last_acc, const infinint & last_mod);
00396 
00398 
00402     extern void tools_make_date(const std::string & chemin, infinint access, infinint modif);
00403 
00405 
00409     extern bool tools_is_case_insensitive_equal(const std::string & a, const std::string & b);
00410 
00414     extern void tools_to_upper(char *nts);
00415 
00419     extern void tools_to_upper(std::string & r);
00420 
00422 
00425     extern void tools_remove_last_char_if_equal_to(char c, std::string & s);
00426 
00428 
00436     extern void tools_read_range(const std::string & s, S_I & min, U_I & max);
00437 
00438 
00440 
00449     extern std::string tools_printf(const char *format, ...);
00450 
00452 
00461     extern std::string tools_vprintf(const char *format, va_list ap);
00462 
00464 
00469     extern bool tools_do_some_files_match_mask_regex(user_interaction & ui, const std::string & c_chemin, const std::string & file_mask);
00470 
00471 
00473 
00479     extern void tools_unlink_file_mask_regex(user_interaction & dialog, const std::string & c_chemin, const std::string & file_mask, bool info_details);
00480 
00481 
00483 
00492     extern void tools_avoid_slice_overwriting_regex(user_interaction & dialog,
00493                                                     const path & chemin,
00494                                                     const std::string & x_file_mask,
00495                                                     bool info_details,
00496                                                     bool allow_overwriting,
00497                                                     bool warn_overwriting,
00498                                                     bool dry_run);
00499 
00501 
00504     extern void tools_add_elastic_buffer(generic_file & f, U_32 max_size);
00505 
00506 
00508 
00514     extern bool tools_are_on_same_filesystem(const std::string & file1, const std::string & file2);
00515 
00516 
00518 
00522     extern path tools_relative2absolute_path(const path & src, const path & cwd);
00523 
00525 
00528     extern void tools_block_all_signals(sigset_t &old_mask);
00529 
00531 
00534     extern void tools_set_back_blocked_signals(sigset_t old_mask);
00535 
00537 
00541     extern U_I tools_count_in_string(const std::string & s, const char a);
00542 
00544 
00547     extern infinint tools_get_mtime(const std::string & s);
00548 
00550 
00553     extern infinint tools_get_size(const std::string & s);
00554 
00556 
00559     extern infinint tools_get_ctime(const std::string & s);
00560 
00562 
00566     extern std::vector<std::string> tools_split_in_words(generic_file & f);
00567 
00569 
00578     extern bool tools_find_next_char_out_of_parenthesis(const std::string & data, const char what,  U_32 start, U_32 & found);
00579 
00580 
00582 
00586     extern std::string tools_substitute(const std::string & hook,
00587                                         const std::map<char, std::string> & corres);
00588 
00589 
00591 
00601     extern std::string tools_hook_substitute(const std::string & hook,
00602                                              const std::string & path,
00603                                              const std::string & basename,
00604                                              const std::string & num,
00605                                              const std::string & padded_num,
00606                                              const std::string & ext,
00607                                              const std::string & context);
00608 
00609 
00611 
00614     extern void tools_hook_execute(user_interaction & ui,
00615                                    const std::string & cmd_line);
00616 
00617 
00619 
00628     extern void tools_hook_substitute_and_execute(user_interaction & ui,
00629                                                   const std::string & hook,
00630                                                   const std::string & path,
00631                                                   const std::string & basename,
00632                                                   const std::string & num,
00633                                                   const std::string & padded_num,
00634                                                   const std::string & ext,
00635                                                   const std::string & context);
00636 
00638 
00639 
00643     extern std::string tools_build_regex_for_exclude_mask(const std::string & prefix,
00644                                                           const std::string & relative_part);
00645 
00647 
00650     extern std::string tools_output2xml(const std::string & src);
00651 
00653 
00656     extern U_I tools_octal2int(const std::string & perm);
00657 
00658 
00660 
00663     extern std::string tools_int2octal(const U_I & perm);
00664 
00666 
00669     extern void tools_set_permission(S_I fd, U_I perm);
00670 
00672 
00676     extern U_I tools_get_permission(S_I fd);
00677 
00679 
00684     extern void tools_set_ownership(S_I fd, const std::string & slice_user, const std::string & slice_group);
00685 
00687 
00692     extern void tools_memxor(void *dest, const void *src, U_I n);
00693 
00695 
00700     extern tlv_list tools_string2tlv_list(user_interaction & dialog, const U_16 & type, const std::vector<std::string> & data);
00701 
00702 
00703 
00705 
00709     extern void tools_read_from_pipe(user_interaction & dialog, S_I fd, tlv_list & result);
00710 
00711 
00712 
00714 
00718     extern U_I tools_pseudo_random(U_I max);
00719 
00720 
00722 
00729 
00730     template <class N, class B> std::vector<B> tools_number_base_decomposition_in_big_endian(N number, const B & base)
00731     {
00732         std::vector<B> ret;
00733 
00734         if(base <= 0)
00735             throw Erange("tools_number_decoupe_in_big_endian", "base must be strictly positive");
00736 
00737         while(number != 0)
00738         {
00739             ret.push_back(number % base);
00740             number /= base;
00741         }
00742 
00743         return ret;
00744     }
00745 
00747 
00750     std::string tools_unsigned_char_to_hexa(unsigned char x);
00751 
00753 
00756 
00757     std::string tools_string_to_hexa(const std::string & input);
00758 
00760 
00763     extern infinint tools_file_size_to_crc_size(const infinint & size);
00764 
00766 
00767     extern std::string tools_get_euid();
00768 
00769 
00771 
00772     extern std::string tools_get_egid();
00773 
00775 
00776     extern std::string tools_get_hostname();
00777 
00779 
00780     extern std::string tools_get_date_utc();
00781 
00783 
00784     extern std::string tools_get_compression_ratio(const infinint & storage_size, const infinint & file_size);
00785 
00786 } 
00787 
00788 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines