void wr_transfer_download_add_file(wr_transfer_t *transfer, wr_file_t *file, wi_boolean_t recursive) { wi_string_t *remote_path, *local_path, *parent_path; remote_path = wr_file_path(file); if(recursive) { local_path = wi_string_by_appending_path_component(transfer->name, wi_string_substring_from_index(remote_path, wi_string_length(transfer->master_path))); } else { local_path = wi_string_last_path_component(remote_path); } local_path = wi_string_by_appending_path_component(wi_string_by_normalizing_path(wr_download_path), local_path); parent_path = wi_string_by_deleting_last_path_component(local_path); wi_fs_create_directory(parent_path, 0755); if(wr_file_type(file) == WR_FILE_FILE) { wi_mutable_array_add_data(transfer->remote_paths, remote_path); wi_mutable_array_add_data(transfer->local_paths, local_path); wi_mutable_array_add_data(transfer->files, file); transfer->total_size += wr_file_size(file); } else { if(!wi_fs_path_exists(local_path, NULL)) { if(!wi_fs_create_directory(local_path, 0755)) { wr_printf_prefix(WI_STR("get: Unable to create directory at %@: %m"), local_path); } } } }
void wi_parse_wire_command(wi_string_t *buffer, wi_string_t **out_command, wi_string_t **out_arguments) { wi_uinteger_t index; index = wi_string_index_of_string(buffer, WI_STR(" "), 0); if(index != WI_NOT_FOUND) { *out_command = wi_string_substring_to_index(buffer, index); *out_arguments = wi_string_substring_from_index(buffer, index + 1); } else { *out_command = wi_autorelease(wi_copy(buffer)); *out_arguments = wi_string(); } if(wi_string_has_prefix(*out_command, WI_STR("/"))) *out_command = wi_string_by_deleting_characters_to_index(*out_command, 1); }
wi_url_t * wi_url_init_with_string(wi_url_t *url, wi_string_t *string) { wi_string_t *userpassword; wi_range_t range; range = wi_string_range_of_string(string, WI_STR("://"), 0); if(range.location != WI_NOT_FOUND) { url->scheme = wi_retain(wi_string_substring_to_index(string, range.location)); if(range.location + range.length >= wi_string_length(string)) goto end; else string = wi_string_substring_from_index(string, range.location + 3); } range = wi_string_range_of_string(string, WI_STR("/"), 0); if(range.location != WI_NOT_FOUND) { url->path = wi_retain(wi_string_substring_from_index(string, range.location)); string = wi_string_substring_to_index(string, range.location); } range = wi_string_range_of_string(string, WI_STR("@"), 0); if(range.location != WI_NOT_FOUND) { userpassword = wi_string_substring_to_index(string, range.location); string = wi_string_substring_from_index(string, range.location + 1); range = wi_string_range_of_string(userpassword, WI_STR(":"), 0); if(range.location != WI_NOT_FOUND && range.location != wi_string_length(userpassword) - 1) { url->user = wi_retain(wi_string_substring_to_index(userpassword, range.location)); url->password = wi_retain(wi_string_substring_from_index(userpassword, range.location + 1)); } else { url->user = wi_retain(userpassword); } } range = wi_string_range_of_string(string, WI_STR(":"), 0); if(range.location == WI_NOT_FOUND || range.location + range.length >= wi_string_length(string) || wi_string_contains_string(wi_string_substring_from_index(string, range.location + 1), WI_STR(":"), 0)) { url->host = wi_copy(string); } else { url->host = wi_retain(wi_string_substring_to_index(string, range.location)); url->port = wi_string_uint32(wi_string_substring_from_index(string, range.location + 1)); } end: _wi_url_regenerate_string(url); return url; }
void wi_parse_wired_command(wi_string_t *buffer, wi_string_t **out_command, wi_array_t **out_arguments) { wi_string_t *string, *substring; wi_uinteger_t index; index = wi_string_index_of_string(buffer, WI_STR(_WI_WIRED_MESSAGE_SEPARATOR), WI_STRING_BACKWARDS); if(index != WI_NOT_FOUND) string = wi_string_by_deleting_characters_from_index(buffer, index); else string = wi_autorelease(wi_copy(buffer)); index = wi_string_index_of_string(string, WI_STR(" "), 0); if(index != WI_NOT_FOUND) { *out_command = wi_string_substring_to_index(string, index); substring = wi_string_substring_from_index(string, index + 1); *out_arguments = wi_string_components_separated_by_string(substring, WI_STR(_WI_WIRED_FIELD_SEPARATOR)); } else { *out_command = string; *out_arguments = wi_array(); } }
wi_date_t * wi_date_init_with_rfc3339_string(wi_date_t *date, wi_string_t *string) { wi_mutable_string_t *fullstring; wi_string_t *timezone; if(wi_string_length(string) >= 19) { fullstring = wi_autorelease(wi_mutable_copy(string)); timezone = wi_string_substring_from_index(fullstring, 19); if(wi_is_equal(timezone, WI_STR("Z"))) { wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(19, 1)); return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S")); } else if(wi_string_length(timezone) == 6) { wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(22, 1)); return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S%z")); } } wi_release(date); return NULL; }
wi_boolean_t wi_config_write_file(wi_config_t *config) { wi_enumerator_t *enumerator; wi_file_t *file, *tmpfile; wi_string_t *string, *name, *value; wi_mutable_set_t *keys; wi_boolean_t write; file = wi_file_for_updating(config->path); if(!file) { wi_log_err(WI_STR("Could not open %@: %m"), config->path); return false; } tmpfile = wi_file_temporary_file(); wi_lock_lock(config->lock); keys = wi_autorelease(wi_mutable_copy(config->changes)); while((string = wi_file_read_line(file))) { if(wi_string_length(string) == 0) { wi_file_write_format(tmpfile, WI_STR("\n")); } else if(wi_string_has_prefix(string, WI_STR("#"))) { write = true; string = wi_string_substring_from_index(string, 1); if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); } else { write = true; if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("%@\n"), string); } } enumerator = wi_set_data_enumerator(keys); while((name = wi_enumerator_next_data(enumerator))) _wi_config_write_setting_to_file(config, name, tmpfile); wi_mutable_set_remove_all_data(config->changes); wi_lock_unlock(config->lock); wi_file_truncate(file, 0); wi_file_seek(tmpfile, 0); while((string = wi_file_read_line(tmpfile))) wi_file_write_format(file, WI_STR("%@\n"), string); return true; }
void wd_trackers_apply_settings(void) { wi_enumerator_t *enumerator, *address_enumerator; wi_string_t *string, *path; wi_url_t *url; wi_address_t *address; wd_tracker_t *tracker; wi_uinteger_t port; wi_array_wrlock(wd_trackers); wi_mutable_array_remove_all_data(wd_trackers); enumerator = wi_array_data_enumerator(wd_settings.tracker); while((string = wi_enumerator_next_data(enumerator))) { tracker = wi_autorelease(wd_tracker_init(wd_tracker_alloc())); url = wi_autorelease(wi_url_init_with_string(wi_url_alloc(), string)); if(!wi_url_is_valid(url)) { wi_log_warn(WI_STR("Could not parse tracker URL \"%@\""), string); continue; } tracker->tls = wi_socket_tls_init_with_type(wi_socket_tls_alloc(), WI_SOCKET_TLS_CLIENT); if(!tracker->tls) { wi_log_warn(WI_STR("Could not create TLS context: %m")); continue; } if(wd_settings.controlcipher) { if(!wi_socket_tls_set_ciphers(tracker->tls, wd_settings.controlcipher)) { wi_log_err(WI_STR("Could not set TLS cipher list \"%@\": %m"), wd_settings.controlcipher); continue; } } path = wi_url_path(url); if(!path || wi_string_length(path) == 0) path = WI_STR("/"); tracker->host = wi_retain(wi_url_host(url)); tracker->category = wi_retain(wi_string_substring_from_index(path, 1)); tracker->addresses = wi_retain(wi_host_addresses(wi_host_with_string(tracker->host))); if(!tracker->addresses) { wi_log_warn(WI_STR("Could not resolve \"%@\": %m"), tracker->host); continue; } port = wi_url_port(url); if(port == 0) port = WD_TRACKER_PORT; address_enumerator = wi_array_data_enumerator(tracker->addresses); while((address = wi_enumerator_next_data(address_enumerator))) wi_address_set_port(address, port); wi_mutable_array_add_data(wd_trackers, tracker); } wi_array_unlock(wd_trackers); }
wi_boolean_t wr_transfer_upload_add_file(wr_transfer_t *transfer, wr_file_t *file) { WI_FTS *fts = NULL; WI_FTSENT *p; wi_string_t *local_path, *remote_path; wr_file_t *fts_file; char *paths[2]; if(wr_file_type(file) == WR_FILE_FILE) { wi_mutable_array_add_data(transfer->local_paths, wr_file_path(file)); wi_mutable_array_add_data(transfer->remote_paths, wr_files_full_path(transfer->name)); wi_mutable_array_add_data(transfer->files, file); transfer->total_size += wr_file_size(file); } else { wr_transfers_recursive_upload = true; transfer->recursive = true; paths[0] = (char *) wi_string_cstring(wr_file_path(file)); paths[1] = NULL; errno = 0; fts = wi_fts_open(paths, WI_FTS_NOSTAT | WI_FTS_LOGICAL, NULL); if(!fts) return false; if(fts && errno != 0) { wi_fts_close(fts); return false; } while((p = wi_fts_read(fts))) { if(p->fts_level > 10) { wi_fts_set(fts, p, WI_FTS_SKIP); continue; } switch(p->fts_info) { case WI_FTS_DC: errno = ELOOP; wr_printf_prefix(WI_STR("put: Could not read %s: %s"), strerror(errno)); continue; break; case WI_FTS_DP: continue; break; case WI_FTS_DNR: case WI_FTS_ERR: wr_printf_prefix(WI_STR("put: Could not read %s: %s"), strerror(p->fts_errno)); continue; break; } if(p->fts_name[0] == '.') { wi_fts_set(fts, p, WI_FTS_SKIP); continue; } local_path = wi_string_with_cstring(p->fts_path); remote_path = wi_string_by_normalizing_path(wi_string_by_appending_path_component(transfer->master_path, wi_string_substring_from_index(local_path, wi_string_length(transfer->source_path)))); if(p->fts_info == WI_FTS_D) { // wr_send_command(WI_STR("FOLDER %#@"), remote_path); } else { wi_mutable_array_add_data(transfer->local_paths, local_path); wi_mutable_array_add_data(transfer->remote_paths, remote_path); fts_file = wr_file_init_with_local_path(wr_file_alloc(), local_path); transfer->total_size += wr_file_size(fts_file); wi_mutable_array_add_data(transfer->files, fts_file); wi_release(fts_file); } } wi_fts_close(fts); } return true; }