t_dirtree_fcopy.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_fcopy_file_transfer                                          *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Tests the directory_tree object as a file copy prompter.                 *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
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   // read the source location.
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   // read the stuff that exists at the target.
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 //LOG("missing files:");
00088 //LOG(diffs.text_form());
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 //LOG("after packing and restoring:");
00098 //LOG(regen.text_form());
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 //LOG(istring("cp source: ") + source_file.raw());
00119 //LOG(istring("-> target: ") + target_file.raw());
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 //do the copy by going across the entire set of files and making sure
00136 //they get copied to target.
00137 //
00138 //reread the target location.
00139 //
00140 //compare with source tree read before.
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 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1