2020-04-28 10:18:14 +00:00
|
|
|
use std::sync::Arc;
|
2022-01-11 11:43:46 +00:00
|
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-03-15 15:21:41 +00:00
|
|
|
use hyper::{Body, Request, Response};
|
2020-04-28 10:18:14 +00:00
|
|
|
|
|
|
|
use garage_table::*;
|
|
|
|
use garage_util::data::*;
|
2021-03-15 15:21:41 +00:00
|
|
|
use garage_util::time::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
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::*;
|
2021-03-15 15:21:41 +00:00
|
|
|
use crate::s3_put::get_headers;
|
2021-05-03 20:45:42 +00:00
|
|
|
use crate::s3_xml;
|
2020-11-08 14:04:30 +00:00
|
|
|
|
2020-04-28 10:18:14 +00:00
|
|
|
pub async fn handle_copy(
|
|
|
|
garage: Arc<Garage>,
|
2021-03-15 15:21:41 +00:00
|
|
|
req: &Request<Body>,
|
2021-12-14 12:55:11 +00:00
|
|
|
dest_bucket_id: Uuid,
|
2020-04-28 10:18:14 +00:00
|
|
|
dest_key: &str,
|
2021-12-14 12:55:11 +00:00
|
|
|
source_bucket_id: Uuid,
|
2020-04-28 10:18:14 +00:00
|
|
|
source_key: &str,
|
2020-07-07 15:15:53 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2022-01-11 11:43:46 +00:00
|
|
|
let copy_precondition = CopyPreconditionHeaders::parse(req)?;
|
|
|
|
|
2020-11-11 15:12:42 +00:00
|
|
|
let source_object = garage
|
2020-04-28 10:18:14 +00:00
|
|
|
.object_table
|
2021-12-14 12:55:11 +00:00
|
|
|
.get(&source_bucket_id, &source_key.to_string())
|
2020-04-28 10:18:14 +00:00
|
|
|
.await?
|
2022-01-05 16:07:36 +00:00
|
|
|
.ok_or(Error::NoSuchKey)?;
|
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()
|
2021-04-23 20:18:00 +00:00
|
|
|
.find(|v| v.is_complete())
|
2022-01-05 16:07:36 +00:00
|
|
|
.ok_or(Error::NoSuchKey)?;
|
2020-11-11 15:12:42 +00:00
|
|
|
|
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
|
|
|
|
2021-03-15 15:21:41 +00:00
|
|
|
// Implement x-amz-metadata-directive: REPLACE
|
|
|
|
let old_meta = match source_last_state {
|
2020-04-28 10:18:14 +00:00
|
|
|
ObjectVersionData::DeleteMarker => {
|
2022-01-05 16:07:36 +00:00
|
|
|
return Err(Error::NoSuchKey);
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
2021-03-15 15:21:41 +00:00
|
|
|
ObjectVersionData::Inline(meta, _bytes) => meta,
|
|
|
|
ObjectVersionData::FirstBlock(meta, _fbh) => meta,
|
|
|
|
};
|
|
|
|
let new_meta = match req.headers().get("x-amz-metadata-directive") {
|
|
|
|
Some(v) if v == hyper::header::HeaderValue::from_static("REPLACE") => ObjectVersionMeta {
|
|
|
|
headers: get_headers(req)?,
|
|
|
|
size: old_meta.size,
|
|
|
|
etag: old_meta.etag.clone(),
|
|
|
|
},
|
|
|
|
_ => old_meta.clone(),
|
|
|
|
};
|
|
|
|
|
2021-05-03 20:45:42 +00:00
|
|
|
let etag = new_meta.etag.to_string();
|
|
|
|
|
2022-01-11 11:43:46 +00:00
|
|
|
// Check precondition, e.g. x-amz-copy-source-if-match
|
|
|
|
copy_precondition.check(source_last_v, etag.as_str())?;
|
|
|
|
|
2021-03-15 15:21:41 +00:00
|
|
|
// Save object copy
|
|
|
|
match source_last_state {
|
|
|
|
ObjectVersionData::DeleteMarker => unreachable!(),
|
|
|
|
ObjectVersionData::Inline(_meta, bytes) => {
|
|
|
|
let dest_object_version = ObjectVersion {
|
|
|
|
uuid: new_uuid,
|
|
|
|
timestamp: new_timestamp,
|
|
|
|
state: ObjectVersionState::Complete(ObjectVersionData::Inline(
|
|
|
|
new_meta,
|
|
|
|
bytes.clone(),
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
let dest_object = Object::new(
|
2021-12-14 12:55:11 +00:00
|
|
|
dest_bucket_id,
|
2021-03-15 15:21:41 +00:00
|
|
|
dest_key.to_string(),
|
|
|
|
vec![dest_object_version],
|
|
|
|
);
|
2020-04-28 10:18:14 +00:00
|
|
|
garage.object_table.insert(&dest_object).await?;
|
|
|
|
}
|
2021-03-15 15:21:41 +00:00
|
|
|
ObjectVersionData::FirstBlock(_meta, first_block_hash) => {
|
2021-03-15 14:26:29 +00:00
|
|
|
// 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?;
|
2022-01-05 16:07:36 +00:00
|
|
|
let source_version = source_version.ok_or(Error::NoSuchKey)?;
|
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,
|
2021-03-15 15:21:41 +00:00
|
|
|
state: ObjectVersionState::Uploading(new_meta.headers.clone()),
|
2021-03-15 14:26:29 +00:00
|
|
|
};
|
|
|
|
let tmp_dest_object = Object::new(
|
2021-12-14 12:55:11 +00:00
|
|
|
dest_bucket_id,
|
2021-03-15 14:26:29 +00:00
|
|
|
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-12-14 12:55:11 +00:00
|
|
|
let mut dest_version =
|
|
|
|
Version::new(new_uuid, dest_bucket_id, 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.
|
2021-03-15 15:21:41 +00:00
|
|
|
let dest_object_version = ObjectVersion {
|
|
|
|
uuid: new_uuid,
|
|
|
|
timestamp: new_timestamp,
|
|
|
|
state: ObjectVersionState::Complete(ObjectVersionData::FirstBlock(
|
|
|
|
new_meta,
|
|
|
|
*first_block_hash,
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
let dest_object = Object::new(
|
2021-12-14 12:55:11 +00:00
|
|
|
dest_bucket_id,
|
2021-03-15 15:21:41 +00:00
|
|
|
dest_key.to_string(),
|
|
|
|
vec![dest_object_version],
|
|
|
|
);
|
2021-03-15 14:26:29 +00:00
|
|
|
garage.object_table.insert(&dest_object).await?;
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:21:41 +00:00
|
|
|
let last_modified = msec_to_rfc3339(new_timestamp);
|
2021-05-03 20:45:42 +00:00
|
|
|
let result = s3_xml::CopyObjectResult {
|
|
|
|
last_modified: s3_xml::Value(last_modified),
|
|
|
|
etag: s3_xml::Value(etag),
|
|
|
|
};
|
|
|
|
let xml = s3_xml::to_xml_with_header(&result)?;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2021-02-19 22:40:18 +00:00
|
|
|
Ok(Response::builder()
|
2021-02-23 17:46:25 +00:00
|
|
|
.header("Content-Type", "application/xml")
|
2022-01-11 11:43:46 +00:00
|
|
|
.header("x-amz-version-id", hex::encode(new_uuid))
|
|
|
|
.header(
|
|
|
|
"x-amz-copy-source-version-id",
|
|
|
|
hex::encode(source_last_v.uuid),
|
|
|
|
)
|
2021-05-03 20:45:42 +00:00
|
|
|
.body(Body::from(xml))?)
|
2020-04-28 10:18:14 +00:00
|
|
|
}
|
2022-01-11 11:43:46 +00:00
|
|
|
|
|
|
|
struct CopyPreconditionHeaders {
|
|
|
|
copy_source_if_match: Option<Vec<String>>,
|
|
|
|
copy_source_if_modified_since: Option<SystemTime>,
|
|
|
|
copy_source_if_none_match: Option<Vec<String>>,
|
|
|
|
copy_source_if_unmodified_since: Option<SystemTime>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CopyPreconditionHeaders {
|
|
|
|
fn parse(req: &Request<Body>) -> Result<Self, Error> {
|
|
|
|
Ok(Self {
|
|
|
|
copy_source_if_match: req
|
|
|
|
.headers()
|
|
|
|
.get("x-amz-copy-source-if-match")
|
|
|
|
.map(|x| x.to_str())
|
|
|
|
.transpose()?
|
|
|
|
.map(|x| {
|
|
|
|
x.split(',')
|
|
|
|
.map(|m| m.trim().trim_matches('"').to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}),
|
|
|
|
copy_source_if_modified_since: req
|
|
|
|
.headers()
|
|
|
|
.get("x-amz-copy-source-if-modified-since")
|
|
|
|
.map(|x| x.to_str())
|
|
|
|
.transpose()?
|
|
|
|
.map(|x| httpdate::parse_http_date(x))
|
|
|
|
.transpose()
|
|
|
|
.ok_or_bad_request("Invalid date in x-amz-copy-source-if-modified-since")?,
|
|
|
|
copy_source_if_none_match: req
|
|
|
|
.headers()
|
|
|
|
.get("x-amz-copy-source-if-none-match")
|
|
|
|
.map(|x| x.to_str())
|
|
|
|
.transpose()?
|
|
|
|
.map(|x| {
|
|
|
|
x.split(',')
|
|
|
|
.map(|m| m.trim().trim_matches('"').to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}),
|
|
|
|
copy_source_if_unmodified_since: req
|
|
|
|
.headers()
|
|
|
|
.get("x-amz-copy-source-if-unmodified-since")
|
|
|
|
.map(|x| x.to_str())
|
|
|
|
.transpose()?
|
|
|
|
.map(|x| httpdate::parse_http_date(x))
|
|
|
|
.transpose()
|
|
|
|
.ok_or_bad_request("Invalid date in x-amz-copy-source-if-unmodified-since")?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check(&self, v: &ObjectVersion, etag: &str) -> Result<(), Error> {
|
|
|
|
let v_date = UNIX_EPOCH + Duration::from_millis(v.timestamp);
|
|
|
|
|
|
|
|
let ok = match (
|
|
|
|
&self.copy_source_if_match,
|
|
|
|
&self.copy_source_if_unmodified_since,
|
|
|
|
&self.copy_source_if_none_match,
|
|
|
|
&self.copy_source_if_modified_since,
|
|
|
|
) {
|
|
|
|
// TODO I'm not sure all of the conditions are evaluated correctly here
|
|
|
|
|
|
|
|
// If we have both if-match and if-unmodified-since,
|
|
|
|
// basically we don't care about if-unmodified-since,
|
|
|
|
// because in the spec it says that if if-match evaluates to
|
|
|
|
// true but if-unmodified-since evaluates to false,
|
|
|
|
// the copy is still done.
|
|
|
|
(Some(im), _, None, None) => im.iter().any(|x| x == etag || x == "*"),
|
|
|
|
(None, Some(ius), None, None) => v_date <= *ius,
|
|
|
|
|
|
|
|
// If we have both if-none-match and if-modified-since,
|
|
|
|
// then both of the two conditions must evaluate to true
|
|
|
|
(None, None, Some(inm), Some(ims)) => {
|
|
|
|
!inm.iter().any(|x| x == etag || x == "*") && v_date > *ims
|
|
|
|
}
|
|
|
|
(None, None, Some(inm), None) => !inm.iter().any(|x| x == etag || x == "*"),
|
|
|
|
(None, None, None, Some(ims)) => v_date > *ims,
|
|
|
|
_ => {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
"Invalid combination of x-amz-copy-source-if-xxxxx headers".into(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::PreconditionFailed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|