1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
300
305
:param retries: Number of retries after temporary failures so far
301
306
for this operation.
303
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
305
311
abspath = self._abspath(relpath)
306
312
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
312
318
f = self._get_FTP()
314
320
f.storbinary('STOR '+tmp_abspath, fp)
315
f.rename(tmp_abspath, abspath)
321
self._rename_and_overwrite(tmp_abspath, abspath, f)
316
322
except (ftplib.error_temp,EOFError), e:
317
323
warning("Failure during ftp PUT. Deleting temporary file.")
431
437
# to give it its own address as the 'to' location.
432
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))
434
454
def move(self, rel_from, rel_to):
435
455
"""Move the item at rel_from to the location at rel_to"""
436
456
abs_from = self._abspath(rel_from)
439
459
mutter("FTP mv: %s => %s", abs_from, abs_to)
440
460
f = self._get_FTP()
441
f.rename(abs_from, abs_to)
461
self._rename_and_overwrite(abs_from, abs_to, f)
442
462
except ftplib.error_perm, e:
443
463
self._translate_perm_error(e, abs_from,
444
464
extra='unable to rename to %r' % (rel_to,),
445
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))
449
476
def delete(self, relpath):
450
477
"""Delete the item at relpath"""
451
478
abspath = self._abspath(relpath)
480
self._delete(abspath, f)
482
def _delete(self, abspath, f):
453
484
mutter("FTP rm: %s", abspath)
455
485
f.delete(abspath)
456
486
except ftplib.error_perm, e:
457
487
self._translate_perm_error(e, abspath, 'error deleting',
549
579
"""This is used by medusa.ftp_server to log connections, etc."""
550
580
self.logs.append(message)
582
def setUp(self, vfs_server=None):
554
583
if not _have_medusa:
555
584
raise RuntimeError('Must have medusa to run the FtpServer')
586
assert vfs_server is None or isinstance(vfs_server, LocalURLServer), \
587
"FtpServer currently assumes local transport, got %s" % vfs_server
557
589
self._root = os.getcwdu()
558
590
self._ftp_server = _ftp_server(
559
591
authorizer=_test_authorizer(root=self._root),
565
597
self._port = self._ftp_server.getsockname()[1]
566
598
# Don't let it loop forever, or handle an infinite number of requests.
567
599
# In this case it will run for 100s, or 1000 requests
568
self._async_thread = threading.Thread(target=asyncore.loop,
600
self._async_thread = threading.Thread(
601
target=FtpServer._asyncore_loop_ignore_EBADF,
569
602
kwargs={'timeout':0.1, 'count':1000})
570
603
self._async_thread.setDaemon(True)
571
604
self._async_thread.start()
577
610
asyncore.close_all()
578
611
self._async_thread.join()
614
def _asyncore_loop_ignore_EBADF(*args, **kwargs):
615
"""Ignore EBADF during server shutdown.
617
We close the socket to get the server to shutdown, but this causes
618
select.select() to raise EBADF.
621
asyncore.loop(*args, **kwargs)
622
except select.error, e:
623
if e.args[0] != errno.EBADF:
581
627
_ftp_channel = None
582
628
_ftp_server = None
647
693
pfrom = self.filesystem.translate(self._renaming)
648
694
self._renaming = None
649
695
pto = self.filesystem.translate(line[1])
696
if os.path.exists(pto):
697
self.respond('550 RNTO failed: file exists')
651
700
os.rename(pfrom, pto)
652
701
except (IOError, OSError), e:
653
702
# TODO: jam 20060516 return custom responses based on
654
703
# why the command failed
655
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))
657
709
self.respond('550 RNTO failed')
658
710
# For a test server, we will go ahead and just die
690
742
self.filesystem.mkdir (path)
691
743
self.respond ('257 MKD command successful.')
692
744
except (IOError, OSError), e:
693
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))
695
751
self.respond ('550 error creating directory.')