darshan-mk-log-dirs.pl.in 2.44 KB
Newer Older
1
#!/usr/bin/perl -w
2 3 4 5 6
#
#  (C) 2009 by Argonne National Laboratory.
#      See COPYRIGHT in top-level directory.
#

7
use File::Basename;
8 9 10
use English;
use Getopt::Long;
use Fcntl ':mode';
11

12 13 14 15
# Creates a hierarchy of subdirectories for darshan log files in the format
# LOGDIR/<year>/<month>/
# The outermost directories will have setgid bits set so that user subdirs
# maintain appropriate group ownership
16 17 18 19 20 21 22 23 24

# use log dir specified at configure time
$LOGDIR = "@__CP_LOG_PATH@";


my $year = (localtime)[5] + 1900;
my $month;
my $i;
my $j;
25 26 27
my $target_group = "";

process_args();
28 29 30

umask(0);

31 32
# go through three years worth of directories
for ($i=$year; $i<($year+3); $i++)
33
{
34 35 36 37 38 39 40 41
    mkdir("$LOGDIR/$i") or die("Error: could not mkdir $LOGDIR/$i.\n");
    chmod ((S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH), "$LOGDIR/$i") or die("Error: could not set permissions on $LOGDIR/$i.\n");
    if(!($target_group eq ""))
    {
        my $target_gid = getgrnam($target_group) || die("Error looking up group $target_group");
        chown($UID, $target_gid, "$LOGDIR/$i") || die ("Error setting group ownership to $target_group on $LOGDIR/$i");
    }

42 43
    for ($j=1; $j<13; $j++)
    {
44 45 46
        mkdir("$LOGDIR/$i/$j") or die("Error: could not mkdir $LOGDIR/$i/$j.\n");
        chmod ((S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH), "$LOGDIR/$i/$j") or die("Error: could not set permissions on $LOGDIR/$i/$j.\n"); 
        if(!($target_group eq ""))
47
        {
48 49
            my $target_gid = getgrnam($target_group) || die("Error looking up group $target_group");
            chown($UID, $target_gid, "$LOGDIR/$i/$j") || die ("Error setting group ownership to $target_group on $LOGDIR/$i/$j");
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
sub process_args
{
    use vars qw( $opt_help $opt_group );

    Getopt::Long::Configure("no_ignore_case", "bundling");
    GetOptions( "help",
        "group=s");

    if($opt_help)
    {
        print_help();
        exit(0);
    }

    if($opt_group)
    {
        $target_group = $opt_group;
    }

    return;
}

sub print_help
{
    print <<EOF;

Usage: $PROGRAM_NAME <options>

    --help          Prints this help message
    --group         Group ownership to assign to leaf directories

Purpose:

    Creates a hierarchy of subdirectories for Darshan log files.  The
    directory layout is LOGDIR/<year>/<month>.  Leaf directories will have 
    setgid bits set, and will be owned by the group specified with --group
    (if provided).
    
EOF
    return;
}
96