Patch to detect unused files

Han Holl (han.holl@pobox.com)
Wed, 6 Jan 1999 11:53:20 +0100 (MET)


Hello,

We've got a couple of ancient directory trees (going back to the early eighties)
that have accumulated a lot of garbage over the years.
But nobody is sure which files are actually _used_ and which can be safely
deleted.

The following patch lets me do the following: all filenames are prefixed with
.~ , and after an unsuccesful lookup a file will be automagically restored if
it's actually used. In this way we can see which files have been used and
which haven't.

It seems to work fine, but nevertheless I'd appreciate a word of advice or
warning if someone spots a glaring error. Especially the fact that I have to
add 2 to dir->i_count to be able to call ext2_rename() is kind of scary.
And I'm not quite sure if (and how) I must remove the shadow name from the
cache if it isn't found.

Han Holl

(Patch against 2.0.36)
--- namei.c.orig Mon Jan 4 14:16:24 1999
+++ namei.c Mon Jan 4 17:01:49 1999
@@ -151,6 +151,30 @@
return NULL;
}

+static struct buffer_head * ext2_find_shadow_entry (struct inode * dir,
+ const char * const name, int namelen,
+ struct ext2_dir_entry ** res_dir)
+{
+ char shadowname[EXT2_NAME_LEN];
+ struct buffer_head * bh;
+
+ if (namelen > EXT2_NAME_LEN - 2) {
+ return NULL;
+ }
+ if (!(bh = ext2_find_entry (dir, ".cruftremoval", 13, NULL))) {
+ return NULL;
+ }
+ brelse(bh);
+ strcpy(shadowname, ".~");
+ strncpy(shadowname + 2, name, namelen);
+ dir->i_count += 2;
+ if (ext2_rename(dir, shadowname, namelen + 2, dir, name, namelen, 0) == 0) {
+ return ext2_find_entry (dir, name, namelen , res_dir);
+ }
+ return NULL;
+
+}
+
int ext2_lookup (struct inode * dir, const char * name, int len,
struct inode ** result)
{
@@ -185,8 +209,10 @@
if (!(bh = ext2_find_entry (dir, name, len, &de))) {
if (ino == dir->i_version)
dcache_add(dir, name, len, 0);
- iput (dir);
- return -ENOENT;
+ if (!(bh = ext2_find_shadow_entry (dir, name, len, &de))) {
+ iput (dir);
+ return -ENOENT;
+ }
}
ino = de->inode;
dcache_add(dir, name, len, ino);

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/