1 /*************************************************************************/
2 /*  file_access_network.cpp                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "file_access_network.h"
32 
33 #include "core/io/ip.h"
34 #include "core/io/marshalls.h"
35 #include "core/os/os.h"
36 #include "core/project_settings.h"
37 
38 //#define DEBUG_PRINT(m_p) print_line(m_p)
39 //#define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec());
40 #define DEBUG_PRINT(m_p)
41 #define DEBUG_TIME(m_what)
42 
lock_mutex()43 void FileAccessNetworkClient::lock_mutex() {
44 
45 	mutex->lock();
46 	lockcount++;
47 }
48 
unlock_mutex()49 void FileAccessNetworkClient::unlock_mutex() {
50 
51 	lockcount--;
52 	mutex->unlock();
53 }
54 
put_32(int p_32)55 void FileAccessNetworkClient::put_32(int p_32) {
56 
57 	uint8_t buf[4];
58 	encode_uint32(p_32, buf);
59 	client->put_data(buf, 4);
60 	DEBUG_PRINT("put32: " + itos(p_32));
61 }
62 
put_64(int64_t p_64)63 void FileAccessNetworkClient::put_64(int64_t p_64) {
64 
65 	uint8_t buf[8];
66 	encode_uint64(p_64, buf);
67 	client->put_data(buf, 8);
68 	DEBUG_PRINT("put64: " + itos(p_64));
69 }
70 
get_32()71 int FileAccessNetworkClient::get_32() {
72 
73 	uint8_t buf[4];
74 	client->get_data(buf, 4);
75 	return decode_uint32(buf);
76 }
77 
get_64()78 int64_t FileAccessNetworkClient::get_64() {
79 
80 	uint8_t buf[8];
81 	client->get_data(buf, 8);
82 	return decode_uint64(buf);
83 }
84 
_thread_func()85 void FileAccessNetworkClient::_thread_func() {
86 
87 	client->set_no_delay(true);
88 	while (!quit) {
89 
90 		DEBUG_PRINT("SEM WAIT - " + itos(sem->get()));
91 		Error err = sem->wait();
92 		if (err != OK)
93 			ERR_PRINT("sem->wait() failed");
94 		DEBUG_TIME("sem_unlock");
95 		//DEBUG_PRINT("semwait returned "+itos(werr));
96 		DEBUG_PRINT("MUTEX LOCK " + itos(lockcount));
97 		lock_mutex();
98 		DEBUG_PRINT("MUTEX PASS");
99 
100 		blockrequest_mutex->lock();
101 		while (block_requests.size()) {
102 			put_32(block_requests.front()->get().id);
103 			put_32(FileAccessNetwork::COMMAND_READ_BLOCK);
104 			put_64(block_requests.front()->get().offset);
105 			put_32(block_requests.front()->get().size);
106 			block_requests.pop_front();
107 		}
108 		blockrequest_mutex->unlock();
109 
110 		DEBUG_PRINT("THREAD ITER");
111 
112 		DEBUG_TIME("sem_read");
113 		int id = get_32();
114 
115 		int response = get_32();
116 		DEBUG_PRINT("GET RESPONSE: " + itos(response));
117 
118 		FileAccessNetwork *fa = NULL;
119 
120 		if (response != FileAccessNetwork::RESPONSE_DATA) {
121 			if (!accesses.has(id)) {
122 				unlock_mutex();
123 				ERR_FAIL_COND(!accesses.has(id));
124 			}
125 		}
126 
127 		if (accesses.has(id))
128 			fa = accesses[id];
129 
130 		switch (response) {
131 
132 			case FileAccessNetwork::RESPONSE_OPEN: {
133 
134 				DEBUG_TIME("sem_open");
135 				int status = get_32();
136 				if (status != OK) {
137 					fa->_respond(0, Error(status));
138 				} else {
139 					uint64_t len = get_64();
140 					fa->_respond(len, Error(status));
141 				}
142 
143 				fa->sem->post();
144 
145 			} break;
146 			case FileAccessNetwork::RESPONSE_DATA: {
147 
148 				int64_t offset = get_64();
149 				uint32_t len = get_32();
150 
151 				Vector<uint8_t> block;
152 				block.resize(len);
153 				client->get_data(block.ptrw(), len);
154 
155 				if (fa) //may have been queued
156 					fa->_set_block(offset, block);
157 
158 			} break;
159 			case FileAccessNetwork::RESPONSE_FILE_EXISTS: {
160 
161 				int status = get_32();
162 				fa->exists_modtime = status != 0;
163 				fa->sem->post();
164 
165 			} break;
166 			case FileAccessNetwork::RESPONSE_GET_MODTIME: {
167 
168 				uint64_t status = get_64();
169 				fa->exists_modtime = status;
170 				fa->sem->post();
171 
172 			} break;
173 		}
174 
175 		unlock_mutex();
176 	}
177 }
178 
_thread_func(void * s)179 void FileAccessNetworkClient::_thread_func(void *s) {
180 
181 	FileAccessNetworkClient *self = (FileAccessNetworkClient *)s;
182 
183 	self->_thread_func();
184 }
185 
connect(const String & p_host,int p_port,const String & p_password)186 Error FileAccessNetworkClient::connect(const String &p_host, int p_port, const String &p_password) {
187 
188 	IP_Address ip;
189 
190 	if (p_host.is_valid_ip_address()) {
191 		ip = p_host;
192 	} else {
193 		ip = IP::get_singleton()->resolve_hostname(p_host);
194 	}
195 
196 	DEBUG_PRINT("IP: " + String(ip) + " port " + itos(p_port));
197 	Error err = client->connect_to_host(ip, p_port);
198 	ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot connect to host with IP: " + String(ip) + " and port: " + itos(p_port));
199 	while (client->get_status() == StreamPeerTCP::STATUS_CONNECTING) {
200 		//DEBUG_PRINT("trying to connect....");
201 		OS::get_singleton()->delay_usec(1000);
202 	}
203 
204 	if (client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
205 		return ERR_CANT_CONNECT;
206 	}
207 
208 	CharString cs = p_password.utf8();
209 	put_32(cs.length());
210 	client->put_data((const uint8_t *)cs.ptr(), cs.length());
211 
212 	int e = get_32();
213 
214 	if (e != OK) {
215 		return ERR_INVALID_PARAMETER;
216 	}
217 
218 	thread = Thread::create(_thread_func, this);
219 
220 	return OK;
221 }
222 
223 FileAccessNetworkClient *FileAccessNetworkClient::singleton = NULL;
224 
FileAccessNetworkClient()225 FileAccessNetworkClient::FileAccessNetworkClient() {
226 
227 	thread = NULL;
228 	mutex = Mutex::create();
229 	blockrequest_mutex = Mutex::create();
230 	quit = false;
231 	singleton = this;
232 	last_id = 0;
233 	client.instance();
234 	sem = Semaphore::create();
235 	lockcount = 0;
236 }
237 
~FileAccessNetworkClient()238 FileAccessNetworkClient::~FileAccessNetworkClient() {
239 
240 	if (thread) {
241 		quit = true;
242 		sem->post();
243 		Thread::wait_to_finish(thread);
244 		memdelete(thread);
245 	}
246 
247 	memdelete(blockrequest_mutex);
248 	memdelete(mutex);
249 	memdelete(sem);
250 }
251 
_set_block(int p_offset,const Vector<uint8_t> & p_block)252 void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) {
253 
254 	int page = p_offset / page_size;
255 	ERR_FAIL_INDEX(page, pages.size());
256 	if (page < pages.size() - 1) {
257 		ERR_FAIL_COND(p_block.size() != page_size);
258 	} else {
259 		ERR_FAIL_COND((p_block.size() != (int)(total_size % page_size)));
260 	}
261 
262 	buffer_mutex->lock();
263 	pages.write[page].buffer = p_block;
264 	pages.write[page].queued = false;
265 	buffer_mutex->unlock();
266 
267 	if (waiting_on_page == page) {
268 		waiting_on_page = -1;
269 		page_sem->post();
270 	}
271 }
272 
_respond(size_t p_len,Error p_status)273 void FileAccessNetwork::_respond(size_t p_len, Error p_status) {
274 
275 	DEBUG_PRINT("GOT RESPONSE - len: " + itos(p_len) + " status: " + itos(p_status));
276 	response = p_status;
277 	if (response != OK)
278 		return;
279 	opened = true;
280 	total_size = p_len;
281 	int pc = ((total_size - 1) / page_size) + 1;
282 	pages.resize(pc);
283 }
284 
_open(const String & p_path,int p_mode_flags)285 Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) {
286 
287 	ERR_FAIL_COND_V(p_mode_flags != READ, ERR_UNAVAILABLE);
288 	if (opened)
289 		close();
290 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
291 	DEBUG_PRINT("open: " + p_path);
292 
293 	DEBUG_TIME("open_begin");
294 
295 	nc->lock_mutex();
296 	nc->put_32(id);
297 	nc->accesses[id] = this;
298 	nc->put_32(COMMAND_OPEN_FILE);
299 	CharString cs = p_path.utf8();
300 	nc->put_32(cs.length());
301 	nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
302 	pos = 0;
303 	eof_flag = false;
304 	last_page = -1;
305 	last_page_buff = NULL;
306 
307 	//buffers.clear();
308 	nc->unlock_mutex();
309 	DEBUG_PRINT("OPEN POST");
310 	DEBUG_TIME("open_post");
311 	nc->sem->post(); //awaiting answer
312 	DEBUG_PRINT("WAIT...");
313 	sem->wait();
314 	DEBUG_TIME("open_end");
315 	DEBUG_PRINT("WAIT ENDED...");
316 
317 	return response;
318 }
319 
close()320 void FileAccessNetwork::close() {
321 
322 	if (!opened)
323 		return;
324 
325 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
326 
327 	DEBUG_PRINT("CLOSE");
328 	nc->lock_mutex();
329 	nc->put_32(id);
330 	nc->put_32(COMMAND_CLOSE);
331 	pages.clear();
332 	opened = false;
333 	nc->unlock_mutex();
334 }
is_open() const335 bool FileAccessNetwork::is_open() const {
336 
337 	return opened;
338 }
339 
seek(size_t p_position)340 void FileAccessNetwork::seek(size_t p_position) {
341 
342 	ERR_FAIL_COND_MSG(!opened, "File must be opened before use.");
343 	eof_flag = p_position > total_size;
344 
345 	if (p_position >= total_size) {
346 		p_position = total_size;
347 	}
348 
349 	pos = p_position;
350 }
351 
seek_end(int64_t p_position)352 void FileAccessNetwork::seek_end(int64_t p_position) {
353 
354 	seek(total_size + p_position);
355 }
get_position() const356 size_t FileAccessNetwork::get_position() const {
357 
358 	ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use.");
359 	return pos;
360 }
get_len() const361 size_t FileAccessNetwork::get_len() const {
362 
363 	ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use.");
364 	return total_size;
365 }
366 
eof_reached() const367 bool FileAccessNetwork::eof_reached() const {
368 
369 	ERR_FAIL_COND_V_MSG(!opened, false, "File must be opened before use.");
370 	return eof_flag;
371 }
372 
get_8() const373 uint8_t FileAccessNetwork::get_8() const {
374 
375 	uint8_t v;
376 	get_buffer(&v, 1);
377 	return v;
378 }
379 
_queue_page(int p_page) const380 void FileAccessNetwork::_queue_page(int p_page) const {
381 
382 	if (p_page >= pages.size())
383 		return;
384 	if (pages[p_page].buffer.empty() && !pages[p_page].queued) {
385 
386 		FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
387 
388 		nc->blockrequest_mutex->lock();
389 		FileAccessNetworkClient::BlockRequest br;
390 		br.id = id;
391 		br.offset = size_t(p_page) * page_size;
392 		br.size = page_size;
393 		nc->block_requests.push_back(br);
394 		pages.write[p_page].queued = true;
395 		nc->blockrequest_mutex->unlock();
396 		DEBUG_PRINT("QUEUE PAGE POST");
397 		nc->sem->post();
398 		DEBUG_PRINT("queued " + itos(p_page));
399 	}
400 }
401 
get_buffer(uint8_t * p_dst,int p_length) const402 int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const {
403 
404 	//bool eof=false;
405 	if (pos + p_length > total_size) {
406 		eof_flag = true;
407 	}
408 	if (pos + p_length >= total_size) {
409 		p_length = total_size - pos;
410 	}
411 
412 	//FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
413 
414 	uint8_t *buff = last_page_buff;
415 
416 	for (int i = 0; i < p_length; i++) {
417 
418 		int page = pos / page_size;
419 
420 		if (page != last_page) {
421 			buffer_mutex->lock();
422 			if (pages[page].buffer.empty()) {
423 				waiting_on_page = page;
424 				for (int j = 0; j < read_ahead; j++) {
425 
426 					_queue_page(page + j);
427 				}
428 				buffer_mutex->unlock();
429 				DEBUG_PRINT("wait");
430 				page_sem->wait();
431 				DEBUG_PRINT("done");
432 			} else {
433 
434 				for (int j = 0; j < read_ahead; j++) {
435 
436 					_queue_page(page + j);
437 				}
438 				//queue pages
439 				buffer_mutex->unlock();
440 			}
441 
442 			buff = pages.write[page].buffer.ptrw();
443 			last_page_buff = buff;
444 			last_page = page;
445 		}
446 
447 		p_dst[i] = buff[pos - uint64_t(page) * page_size];
448 		pos++;
449 	}
450 
451 	return p_length;
452 }
453 
get_error() const454 Error FileAccessNetwork::get_error() const {
455 
456 	return pos == total_size ? ERR_FILE_EOF : OK;
457 }
458 
flush()459 void FileAccessNetwork::flush() {
460 	ERR_FAIL();
461 }
462 
store_8(uint8_t p_dest)463 void FileAccessNetwork::store_8(uint8_t p_dest) {
464 
465 	ERR_FAIL();
466 }
467 
file_exists(const String & p_path)468 bool FileAccessNetwork::file_exists(const String &p_path) {
469 
470 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
471 	nc->lock_mutex();
472 	nc->put_32(id);
473 	nc->put_32(COMMAND_FILE_EXISTS);
474 	CharString cs = p_path.utf8();
475 	nc->put_32(cs.length());
476 	nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
477 	nc->unlock_mutex();
478 	DEBUG_PRINT("FILE EXISTS POST");
479 	nc->sem->post();
480 	sem->wait();
481 
482 	return exists_modtime != 0;
483 }
484 
_get_modified_time(const String & p_file)485 uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) {
486 
487 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
488 	nc->lock_mutex();
489 	nc->put_32(id);
490 	nc->put_32(COMMAND_GET_MODTIME);
491 	CharString cs = p_file.utf8();
492 	nc->put_32(cs.length());
493 	nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
494 	nc->unlock_mutex();
495 	DEBUG_PRINT("MODTIME POST");
496 	nc->sem->post();
497 	sem->wait();
498 
499 	return exists_modtime;
500 }
501 
_get_unix_permissions(const String & p_file)502 uint32_t FileAccessNetwork::_get_unix_permissions(const String &p_file) {
503 	ERR_PRINT("Getting UNIX permissions from network drives is not implemented yet");
504 	return 0;
505 }
506 
_set_unix_permissions(const String & p_file,uint32_t p_permissions)507 Error FileAccessNetwork::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
508 	ERR_PRINT("Setting UNIX permissions on network drives is not implemented yet");
509 	return ERR_UNAVAILABLE;
510 }
511 
configure()512 void FileAccessNetwork::configure() {
513 
514 	GLOBAL_DEF("network/remote_fs/page_size", 65536);
515 	ProjectSettings::get_singleton()->set_custom_property_info("network/remote_fs/page_size", PropertyInfo(Variant::INT, "network/remote_fs/page_size", PROPERTY_HINT_RANGE, "1,65536,1,or_greater")); //is used as denominator and can't be zero
516 	GLOBAL_DEF("network/remote_fs/page_read_ahead", 4);
517 	ProjectSettings::get_singleton()->set_custom_property_info("network/remote_fs/page_read_ahead", PropertyInfo(Variant::INT, "network/remote_fs/page_read_ahead", PROPERTY_HINT_RANGE, "0,8,1,or_greater"));
518 }
519 
FileAccessNetwork()520 FileAccessNetwork::FileAccessNetwork() {
521 
522 	eof_flag = false;
523 	opened = false;
524 	pos = 0;
525 	sem = Semaphore::create();
526 	page_sem = Semaphore::create();
527 	buffer_mutex = Mutex::create();
528 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
529 	nc->lock_mutex();
530 	id = nc->last_id++;
531 	nc->accesses[id] = this;
532 	nc->unlock_mutex();
533 	page_size = GLOBAL_GET("network/remote_fs/page_size");
534 	read_ahead = GLOBAL_GET("network/remote_fs/page_read_ahead");
535 	last_activity_val = 0;
536 	waiting_on_page = -1;
537 	last_page = -1;
538 }
539 
~FileAccessNetwork()540 FileAccessNetwork::~FileAccessNetwork() {
541 
542 	close();
543 	memdelete(sem);
544 	memdelete(page_sem);
545 	memdelete(buffer_mutex);
546 
547 	FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
548 	nc->lock_mutex();
549 	id = nc->last_id++;
550 	nc->accesses.erase(id);
551 	nc->unlock_mutex();
552 }
553