Subversion Repositories DevTools

Rev

Rev 5272 | Rev 5813 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3410 dpurdie 1
#!/usr/bin/perl -w
2
# nbannon 20050523
3413 dpurdie 3
# chmod 0775 /devl/dpkg_archive/*
4
############################
5
#
6
#   Ensure that package directories in dpkg_archive are:
7
#       chmod 0775      - Not world writable
8
#       chown buildadm  - Owned by buildadm
9
#
10
#   Script should be run as root - so that it can fix file attributes
11
#
3410 dpurdie 12
 
13
use File::stat;
14
 
15
$DIR="/export/devl/dpkg_archive";
16
chdir($DIR) or die "$0: Could not change to \"$DIR\": $!\n";
17
 
18
($name, $passwd, $buildadm, $buildgid) = getpwnam ('buildadm');
19
$name = '';
20
$passwd = '';
21
 
22
die "$0: Could not determine uid of builadadm\n"
5253 dpurdie 23
    unless ($buildadm && $buildgid);
3410 dpurdie 24
 
25
die "$0: Not run as root\n"
26
        if ( $> );
27
 
28
opendir(DPKGARCHIVE, ".") or die "$0: Could not open \"$DIR\": $!\n";
3413 dpurdie 29
while (defined($pkg=readdir(DPKGARCHIVE))) {
30
    next if ( $pkg eq ".." );
31
    next if ( $pkg eq "." );
32
    next if ( $pkg eq "dpkg_archive_list.txt" );
3410 dpurdie 33
 
34
    $sb=lstat($pkg);
35
    $rwx=$sb->mode & 00777;
36
    $uid=$sb->uid;
37
 
5253 dpurdie 38
    if ( -d $pkg && isEmptyDir($pkg) == 1 )
39
    {
40
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
41
               sprintf("%s/%s: is empty", $DIR, $pkg, $rwx));
42
        printf STDERR ("%s: %s/%s: is empty\n", $0, $DIR, $pkg, $rwx);
43
        unless (rmdir($pkg)) {
44
            printf STDERR "%s: Could not delete empty directiry %s/%s: %s\n", $0, $rwx, $DIR, $pkg, $!;
45
        }
5272 dpurdie 46
        next;
5253 dpurdie 47
    }
48
 
3410 dpurdie 49
    # Rather than change everything every day, change it only if it's wrong
50
    if ($rwx != 00775) {
3413 dpurdie 51
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
52
               sprintf("%s/%s: was chmod %04o", $DIR, $pkg, $rwx));
53
        printf STDERR ("%s: %s/%s: was chmod %04o\n", $0, $DIR, $pkg, $rwx);
54
        $rwx=($sb->mode & 07000) | 00775;
55
        unless (chmod($rwx, $pkg) == 1) {
56
            printf STDERR "%s: Could not chmod %04o %s/%s: %s\n", $0, $rwx, $DIR, $pkg, $!;
57
        }
3410 dpurdie 58
    }
59
 
60
    if ($uid != $buildadm ) {
3413 dpurdie 61
        system("/usr/bin/logger", "-p", "local0.notice", "-t", $0,
62
               sprintf("%s/%s: was uid %d", $DIR, $pkg, $uid));
63
        printf STDERR ("%s: %s/%s: was uid %d\n", $0, $DIR, $pkg, $uid);
64
        unless (chown($buildadm, $buildgid, $pkg) == 1) {
65
            printf STDERR "%s: Could not chown buildadm (%d) %s/%s: %s\n", $0,$uid, $DIR, $pkg, $!;
66
        }
3410 dpurdie 67
    }
68
}
69
closedir(DPKGARCHIVE);
70
 
5253 dpurdie 71
#-------------------------------------------------------------------------------
72
# Function        : isEmptyDir 
73
#
5608 dpurdie 74
# Description     : Test for empty directiry
5253 dpurdie 75
#
76
# Inputs          : $dir        - Path to directory
77
#
78
# Returns         : 0           - Contains stuff
79
#                   1           - Does not contain stuff
80
#                  -1           - Does not exist
81
#
82
sub isEmptyDir
83
{
84
    my ($dir) = @_;
85
    my $rv = -1;
86
 
87
    if (opendir (my $dh, $dir ))
88
    {
89
        $rv = 1;
90
        while (readdir $dh)
91
        {
92
            next if ($_ eq '.');
93
            next if ($_ eq '..');
94
            $rv = 0;
95
            last;
96
        }
97
        closedir $dh;
98
    }
99
    return $rv;
100
}
101