00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <basis/function.h>
00020 #include <basis/guards.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <opsystem/directory.h>
00024 #include <opsystem/directory_tree.h>
00025 #include <opsystem/filename.h>
00026 #include <opsystem/filename_list.h>
00027 #include <opsystem/heavy_file_ops.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029 #include <textual/string_manipulation.h>
00030
00031 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00032
00033 class test_fcopy : public application_shell
00034 {
00035 public:
00036 test_fcopy() : application_shell(static_class_name()) {}
00037 IMPLEMENT_CLASS_NAME("test_dirtree_fcopy");
00038 int execute();
00039 };
00040
00041 int test_fcopy::execute()
00042 {
00043 FUNCDEF("execute");
00044
00045 if (__argc < 3)
00046 non_continuable_error(class_name(), "command line", "this program needs two "
00047 "parameters:\na directory for the source and one for the target.");
00048
00049 istring source_dir = __argv[1];
00050 istring target_dir = __argv[2];
00051
00052
00053 log(istring("Scanning source tree at \"") + source_dir + "\"");
00054 directory_tree source(source_dir, "*");
00055 if (!source.good())
00056 non_continuable_error(class_name(), "directory_tree construction",
00057 "the source directory could not be read");
00058
00059
00060 log(istring("Scanning target tree at \"") + target_dir + "\"");
00061 directory_tree target(target_dir, "*");
00062 if (!target.good())
00063 non_continuable_error(class_name(), "directory_tree construction",
00064 "the target directory could not be read");
00065
00066 LOG("calculating checksums for source.");
00067 if (!source.calculate())
00068 non_continuable_error(class_name(), "directory_tree calculation",
00069 "the source tree could not be calculated");
00070
00071 LOG("calculating checksums for target.");
00072 if (!target.calculate())
00073 non_continuable_error(class_name(), "directory_tree calculation",
00074 "the target tree could not be calculated");
00075
00076 istring source_start;
00077 istring target_start;
00078 if (__argc > 3)
00079 source_start = __argv[3];
00080 if (__argc > 4)
00081 target_start = __argv[4];
00082
00083 LOG("comparing the two trees.");
00084 filename_list diffs;
00085 directory_tree::compare_trees(source, source_start, target, target_start,
00086 diffs);
00087
00088
00089
00090 byte_array packed_form;
00091 diffs.pack(packed_form);
00092 filename_list regen;
00093 if (!regen.unpack(packed_form))
00094 non_continuable_error(class_name(), "filename_list packing",
00095 "could not unpack the list of differences");
00096
00097
00098
00099
00100 if (regen.elements() != diffs.elements())
00101 non_continuable_error(class_name(), "filename_list packing",
00102 "there were a different number of elements in unpacked form");
00103 for (int i = 0; i < diffs.elements(); i++) {
00104 if (!regen.member(*diffs[i])) {
00105 istring name = diffs[i]->raw();
00106 non_continuable_error(class_name(), "filename_list packing",
00107 istring("name from original set was missing in regenerated: ")
00108 + name);
00109 }
00110 }
00111
00112 for (int i = 0; i < diffs.elements(); i++) {
00113 file_info *curr = diffs[i];
00114 filename source_file = source.path() + filename::default_separator()
00115 + curr->raw();
00116 filename target_file = target.path() + filename::default_separator()
00117 + curr->secondary();
00118
00119
00120 filename targ_dir = target_file.dirname();
00121 if (!targ_dir.is_directory()) {
00122 bool worked = directory::recursive_create(targ_dir);
00123 if (!worked)
00124 non_continuable_error(class_name(), "recursive mkdir",
00125 istring("failed to create the target directory ") + targ_dir);
00126 }
00127
00128 outcome ret = heavy_file_operations::copy_file(source_file, target_file);
00129 if (ret != heavy_file_operations::OKAY)
00130 non_continuable_error(class_name(), "copying file", istring("there was an error ")
00131 + heavy_file_operations::outcome_name(ret)
00132 + " when copying the file.");
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142 guards::alert_message("directory_tree file transfer:: works for those functions tested.");
00143 return 0;
00144 }
00145
00146 HOOPLE_MAIN(test_fcopy, )
00147