Skip to content

Commit 970e245

Browse files
maminrayejtheduke
authored andcommitted
fix the default mtime when timestamps are not present
1 parent 0040ef3 commit 970e245

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lib/virtual-fs/src/webc_volume_fs.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::{
44
path::{Path, PathBuf},
55
pin::Pin,
66
result::Result,
7+
sync::OnceLock,
78
task::Poll,
9+
time::UNIX_EPOCH,
810
};
911

1012
use futures::future::BoxFuture;
@@ -206,13 +208,9 @@ impl VirtualFile for File {
206208
}
207209

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

218216
fn created_time(&self) -> u64 {
@@ -295,17 +293,19 @@ impl AsyncWrite for File {
295293
}
296294
}
297295

298-
fn compat_meta(meta: webc::compat::Metadata) -> Metadata {
299-
// HACK: timestamps are not present in webc v2, so we have to return
300-
// a stub modified time. previously we used to just return 0, but that
301-
// proved to cause problems with programs that interpret the value 0.
302-
// to circumvent this problem, we decided to return a non-zero value.
303-
fn get_modified(timestamps: Option<webc::Timestamps>) -> u64 {
304-
timestamps
305-
.map(|t| t.modified())
306-
.unwrap_or(std::time::UNIX_EPOCH.elapsed().unwrap().as_secs())
307-
}
296+
// HACK: timestamps are not present in webc v2, so we have to return
297+
// a stub modified time. previously we used to just return 0, but that
298+
// proved to cause problems with programs that interpret the value 0.
299+
// to circumvent this problem, we decided to return a non-zero value.
300+
fn get_modified(timestamps: Option<webc::Timestamps>) -> u64 {
301+
static DEFAULT: OnceLock<u64> = OnceLock::new();
308302

303+
timestamps
304+
.map(|t| t.modified())
305+
.unwrap_or(*DEFAULT.get_or_init(|| UNIX_EPOCH.elapsed().unwrap().as_secs()))
306+
}
307+
308+
fn compat_meta(meta: webc::compat::Metadata) -> Metadata {
309309
match meta {
310310
webc::compat::Metadata::Dir { timestamps } => Metadata {
311311
ft: FileType {
@@ -437,6 +437,8 @@ mod tests {
437437
.unwrap()
438438
.map(|r| r.unwrap())
439439
.collect();
440+
441+
let modified = get_modified(None);
440442
let expected = vec![
441443
DirEntry {
442444
path: "/lib/.DS_Store".into(),
@@ -447,7 +449,7 @@ mod tests {
447449
},
448450
accessed: 0,
449451
created: 0,
450-
modified: 0,
452+
modified,
451453
len: 6148,
452454
}),
453455
},
@@ -460,7 +462,7 @@ mod tests {
460462
},
461463
accessed: 0,
462464
created: 0,
463-
modified: 0,
465+
modified,
464466
len: 0,
465467
}),
466468
},
@@ -473,7 +475,7 @@ mod tests {
473475
},
474476
accessed: 0,
475477
created: 0,
476-
modified: 0,
478+
modified,
477479
len: 4694941,
478480
}),
479481
},
@@ -486,7 +488,7 @@ mod tests {
486488
},
487489
accessed: 0,
488490
created: 0,
489-
modified: 0,
491+
modified,
490492
len: 0,
491493
}),
492494
},
@@ -502,14 +504,15 @@ mod tests {
502504

503505
let fs = WebcVolumeFileSystem::new(volume);
504506

507+
let modified = get_modified(None);
505508
let python_wasm = crate::Metadata {
506509
ft: crate::FileType {
507510
file: true,
508511
..Default::default()
509512
},
510513
accessed: 0,
511514
created: 0,
512-
modified: 0,
515+
modified,
513516
len: 4694941,
514517
};
515518
assert_eq!(
@@ -535,7 +538,7 @@ mod tests {
535538
},
536539
accessed: 0,
537540
created: 0,
538-
modified: 0,
541+
modified,
539542
len: 0,
540543
},
541544
);

0 commit comments

Comments
 (0)