~bzr-pqm/bzr/bzr.dev

2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
1
# Copyright (C) 2006 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Test that WorkingTrees don't fail if they are in a readonly dir."""
18
19
import os
20
import sys
21
import time
22
23
from bzrlib import (
24
    errors,
25
    hashcache,
26
    tests,
27
    )
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
29
30
31
class TestReadonly(TestCaseWithWorkingTree):
32
33
    def setUp(self):
34
        if not self.platform_supports_readonly_dirs():
35
            raise tests.TestSkipped('platform does not support readonly'
36
                                    ' directories.')
37
        super(TestReadonly, self).setUp()
38
39
    def platform_supports_readonly_dirs(self):
40
        if sys.platform in ('win32', 'cygwin'):
41
            # Setting a directory to readonly in windows or cygwin doesn't seem
42
            # to have any effect. You can still create files in subdirectories.
43
            # TODO: jam 20061219 We could cheat and set just the hashcache file
44
            #       to readonly, which would make it fail when we try to delete
45
            #       or rewrite it. But that is a lot of cheating...
46
            return False
47
        return True
48
49
    def _set_all_dirs(self, basedir, readonly=True):
50
        """Recursively set all directories beneath this one."""
51
        if readonly:
52
            mode = 0555
53
        else:
54
            mode = 0755
55
56
        for root, dirs, files in os.walk(basedir, topdown=False):
57
            for d in dirs:
58
                path = os.path.join(root, d)
59
                os.chmod(path, mode)
60
61
    def set_dirs_readonly(self, basedir):
62
        """Set all directories readonly, and have it cleanup on test exit."""
63
        self._set_all_dirs(basedir, readonly=True)
64
65
        def cleanup():
66
            self._set_all_dirs(basedir, readonly=False)
67
68
        self.addCleanup(cleanup)
69
70
    def create_basic_tree(self):
71
        tree = self.make_branch_and_tree('tree')
72
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
73
        tree.add(['a', 'b', 'b/c'])
74
        tree.commit('creating an initial tree.')
75
        return tree
76
77
    def _custom_cutoff_time(self):
78
        """We need to fake the cutoff time."""
79
        return time.time() + 10
80
81
    def test_readonly_unclean(self):
82
        """Even if the tree is unclean, we should still handle readonly dirs."""
83
        # First create a tree
84
        tree = self.create_basic_tree()
85
86
        # XXX: *Ugly* *ugly* hack, we need the hashcache to think it is out of
87
        # date, but we don't want to actually wait 3 seconds doing nothing.
2201.1.2 by John Arbash Meinel
Update from Aaron
88
        # WorkingTree formats that don't have a _hashcache should update this
89
        # test so that they pass. For now, we just assert that we have the
90
        # right type of objects available.
91
        the_hashcache = getattr(tree, '_hashcache', None)
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
92
        if the_hashcache is not None:
93
            self.assertIsInstance(the_hashcache, hashcache.HashCache)
94
            the_hashcache._cutoff_time = self._custom_cutoff_time
95
            hack_dirstate = False
96
        else:
97
            # DirState trees don't have a HashCache, but they do have the same
98
            # function as part of the DirState. However, until the tree is
99
            # locked, we don't have a DirState to modify
100
            hack_dirstate = True
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
101
102
        # Make it a little dirty
103
        self.build_tree_contents([('tree/a', 'new contents of a\n')])
104
105
        # Make it readonly, and do some operations and then unlock
106
        self.set_dirs_readonly('tree')
107
108
        tree.lock_read()
109
        try:
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
110
            if hack_dirstate:
111
                tree._dirstate._sha_cutoff_time = self._custom_cutoff_time
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
112
            # Make sure we check all the files
113
            for file_id in tree:
114
                size = tree.get_file_size(file_id)
115
                sha1 = tree.get_file_sha1(file_id)
116
        finally:
117
            tree.unlock()