File: cleantmp.html

package info (click to toggle)
lg-issue18 4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 148
  • sloc: makefile: 36; sh: 4
file content (96 lines) | stat: -rw-r--r-- 1,869 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD></title>Perl Script </title>
</HEAD>
<BODY BGCOLOR="#EEE1CC" TEXT="#000000" LINK="#0000FF" VLINK="#0020F0"
ALINK="#FF0000">

<center>
<H2>Perl Script</H2>
<H4>By Guy Geens,
</center>
</H4>

<p>
<pre>
#! /usr/bin/perl -w
# cleantmp: Remove old files from /tmp partition
# Copyright (C) 1997 by Guy Geens <Guy.Geens@elis.rug.ac.be>
# Snail Mail:
# Zwijnaardsesteenweg 183
# 9000 Gent
# Belgium

use File::Find;

# Security measure: chroot to /tmp
$tmpdir = '/tmp/';
chdir ($tmpdir) || die "$tmpdir not accessible: $!";
if (chroot($tmpdir)) {		# chroot() fails when not run by root
    ($prefix = $tmpdir) =~ s,/+$,,;
    $root = '/';
    $test = 0;
} else {
# Not run by root - test only
    $prefix = '';
    $root = $tmpdir;
    $test = 1;
}

@list = ();

&find(\&do_files, $root);
&find(\&do_dirs, $root);

if (@list) {
    print "Cleaned $tmpdir\n";
    print "Deleted files are:\n";
    for (sort @list) {
	print "$prefix$_\n";
    }
}

exit;

sub do_files {
    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
	(-f _ || -l _ ) &&
	    (int(-A _) > 3) &&
		! /^\.X.*lock$/ &&
		    &removefile ($_) && push @list, $File::Find::name;
}

sub do_dirs {
    /^\..*-unix$/ && ($File::Find::prune = 1) ||
	(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
	    -d _ && ($nlink == 2) &&
		! /^lost\+found$/ &&
		    &removedir ($_) && push @list, "$File::Find::name/";
}

sub removedir {
    if ( $test ) {
	1;
    } else {
# Can't use @_: rmdir doesn't take a list argument
	rmdir $_[0];
    }
}

sub removefile {
    if ( $test ) {
	1;
    } else {
	unlink @_;
    }
}
</pre>

<!--===================================================================-->
<P> <hr> <P> 
<center><H5>Copyright &copy; 1997, Guy Geens<BR> 
Published in Issue 18 of the Linux Gazette, June 1997</H5></center>

</BODY>
</HTML>