IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 7, 2007, 4:27:42 PM (19 years ago)
Author:
jhoblitt
Message:

make filesystem xattr support optional & disabled by default

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r13278 r13302  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.40 2007-05-05 02:59:31 jhoblitt Exp $
     3# $Id: Server.pm,v 1.41 2007-05-08 02:27:42 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    1313
    1414use DBI;
     15use File::ExtAttr qw( setfattr );
    1516use Log::Log4perl;
    1617use Nebulous::Server::Config;
     
    124125    my $self = shift;
    125126
    126     my ($key, $volume) = validate_pos(@_,
     127    my ($key, $vol_name) = validate_pos(@_,
    127128        {
    128129            type        => SCALAR,
     
    143144
    144145    $log->debug( "entered - @_" );
    145 
    146     # Find the storage volume to use.  We are doing this first as there's no
    147     # point in creating a new storage object if we can't allocate a new
    148     # instance. ->_get_storage_volume() throws an exception on failure.
    149     my ($vol_id, $vol_path);
    150     eval {
    151         ($vol_id, $vol_path) = $self->_get_storage_volume($volume);
    152     };
    153     if ($@) {
    154         $log->logdie($@);
    155     }
    156146
    157147    my $ins_id;
     
    206196    # actual on disk file name we can't try to create the file until after
    207197    # we've create both a new storage_storage object and instance.
    208     my $filename = $key;
    209     # mange '/'s into ':'
    210     $filename =~ s|/|:|g;
    211     my $uri = URI::file->new("$vol_path/$ins_id.$filename");
    212     $log->debug("generated uri $uri");
    213198
    214199    # TODO add some stuff here to retry if unsucessful
    215     eval {
    216         _create_empty_file($uri->file, $key);
     200    my ($uri, $vol_id);
     201    eval {
     202        ($uri, $vol_id) = $self->_create_empty_instance_file($key, $ins_id, $vol_name);
    217203    };
    218204    if ($@) {
     
    220206        $log->logdie($@);
    221207    }
     208
     209    $log->debug("created $uri on volume ID: $vol_id");
    222210
    223211    eval {
     
    225213            my $query = $db->prepare_cached( $sql->update_instance_uri );
    226214            # vol_id, uri, ins_id
    227             $query->execute($vol_id, $uri, $ins_id);
     215            $query->execute($vol_id, "$uri", $ins_id);
    228216        }
    229217
    230218        $db->commit;
     219        $log->debug("commit");
    231220    };
    232221    if ($@) {
     
    315304
    316305    $log->debug("entered - @_");
    317 
    318     my ($vol_id, $vol_path);
    319     eval {
    320         ($vol_id, $vol_path) = $self->_get_storage_volume($vol_name);
    321     };
    322     if ($@) {
    323         $log->logdie($@);
    324     }
    325306
    326307    my $ins_id;
     
    361342    }
    362343
    363     my $filename = $key;
    364     # mange '/'s into ':'
    365     $filename =~ s|/|:|g;
    366     my $uri = URI::file->new("$vol_path/$ins_id.$filename");
    367     $log->debug("generated uri $uri");
     344    # Unfortunately, since we want to use the instance row's ID as part of the
     345    # actual on disk file name we can't try to create the file until after
     346    # we've create both a new storage_storage object and instance.
    368347
    369348    # TODO add some stuff here to retry if unsucessful
    370     eval {
    371         _create_empty_file($uri->file, $key);
     349    my ($uri, $vol_id);
     350    eval {
     351        ($uri, $vol_id) = $self->_create_empty_instance_file($key, $ins_id, $vol_name);
    372352    };
    373353    if ($@) {
     
    11071087    my $name = shift;
    11081088
    1109     my ($vol_id, $vol_path);
     1089    my ($vol_id, $vol_path, $xattr);
    11101090    eval {
    11111091        # TODO cache this?
     
    11371117
    11381118        my $free;
    1139         ($vol_id, $vol_path, $free) = $query->fetchrow_array;
     1119        ($vol_id, $vol_path, $xattr, $free) = $query->fetchrow_array;
    11401120        $query->finish;
    11411121    };
     
    11501130    $log->debug( "leaving" );
    11511131
    1152     return ($vol_id, $vol_path);
     1132    return ($vol_id, $vol_path, $xattr);
    11531133}
    11541134
     
    12101190}
    12111191
     1192sub _create_empty_instance_file
     1193{
     1194    my $self = shift;
     1195
     1196    my ($key, $ins_id, $volume) = @_;
     1197
     1198    my $log = $self->log;
     1199    my $sql = $self->sql;
     1200    my $db  =$self->db;
     1201
     1202    # Find the storage volume to use.
     1203    # ->_get_storage_volume() throws an exception on failure.
     1204    my ($vol_id, $vol_path, $xattr);
     1205    my ($storage_filename);
     1206    my ($uri);
     1207    eval {
     1208        ($vol_id, $vol_path, $xattr) = $self->_get_storage_volume($volume);
     1209        $storage_filename = $self->_generate_storage_filename($key, $ins_id);
     1210        # XXX put a hook for directory hashing here
     1211        $uri = URI::file->new("$vol_path/$storage_filename");
     1212        $log->debug("generated uri $uri");
     1213        $self->_create_empty_file($uri->file);
     1214    };
     1215    if ($@) {
     1216        $log->logdie($@);
     1217    }
     1218
     1219    if ($xattr) {
     1220        my $path = $uri->file;
     1221        die "can not set xattr on $path: $!"
     1222            unless (setfattr($path, 'user.nebulous_key', $key));
     1223    }
     1224
     1225    return ($uri, $vol_id);
     1226}
     1227
     1228
     1229sub _create_empty_file
     1230{
     1231    my $self = shift;
     1232
     1233    my ($path) = @_;
     1234
     1235    # perl's open() can't do an O_CREAT | O_EXCL
     1236    die "file $path already exists" if (-e $path);
     1237
     1238    my $fh;
     1239    die "can not open $path: $!"
     1240        unless (open($fh, '>', $path));
     1241
     1242    die "can not close $path: $!"
     1243        unless (close($fh));
     1244
     1245    die "can not chmod $path: $!"
     1246        unless (chmod 0664, $path);
     1247
     1248    return $path;
     1249}
     1250
     1251
     1252sub _generate_storage_filename
     1253{
     1254    my $self = shift;
     1255
     1256    my ($key, $ins_id) = @_;
     1257
     1258    my $filename = $key;
     1259    # mangle '/'s into ':'
     1260    $filename =~ s|/|:|g;
     1261
     1262    return "$ins_id.$filename"
     1263}
     1264
    12121265
    12131266sub DESTROY
Note: See TracChangeset for help on using the changeset viewer.