예제 #1
0
파일: wi-config.c 프로젝트: asvitkine/phxd
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;
}
예제 #2
0
void wr_transfer_request(wr_transfer_t *transfer) {
    
	wi_string_t			*local_path;
	wi_fs_stat_t		sb;
    
	if(wi_array_count(transfer->local_paths) == 0) {
		wr_transfer_start_next_or_stop(transfer);
		
		return;
	}
    
	if(transfer->type == WR_TRANSFER_DOWNLOAD) {
		local_path = wi_array_first_data(transfer->local_paths);
		
		if(wi_fs_stat_path(local_path, &sb)) {
			if(!transfer->recursive) {
				wr_printf_prefix(WI_STR("get: File already exists at %@"),
                                 local_path);
			}
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive) {
				transfer->total_transferred += sb.size;
				wr_transfer_start_next_or_stop(transfer);
			}
			
			return;
		}
		
		if(!wi_string_has_suffix(local_path, WI_STR(WR_TRANSFERS_SUFFIX))) {
			local_path = wi_string_by_appending_string(local_path, WI_STR(WR_TRANSFERS_SUFFIX));
			
			wi_mutable_array_replace_data_at_index(transfer->local_paths, local_path, 0);
		}
		
		if(wi_fs_stat_path(local_path, &sb)) {
			transfer->file_offset = sb.size;
			
			if(sb.size >= WR_CHECKSUM_SIZE)
				transfer->checksum = wi_retain(wi_fs_sha1_for_path(local_path, WR_CHECKSUM_SIZE));
		}
		
		transfer->file = wi_retain(wi_file_for_updating(local_path));
		
		if(!transfer->file) {
			wr_printf_prefix(WI_STR("get: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
		
		transfer->file_size = wr_file_size(wi_array_first_data(transfer->files));
        
		// wr_send_command(WI_STR("GET %#@%c%llu"),
        //                wi_array_first_data(transfer->remote_paths),	WR_FIELD_SEPARATOR,
        //                transfer->file_offset);
        
	} else {
		local_path = wi_array_first_data(transfer->local_paths);
        
		transfer->file = wi_retain(wi_file_for_reading(local_path));
        
		if(!transfer->file) {
			wr_printf_prefix(WI_STR("put: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
		
		if(!wi_fs_stat_path(local_path, &sb)) {
			wr_printf_prefix(WI_STR("put: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
        
		transfer->file_size = sb.size;
		transfer->checksum = wi_retain(wi_fs_sha1_for_path(local_path, WR_CHECKSUM_SIZE));
		
		// wr_send_command(WI_STR("PUT %#@%c%llu%c%#@"),
		//				wi_array_first_data(transfer->remote_paths),	WR_FIELD_SEPARATOR,
		//				transfer->file_size,							WR_FIELD_SEPARATOR,
		//				transfer->checksum);
	}
     
}