Commit ad0675c8 by rfkelly0

cross-directory rename() support for MemoryFS

parent 82223948
...@@ -398,28 +398,25 @@ class MemoryFS(FS): ...@@ -398,28 +398,25 @@ class MemoryFS(FS):
@synchronize @synchronize
def rename(self, src, dst): def rename(self, src, dst):
dst = pathsplit(dst)[-1] src_dir,src_name = pathsplit(src)
src_entry = self._get_dir_entry(src)
dir_entry = self._get_dir_entry(src) if src_entry is None:
if dir_entry is None:
raise ResourceNotFoundError(src) raise ResourceNotFoundError(src)
#if dir_entry.islocked(): open_files = src_entry.open_files[:]
# raise ResourceLockedError(src)
open_files = dir_entry.open_files[:]
for f in open_files: for f in open_files:
f.flush() f.flush()
f.path = dst f.path = dst
dst_dir_entry = self._get_dir_entry(dst) dst_dir,dst_name = pathsplit(dst)
if dst_dir_entry is not None: dst_entry = self._get_dir_entry(dst)
if dst_entry is not None:
raise DestinationExistsError(path) raise DestinationExistsError(path)
pathname, dirname = pathsplit(src) src_dir_entry = self._get_dir_entry(src_dir)
parent_dir = self._get_dir_entry(pathname) dst_dir_entry = self._get_dir_entry(dst_dir)
parent_dir.contents[dst] = parent_dir.contents[dirname] dst_dir_entry.contents[dst_name] = src_dir_entry.contents[src_name]
parent_dir.name = dst dst_dir_entry.contents[dst_name].name = dst_name
del parent_dir.contents[dirname] del src_dir_entry.contents[src_name]
@synchronize @synchronize
......
...@@ -9,8 +9,8 @@ Utilities for interfacing with remote filesystems ...@@ -9,8 +9,8 @@ Utilities for interfacing with remote filesystems
This module provides reusable utility functions that can be used to construct This module provides reusable utility functions that can be used to construct
FS subclasses interfacing with a remote filesystem. These include: FS subclasses interfacing with a remote filesystem. These include:
* RemoteFileBuffer: a file-like object that locally buffers the contents * RemoteFileBuffer: a file-like object that locally buffers the contents of
of a remote file, writing them back on flush() or close(). a remote file, writing them back on flush() or close().
* ConnectionManagerFS: a WrapFS subclass that tracks the connection state * ConnectionManagerFS: a WrapFS subclass that tracks the connection state
of a remote FS, and allows client code to wait for of a remote FS, and allows client code to wait for
......
...@@ -239,11 +239,26 @@ class FSTestCases(object): ...@@ -239,11 +239,26 @@ class FSTestCases(object):
def test_rename(self): def test_rename(self):
check = self.check check = self.check
# test renaming a file in the same directory
self.fs.createfile("foo.txt","Hello, World!") self.fs.createfile("foo.txt","Hello, World!")
self.assert_(check("foo.txt")) self.assert_(check("foo.txt"))
self.fs.rename("foo.txt", "bar.txt") self.fs.rename("foo.txt", "bar.txt")
self.assert_(check("bar.txt")) self.assert_(check("bar.txt"))
self.assert_(not check("foo.txt")) self.assert_(not check("foo.txt"))
# test renaming a directory in the same directory
self.fs.makedir("dir_a")
self.fs.createfile("dir_a/test.txt","testerific")
self.assert_(check("dir_a"))
self.fs.rename("dir_a","dir_b")
self.assert_(check("dir_b"))
self.assert_(check("dir_b/test.txt"))
self.assert_(not check("dir_a/test.txt"))
self.assert_(not check("dir_a"))
# test renaming a file into a different directory
self.fs.makedir("dir_a")
self.fs.rename("dir_b/test.txt","dir_a/test.txt")
self.assert_(not check("dir_b/test.txt"))
self.assert_(check("dir_a/test.txt"))
def test_info(self): def test_info(self):
test_str = "Hello, World!" test_str = "Hello, World!"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment