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