2020-04-28 10:18:14 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use chrono::{SecondsFormat, Utc};
|
2020-07-07 15:15:53 +00:00
|
|
|
use hyper::{Body, Response};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
use garage_table::*;
|
|
|
|
use garage_util::data::*;
|
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::block_ref_table::*;
|
|
|
|
use garage_model::garage::Garage;
|
|
|
|
use garage_model::object_table::*;
|
|
|
|
use garage_model::version_table::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-11-08 14:04:30 +00:00
|
|
|
use crate::error::*;
|
|
|
|
|
2020-04-28 10:18:14 +00:00
|
|
|
pub async fn handle_copy(
|
|
|
|
garage: Arc<Garage>,
|
|
|
|
dest_bucket: &str,
|
|
|
|
dest_key: &str,
|
|
|
|
source_bucket: &str,
|
|
|
|
source_key: &str,
|
2020-07-07 15:15:53 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2020-11-11 15:12:42 +00:00
|
|
|
let source_object = garage
|
2020-04-28 10:18:14 +00:00
|
|
|
.object_table
|
|
|
|
.get(&source_bucket.to_string(), &source_key.to_string())
|
|
|
|
.await?
|
2020-11-11 15:12:42 +00:00
|
|
|
.ok_or(Error::NotFound)?;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-11-11 15:12:42 +00:00
|
|
|
let source_last_v = source_object
|
2020-04-28 10:18:14 +00:00
|
|
|
.versions()
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.filter(|v| v.is_complete())
|
|
|
|
.next()
|
2020-11-11 15:12:42 +00:00
|
|
|
.ok_or(Error::NotFound)?;
|
|
|
|
|
2020-07-08 15:34:37 +00:00
|
|
|
let source_last_state = match &source_last_v.state {
|
|
|
|
ObjectVersionState::Complete(x) => x,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
let new_uuid = gen_uuid();
|
2021-03-15 14:26:29 +00:00
|
|
|
let new_timestamp = now_msec();
|
2020-04-28 10:18:14 +00:00
|
|
|
let dest_object_version = ObjectVersion {
|
|
|
|
uuid: new_uuid,
|
2021-03-15 14:26:29 +00:00
|
|
|
timestamp: new_timestamp,
|
2020-07-08 15:34:37 +00:00
|
|
|
state: ObjectVersionState::Complete(source_last_state.clone()),
|
2020-04-28 10:18:14 +00:00
|
|
|
};
|
2021-03-15 14:26:29 +00:00
|
|
|
let dest_object = Object::new(
|
|
|
|
dest_bucket.to_string(),
|
|
|
|
dest_key.to_string(),
|
|
|
|
vec![dest_object_version],
|
|
|
|
);
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-03-15 14:26:29 +00:00
|
|
|
match source_last_state {
|
2020-04-28 10:18:14 +00:00
|
|
|
ObjectVersionData::DeleteMarker => {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
2020-07-08 15:33:24 +00:00
|
|
|
ObjectVersionData::Inline(_meta, _bytes) => {
|
2020-04-28 10:18:14 +00:00
|
|
|
garage.object_table.insert(&dest_object).await?;
|
|
|
|
}
|
2021-03-15 14:26:29 +00:00
|
|
|
ObjectVersionData::FirstBlock(meta, _first_block_hash) => {
|
|
|
|
// Get block list from source version
|
2020-04-28 10:18:14 +00:00
|
|
|
let source_version = garage
|
|
|
|
.version_table
|
|
|
|
.get(&source_last_v.uuid, &EmptyKey)
|
|
|
|
.await?;
|
2020-11-11 15:12:42 +00:00
|
|
|
let source_version = source_version.ok_or(Error::NotFound)?;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-03-15 14:26:29 +00:00
|
|
|
// Write an "uploading" marker in Object table
|
|
|
|
// This holds a reference to the object in the Version table
|
|
|
|
// so that it won't be deleted, e.g. by repair_versions.
|
|
|
|
let tmp_dest_object_version = ObjectVersion {
|
|
|
|
uuid: new_uuid,
|
|
|
|
timestamp: new_timestamp,
|
|
|
|
state: ObjectVersionState::Uploading(meta.headers.clone()),
|
|
|
|
};
|
|
|
|
let tmp_dest_object = Object::new(
|
|
|
|
dest_bucket.to_string(),
|
|
|
|
dest_key.to_string(),
|
|
|
|
vec![tmp_dest_object_version],
|
|
|
|
);
|
|
|
|
garage.object_table.insert(&tmp_dest_object).await?;
|
|
|
|
|
|
|
|
// Write version in the version table. Even with empty block list,
|
|
|
|
// this means that the BlockRef entries linked to this version cannot be
|
|
|
|
// marked as deleted (they are marked as deleted only if the Version
|
|
|
|
// doesn't exist or is marked as deleted).
|
2021-03-10 15:21:56 +00:00
|
|
|
let mut dest_version = Version::new(
|
2020-04-28 10:18:14 +00:00
|
|
|
new_uuid,
|
|
|
|
dest_bucket.to_string(),
|
|
|
|
dest_key.to_string(),
|
|
|
|
false,
|
|
|
|
);
|
2021-03-15 14:26:29 +00:00
|
|
|
garage.version_table.insert(&dest_version).await?;
|
|
|
|
|
|
|
|
// Fill in block list for version and insert block refs
|
2021-03-10 15:21:56 +00:00
|
|
|
for (bk, bv) in source_version.blocks.items().iter() {
|
|
|
|
dest_version.blocks.put(*bk, *bv);
|
|
|
|
}
|
2020-04-28 10:18:14 +00:00
|
|
|
let dest_block_refs = dest_version
|
2021-03-10 15:21:56 +00:00
|
|
|
.blocks
|
|
|
|
.items()
|
2020-04-28 10:18:14 +00:00
|
|
|
.iter()
|
|
|
|
.map(|b| BlockRef {
|
2021-03-10 15:21:56 +00:00
|
|
|
block: b.1.hash,
|
2020-04-28 10:18:14 +00:00
|
|
|
version: new_uuid,
|
2021-03-10 15:21:56 +00:00
|
|
|
deleted: false.into(),
|
2020-04-28 10:18:14 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
futures::try_join!(
|
|
|
|
garage.version_table.insert(&dest_version),
|
|
|
|
garage.block_ref_table.insert_many(&dest_block_refs[..]),
|
|
|
|
)?;
|
2021-03-15 14:26:29 +00:00
|
|
|
|
|
|
|
// Insert final object
|
|
|
|
// We do this last because otherwise there is a race condition in the case where
|
|
|
|
// the copy call has the same source and destination (this happens, rclone does
|
|
|
|
// it to update the modification timestamp for instance). If we did this concurrently
|
|
|
|
// with the stuff before, the block's reference counts could be decremented before
|
|
|
|
// they are incremented again for the new version, leading to data being deleted.
|
|
|
|
garage.object_table.insert(&dest_object).await?;
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:26:29 +00:00
|
|
|
let now = Utc::now(); // FIXME use the unix timestamp from above
|
2020-04-28 10:18:14 +00:00
|
|
|
let last_modified = now.to_rfc3339_opts(SecondsFormat::Secs, true);
|
|
|
|
let mut xml = String::new();
|
|
|
|
writeln!(&mut xml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
|
|
|
|
writeln!(&mut xml, r#"<CopyObjectResult>"#).unwrap();
|
|
|
|
writeln!(&mut xml, "\t<LastModified>{}</LastModified>", last_modified).unwrap();
|
|
|
|
writeln!(&mut xml, "</CopyObjectResult>").unwrap();
|
|
|
|
|
2021-02-19 22:40:18 +00:00
|
|
|
Ok(Response::builder()
|
2021-02-23 17:46:25 +00:00
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(xml.into_bytes()))?)
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|