Skip to content

Commit

Permalink
feat: Check last modified metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Sep 7, 2022
1 parent f79bfa0 commit e280c80
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ notify-rust = "4.5.8"
tokio = { version = "1.21.0", features = ["full"] }
futures = "0.3.24"
async-trait = "0.1.57"
time = "0.3.14"

[profile.release]
codegen-units = 1
Expand Down
57 changes: 43 additions & 14 deletions src/cloud/providers/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::SYNCED_PATHS;
use dashmap::DashSet;
use s3::{creds::Credentials, Bucket, Region};
use std::path::Path;
use tokio::{fs, io::AsyncWriteExt};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use tokio::fs;

pub struct S3Storage {
bucket: Bucket,
Expand Down Expand Up @@ -50,22 +51,49 @@ impl CloudAdapter for S3Storage {
fs::create_dir_all(parent).await?;
}

let mut file = fs::OpenOptions::new()
.write(true)
.read(true)
.create(true)
.open(&path)
.await?;
let metadata = file.metadata().await?;
let (size, last_modified) = fs::metadata(&path)
.await
.map(|m| (m.len(), m.modified().ok()))
.unwrap_or((0, None));

SYNCED_PATHS.0.insert(stringify_path(&path));

if metadata.len() != obj.size
/* || !compare_date(metadata.modified().await?, obj.last_modified) */
{
file.write_all(&self.get(&path).await?).await?;
synced += 1;
if size == obj.size {
continue;
}

log::debug!(
"{:?} has different size, cloud({}) != local({})",
path,
obj.size,
size
);

let prefer_local = || {
if let Some(last_modified) = last_modified {
let cloud_last_modified =
OffsetDateTime::parse(&obj.last_modified, &Rfc3339).unwrap();
let local_last_modified = OffsetDateTime::from(last_modified);
log::debug!("{path:?} last modified: local({local_last_modified}) > cloud({cloud_last_modified}) = {}", local_last_modified > cloud_last_modified);
return obj.size == 0 || local_last_modified > cloud_last_modified;
}

obj.size == 0
};

if prefer_local() {
log::debug!("Preferring local {path:?} instead of cloud version");
self.save(&path).await?;
} else {
let buffer = if obj.size == 0 {
vec![]
} else {
self.get(&path).await?
};
fs::write(&path, &buffer).await?;
}

synced += 1;
}
}

Expand All @@ -82,6 +110,7 @@ impl CloudAdapter for S3Storage {
unreachable!()
}
} else {
log::debug!("{:?} not synced, Saving to cloud...", path);
self.save(&path).await?;
SYNCED_PATHS.0.insert(stringify_path(&path));
synced += 1;
Expand All @@ -95,7 +124,7 @@ impl CloudAdapter for S3Storage {
}

async fn exists(&self, path: &Path) -> Result<bool> {
let (_, code): (_, u16) = self.bucket.head_object(normalize_path(path)).await?;
let (_, code) = self.bucket.head_object(normalize_path(path)).await?;
Ok(code == 200)
}

Expand Down

0 comments on commit e280c80

Please sign in to comment.