@@ -4,7 +4,9 @@ use std::{
4
4
path:: { Path , PathBuf } ,
5
5
pin:: Pin ,
6
6
result:: Result ,
7
+ sync:: OnceLock ,
7
8
task:: Poll ,
9
+ time:: UNIX_EPOCH ,
8
10
} ;
9
11
10
12
use futures:: future:: BoxFuture ;
@@ -206,13 +208,9 @@ impl VirtualFile for File {
206
208
}
207
209
208
210
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 ) )
216
214
}
217
215
218
216
fn created_time ( & self ) -> u64 {
@@ -295,17 +293,19 @@ impl AsyncWrite for File {
295
293
}
296
294
}
297
295
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 ( ) ;
308
302
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 {
309
309
match meta {
310
310
webc:: compat:: Metadata :: Dir { timestamps } => Metadata {
311
311
ft : FileType {
@@ -437,6 +437,8 @@ mod tests {
437
437
. unwrap ( )
438
438
. map ( |r| r. unwrap ( ) )
439
439
. collect ( ) ;
440
+
441
+ let modified = get_modified ( None ) ;
440
442
let expected = vec ! [
441
443
DirEntry {
442
444
path: "/lib/.DS_Store" . into( ) ,
@@ -447,7 +449,7 @@ mod tests {
447
449
} ,
448
450
accessed: 0 ,
449
451
created: 0 ,
450
- modified: 0 ,
452
+ modified,
451
453
len: 6148 ,
452
454
} ) ,
453
455
} ,
@@ -460,7 +462,7 @@ mod tests {
460
462
} ,
461
463
accessed: 0 ,
462
464
created: 0 ,
463
- modified: 0 ,
465
+ modified,
464
466
len: 0 ,
465
467
} ) ,
466
468
} ,
@@ -473,7 +475,7 @@ mod tests {
473
475
} ,
474
476
accessed: 0 ,
475
477
created: 0 ,
476
- modified: 0 ,
478
+ modified,
477
479
len: 4694941 ,
478
480
} ) ,
479
481
} ,
@@ -486,7 +488,7 @@ mod tests {
486
488
} ,
487
489
accessed: 0 ,
488
490
created: 0 ,
489
- modified: 0 ,
491
+ modified,
490
492
len: 0 ,
491
493
} ) ,
492
494
} ,
@@ -502,14 +504,15 @@ mod tests {
502
504
503
505
let fs = WebcVolumeFileSystem :: new ( volume) ;
504
506
507
+ let modified = get_modified ( None ) ;
505
508
let python_wasm = crate :: Metadata {
506
509
ft : crate :: FileType {
507
510
file : true ,
508
511
..Default :: default ( )
509
512
} ,
510
513
accessed : 0 ,
511
514
created : 0 ,
512
- modified : 0 ,
515
+ modified,
513
516
len : 4694941 ,
514
517
} ;
515
518
assert_eq ! (
@@ -535,7 +538,7 @@ mod tests {
535
538
} ,
536
539
accessed: 0 ,
537
540
created: 0 ,
538
- modified: 0 ,
541
+ modified,
539
542
len: 0 ,
540
543
} ,
541
544
) ;
0 commit comments