'Perform the garbage collection of the filestore.'

@api.model
def _file_gc(self):

    if (self._storage() != 'file'):
        return
    cr = self._cr
    cr.commit()
    cr.execute('LOCK    ir_attachment    IN    SHARE    MODE')
    checklist = {}
    for (dirpath, _, filenames) in os.walk(self._full_path('checklist')):
        dirname = os.path.basename(dirpath)
        for filename in filenames:
            fname = ('%s/%s' % (dirname, filename))
            checklist[fname] = os.path.join(dirpath, filename)
    whitelist = set()
    for names in cr.split_for_in_conditions(checklist):
        cr.execute('SELECT    store_fname    FROM    ir_attachment    WHERE    store_fname    IN    %s', [names])
        whitelist.update((row[0] for row in cr.fetchall()))
    removed = 0
    for (fname, filepath) in checklist.iteritems():
        if (fname not in whitelist):
            try:
                os.unlink(self._full_path(fname))
                removed += 1
            except (OSError, IOError):
                _logger.info('_file_gc    could    not    unlink    %s', self._full_path(fname), exc_info=True)
        with tools.ignore(OSError):
            os.unlink(filepath)
    cr.commit()
    _logger.info('filestore    gc    %d    checked,    %d    removed', len(checklist), removed)
