Revision c4399e62 snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py

b/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py
77 77
    def _pad(self, block):
78 78
        return block + ('\x00' * (self.blocksize - len(block)))
79 79

  
80
    def _get_rear_block(self, blkhash, create=0):
80
    def _get_rear_block(self, blkhash, create=False, write=False):
81 81
        filename = hexlify(blkhash)
82
        dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6])
83
        if not exists(dir):
84
            makedirs(dir)
85
        name = join(dir, filename)
86
        return ContextFile(name, create)
82
        path = join(self.blockpath,
83
                    filename[0:2], filename[2:4], filename[4:6],
84
                    filename)
85
        return ContextFile(path, create=create, write=False)
87 86

  
88 87
    def _check_rear_block(self, blkhash):
89 88
        filename = hexlify(blkhash)
90
        dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6])
91
        name = join(dir, filename)
92
        return exists(name)
89
        path = join(self.blockpath,
90
                    filename[0:2], filename[2:4], filename[4:6],
91
                    filename)
92
        return exists(path)
93 93

  
94 94
    def block_hash(self, data):
95 95
        """Hash a block of data"""
......
121 121
            if h == self.emptyhash:
122 122
                append(self._pad(''))
123 123
                continue
124
            with self._get_rear_block(h, 0) as rbl:
124
            with self._get_rear_block(h, create=False, write=False) as rbl:
125 125
                if not rbl:
126 126
                    break
127 127
                for block in rbl.sync_read_chunks(blocksize, 1, 0):
......
140 140
        """
141 141
        block_hash = self.block_hash
142 142
        hashlist = [block_hash(b) for b in blocklist]
143
        mf = None
144
        missing = [i for i, h in enumerate(hashlist) if not self._check_rear_block(h)]
143
        missing = [i for i, h in enumerate(hashlist)
144
                   if not self._check_rear_block(h)]
145 145
        for i in missing:
146
            with self._get_rear_block(hashlist[i], 1) as rbl:
147
                 rbl.sync_write(blocklist[i]) #XXX: verify?
146
            with self._get_rear_block(hashlist[i],
147
                                      create=True, write=False) as rbl:
148
                                      #            ^^^^^^^^^^^
149
                                      # do not overwrite if exists
150
                rbl.sync_write(blocklist[i])  # XXX: verify?
148 151

  
149 152
        return hashlist, missing
150 153

  

Also available in: Unified diff