baf40fe75afcd3731996a5d4d4421a839230d848
[feisty_meow.git] / octopi / library / tentacles / recursive_file_copy.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : recursive_file_copy                                               *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "file_transfer_infoton.h"
16 #include "file_transfer_tentacle.h"
17 #include "recursive_file_copy.h"
18
19 #include <application/application_shell.h>
20 #include <basis/guards.h>
21 #include <filesystem/directory.h>
22 #include <filesystem/directory_tree.h>
23 #include <filesystem/filename.h>
24 #include <filesystem/filename_list.h>
25 #include <filesystem/heavy_file_ops.h>
26 #include <filesystem/huge_file.h>
27 #include <loggers/program_wide_logger.h>
28 #include <octopus/entity_defs.h>
29 #include <octopus/octopus.h>
30 #include <structures/static_memory_gremlin.h>
31 #include <textual/string_manipulation.h>
32 #include <timely/time_control.h>
33
34 using namespace application;
35 using namespace basis;
36 using namespace filesystem;
37 using namespace loggers;
38 using namespace structures;
39 using namespace textual;
40 using namespace timely;
41
42 namespace octopi {
43
44 #define DEBUG_RECURSIVE_FILE_COPY
45   // uncomment for noisier debugging.
46
47 #define FAKE_HOSTNAME "internal_fake_host"
48
49 #undef LOG
50 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
51 #undef BASE_LOG
52 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
53
54 #define RETURN_ERROR_RFC(msg, err) { \
55   LOG(msg); \
56   return err; \
57 }
58
59 const int MAX_CHUNK_RFC_COPY_HIER = 1 * MEGABYTE;
60   // maximum size for each transfer chunk.
61
62 const int EXPECTED_MAXIMUM_TRANSFER_TIME = 10 * HOUR_ms;
63   // how long do we allow the scanned file lists to stay relevant.
64   // we allow it a long time, since this is a copy and not an active
65   // synchronization.
66
67 recursive_file_copy::~recursive_file_copy() {}
68
69 const char *recursive_file_copy::outcome_name(const outcome &to_name)
70 { return common::outcome_name(to_name); }
71
72 outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
73   const astring &source_dir, const astring &target_dir,
74   const string_array &includes, const astring &source_start)
75 {
76   FUNCDEF("copy_hierarchy");
77
78 /*
79   string_array includes;
80   if (_global_argc >= 5) {
81     for (int i = 4; i < _global_argc; i++) {
82       includes += _global_argv[i];
83     }
84   }
85 */
86
87   astring source_root = "snootums";
88   if (source_start.t()) {
89     source_root += filename::default_separator() + source_start;
90   }
91
92   octopus ring_leader(FAKE_HOSTNAME, 10 * MEGABYTE);
93   file_transfer_tentacle *tran = new file_transfer_tentacle
94       (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
95   ring_leader.add_tentacle(tran);
96
97   outcome add_ret = tran->add_correspondence("snootums", source_dir,
98       EXPECTED_MAXIMUM_TRANSFER_TIME);
99   if (add_ret != tentacle::OKAY)
100     RETURN_ERROR_RFC("failed to add the correspondence", NOT_FOUND);
101
102   file_transfer_infoton *initiate = new file_transfer_infoton;
103   initiate->_request = true;
104   initiate->_command = file_transfer_infoton::TREE_COMPARISON;
105   initiate->_src_root = source_root;
106   initiate->_dest_root = target_dir;
107   directory_tree target_area(target_dir);
108 //hmmm: simple asset counting debugging in calculate would be nice too.
109   target_area.calculate( !(transfer_mode & file_transfer_tentacle::COMPARE_CONTENT_SAMPLE) );
110   initiate->package_tree_info(target_area, includes);
111
112   octopus_entity ent = ring_leader.issue_identity();
113   octopus_request_id req_id(ent, 1);
114   outcome start_ret = ring_leader.evaluate(initiate, req_id);
115   if (start_ret != tentacle::OKAY)
116     RETURN_ERROR_RFC("failed to start the comparison", NONE_READY);
117
118   file_transfer_infoton *reply_from_init
119       = (file_transfer_infoton *)ring_leader.acquire_specific_result(req_id);
120   if (!reply_from_init)
121     RETURN_ERROR_RFC("no response to tree compare start", NONE_READY);
122
123   filename_list diffs;
124   byte_array pack_copy = reply_from_init->_packed_data;
125   if (!diffs.unpack(pack_copy)) {
126     RETURN_ERROR_RFC("could not unpack filename list!", GARBAGE);
127   }
128 //  LOG(astring("got list of diffs:\n") + diffs.text_form());
129
130   octopus client_spider(FAKE_HOSTNAME, 10 * MEGABYTE);
131   file_transfer_tentacle *tran2 = new file_transfer_tentacle
132       (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
133   tran2->register_file_transfer(ent, source_root, target_dir, includes);
134   client_spider.add_tentacle(tran2);
135
136   octopus_request_id resp_id(ent, 2);
137   outcome ini_resp_ret = client_spider.evaluate(reply_from_init, resp_id);
138   if (ini_resp_ret != tentacle::OKAY)
139     RETURN_ERROR_RFC("failed to process the start response!", FAILURE);
140
141   infoton *junk = client_spider.acquire_specific_result(resp_id);
142   if (junk)
143     RETURN_ERROR_RFC("got a response we shouldn't have!", FAILURE);
144
145   astring current_file;  // what file is in progress right now?
146
147   int iter = 0;
148   while (true) {
149 #ifdef DEBUG_RECURSIVE_FILE_COPY
150     LOG(a_sprintf("ongoing chunk %d", ++iter));
151 #endif
152
153     // keep going until we find a broken reply.
154     file_transfer_infoton *ongoing = new file_transfer_infoton;
155     ongoing->_request = true;
156     ongoing->_command = file_transfer_infoton::PLACE_FILE_CHUNKS;
157     ongoing->_src_root = source_root;
158     ongoing->_dest_root = target_dir;
159
160     octopus_request_id chunk_id(ent, iter + 10);
161     outcome place_ret = ring_leader.evaluate(ongoing, chunk_id);
162     if (place_ret != tentacle::OKAY)
163       RETURN_ERROR_RFC("failed to run ongoing transfer", FAILURE);
164   
165     file_transfer_infoton *reply = (file_transfer_infoton *)ring_leader
166          .acquire_specific_result(chunk_id);
167     if (!reply)
168       RETURN_ERROR_RFC("failed to get ongoing transfer reply", NONE_READY);
169
170     if (reply->_command == file_transfer_infoton::CONCLUDE_TRANSFER_MARKER) {
171       BASE_LOG(astring("finished transfer from \"") + source_dir
172           + "\" to \"" + target_dir + "\"");
173       break;
174     }
175
176     byte_array copy = reply->_packed_data;
177     while (copy.length()) {
178       file_time empty;
179       file_transfer_header head(empty);
180       if (!head.unpack(copy)) 
181         RETURN_ERROR_RFC("failed to unpack header", GARBAGE);
182       if (copy.length() < head._length)
183         RETURN_ERROR_RFC("not enough length in array", GARBAGE);
184       if (head._length > 0)
185         copy.zap(0, head._length - 1);
186
187 //hmmm: this needs better formatting, and should not repeat the same file name even
188 //      if it's in multiple chunks.
189       BASE_LOG(head.readable_text_form());
190     }
191     if (copy.length())
192       RETURN_ERROR_RFC("still had data in array", GARBAGE);
193
194     octopus_request_id resp_id(ent, iter + 11);
195     outcome resp_ret = client_spider.evaluate(reply, resp_id);
196     if (resp_ret != tentacle::OKAY)
197       RETURN_ERROR_RFC("failed to process the transfer reply!", FAILURE);
198   }
199
200   return OKAY;
201 }
202
203 } //namespace.
204
205