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 FAKE_HOSTNAME "internal_fake_host"
45 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
47 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
49 const int MAX_CHUNK_RFC_COPY_HIER = 1 * MEGABYTE;
50 // maximum size for each transfer chunk.
52 const int EXPECTED_MAXIMUM_TRANSFER_TIME = 10 * HOUR_ms;
53 // how long do we allow the scanned file lists to stay relevant.
54 // we allow it a long time, since this is a copy and not an active
57 recursive_file_copy::~recursive_file_copy() {}
59 const char *recursive_file_copy::outcome_name(const outcome &to_name)
60 { return common::outcome_name(to_name); }
62 #define RETURN_ERROR_RFC(msg, err) { \
67 outcome recursive_file_copy::copy_hierarchy(int transfer_mode,
68 const astring &source_dir, const astring &target_dir,
69 const string_array &includes, const astring &source_start)
71 FUNCDEF("copy_hierarchy");
74 string_array includes;
75 if (_global_argc >= 5) {
76 for (int i = 4; i < _global_argc; i++) {
77 includes += _global_argv[i];
82 astring source_root = "snootums";
83 if (source_start.t()) {
84 source_root += filename::default_separator() + source_start;
87 octopus ring_leader(FAKE_HOSTNAME, 10 * MEGABYTE);
88 file_transfer_tentacle *tran = new file_transfer_tentacle
89 (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
90 ring_leader.add_tentacle(tran);
92 outcome add_ret = tran->add_correspondence("snootums", source_dir,
93 EXPECTED_MAXIMUM_TRANSFER_TIME);
94 if (add_ret != tentacle::OKAY)
95 RETURN_ERROR_RFC("failed to add the correspondence", NOT_FOUND);
97 file_transfer_infoton *initiate = new file_transfer_infoton;
98 initiate->_request = true;
99 initiate->_command = file_transfer_infoton::TREE_COMPARISON;
100 initiate->_src_root = source_root;
101 initiate->_dest_root = target_dir;
102 directory_tree target_area(target_dir);
103 //hmmm: simple asset counting debugging in calculate would be nice too.
104 target_area.calculate( !(transfer_mode & file_transfer_tentacle::COMPARE_CONTENT_SAMPLE) );
105 initiate->package_tree_info(target_area, includes);
107 octopus_entity ent = ring_leader.issue_identity();
108 octopus_request_id req_id(ent, 1);
109 outcome start_ret = ring_leader.evaluate(initiate, req_id);
110 if (start_ret != tentacle::OKAY)
111 RETURN_ERROR_RFC("failed to start the comparison", NONE_READY);
113 file_transfer_infoton *reply_from_init
114 = (file_transfer_infoton *)ring_leader.acquire_specific_result(req_id);
115 if (!reply_from_init)
116 RETURN_ERROR_RFC("no response to tree compare start", NONE_READY);
119 byte_array pack_copy = reply_from_init->_packed_data;
120 if (!diffs.unpack(pack_copy)) {
121 RETURN_ERROR_RFC("could not unpack filename list!", GARBAGE);
123 // LOG(astring("got list of diffs:\n") + diffs.text_form());
125 octopus client_spider(FAKE_HOSTNAME, 10 * MEGABYTE);
126 file_transfer_tentacle *tran2 = new file_transfer_tentacle
127 (MAX_CHUNK_RFC_COPY_HIER, (file_transfer_tentacle::transfer_modes)transfer_mode);
128 tran2->register_file_transfer(ent, source_root, target_dir, includes);
129 client_spider.add_tentacle(tran2);
131 octopus_request_id resp_id(ent, 2);
132 outcome ini_resp_ret = client_spider.evaluate(reply_from_init, resp_id);
133 if (ini_resp_ret != tentacle::OKAY)
134 RETURN_ERROR_RFC("failed to process the start response!", FAILURE);
136 infoton *junk = client_spider.acquire_specific_result(resp_id);
138 RETURN_ERROR_RFC("got a response we shouldn't have!", FAILURE);
140 astring current_file; // what file is in progress right now?
144 LOG(a_sprintf("ongoing chunk %d", ++iter));
146 // keep going until we find a broken reply.
147 file_transfer_infoton *ongoing = new file_transfer_infoton;
148 ongoing->_request = true;
149 ongoing->_command = file_transfer_infoton::PLACE_FILE_CHUNKS;
150 ongoing->_src_root = source_root;
151 ongoing->_dest_root = target_dir;
153 octopus_request_id chunk_id(ent, iter + 10);
154 outcome place_ret = ring_leader.evaluate(ongoing, chunk_id);
155 if (place_ret != tentacle::OKAY)
156 RETURN_ERROR_RFC("failed to run ongoing transfer", FAILURE);
158 file_transfer_infoton *reply = (file_transfer_infoton *)ring_leader
159 .acquire_specific_result(chunk_id);
161 RETURN_ERROR_RFC("failed to get ongoing transfer reply", NONE_READY);
163 if (!reply->_packed_data.length()) {
164 BASE_LOG(astring("finished transfer from \"") + source_dir
165 + "\" to \"" + target_dir + "\"");
169 byte_array copy = reply->_packed_data;
170 while (copy.length()) {
172 file_transfer_header head(empty);
173 if (!head.unpack(copy))
174 RETURN_ERROR_RFC("failed to unpack header", GARBAGE);
175 //LOG(a_sprintf("size in array: %d", copy.length()));
176 if (copy.length() < head._length)
177 RETURN_ERROR_RFC("not enough length in array", GARBAGE);
178 //hmmm: are we doing nothing here besides validating that we GOT something in the header?
179 copy.zap(0, head._length - 1);
180 //LOG(a_sprintf("size in array now: %d", copy.length()));
182 //hmmm: if logging, then...
183 BASE_LOG(head.readable_text_form());
186 RETURN_ERROR_RFC("still had data in array", GARBAGE);
188 octopus_request_id resp_id(ent, iter + 11);
189 outcome resp_ret = client_spider.evaluate(reply, resp_id);
190 if (resp_ret != tentacle::OKAY)
191 RETURN_ERROR_RFC("failed to process the transfer reply!", FAILURE);