/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lockdir.py

  • Committer: Andrew Bennetts
  • Date: 2007-02-12 04:49:32 UTC
  • mto: (2018.5.74 hpss)
  • mto: This revision was merged to the branch mainline in revision 2414.
  • Revision ID: andrew.bennetts@canonical.com-20070212044932-k9keo85c0s9gg5wv
``LockableFiles.lock_write()`` now accepts a ``token`` keyword argument, so that
a seperate LockableFiles instance can share a lock if it has the right token.
(Andrew Bennetts, Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
160
160
        self.transport = transport
161
161
        self.path = path
162
162
        self._lock_held = False
 
163
        self._locked_via_token = False
163
164
        self._fake_read_lock = False
164
165
        self._held_dir = path + '/held'
165
166
        self._held_info_path = self._held_dir + self.__INFO_NAME
234
235
            return
235
236
        if not self._lock_held:
236
237
            raise LockNotHeld(self)
237
 
        # rename before deleting, because we can't atomically remove the whole
238
 
        # tree
239
 
        tmpname = '%s/releasing.%s.tmp' % (self.path, rand_chars(20))
240
 
        # gotta own it to unlock
241
 
        self.confirm()
242
 
        self.transport.rename(self._held_dir, tmpname)
243
 
        self._lock_held = False
244
 
        self.transport.delete(tmpname + self.__INFO_NAME)
245
 
        self.transport.rmdir(tmpname)
 
238
        if self._locked_via_token:
 
239
            self._locked_via_token = False
 
240
            self._lock_held = False
 
241
        else:
 
242
            # rename before deleting, because we can't atomically remove the
 
243
            # whole tree
 
244
            tmpname = '%s/releasing.%s.tmp' % (self.path, rand_chars(20))
 
245
            # gotta own it to unlock
 
246
            self.confirm()
 
247
            self.transport.rename(self._held_dir, tmpname)
 
248
            self._lock_held = False
 
249
            self.transport.delete(tmpname + self.__INFO_NAME)
 
250
            self.transport.rmdir(tmpname)
246
251
 
247
252
    def break_lock(self):
248
253
        """Break a lock not held by this instance of LockDir.
415
420
            else:
416
421
                raise LockContention(self)
417
422
 
418
 
    def lock_write(self):
419
 
        """Wait for and acquire the lock."""
420
 
        self.wait_lock()
 
423
    def lock_write(self, token=None):
 
424
        """Wait for and acquire the lock.
 
425
        
 
426
        :param token: if this is already locked, then lock_write will fail
 
427
            unless the token matches the existing lock.
 
428
        :returns: a token if this instance supports tokens, otherwise None.
 
429
        :raises TokenLockingNotSupported: when a token is given but this
 
430
            instance doesn't support using token locks.
 
431
        :raises MismatchedToken: if the specified token doesn't match the token
 
432
            of the existing lock.
 
433
         
 
434
        XXX: docstring duplicated from LockableFiles.lock_write.
 
435
        """
 
436
        if token is not None:
 
437
            self.validate_token(token)
 
438
            self._lock_held = True
 
439
            self._locked_via_token = True
 
440
        else:
 
441
            self.wait_lock()
 
442
            return self.peek().get('nonce')
421
443
 
422
444
    def lock_read(self):
423
445
        """Compatibility-mode shared lock.
457
479
            'locked %s' % (format_delta(delta),),
458
480
            ]
459
481
 
 
482
    def validate_token(self, token):
 
483
        if token is not None:
 
484
            info = self.peek()
 
485
            if info is None:
 
486
                # Lock isn't held
 
487
                lock_token = None
 
488
            else:
 
489
                lock_token = info.get('nonce')
 
490
            if token != lock_token:
 
491
                raise errors.TokenMismatch(token, lock_token)
 
492