Logfile.pm.in 3.88 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2007-2010 University of Utah and the Flux Group.
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
# All rights reserved.
#
package Logfile;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use libdb;
use libtestbed;
use English;
use Data::Dumper;

# Configure variables
my $TB		  = "@prefix@";
my $TBAUDIT       = "@TBAUDITEMAIL@";

#
# Lookup by uuid. For now, just knowing the uuid says you can read the file. 
#
sub Lookup($$)
{
    my ($class, $logid) = @_;
    my $logfile;

    #
    # Argument must be alphanumeric.
    #
    if ($logid =~ /^([\w]*)$/) {
	$logid = $1;
    }
    else {
	return undef;
    }

    my $query_result =
	DBQueryWarn("select * from logfiles where logid='$logid'");

    return undef
	if (!$query_result || !$query_result->numrows);

    my $self           = {};
    $self->{'LOGFILE'} = $query_result->fetchrow_hashref();

    bless($self, $class);
    
    return $self;
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'LOGFILE'}->{$_[1]}); }
sub logid($)            { return field($_[0], "logid"); }
sub filename($)         { return field($_[0], "filename"); }
sub isopen($)           { return field($_[0], "isopen"); }
62
sub gid_idx($)          { return field($_[0], "gid_idx"); }
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
sub date_created($)     { return field($_[0], "date_created"); }

#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    my $logid = $self->logid();
    
    my $query_result =
	DBQueryWarn("select * from logfiles where logid='$logid'");

    return -1
	if (!$query_result || !$query_result->numrows);

    $self->{'LOGFILE'} = $query_result->fetchrow_hashref();

    return 0;
}

#
# Create a new logfile. We are given the optional filename, otherwise
# generate one. 
#
92
sub Create($$;$)
93
{
94
    my ($class, $gid_idx, $filename) = @_;
95 96 97 98 99 100 101 102 103 104 105 106 107 108

    return undef
	if (ref($class));

    $filename = TBMakeLogname("logfile")
	if (!defined($filename));

    # Plain secret key, which is used to reference the file.
    my $logid = TBGenSecretKey();

    if (!DBQueryWarn("insert into logfiles set ".
		     "  logid='$logid', ".
		     "  isopen=0, ".
		     "  filename='$filename', ".
109
		     "  gid_idx='$gid_idx', ".
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		     "  date_created=now()")) {
	return undef;
    }
    return Logfile->Lookup($logid);
}

#
# Delete a logfile record. Optionally delete the logfile too.
#
sub Delete($;$)
{
    my ($self, $delete) = @_;

    return -1
	if (!ref($self));

    $delete = 0
	if (!defined($delete));

    my $logid    = $self->logid();
    my $filename = $self->filename();
    if ($delete) {
	unlink($filename);
    }
    return -1
	if (!DBQueryWarn("delete from logfiles where logid='$logid'"));

    return 0;
}

140 141 142 143 144 145 146
#
# AccessCheck. The user needs to be a member of the group that the logfile
# was created in. 
#
sub AccessCheck($$)
{
    my ($self, $user) = @_;
147
    require Group;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    # Must be a real reference. 
    return 0
	if (! ref($self));

    # Admins do whatever they want.
    return 1
	if ($user->IsAdmin());

    my $group = Group->Lookup($self->gid_idx());
    return 0
	if (!defined($group));

    # Membership in group.
    return 1
	if (defined($group->LookupUser($user)));

    return 0;
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#
# Mark a file open so that the web interface knows to watch it.
#
sub Open($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));

    my $logid = $self->logid();
    DBQueryWarn("update logfiles set isopen=1 where logid='$logid'")
	or return -1;
    
    return $self->Refresh();
}

#
# Mark file closed, which is used to stop the web interface from spewing.
#
sub Close($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));

    my $logid = $self->logid();
    DBQueryWarn("update logfiles set isopen=0 where logid='$logid'")
	or return -1;
    
    return $self->Refresh();
}

# _Always_ make sure that this 1 is at the end of the file...
1;