~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockdir.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-22 19:37:57 UTC
  • mto: This revision was merged to the branch mainline in revision 2032.
  • Revision ID: john@arbash-meinel.com-20060922193757-a8341ac119f0d33c
Create a 'read_stanza_unicode' to handle unicode processing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
2
 
 
 
2
#
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
114
114
from bzrlib.trace import mutter
115
115
from bzrlib.transport import Transport
116
116
from bzrlib.osutils import rand_chars
117
 
from bzrlib.rio import RioWriter, read_stanza, Stanza
 
117
from bzrlib.rio import read_stanza, Stanza
118
118
 
119
119
# XXX: At the moment there is no consideration of thread safety on LockDir
120
120
# objects.  This should perhaps be updated - e.g. if two threads try to take a
189
189
            raise UnlockableTransport(self.transport)
190
190
        try:
191
191
            tmpname = '%s/pending.%s.tmp' % (self.path, rand_chars(20))
192
 
            self.transport.mkdir(tmpname)
193
 
            sio = StringIO()
194
 
            self._prepare_info(sio)
195
 
            sio.seek(0)
196
 
            # append will create a new file; we use append rather than put
197
 
            # because we don't want to write to a temporary file and rename
198
 
            # into place, because that's going to happen to the whole
199
 
            # directory
200
 
            self.transport.append(tmpname + self.__INFO_NAME, sio)
 
192
            try:
 
193
                self.transport.mkdir(tmpname)
 
194
            except NoSuchFile:
 
195
                # This may raise a FileExists exception
 
196
                # which is okay, it will be caught later and determined
 
197
                # to be a LockContention.
 
198
                self.create(mode=self._dir_modebits)
 
199
                
 
200
                # After creating the lock directory, try again
 
201
                self.transport.mkdir(tmpname)
 
202
 
 
203
            info_bytes = self._prepare_info()
 
204
            # We use put_file_non_atomic because we just created a new unique
 
205
            # directory so we don't have to worry about files existing there.
 
206
            # We'll rename the whole directory into place to get atomic
 
207
            # properties
 
208
            self.transport.put_bytes_non_atomic(tmpname + self.__INFO_NAME,
 
209
                                                info_bytes)
 
210
 
201
211
            self.transport.rename(tmpname, self._held_dir)
202
212
            self._lock_held = True
203
213
            self.confirm()
325
335
        except NoSuchFile, e:
326
336
            return None
327
337
 
328
 
    def _prepare_info(self, outf):
 
338
    def _prepare_info(self):
329
339
        """Write information about a pending lock to a temporary file.
330
340
        """
331
341
        import socket
337
347
                   nonce=self.nonce,
338
348
                   user=config.user_email(),
339
349
                   )
340
 
        RioWriter(outf).write_stanza(s)
 
350
        return s.to_string()
341
351
 
342
352
    def _parse_info(self, info_file):
343
353
        return read_stanza(info_file.readlines()).as_dict()