Logo Search packages:      
Sourcecode: s3ql version File versions  Download package

def s3ql::fsck::Fsck::check_cache (   self )
Commit uncommitted cache files

Definition at line 63 of file fsck.py.

                         :
        """Commit uncommitted cache files"""
    
    
        log.info("Checking cached objects...")
        if not os.path.exists(self.cachedir):
            return
        
        for filename in os.listdir(self.cachedir):
            self.found_errors = True
    
            match = re.match('^inode_(\\d+)_block_(\\d+)(\\.d)?$', filename)
            if match:
                inode = int(match.group(1))
                blockno = int(match.group(2))
                dirty = match.group(3) == '.d'
            else:
                raise RuntimeError('Strange file in cache directory: %s' % filename)
            
            if not dirty:
                self.log_error('Removing cached block %d of inode %d', blockno, inode)
                os.unlink(os.path.join(self.cachedir, filename))
                continue
    
            self.log_error("Committing changed block %d of inode %d to backend",
                      blockno, inode)
    
            fh = open(os.path.join(self.cachedir, filename), "rb")
            fh.seek(0, 2)
            size = fh.tell()
            fh.seek(0)
            hash_ = sha256_fh(fh)
    
            try:
                obj_id = self.conn.get_val('SELECT id FROM objects WHERE hash=?', (hash_,))
                
            except NoSuchRowError:
                obj_id = self.conn.rowid('INSERT INTO objects (refcount, hash, size) VALUES(?, ?, ?)',
                                   (1, hash_, size))
                self.bucket.store_fh('s3ql_data_%d' % obj_id, fh)
    
            else:
                # Verify that this object actually exists
                if self.bucket.contains('s3ql_data_%d' % obj_id):
                    self.conn.execute('UPDATE objects SET refcount=refcount+1 WHERE id=?',
                                 (obj_id,))    
                else:
                    # We don't want to delete the vanished object here, because
                    # check_keylist will do a better job and print all affected
                    # inodes. However, we need to reset the hash so that we can
                    # insert the new object.
                    self.conn.execute('UPDATE objects SET hash=NULL WHERE id=?', (obj_id,))
                    obj_id = self.conn.rowid('INSERT INTO objects (refcount, hash, size) VALUES(?, ?, ?)',
                                        (1, hash_, size))
                    self.bucket.store_fh('s3ql_data_%d' % obj_id, fh)
    
            try:
                old_obj_id = self.conn.get_val('SELECT obj_id FROM blocks WHERE inode=? AND blockno=?',
                                         (inode, blockno))
            except NoSuchRowError:
                self.conn.execute('INSERT INTO blocks (obj_id, inode, blockno) VALUES(?,?,?)',
                             (obj_id, inode, blockno))
            else:
                self.conn.execute('UPDATE blocks SET obj_id=? WHERE inode=? AND blockno=?',
                             (obj_id, inode, blockno))
    
                refcount = self.conn.get_val('SELECT refcount FROM objects WHERE id=?',
                                        (old_obj_id,))
                if refcount > 1:
                    self.conn.execute('UPDATE objects SET refcount=refcount-1 WHERE id=?',
                                 (old_obj_id,))
                else:
                    # Don't delete yet, maybe it's still referenced
                    pass
    
    
                fh.close()
            os.unlink(os.path.join(self.cachedir, filename))
            
    

Generated by  Doxygen 1.6.0   Back to index