303
305
:param retries: Number of retries after temporary failures so far
304
306
for this operation.
306
TODO: jam 20051215 ftp as a protocol seems to support chmod, but ftplib does not
308
TODO: jam 20051215 ftp as a protocol seems to support chmod, but
308
311
abspath = self._abspath(relpath)
309
312
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
315
318
f = self._get_FTP()
317
320
f.storbinary('STOR '+tmp_abspath, fp)
318
f.rename(tmp_abspath, abspath)
321
self._rename_and_overwrite(tmp_abspath, abspath, f)
319
322
except (ftplib.error_temp,EOFError), e:
320
323
warning("Failure during ftp PUT. Deleting temporary file.")
434
437
# to give it its own address as the 'to' location.
435
438
# So implement a fancier 'copy()'
440
def rename(self, rel_from, rel_to):
441
abs_from = self._abspath(rel_from)
442
abs_to = self._abspath(rel_to)
443
mutter("FTP rename: %s => %s", abs_from, abs_to)
445
return self._rename(abs_from, abs_to, f)
447
def _rename(self, abs_from, abs_to, f):
449
f.rename(abs_from, abs_to)
450
except ftplib.error_perm, e:
451
self._translate_perm_error(e, abs_from,
452
': unable to rename to %r' % (abs_to))
437
454
def move(self, rel_from, rel_to):
438
455
"""Move the item at rel_from to the location at rel_to"""
439
456
abs_from = self._abspath(rel_from)
442
459
mutter("FTP mv: %s => %s", abs_from, abs_to)
443
460
f = self._get_FTP()
444
f.rename(abs_from, abs_to)
461
self._rename_and_overwrite(abs_from, abs_to, f)
445
462
except ftplib.error_perm, e:
446
463
self._translate_perm_error(e, abs_from,
447
464
extra='unable to rename to %r' % (rel_to,),
448
465
unknown_exc=errors.PathError)
467
def _rename_and_overwrite(self, abs_from, abs_to, f):
468
"""Do a fancy rename on the remote server.
470
Using the implementation provided by osutils.
472
osutils.fancy_rename(abs_from, abs_to,
473
rename_func=lambda p1, p2: self._rename(p1, p2, f),
474
unlink_func=lambda p: self._delete(p, f))
452
476
def delete(self, relpath):
453
477
"""Delete the item at relpath"""
454
478
abspath = self._abspath(relpath)
480
self._delete(abspath, f)
482
def _delete(self, abspath, f):
456
484
mutter("FTP rm: %s", abspath)
458
485
f.delete(abspath)
459
486
except ftplib.error_perm, e:
460
487
self._translate_perm_error(e, abspath, 'error deleting',
666
693
pfrom = self.filesystem.translate(self._renaming)
667
694
self._renaming = None
668
695
pto = self.filesystem.translate(line[1])
696
if os.path.exists(pto):
697
self.respond('550 RNTO failed: file exists')
670
700
os.rename(pfrom, pto)
671
701
except (IOError, OSError), e:
672
702
# TODO: jam 20060516 return custom responses based on
673
703
# why the command failed
674
self.respond('550 RNTO failed: %s' % (e,))
704
# (bialix 20070418) str(e) on Python 2.5 @ Windows
705
# sometimes don't provide expected error message;
706
# so we obtain such message via os.strerror()
707
self.respond('550 RNTO failed: %s' % os.strerror(e.errno))
676
709
self.respond('550 RNTO failed')
677
710
# For a test server, we will go ahead and just die
709
742
self.filesystem.mkdir (path)
710
743
self.respond ('257 MKD command successful.')
711
744
except (IOError, OSError), e:
712
self.respond ('550 error creating directory: %s' % (e,))
745
# (bialix 20070418) str(e) on Python 2.5 @ Windows
746
# sometimes don't provide expected error message;
747
# so we obtain such message via os.strerror()
748
self.respond ('550 error creating directory: %s' %
749
os.strerror(e.errno))
714
751
self.respond ('550 error creating directory.')