1 /*****************************************************************************\
3 * Name : recursive_file_copy *
4 * Author : Chris Koeritz *
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 \*****************************************************************************/
15 #include "file_transfer_infoton.h"
16 #include "file_transfer_tentacle.h"
17 #include "recursive_file_copy.h"
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>
33 using namespace application;
34 using namespace basis;
35 using namespace filesystem;
36 using namespace loggers;
37 using namespace structures;
38 using namespace textual;
42 #define DEBUG_RECURSIVE_FILE_COPY
43 // uncomment for noisier debugging.
45 #define FAKE_HOSTNAME "internal_fake_host"
48 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
50 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
52 #define RETURN_ERROR_RFC(msg, err) { \
57 const int MAX_CHUNK_RFC_COPY_HIER = 1 * MEGABYTE;
58 // maximum size for each transfer chunk.
60 const int EXPECTED_MAXIMUM_TRANSFER_TIME = 10 * HOUR_ms;
61 // how long do we allow the scanned file lists to stay relevant.
62 // we allow it a long time, since this is a copy and not an active
65 recursive_file_copy::~recursive_file_copy() {}
67 const char *recursive_file_copy::outcome_name(const outcome &to_name)
68 { return common::outcome_name(to_name); }
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)
74 FUNCDEF("copy_hierarchy");
77 string_array includes;
78 if (_global_argc >= 5) {
79 for (int i = 4; i < _global_argc; i++) {
80 includes += _global_argv[i];
85 astring source_root = "snootums";
86 if (source_start.t()) {
87 source_root += filename::default_separator() + source_start;
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);
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);
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);
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);
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);
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);
126 // LOG(astring("got list of diffs:\n") + diffs.text_form());
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);
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);
139 infoton *junk = client_spider.acquire_specific_result(resp_id);
141 RETURN_ERROR_RFC("got a response we shouldn't have!", FAILURE);
143 astring current_file; // what file is in progress right now?
147 #ifdef DEBUG_RECURSIVE_FILE_COPY
148 LOG(a_sprintf("ongoing chunk %d", ++iter));
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;
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);
163 file_transfer_infoton *reply = (file_transfer_infoton *)ring_leader
164 .acquire_specific_result(chunk_id);
166 RETURN_ERROR_RFC("failed to get ongoing transfer reply", NONE_READY);
168 byte_array copy = reply->_packed_data;
169 while (copy.length()) {
171 file_transfer_header head(empty);
172 if (!head.unpack(copy))
173 RETURN_ERROR_RFC("failed to unpack header", GARBAGE);
174 if (copy.length() < head._length)
175 RETURN_ERROR_RFC("not enough length in array", GARBAGE);
176 if (head._length > 0)
177 copy.zap(0, head._length - 1);
179 //hmmm: this needs better formatting, and should not repeat the same file name even
180 // if it's in multiple chunks.
181 //hmmm: if logging, then...
182 BASE_LOG(head.readable_text_form());
185 RETURN_ERROR_RFC("still had data in array", GARBAGE);
187 if (reply->_command == file_transfer_infoton::CONCLUDE_TRANSFER_MARKER) {
188 BASE_LOG(astring("finished transfer from \"") + source_dir
189 + "\" to \"" + target_dir + "\"");
193 octopus_request_id resp_id(ent, iter + 11);
194 outcome resp_ret = client_spider.evaluate(reply, resp_id);
195 if (resp_ret != tentacle::OKAY)
196 RETURN_ERROR_RFC("failed to process the transfer reply!", FAILURE);