@@ -14,6 +14,7 @@ use std::{
1414 collections:: BTreeMap ,
1515 fs:: { self , FileType , Permissions } ,
1616 io:: { Seek , SeekFrom } ,
17+ path:: PathBuf ,
1718} ;
1819
1920pub struct SimpleRng {
@@ -69,12 +70,21 @@ pub fn get_seed() -> u64 {
6970}
7071
7172pub ( crate ) fn new_log ( path : & str ) {
72- let file = fs:: OpenOptions :: new ( )
73- . create ( true )
74- . write ( true )
75- . truncate ( true )
76- . open ( path)
77- . unwrap ( ) ;
73+ let p = std:: path:: Path :: new ( path) ;
74+
75+ let file = if p. exists ( ) {
76+ fs:: OpenOptions :: new ( )
77+ . create ( false )
78+ . append ( true )
79+ . open ( path)
80+ . unwrap ( )
81+ } else {
82+ fs:: OpenOptions :: new ( )
83+ . create ( true )
84+ . append ( true )
85+ . open ( path)
86+ . unwrap ( )
87+ } ;
7888
7989 LOG . with ( |log| {
8090 * log. borrow_mut ( ) = Some ( file) ;
@@ -112,6 +122,11 @@ fn init() {
112122 } ) ;
113123}
114124
125+ #[ ic_cdk:: post_upgrade]
126+ fn upgrade ( ) {
127+ init ( ) ;
128+ }
129+
115130use sha2:: { Digest , Sha256 } ;
116131use std:: fs:: File ;
117132use std:: io:: BufReader ;
@@ -265,7 +280,28 @@ pub fn get_log() -> String {
265280fn get_random_file (
266281 parent_path : & std:: path:: Path ,
267282 op_count : u64 ,
283+ opened_files : & mut BTreeMap < String , File > ,
268284) -> anyhow:: Result < std:: path:: PathBuf > {
285+ // sometimes return an opened file
286+ if next_rand ( 2 ) < 1 {
287+ let len = opened_files. len ( ) ;
288+ let file = opened_files. iter ( ) . nth ( next_rand ( len as u64 ) as usize ) ;
289+
290+ if let Some ( f) = file {
291+ let path = PathBuf :: new ( ) . join ( f. 0 ) ;
292+
293+ // just recreate the file, if it doesn't exist
294+ let _res = fs:: OpenOptions :: new ( )
295+ . write ( true )
296+ . append ( false )
297+ . truncate ( false )
298+ . create ( true )
299+ . open ( & path) ;
300+
301+ return Ok ( path) ;
302+ }
303+ }
304+
269305 let mut files: Vec < fs:: DirEntry > = fs:: read_dir ( parent_path) ?
270306 . filter_map ( Result :: ok)
271307 . filter ( |e| e. path ( ) . is_file ( ) && e. file_name ( ) . to_string_lossy ( ) != "log.txt" )
@@ -289,6 +325,9 @@ fn get_random_file(
289325
290326 // no files, create a new one for writing
291327 let path = parent_path. join ( format ! ( "file{op_count}.txt" ) ) ;
328+
329+ File :: create ( & path) . unwrap ( ) ;
330+
292331 Ok ( path)
293332}
294333
@@ -354,6 +393,7 @@ fn generate_random_file_structure(
354393 max_depth : u64 ,
355394
356395 parent_path : & std:: path:: Path ,
396+
357397 opened_files : & mut BTreeMap < String , File > ,
358398) -> anyhow:: Result < u64 > {
359399 let depth = depth + 1 ;
@@ -375,7 +415,7 @@ fn generate_random_file_structure(
375415 // Create a new file
376416 let path = parent_path. join ( format ! ( "file{current_op}.txt" ) ) ;
377417 log ( & format ! ( "Open or create file {path:?}" ) ) ;
378- let mut file = File :: create ( & path) ? ;
418+ let mut file = File :: create ( & path) . unwrap ( ) ;
379419 file. flush ( ) ?;
380420 opened_files. insert ( path. as_path ( ) . to_string_lossy ( ) . to_string ( ) , file) ;
381421 }
@@ -446,7 +486,7 @@ fn generate_random_file_structure(
446486 }
447487 4 => {
448488 // Read text from a random file
449- let file = get_random_file ( parent_path, current_op) ?;
489+ let file = get_random_file ( parent_path, current_op, opened_files ) ?;
450490
451491 if file. exists ( ) {
452492 //
@@ -469,7 +509,7 @@ fn generate_random_file_structure(
469509 }
470510 5 => {
471511 // Truncate file (delete its contents)
472- let file = get_random_file ( parent_path, current_op) ?;
512+ let file = get_random_file ( parent_path, current_op, opened_files ) ?;
473513
474514 log ( & format ! ( "Truncate {file:?}" ) ) ;
475515
@@ -483,7 +523,8 @@ fn generate_random_file_structure(
483523 }
484524 6 => {
485525 // Rename file
486- let from = get_random_file ( parent_path, current_op) ?;
526+ let from = get_random_file ( parent_path, current_op, opened_files) ?;
527+
487528 let to = parent_path. join ( format ! ( "file{current_op}_renamed.txt" ) ) ;
488529 log ( & format ! ( "Rename file from {from:?} to {to:?}" ) ) ;
489530 let res = fs:: rename ( & from, & to) ;
@@ -500,7 +541,8 @@ fn generate_random_file_structure(
500541 }
501542 7 => {
502543 // Copy file
503- let from = get_random_file ( parent_path, current_op) ?;
544+ let from = get_random_file ( parent_path, current_op, opened_files) ?;
545+
504546 let to = parent_path. join ( format ! ( "file{current_op}_copy.txt" ) ) ;
505547 log ( & format ! ( "Copy file from {from:?} to {to:?}" ) ) ;
506548 let res = fs:: copy ( & from, & to) ;
@@ -510,7 +552,7 @@ fn generate_random_file_structure(
510552 }
511553 8 => {
512554 // Delete file
513- let path = get_random_file ( parent_path, current_op) ?;
555+ let path = get_random_file ( parent_path, current_op, opened_files ) ?;
514556
515557 // we do not have dangling file support yet, so do not try to delete an opened file...
516558 if opened_files. contains_key ( & path. as_path ( ) . to_string_lossy ( ) . to_string ( ) ) {
@@ -584,7 +626,8 @@ fn generate_random_file_structure(
584626 . create ( true )
585627 . open ( & save_path) ?;
586628
587- let path = get_random_file ( parent_path, current_op) ?;
629+ let path = get_random_file ( parent_path, current_op, opened_files) ?;
630+
588631 let meta = fs:: metadata ( & path) ?;
589632
590633 save. write_all (
@@ -634,7 +677,7 @@ fn generate_random_file_structure(
634677 }
635678 15 => {
636679 // Move file into subdirectory
637- let from = get_random_file ( parent_path, current_op) ?;
680+ let from = get_random_file ( parent_path, current_op, opened_files ) ?;
638681 let filename = from. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ;
639682
640683 let dir = get_random_dir ( parent_path, current_op) ?;
@@ -647,7 +690,7 @@ fn generate_random_file_structure(
647690 }
648691 16 => {
649692 // write some prepared text into one of the files at a random position
650- let path = get_random_file ( parent_path, current_op) ?;
693+ let path = get_random_file ( parent_path, current_op, opened_files ) ?;
651694
652695 let mut save = fs:: OpenOptions :: new ( )
653696 . write ( true )
0 commit comments