[svn r37779] Fix and a test for disappearing files.

--HG--
branch : trunk
This commit is contained in:
fijal 2007-02-02 00:40:49 +01:00
parent 23aeccc8c8
commit cf7e2d7c28
3 changed files with 50 additions and 15 deletions

View File

@ -85,26 +85,33 @@ class RSync(object):
elif command == "send": elif command == "send":
modified_rel_path, checksum = data modified_rel_path, checksum = data
modifiedpath = os.path.join(self.sourcedir, *modified_rel_path) modifiedpath = os.path.join(self.sourcedir, *modified_rel_path)
f = open(modifiedpath, 'rb') try:
data = f.read() f = open(modifiedpath, 'rb')
data = f.read()
except IOError:
data = None
# provide info to progress callback function # provide info to progress callback function
modified_rel_path = "/".join(modified_rel_path) modified_rel_path = "/".join(modified_rel_path)
self.paths[modified_rel_path] = len(data) if data is not None:
self.paths[modified_rel_path] = len(data)
else:
self.paths[modified_rel_path] = 0
if channel not in self.to_send: if channel not in self.to_send:
self.to_send[channel] = [] self.to_send[channel] = []
self.to_send[channel].append(modified_rel_path) self.to_send[channel].append(modified_rel_path)
f.close() if data is not None:
if checksum is not None and checksum == md5.md5(data).digest(): f.close()
data = None # not really modified if checksum is not None and checksum == md5.md5(data).digest():
else: data = None # not really modified
# ! there is a reason for the interning: else:
# sharing multiple copies of the file's data # ! there is a reason for the interning:
data = intern(data) # sharing multiple copies of the file's data
print '%s <= %s' % ( data = intern(data)
channel.gateway._getremoteaddress(), print '%s <= %s' % (
modified_rel_path) channel.gateway._getremoteaddress(),
modified_rel_path)
channel.send(data) channel.send(data)
del data del data
else: else:
@ -118,7 +125,11 @@ class RSync(object):
self.links.append(("link", basename, linkpoint)) self.links.append(("link", basename, linkpoint))
def _send_directory_structure(self, path): def _send_directory_structure(self, path):
st = os.lstat(path) try:
st = os.lstat(path)
except OSError:
self._broadcast((0, 0))
return
if stat.S_ISREG(st.st_mode): if stat.S_ISREG(st.st_mode):
# regular file: send a timestamp/size pair # regular file: send a timestamp/size pair
self._broadcast((st.st_mtime, st.st_size)) self._broadcast((st.st_mtime, st.st_size))

View File

@ -65,7 +65,10 @@ def f():
f = open(path, 'wb') f = open(path, 'wb')
f.write(data) f.write(data)
f.close() f.close()
os.utime(path, (time, time)) try:
os.utime(path, (time, time))
except OSError:
pass
del data del data
channel.send(("links", None)) channel.send(("links", None))

View File

@ -78,3 +78,24 @@ def test_callback():
assert total == {("list", 110):True, ("ack", 100):True, ("ack", 10):True} assert total == {("list", 110):True, ("ack", 100):True, ("ack", 10):True}
def test_file_disappearing():
base = py.test.ensuretemp("file_disappearing")
dest = base.join("dest")
source = base.join("source")
source.ensure("ex").write("a" * 100)
source.ensure("ex2").write("a" * 100)
class DRsync(RSync):
def filter(self, x):
if x.endswith("ex2"):
self.x = 1
source.join("ex2").remove()
return True
rsync = DRsync()
rsync.add_target(gw, dest)
rsync.send(source)
assert rsync.x == 1
assert len(dest.listdir()) == 1
assert len(source.listdir()) == 1