~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ftp.py

  • Committer: Matthieu Moy
  • Date: 2006-01-30 19:01:56 UTC
  • mto: (1185.50.62 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: Matthieu.Moy@imag.fr-20060130190156-4a4fd09d31569532
Make ftp put atomic

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import urllib
32
32
import urlparse
33
33
import stat
 
34
import random
34
35
from warnings import warn
35
36
 
36
37
 
214
215
        """Copy the file-like or string object into the location.
215
216
 
216
217
        :param relpath: Location to put the contents, relative to base.
217
 
        :param f:       File-like or string object.
 
218
        :param fp:       File-like or string object.
218
219
        :param retries: Number of retries after temporary failures so far
219
220
                        for this operation.
220
221
 
221
222
        TODO: jam 20051215 This should be an atomic put, not overwritting files in place
222
223
        TODO: jam 20051215 ftp as a protocol seems to support chmod, but ftplib does not
223
224
        """
 
225
        tmp_abspath = '%s.tmp.%.9f.%d.%d' % (self._abspath(relpath), time.time(),
 
226
                        os.getpid(), random.randint(0,0x7FFFFFFF))
224
227
        if not hasattr(fp, 'read'):
225
228
            fp = StringIO(fp)
226
229
        try:
227
230
            mutter("FTP put: %s" % self._abspath(relpath))
228
231
            f = self._get_FTP()
229
 
            f.storbinary('STOR '+self._abspath(relpath), fp, 8192)
 
232
            try:
 
233
                f.storbinary('STOR '+tmp_abspath, fp)
 
234
                f.rename(tmp_abspath, self._abspath(relpath))
 
235
            except (ftplib.error_temp,EOFError), e:
 
236
                try:
 
237
                    f.delete(tmp_abspath)
 
238
                except:
 
239
                    raise e
 
240
                raise
230
241
        except ftplib.error_perm, e:
231
242
            if "no such file" in str(e).lower():
232
243
                raise NoSuchFile("Error storing %s: %s"