Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Orphan files finder
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
drsound
n00b
n00b


Joined: 20 Sep 2004
Posts: 10
Location: Italy

PostPosted: Mon Oct 10, 2005 4:51 pm    Post subject: Orphan files finder Reply with quote

The following script will find all of the orphan files (i.e. not owned by any installed package) residing in a user specified directory. The script will NOT DELETE any file from your system, it will only produce a list: you can use this list to decide what to manually delete and what to keep. There are reasons for this: for example the /etc/ssh/*key* files look like orphans but it's important to keep them.

Usage example:
# su
# orphan_finder.py /usr > orphans.txt


NB #1: progress messages are printed on stderror, while actual results go on stdoutput.
NB #2: execute the script as root, otherwise it will not be able to read the contents of certain directories.

Code:
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import os
import sys
import glob
import re
import sets

if len(sys.argv) < 2:
    sys.exit('Use: %s DIRECTORY' % os.path.basename(sys.argv[0]))

if not os.path.isdir(sys.argv[1]):
    sys.exit('"%s" is not a valid directory!' % sys.argv[1])

packages = glob.glob('/var/db/pkg/*/*/CONTENTS')
packages.sort()
installedFiles = sets.Set()
objSearcher = re.compile(r'^(?P<filename>.*\S)\s+(?P<hash>\S+)\s+(?P<timestamp>\S+)$')
symSearcher = re.compile(r'^(?P<filename>.*\S) -> ')
for package in packages:
    contentsFile = file(package)
    print >> sys.stderr, "Examining package '%s'..." % package.replace('/var/db/pkg/', '').replace('/CONTENTS', '')
    for line in contentsFile:
        type, remainingData = line.split(None, 1)
        if type == 'obj':
            filename = objSearcher.search(remainingData).group('filename')
        elif type == 'sym':
            filename = symSearcher.search(remainingData).group('filename')
        else:
            filename = remainingData[:-1]
        installedFiles.add(filename)
    contentsFile.close()

for path, dirs, filenames in os.walk(sys.argv[1]):
    dirs.sort()
    filenames.sort()
    print >> sys.stderr, "Examining dir '%s'..." % path
    for dir in dirs:
        dirname = os.path.join(path, dir)
        if not dirname in installedFiles:
            if os.path.islink(dirname):
                print "Orphan link found: %s" % dirname
            else:
                print "Orphan dir found:  %s" % dirname
    for filename in filenames:
        filename = os.path.join(path, filename)
        if not filename in installedFiles:
            if os.path.islink(filename):
                print "Orphan link found: %s" % filename
            else:
                print "Orphan file found: %s" % filename
Back to top
View user's profile Send private message
bonbons
Apprentice
Apprentice


Joined: 04 Sep 2004
Posts: 250

PostPosted: Mon Oct 10, 2005 5:07 pm    Post subject: Reply with quote

I wrote a patch to portage-utils a while ago and posted a reference to it here in the forum. Has not yet been integrated. It allows staying on same partition, mentionning files owned by multiple packages (aka collisions).
Back to top
View user's profile Send private message
Xamindar
Veteran
Veteran


Joined: 03 Oct 2004
Posts: 1155
Location: California

PostPosted: Mon Oct 10, 2005 5:58 pm    Post subject: Reply with quote

Thanks, looks interesting. I'll give it a try when I get home.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum