Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata conversion to carry over the modified timestamp #4783

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions lib/virtual-fs/src/webc_volume_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,9 @@ impl VirtualFile for File {
}

fn last_modified(&self) -> u64 {
self.timestamps.map(|t| t.modified()).unwrap_or_else(|| {
// HACK: timestamps are not present in webc v2, so we have to return
// a stub modified time. previously we used to just return 0, but that
// proved to cause problems with programs that interpret the value 0.
// to circumvent this problem, we decided to return a non-zero value.
std::time::UNIX_EPOCH.elapsed().unwrap().as_secs()
})
self.timestamps
.map(|t| t.modified())
.unwrap_or_else(|| get_modified(None))
}

fn created_time(&self) -> u64 {
Expand Down Expand Up @@ -295,21 +291,33 @@ impl AsyncWrite for File {
}
}

// HACK: timestamps are not present in webc v2, so we have to return
// a stub modified time. previously we used to just return 0, but that
// proved to cause problems with programs that interpret the value 0.
// to circumvent this problem, we decided to return a non-zero value.
fn get_modified(timestamps: Option<webc::Timestamps>) -> u64 {
timestamps.map(|t| t.modified()).unwrap_or(1)
}

fn compat_meta(meta: webc::compat::Metadata) -> Metadata {
match meta {
webc::compat::Metadata::Dir { .. } => Metadata {
webc::compat::Metadata::Dir { timestamps } => Metadata {
ft: FileType {
dir: true,
..Default::default()
},
modified: get_modified(timestamps),
..Default::default()
},
webc::compat::Metadata::File { length, .. } => Metadata {
webc::compat::Metadata::File {
length, timestamps, ..
} => Metadata {
ft: FileType {
file: true,
..Default::default()
},
len: length.try_into().unwrap(),
modified: get_modified(timestamps),
..Default::default()
},
}
Expand Down Expand Up @@ -423,6 +431,8 @@ mod tests {
.unwrap()
.map(|r| r.unwrap())
.collect();

let modified = get_modified(None);
let expected = vec![
DirEntry {
path: "/lib/.DS_Store".into(),
Expand All @@ -433,7 +443,7 @@ mod tests {
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 6148,
}),
},
Expand All @@ -446,7 +456,7 @@ mod tests {
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 0,
}),
},
Expand All @@ -459,7 +469,7 @@ mod tests {
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 4694941,
}),
},
Expand All @@ -472,7 +482,7 @@ mod tests {
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 0,
}),
},
Expand All @@ -488,14 +498,15 @@ mod tests {

let fs = WebcVolumeFileSystem::new(volume);

let modified = get_modified(None);
let python_wasm = crate::Metadata {
ft: crate::FileType {
file: true,
..Default::default()
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 4694941,
};
assert_eq!(
Expand All @@ -521,7 +532,7 @@ mod tests {
},
accessed: 0,
created: 0,
modified: 0,
modified,
len: 0,
},
);
Expand Down
Loading