示例#1
0
void Container::update_file(const std::string& filename, const path& relative_path, const path& src)
{
	
	{
		if (!validate())
		{
			if (has_callback_)
				send_callback(CONFLICT, MISSING_ENC_FILES);
			else
				throw(std::logic_error("severe: synchronization with cloud incomplete!"));
			return;
		}

		path location = relative_path.append_filename(filename);
		std::string file_name = location.get_filename();
		container_handle::file_node fnode = handle_.get_filenode(location);

		if (!FileSystem::file_exists(src.str()))
		{
			return;
		}

		using namespace CryptoPP;
		/*std::string crc;
		if (!secure_crc(src.str(), crc))
		{
			return;
		}*/
    std::string hash;
    if (!secure_hash(src.str(), hash))
    {
      return;
    }

		if (fnode.p == relative_path)
		{
			container_handle::block_node block_node = fnode.blocks[0];
			std::string hashname = block_node.filename;
			//std::string old_crc = block_node.crc;
      std::string old_hash = block_node.hash;
			
			if (old_hash == hash) { return; }
			send_callback(INFORMATION, UPDATE_FILE, filename);
			std::string location_name = block_node.location;

			int64_t size = FileSystem::file_size(src.str());
			int64_t old_size = fnode.size;
			if (!handle_.update_memusage_location(location_name, (size - old_size)))
				return;

			path store_path(handle_.get_location_path(location_name));

			if (!store_path.valid())
			{
				//THROW
				return;
			}

			std::string iv;
			bool ret = false;
			switch (algo_)
			{
			case AES:
				ret =helper_update_file<CryptoPP::AES>(fnode, src, store_path, iv);
				break;
			case SERPENT:
				ret = helper_update_file<CryptoPP::Serpent>(fnode, src, store_path, iv);
				break;
			case TWOFISH:
				ret = helper_update_file<CryptoPP::Twofish>(fnode, src, store_path, iv);
				break;
			default:
				return;
			}

			if (!ret)
			{
				handle_.update_memusage_location(location_name, old_size - size);
				return;
			}

			//handle_.update_file_crc(crc, location);
      handle_.update_file_hash(hash, location);
			//handle_.update_block_crc(crc, location, 1);
      handle_.update_block_hash(hash, location, 1);
			handle_.update_file_size(size, location);
		}
	}
	save();

#ifdef TDEBUG
	handle_.dump(std::string("C:\\Users\\Kai\\Desktop\\new_test.xml"));
#endif
}