~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/dirstate.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
232
232
 
233
233
from bzrlib import (
234
234
    cache_utf8,
 
235
    config,
235
236
    debug,
236
237
    errors,
237
238
    inventory,
239
240
    osutils,
240
241
    static_tuple,
241
242
    trace,
 
243
    urlutils,
242
244
    )
243
245
 
244
246
 
448
450
        self._known_hash_changes = set()
449
451
        # How many hash changed entries can we have without saving
450
452
        self._worth_saving_limit = worth_saving_limit
 
453
        self._config_stack = config.LocationStack(urlutils.local_path_to_url(
 
454
            path))
451
455
 
452
456
    def __repr__(self):
453
457
        return "%s(%r)" % \
2508
2512
        #       IN_MEMORY_HASH_MODIFIED, we should only fail quietly if we fail
2509
2513
        #       to save an IN_MEMORY_HASH_MODIFIED, and fail *noisily* if we
2510
2514
        #       fail to save IN_MEMORY_MODIFIED
2511
 
        if self._worth_saving():
2512
 
            grabbed_write_lock = False
2513
 
            if self._lock_state != 'w':
2514
 
                grabbed_write_lock, new_lock = self._lock_token.temporary_write_lock()
2515
 
                # Switch over to the new lock, as the old one may be closed.
 
2515
        if not self._worth_saving():
 
2516
            return
 
2517
 
 
2518
        grabbed_write_lock = False
 
2519
        if self._lock_state != 'w':
 
2520
            grabbed_write_lock, new_lock = self._lock_token.temporary_write_lock()
 
2521
            # Switch over to the new lock, as the old one may be closed.
 
2522
            # TODO: jam 20070315 We should validate the disk file has
 
2523
            #       not changed contents, since temporary_write_lock may
 
2524
            #       not be an atomic operation.
 
2525
            self._lock_token = new_lock
 
2526
            self._state_file = new_lock.f
 
2527
            if not grabbed_write_lock:
 
2528
                # We couldn't grab a write lock, so we switch back to a read one
 
2529
                return
 
2530
        try:
 
2531
            lines = self.get_lines()
 
2532
            self._state_file.seek(0)
 
2533
            self._state_file.writelines(lines)
 
2534
            self._state_file.truncate()
 
2535
            self._state_file.flush()
 
2536
            self._maybe_fdatasync()
 
2537
            self._mark_unmodified()
 
2538
        finally:
 
2539
            if grabbed_write_lock:
 
2540
                self._lock_token = self._lock_token.restore_read_lock()
 
2541
                self._state_file = self._lock_token.f
2516
2542
                # TODO: jam 20070315 We should validate the disk file has
2517
 
                #       not changed contents. Since temporary_write_lock may
2518
 
                #       not be an atomic operation.
2519
 
                self._lock_token = new_lock
2520
 
                self._state_file = new_lock.f
2521
 
                if not grabbed_write_lock:
2522
 
                    # We couldn't grab a write lock, so we switch back to a read one
2523
 
                    return
2524
 
            try:
2525
 
                lines = self.get_lines()
2526
 
                self._state_file.seek(0)
2527
 
                self._state_file.writelines(lines)
2528
 
                self._state_file.truncate()
2529
 
                self._state_file.flush()
2530
 
                self._mark_unmodified()
2531
 
            finally:
2532
 
                if grabbed_write_lock:
2533
 
                    self._lock_token = self._lock_token.restore_read_lock()
2534
 
                    self._state_file = self._lock_token.f
2535
 
                    # TODO: jam 20070315 We should validate the disk file has
2536
 
                    #       not changed contents. Since restore_read_lock may
2537
 
                    #       not be an atomic operation.
 
2543
                #       not changed contents. Since restore_read_lock may
 
2544
                #       not be an atomic operation.                
 
2545
 
 
2546
    def _maybe_fdatasync(self):
 
2547
        """Flush to disk if possible and if not configured off."""
 
2548
        if self._config_stack.get('dirstate.fdatasync'):
 
2549
            osutils.fdatasync(self._state_file.fileno())
2538
2550
 
2539
2551
    def _worth_saving(self):
2540
2552
        """Is it worth saving the dirstate or not?"""