Thursday, September 15, 2005

Trashy Experiment

In a story I wrote some years ago, I blue-skied a computer system owned by a nomadic TV advertising guru. He roamed the roads in a custom RV, producing TV commercials, completely computer generated, and uploading them via a satellite link to his customers. Well the story was never published, but one detail of this wish-list computer system has stuck with me for a long time. The operating system kept the the trash away from the user.

Fast forward to this year, when I upgraded by laptop to a Powerbook 12" with a 100 GB hard drive -- a considerable increase from the 30 GB iBook I'd been using before. For the first time in memory ... for the first time ever, I had enough disk space.

My work is text documents. Yes I use photos and have tinkered with video, but until I have a bit more free time, I'm not generating more than a few hundred kilobytes per day. I found myself staring at that trash icon at the end of the dock and remembering the trash system I really wanted.

Like a trained rat, I've been conditioned to 'Empty Trash' the instant my eye catches the trash icon with stuff in it. Running out of disk space is no light matter, especially on a system that grows the swap space as needed. But here I was, deleting potentially useful backup items well before I really needed to.

So, I wrote a script that runs under cron to check how much free space exists on my system, and if it ever gets too tight ( I have it set at 2GB ) then it will walk the trash files and delete the oldest ones until free space is back under control. I'd never have to manually 'Empty Trash' again.

So I thought. However, there is the other situation when you need to clear out the trash. When I plugged in an external hard drive, like my iPod, I often had to clear up space on it. However if I used 'Empty Trash' it would delete old trash files everywhere. To fix that situation I wrote a script that deleted trash only on externally mounted drives, used Platypus to wrap a Application icon around it and put that on my dock.

It took six weeks or so to de-condition myself. I no longer notice the trash icon at all, and if I need to empty the external drive, I can just click on the other trash icon up in the application section of the dock.

It's worked very well. Currently I have half a year worth of old trash (1.54 GB) waiting if I need it. There's no penalty in my day by day operations. I'm much more likely to actually throw marginal things away, like old downloads, now that I more comfortable I'll have days, week, months, (maybe years) to change my mind. I've recovered a couple of things, much less than I would have thought. But should a Serious Fumblefinger happen, it's just one more safeguard.

Now I know that this philosophy is in direct contradiction with the current concerns about privacy. I have a few items I don't want public -- a few experimental stories that should never see light of day even if I die famous, for example. Those are created and kept on an encrypted disk image, with a pass phrase that exists only in my head. The very idea of creating a sensitive item in the clear and then counting on the trash system to get rid of it later is unreliable. People get hit by lightning, and at most inconvenient times, too.

So I intend to run my trash experiment until I find a flaw, or until I find something better. A couple of scripts is all it took.

Scripts follow. Errors are likely and I don't recommend anyone else duplicate my experiment unless they have a deathwish. I'm including them here just to show that it's not magic. Scripts are placed in the public domain.

autotrash runs under cron once an hour.

hmpbg4:~ hmelton$ cat /Users/hmelton/bin/autotrash
#!/usr/bin/perl -w
use strict;

my $quota=2e6; # Keep 2gig free
# Check for free space on the volume containing the user's home directory
my $available=&df();
while($available < $quota){ exit if ($available == -1); # df error if (open(LS,"ls -at $ENV{'HOME'}/.Trash|tail -1|")){ my $trashfile=;
close LS;
chomp($trashfile);
my $command="rm -rf $ENV{'HOME'}/.Trash/$trashfile";
# print $command."\n";
system($command);
}
$available=&df();
}
exit;

sub df{
if (open(DF,"df -k $ENV{'HOME'}|")){
my $info=;
$info=;
my @parts=split(/\s+/,$info);
close DF;
return($parts[3]);
}
return(-1);
}

emptyext is the dock application for emptying the external drive's trash. Platypus wraps an application around it and I've added the option to display the text the script generates so I can see the external files being deleted.

hmpbg4:/Applications/emptyext.app/Contents/Resources hmelton$ cat script
#!/bin/bash
find /Volumes/*/.Trashes/501/* -print -exec rm -rfd {} \;
sleep 3

1 comment:

Chris Nystrom said...

Cool idea. I might try your scripts if I get the time to play around with them.