Subversion Repositories DevTools

Rev

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