still not right. works for basic stuff, fails for large hierarchies.
[feisty_meow.git] / octopi / library / tentacles / recursive_file_copy.cpp
index 40e4f0afa62fbd93f3a6aac69f6e5d8256ab5622..baf40fe75afcd3731996a5d4d4421a839230d848 100644 (file)
@@ -29,6 +29,7 @@
 #include <octopus/octopus.h>
 #include <structures/static_memory_gremlin.h>
 #include <textual/string_manipulation.h>
+#include <timely/time_control.h>
 
 using namespace application;
 using namespace basis;
@@ -36,9 +37,13 @@ using namespace filesystem;
 using namespace loggers;
 using namespace structures;
 using namespace textual;
+using namespace timely;
 
 namespace octopi {
 
+#define DEBUG_RECURSIVE_FILE_COPY
+  // uncomment for noisier debugging.
+
 #define FAKE_HOSTNAME "internal_fake_host"
 
 #undef LOG
@@ -46,19 +51,24 @@ namespace octopi {
 #undef BASE_LOG
 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
 
+#define RETURN_ERROR_RFC(msg, err) { \
+  LOG(msg); \
+  return err; \
+}
+
 const int MAX_CHUNK_RFC_COPY_HIER = 1 * MEGABYTE;
   // maximum size for each transfer chunk.
 
+const int EXPECTED_MAXIMUM_TRANSFER_TIME = 10 * HOUR_ms;
+  // how long do we allow the scanned file lists to stay relevant.
+  // we allow it a long time, since this is a copy and not an active
+  // synchronization.
+
 recursive_file_copy::~recursive_file_copy() {}
 
 const char *recursive_file_copy::outcome_name(const outcome &to_name)
 { return common::outcome_name(to_name); }
 
-#define RETURN_ERROR_RFC(msg, err) { \
-  LOG(msg); \
-  return err; \
-}
-
 outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
   const astring &source_dir, const astring &target_dir,
   const string_array &includes, const astring &source_start)
@@ -85,7 +95,7 @@ outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
   ring_leader.add_tentacle(tran);
 
   outcome add_ret = tran->add_correspondence("snootums", source_dir,
-      10 * MINUTE_ms);
+      EXPECTED_MAXIMUM_TRANSFER_TIME);
   if (add_ret != tentacle::OKAY)
     RETURN_ERROR_RFC("failed to add the correspondence", NOT_FOUND);
 
@@ -136,7 +146,9 @@ outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
 
   int iter = 0;
   while (true) {
-//LOG(a_sprintf("ongoing chunk %d", ++iter));
+#ifdef DEBUG_RECURSIVE_FILE_COPY
+    LOG(a_sprintf("ongoing chunk %d", ++iter));
+#endif
 
     // keep going until we find a broken reply.
     file_transfer_infoton *ongoing = new file_transfer_infoton;
@@ -155,7 +167,7 @@ outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
     if (!reply)
       RETURN_ERROR_RFC("failed to get ongoing transfer reply", NONE_READY);
 
-    if (!reply->_packed_data.length()) {
+    if (reply->_command == file_transfer_infoton::CONCLUDE_TRANSFER_MARKER) {
       BASE_LOG(astring("finished transfer from \"") + source_dir
           + "\" to \"" + target_dir + "\"");
       break;
@@ -167,14 +179,13 @@ outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
       file_transfer_header head(empty);
       if (!head.unpack(copy)) 
         RETURN_ERROR_RFC("failed to unpack header", GARBAGE);
-//LOG(a_sprintf("size in array: %d", copy.length()));
       if (copy.length() < head._length)
         RETURN_ERROR_RFC("not enough length in array", GARBAGE);
-//hmmm: are we doing nothing here besides validating that we GOT something in the header?
-      copy.zap(0, head._length - 1);
-//LOG(a_sprintf("size in array now: %d", copy.length()));
+      if (head._length > 0)
+        copy.zap(0, head._length - 1);
 
-//hmmm: if logging, then...
+//hmmm: this needs better formatting, and should not repeat the same file name even
+//      if it's in multiple chunks.
       BASE_LOG(head.readable_text_form());
     }
     if (copy.length())