updated to change how final part of transfer is tagged.
[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
33 using namespace application;
34 using namespace basis;
35 using namespace filesystem;
36 using namespace loggers;
37 using namespace structures;
38 using namespace textual;
39
40 namespace octopi {
41
42 //#define DEBUG_RECURSIVE_FILE_COPY
43   // uncomment for noisier debugging.
44
45 #define FAKE_HOSTNAME "internal_fake_host"
46
47 #undef LOG
48 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
49 #undef BASE_LOG
50 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
51
52 const int MAX_CHUNK_RFC_COPY_HIER = 1 * MEGABYTE;
53   // maximum size for each transfer chunk.
54
55 const int EXPECTED_MAXIMUM_TRANSFER_TIME = 10 * HOUR_ms;
56   // how long do we allow the scanned file lists to stay relevant.
57   // we allow it a long time, since this is a copy and not an active
58   // synchronization.
59
60 recursive_file_copy::~recursive_file_copy() {}
61
62 const char *recursive_file_copy::outcome_name(const outcome &to_name)
63 { return common::outcome_name(to_name); }
64
65 #define RETURN_ERROR_RFC(msg, err) { \
66   LOG(msg); \
67   return err; \
68 }
69
70 outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
71   const astring &source_dir, const astring &target_dir,
72   const string_array &includes, const astring &source_start)
73 {
74   FUNCDEF("copy_hierarchy");
75
76 /*
77   string_array includes;
78   if (_global_argc >= 5) {
79     for (int i = 4; i < _global_argc; i++) {
80       includes += _global_argv[i];
81     }
82   }
83 */
84
85   astring source_root = "snootums";
86   if (source_start.t()) {
87     source_root += filename::default_separator() + source_start;
88   }
89
90   octopus ring_leader(FAKE_HOSTNAME, 10 * MEGABYTE);
91   file_transfer_tentacle *tran = new file_transfer_tentacle
92       (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
93   ring_leader.add_tentacle(tran);
94
95   outcome add_ret = tran->add_correspondence("snootums", source_dir,
96       EXPECTED_MAXIMUM_TRANSFER_TIME);
97   if (add_ret != tentacle::OKAY)
98     RETURN_ERROR_RFC("failed to add the correspondence", NOT_FOUND);
99
100   file_transfer_infoton *initiate = new file_transfer_infoton;
101   initiate->_request = true;
102   initiate->_command = file_transfer_infoton::TREE_COMPARISON;
103   initiate->_src_root = source_root;
104   initiate->_dest_root = target_dir;
105   directory_tree target_area(target_dir);
106 //hmmm: simple asset counting debugging in calculate would be nice too.
107   target_area.calculate( !(transfer_mode & file_transfer_tentacle::COMPARE_CONTENT_SAMPLE) );
108   initiate->package_tree_info(target_area, includes);
109
110   octopus_entity ent = ring_leader.issue_identity();
111   octopus_request_id req_id(ent, 1);
112   outcome start_ret = ring_leader.evaluate(initiate, req_id);
113   if (start_ret != tentacle::OKAY)
114     RETURN_ERROR_RFC("failed to start the comparison", NONE_READY);
115
116   file_transfer_infoton *reply_from_init
117       = (file_transfer_infoton *)ring_leader.acquire_specific_result(req_id);
118   if (!reply_from_init)
119     RETURN_ERROR_RFC("no response to tree compare start", NONE_READY);
120
121   filename_list diffs;
122   byte_array pack_copy = reply_from_init->_packed_data;
123   if (!diffs.unpack(pack_copy)) {
124     RETURN_ERROR_RFC("could not unpack filename list!", GARBAGE);
125   }
126 //  LOG(astring("got list of diffs:\n") + diffs.text_form());
127
128   octopus client_spider(FAKE_HOSTNAME, 10 * MEGABYTE);
129   file_transfer_tentacle *tran2 = new file_transfer_tentacle
130       (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
131   tran2->register_file_transfer(ent, source_root, target_dir, includes);
132   client_spider.add_tentacle(tran2);
133
134   octopus_request_id resp_id(ent, 2);
135   outcome ini_resp_ret = client_spider.evaluate(reply_from_init, resp_id);
136   if (ini_resp_ret != tentacle::OKAY)
137     RETURN_ERROR_RFC("failed to process the start response!", FAILURE);
138
139   infoton *junk = client_spider.acquire_specific_result(resp_id);
140   if (junk)
141     RETURN_ERROR_RFC("got a response we shouldn't have!", FAILURE);
142
143   astring current_file;  // what file is in progress right now?
144
145   int iter = 0;
146   while (true) {
147 #ifdef DEBUG_RECURSIVE_FILE_COPY
148     LOG(a_sprintf("ongoing chunk %d", ++iter));
149 #endif
150
151     // keep going until we find a broken reply.
152     file_transfer_infoton *ongoing = new file_transfer_infoton;
153     ongoing->_request = true;
154     ongoing->_command = file_transfer_infoton::PLACE_FILE_CHUNKS;
155     ongoing->_src_root = source_root;
156     ongoing->_dest_root = target_dir;
157
158     octopus_request_id chunk_id(ent, iter + 10);
159     outcome place_ret = ring_leader.evaluate(ongoing, chunk_id);
160     if (place_ret != tentacle::OKAY)
161       RETURN_ERROR_RFC("failed to run ongoing transfer", FAILURE);
162   
163     file_transfer_infoton *reply = (file_transfer_infoton *)ring_leader
164          .acquire_specific_result(chunk_id);
165     if (!reply)
166       RETURN_ERROR_RFC("failed to get ongoing transfer reply", NONE_READY);
167
168     if (reply->_command == file_transfer_infoton::CONCLUDE_TRANSFER_MARKER) {
169       BASE_LOG(astring("finished transfer from \"") + source_dir
170           + "\" to \"" + target_dir + "\"");
171       break;
172     }
173
174     if (!reply->_packed_data.length()) {
175       RETURN_ERROR_RFC("file transfer had no packed data", GARBAGE);
176     }
177
178     byte_array copy = reply->_packed_data;
179     while (copy.length()) {
180 #ifdef DEBUG_RECURSIVE_FILE_COPY
181       LOG(a_sprintf("starging size in array: %d", copy.length()));
182 #endif
183       file_time empty;
184       file_transfer_header head(empty);
185       if (!head.unpack(copy)) 
186         RETURN_ERROR_RFC("failed to unpack header", GARBAGE);
187 #ifdef DEBUG_RECURSIVE_FILE_COPY
188       LOG(a_sprintf("removed head size in array: %d", copy.length()));
189 #endif
190       if (copy.length() < head._length)
191         RETURN_ERROR_RFC("not enough length in array", GARBAGE);
192 //hmmm: are we doing nothing here besides validating that we GOT something in the header?
193       copy.zap(0, head._length - 1);
194 #ifdef DEBUG_RECURSIVE_FILE_COPY
195       LOG(a_sprintf("size in array now: %d", copy.length()));
196 #endif
197
198 //hmmm: this needs better formatting, and should not repeat the same file name even
199 //      if it's in multiple chunks.
200 //hmmm: if logging, then...
201       BASE_LOG(head.readable_text_form());
202     }
203     if (copy.length())
204       RETURN_ERROR_RFC("still had data in array", GARBAGE);
205
206     octopus_request_id resp_id(ent, iter + 11);
207     outcome resp_ret = client_spider.evaluate(reply, resp_id);
208     if (resp_ret != tentacle::OKAY)
209       RETURN_ERROR_RFC("failed to process the transfer reply!", FAILURE);
210   }
211
212   return OKAY;
213 }
214
215 } //namespace.
216
217