Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Yet another distfiles cleanup - based on last access time
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
whit
Tux's lil' helper
Tux's lil' helper


Joined: 26 Oct 2002
Posts: 121
Location: VT

PostPosted: Wed Mar 17, 2004 7:24 pm    Post subject: Yet another distfiles cleanup - based on last access time Reply with quote

There are several other scripts here which clear out stale distfiles. Some of them look for files with multiple versions and remove all but the latest, some of them compare the files to those currently in "world" and delete those that are not. This one removes files that haven't been accessed for more than a defined number of days - 180 unless you change that variable. Note this depends on your filesystem actually tracking "atime" - most do but some have an option to turn that off for performance. The script requires that you have PHP installed (not just mod_php).

The reasoning here is that with slots you might be maintaining several versions of the same program, so eliminating all but the latest is not ideal. On the other hand after more than a few months the odds are good that revised installs based on the same old distribution files have stopped, and so the value of keeping them around approaches nil - they may still be in "world" at this point but most likely any new ebuild will require a new distfile anyway, so holding on to the old one becomes of very little worth.

Code:

#! /usr/bin/php

<?php

// Removes portage distribution files not accessed on system for more than a
// defined number of days, set with the $fileage variable.

$fileage=180;

$distdir="/usr/portage/distfiles/";

function do_bytes($size) {
  $kb = 1024;        // Kilobyte
  $mb = 1024 * $kb;  // Megabyte
  $gb = 1024 * $mb;  // Gigabyte
  $tb = 1024 * $gb;  // Terabyte
  if($size==0) return "0 bytes";
  if($size < $kb) {
   return $size." bytes";
  } else if($size < $mb) {
   return round($size/$kb,2)." kb";
  } else if($size < $gb) {
   return round($size/$mb,2)." mb";
  } else if($size < $tb) {
   return round($size/$gb,2)." gb";
  } else {
   return round($size/$tb,2)." tb";
  }
}
 
$days=($fileage*86400);
$now=time();
$cut=($now-$days);

$beginspace=diskfreespace($distdir);

$handle=opendir("$distdir");
while ($file = readdir($handle)) {
  $file2 = $distdir . $file;
  if ((is_file($file2)) && (!ereg($_SERVER['PHP_SELF'],$file2))) {
    echo "$file - " . date("F d Y H:i:s.", fileatime($file2));
    if (fileatime($file2) < $cut ) {
      echo " [DELETED]";
      unlink ($file2); 
    }
    echo "\n";
  }

$endspace=diskfreespace($distdir);
$freedspace=($endspace-$beginspace);
echo "\n" . do_bytes($freedspace) . " freed, " . do_bytes($endspace) . " now available\n";

?>
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks 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