~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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
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
    hashcache,
25
    tests,
26
    )
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
27
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
28
29
30
class TestReadonly(TestCaseWithWorkingTree):
31
32
    def setUp(self):
33
        if not self.platform_supports_readonly_dirs():
34
            raise tests.TestSkipped('platform does not support readonly'
35
                                    ' directories.')
36
        super(TestReadonly, self).setUp()
37
38
    def platform_supports_readonly_dirs(self):
39
        if sys.platform in ('win32', 'cygwin'):
40
            # Setting a directory to readonly in windows or cygwin doesn't seem
41
            # to have any effect. You can still create files in subdirectories.
42
            # TODO: jam 20061219 We could cheat and set just the hashcache file
43
            #       to readonly, which would make it fail when we try to delete
44
            #       or rewrite it. But that is a lot of cheating...
45
            return False
46
        return True
47
48
    def _set_all_dirs(self, basedir, readonly=True):
49
        """Recursively set all directories beneath this one."""
50
        if readonly:
51
            mode = 0555
52
        else:
53
            mode = 0755
54
55
        for root, dirs, files in os.walk(basedir, topdown=False):
56
            for d in dirs:
57
                path = os.path.join(root, d)
58
                os.chmod(path, mode)
59
60
    def set_dirs_readonly(self, basedir):
61
        """Set all directories readonly, and have it cleanup on test exit."""
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
62
        self.addCleanup(self._set_all_dirs, basedir, readonly=False)
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
63
        self._set_all_dirs(basedir, readonly=True)
64
65
    def create_basic_tree(self):
66
        tree = self.make_branch_and_tree('tree')
67
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
68
        tree.add(['a', 'b', 'b/c'])
69
        tree.commit('creating an initial tree.')
70
        return tree
71
72
    def _custom_cutoff_time(self):
73
        """We need to fake the cutoff time."""
74
        return time.time() + 10
75
76
    def test_readonly_unclean(self):
77
        """Even if the tree is unclean, we should still handle readonly dirs."""
78
        # First create a tree
79
        tree = self.create_basic_tree()
80
81
        # XXX: *Ugly* *ugly* hack, we need the hashcache to think it is out of
82
        # date, but we don't want to actually wait 3 seconds doing nothing.
2201.1.2 by John Arbash Meinel
Update from Aaron
83
        # WorkingTree formats that don't have a _hashcache should update this
84
        # test so that they pass. For now, we just assert that we have the
85
        # right type of objects available.
86
        the_hashcache = getattr(tree, '_hashcache', None)
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
87
        if the_hashcache is not None:
88
            self.assertIsInstance(the_hashcache, hashcache.HashCache)
89
            the_hashcache._cutoff_time = self._custom_cutoff_time
90
            hack_dirstate = False
91
        else:
92
            # DirState trees don't have a HashCache, but they do have the same
93
            # function as part of the DirState. However, until the tree is
94
            # locked, we don't have a DirState to modify
95
            hack_dirstate = True
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
96
97
        # Make it a little dirty
98
        self.build_tree_contents([('tree/a', 'new contents of a\n')])
99
100
        # Make it readonly, and do some operations and then unlock
101
        self.set_dirs_readonly('tree')
102
103
        tree.lock_read()
104
        try:
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
105
            if hack_dirstate:
106
                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.
107
            # Make sure we check all the files
5837.2.2 by Jelmer Vernooij
Fix more uses of Tree.__iter__
108
            for file_id in tree.all_file_ids():
2201.1.1 by John Arbash Meinel
Fix bug #76299 by ignoring write errors during readonly hashcache write.
109
                size = tree.get_file_size(file_id)
110
                sha1 = tree.get_file_sha1(file_id)
111
        finally:
112
            tree.unlock()