4747 html_logo_url = "https://raw.githubusercontent.com/remotefs-rs/remotefs-rs/main/assets/logo.png"
4848) ]
4949
50+ #[ macro_use]
51+ extern crate log;
52+
5053mod inode;
5154#[ cfg( test) ]
5255mod test;
@@ -164,23 +167,27 @@ impl MemoryFs {
164167
165168impl RemoteFs for MemoryFs {
166169 fn connect ( & mut self ) -> RemoteResult < Welcome > {
170+ debug ! ( "connect()" ) ;
167171 self . connected = true ;
168172 Ok ( Welcome :: default ( ) )
169173 }
170174
171175 fn disconnect ( & mut self ) -> RemoteResult < ( ) > {
176+ debug ! ( "disconnect()" ) ;
172177 self . connected = false ;
173178 Ok ( ( ) )
174179 }
175180
176181 fn is_connected ( & mut self ) -> bool {
182+ debug ! ( "is_connected() -> {}" , self . connected) ;
177183 self . connected
178184 }
179185
180186 fn pwd ( & mut self ) -> RemoteResult < PathBuf > {
181187 if !self . connected {
182188 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
183189 }
190+ debug ! ( "pwd() -> {:?}" , self . wrkdir) ;
184191
185192 Ok ( self . wrkdir . clone ( ) )
186193 }
@@ -192,6 +199,8 @@ impl RemoteFs for MemoryFs {
192199
193200 let dir = self . absolutize ( dir) ;
194201
202+ debug ! ( "change_dir({:?})" , dir) ;
203+
195204 // check if the directory exists
196205 let inode = self
197206 . tree
@@ -219,6 +228,7 @@ impl RemoteFs for MemoryFs {
219228 }
220229
221230 let path = self . absolutize ( path) ;
231+ debug ! ( "list_dir({:?})" , path) ;
222232
223233 // query node
224234 let node = self
@@ -232,6 +242,7 @@ impl RemoteFs for MemoryFs {
232242 for child in node. children ( ) {
233243 let path = child. id ( ) . clone ( ) ;
234244 let metadata = child. value ( ) . metadata ( ) . clone ( ) ;
245+ debug ! ( "list_dir() -> {path:?}, {metadata:?}" ) ;
235246
236247 files. push ( File { path, metadata } )
237248 }
@@ -245,6 +256,7 @@ impl RemoteFs for MemoryFs {
245256 }
246257
247258 let path = self . absolutize ( path) ;
259+ debug ! ( "stat({:?})" , path) ;
248260
249261 let node = self
250262 . tree
@@ -255,6 +267,8 @@ impl RemoteFs for MemoryFs {
255267 let path = node. id ( ) . clone ( ) ;
256268 let metadata = node. value ( ) . metadata ( ) . clone ( ) ;
257269
270+ debug ! ( "stat({path:?}) -> {metadata:?}" ) ;
271+
258272 Ok ( File { path, metadata } )
259273 }
260274
@@ -264,6 +278,7 @@ impl RemoteFs for MemoryFs {
264278 }
265279
266280 let path = self . absolutize ( path) ;
281+ debug ! ( "setstat({:?}, {:?})" , path, metadata) ;
267282
268283 let node = self
269284 . tree
@@ -285,6 +300,7 @@ impl RemoteFs for MemoryFs {
285300 }
286301
287302 let path = self . absolutize ( path) ;
303+ debug ! ( "exists({:?})" , path) ;
288304
289305 Ok ( self . tree . root ( ) . query ( & path) . is_some ( ) )
290306 }
@@ -295,6 +311,7 @@ impl RemoteFs for MemoryFs {
295311 }
296312
297313 let path = self . absolutize ( path) ;
314+ debug ! ( "remove_file({:?})" , path) ;
298315
299316 // get node
300317 let node = self
@@ -320,6 +337,7 @@ impl RemoteFs for MemoryFs {
320337 }
321338
322339 let path = self . absolutize ( path) ;
340+ debug ! ( "remove_dir({:?})" , path) ;
323341
324342 // get node
325343 let node = self
@@ -328,12 +346,18 @@ impl RemoteFs for MemoryFs {
328346 . query_mut ( & path)
329347 . ok_or_else ( || RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ?;
330348 // check if is a leaf and is a directory
331- if !node. is_leaf ( ) || node. value ( ) . metadata ( ) . file_type != FileType :: Directory {
349+ if !node. is_leaf ( ) {
350+ debug ! ( "Directory {path:?} is not empty" ) ;
351+ return Err ( RemoteError :: new ( RemoteErrorType :: DirectoryNotEmpty ) ) ;
352+ }
353+ if node. value ( ) . metadata ( ) . file_type != FileType :: Directory {
354+ debug ! ( "{path:?} is not a directory" ) ;
332355 return Err ( RemoteError :: new ( RemoteErrorType :: CouldNotRemoveFile ) ) ;
333356 }
334357
335358 let parent = self . tree . root_mut ( ) . parent_mut ( & path) . unwrap ( ) ;
336359 parent. remove_child ( & path) ;
360+ debug ! ( "removed {:?}" , path) ;
337361
338362 Ok ( ( ) )
339363 }
@@ -344,6 +368,7 @@ impl RemoteFs for MemoryFs {
344368 }
345369
346370 let path = self . absolutize ( path) ;
371+ debug ! ( "remove_dir_all({:?})" , path) ;
347372
348373 let parent = self
349374 . tree
@@ -355,6 +380,7 @@ impl RemoteFs for MemoryFs {
355380 return Err ( RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ;
356381 }
357382 parent. remove_child ( & path) ;
383+ debug ! ( "removed {:?}" , path) ;
358384
359385 Ok ( ( ) )
360386 }
@@ -365,6 +391,7 @@ impl RemoteFs for MemoryFs {
365391 }
366392
367393 let path = self . absolutize ( path) ;
394+ debug ! ( "create_dir({:?})" , path) ;
368395 let parent = path
369396 . parent ( )
370397 . unwrap_or_else ( || Path :: new ( "/" ) )
@@ -380,11 +407,13 @@ impl RemoteFs for MemoryFs {
380407
381408 // check if the directory already exists
382409 if parent. children ( ) . iter ( ) . any ( |child| * child. id ( ) == path) {
410+ debug ! ( "Directory {path:?} already exists" ) ;
383411 return Err ( RemoteError :: new ( RemoteErrorType :: DirectoryAlreadyExists ) ) ;
384412 }
385413
386414 // add the directory
387415 parent. add_child ( Node :: new ( path. clone ( ) , dir) ) ;
416+ debug ! ( "created directory {path:?}" ) ;
388417
389418 Ok ( ( ) )
390419 }
@@ -395,8 +424,10 @@ impl RemoteFs for MemoryFs {
395424 }
396425 let path = self . absolutize ( path) ;
397426 let target = self . absolutize ( target) ;
427+ debug ! ( "symlink({:?}, {:?})" , path, target) ;
398428 // check if `target` exists
399429 if self . tree . root ( ) . query ( & target) . is_none ( ) {
430+ debug ! ( "target {target:?} does not exist" ) ;
400431 return Err ( RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ;
401432 }
402433
@@ -420,11 +451,13 @@ impl RemoteFs for MemoryFs {
420451
421452 // check if the file already exists
422453 if parent. children ( ) . iter ( ) . any ( |child| * child. id ( ) == path) {
454+ debug ! ( "symbolic link {path:?} already exists" ) ;
423455 return Err ( RemoteError :: new ( RemoteErrorType :: FileCreateDenied ) ) ;
424456 }
425457
426458 // add the directory
427459 parent. add_child ( Node :: new ( path. clone ( ) , symlink) ) ;
460+ debug ! ( "symlink {path:?} -> {target:?}" ) ;
428461
429462 Ok ( ( ) )
430463 }
@@ -434,8 +467,9 @@ impl RemoteFs for MemoryFs {
434467 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
435468 }
436469 let src = self . absolutize ( src) ;
437-
438470 let dest = self . absolutize ( dest) ;
471+ debug ! ( "copy({:?}, {:?})" , src, dest) ;
472+
439473 let dest_parent = dest
440474 . parent ( )
441475 . unwrap_or_else ( || Path :: new ( "/" ) )
@@ -455,6 +489,7 @@ impl RemoteFs for MemoryFs {
455489 . query_mut ( & dest_parent)
456490 . ok_or_else ( || RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ?;
457491
492+ debug ! ( "copied {src:?} to {dest:?}" ) ;
458493 dest_parent. add_child ( Node :: new ( dest, dest_inode) ) ;
459494
460495 Ok ( ( ) )
@@ -465,8 +500,9 @@ impl RemoteFs for MemoryFs {
465500 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
466501 }
467502 let src = self . absolutize ( src) ;
468-
469503 let dest = self . absolutize ( dest) ;
504+ debug ! ( "mov({:?}, {:?})" , src, dest) ;
505+
470506 let dest_parent = dest
471507 . parent ( )
472508 . unwrap_or_else ( || Path :: new ( "/" ) )
@@ -486,7 +522,7 @@ impl RemoteFs for MemoryFs {
486522 . query_mut ( & dest_parent)
487523 . ok_or_else ( || RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ?;
488524
489- dest_parent. add_child ( Node :: new ( dest, dest_inode) ) ;
525+ dest_parent. add_child ( Node :: new ( dest. clone ( ) , dest_inode) ) ;
490526
491527 // remove src
492528 let src_parent = self
@@ -496,6 +532,7 @@ impl RemoteFs for MemoryFs {
496532 . ok_or_else ( || RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ?;
497533
498534 src_parent. remove_child ( & src) ;
535+ debug ! ( "moved {src:?} to {dest:?}" ) ;
499536
500537 Ok ( ( ) )
501538 }
@@ -509,6 +546,7 @@ impl RemoteFs for MemoryFs {
509546 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
510547 }
511548 let path = self . absolutize ( path) ;
549+ debug ! ( "append({:?},{:?})" , path, metadata) ;
512550 let parent = path
513551 . parent ( )
514552 . unwrap_or_else ( || Path :: new ( "/" ) )
@@ -537,6 +575,7 @@ impl RemoteFs for MemoryFs {
537575 parent. add_child ( Node :: new ( path. clone ( ) , file) ) ;
538576
539577 // make stream
578+ debug ! ( "file {path:?} opened for append" ) ;
540579 let handle = WriteHandle {
541580 path,
542581 data : Cursor :: new ( content. unwrap_or_default ( ) ) ,
@@ -553,6 +592,7 @@ impl RemoteFs for MemoryFs {
553592 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
554593 }
555594 let path = self . absolutize ( path) ;
595+ debug ! ( "create({:?},{:?})" , path, metadata) ;
556596 let parent = path
557597 . parent ( )
558598 . unwrap_or_else ( || Path :: new ( "/" ) )
@@ -573,6 +613,7 @@ impl RemoteFs for MemoryFs {
573613
574614 // add new file
575615 parent. add_child ( Node :: new ( path. clone ( ) , file) ) ;
616+ debug ! ( "{:?} created" , path) ;
576617
577618 // make stream
578619 let handle = WriteHandle {
@@ -591,13 +632,16 @@ impl RemoteFs for MemoryFs {
591632 return Err ( RemoteError :: new ( RemoteErrorType :: NotConnected ) ) ;
592633 }
593634 let path = self . absolutize ( path) ;
635+ debug ! ( "open({:?})" , path) ;
594636
595637 let node = self
596638 . tree
597639 . root ( )
598640 . query ( & path)
599641 . ok_or_else ( || RemoteError :: new ( RemoteErrorType :: NoSuchFileOrDirectory ) ) ?;
600642
643+ debug ! ( "{:?} opened" , path) ;
644+
601645 let stream = Cursor :: new ( node. value ( ) . content . as_ref ( ) . cloned ( ) . unwrap_or_default ( ) ) ;
602646 let stream = Box :: new ( stream) as Box < dyn Read + Send > ;
603647
@@ -606,6 +650,7 @@ impl RemoteFs for MemoryFs {
606650
607651 fn on_written ( & mut self , writable : WriteStream ) -> RemoteResult < ( ) > {
608652 let handle = Self :: downcast_write_handle ( writable) ;
653+ debug ! ( "on_written({:?}, {:?})" , handle. path, handle. mode) ;
609654
610655 // get node
611656 let node = self
@@ -632,6 +677,7 @@ impl RemoteFs for MemoryFs {
632677 }
633678 WriteMode :: Create => handle. data . get_ref ( ) . len ( ) as u64 ,
634679 } ;
680+ debug ! ( "{:?} written {:?}" , handle. path, value) ;
635681 node. set_value ( value) ;
636682
637683 Ok ( ( ) )
0 commit comments