Skip to content

Commit 3f4fb6c

Browse files
committed
Implement MemoryFS.move and MemoryFS.movedir without copy
1 parent 93ea917 commit 3f4fb6c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

fs/memoryfs.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,46 @@ def makedir(
444444
parent_dir.set_entry(dir_name, new_dir)
445445
return self.opendir(path)
446446

447+
def move(self, src_path, dst_path, overwrite=False):
448+
src_dir, src_name = split(self.validatepath(src_path))
449+
dst_dir, dst_name = split(self.validatepath(dst_path))
450+
451+
with self._lock:
452+
src_dir_entry = self._get_dir_entry(src_dir)
453+
if src_dir_entry is None or src_name not in src_dir_entry:
454+
raise errors.ResourceNotFound(src_path)
455+
src_entry = src_dir_entry.get_entry(src_name)
456+
if src_entry.is_dir:
457+
raise errors.FileExpected(src_path)
458+
459+
dst_dir_entry = self._get_dir_entry(dst_dir)
460+
if dst_dir_entry is None:
461+
raise errors.ResourceNotFound(dst_path)
462+
elif not overwrite and dst_name in dst_dir_entry:
463+
raise errors.DestinationExists(dst_path)
464+
465+
dst_dir_entry.set_entry(dst_name, src_entry)
466+
src_dir_entry.remove_entry(src_name)
467+
468+
def movedir(self, src_path, dst_path, create=False):
469+
src_dir, src_name = split(self.validatepath(src_path))
470+
dst_dir, dst_name = split(self.validatepath(dst_path))
471+
472+
with self._lock:
473+
src_dir_entry = self._get_dir_entry(src_dir)
474+
if src_dir_entry is None or src_name not in src_dir_entry:
475+
raise errors.ResourceNotFound(src_path)
476+
src_entry = src_dir_entry.get_entry(src_name)
477+
if not src_entry.is_dir:
478+
raise errors.DirectoryExpected(src_path)
479+
480+
dst_dir_entry = self._get_dir_entry(dst_dir)
481+
if dst_dir_entry is None or (not create and dst_name not in dst_dir_entry):
482+
raise errors.ResourceNotFound(dst_path)
483+
484+
dst_dir_entry.set_entry(dst_name, src_entry)
485+
src_dir_entry.remove_entry(src_name)
486+
447487
def openbin(self, path, mode="r", buffering=-1, **options):
448488
# type: (Text, Text, int, **Any) -> BinaryIO
449489
_mode = Mode(mode)

0 commit comments

Comments
 (0)