Changeset 13302 for trunk/Nebulous-Server/lib/Nebulous/Server.pm
- Timestamp:
- May 7, 2007, 4:27:42 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/Nebulous-Server/lib/Nebulous/Server.pm (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Nebulous-Server/lib/Nebulous/Server.pm
r13278 r13302 1 1 # Copyright (c) 2004 Joshua Hoblitt 2 2 # 3 # $Id: Server.pm,v 1.4 0 2007-05-05 02:59:31jhoblitt Exp $3 # $Id: Server.pm,v 1.41 2007-05-08 02:27:42 jhoblitt Exp $ 4 4 5 5 package Nebulous::Server; … … 13 13 14 14 use DBI; 15 use File::ExtAttr qw( setfattr ); 15 16 use Log::Log4perl; 16 17 use Nebulous::Server::Config; … … 124 125 my $self = shift; 125 126 126 my ($key, $vol ume) = validate_pos(@_,127 my ($key, $vol_name) = validate_pos(@_, 127 128 { 128 129 type => SCALAR, … … 143 144 144 145 $log->debug( "entered - @_" ); 145 146 # Find the storage volume to use. We are doing this first as there's no147 # point in creating a new storage object if we can't allocate a new148 # 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 }156 146 157 147 my $ins_id; … … 206 196 # actual on disk file name we can't try to create the file until after 207 197 # 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");213 198 214 199 # 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); 217 203 }; 218 204 if ($@) { … … 220 206 $log->logdie($@); 221 207 } 208 209 $log->debug("created $uri on volume ID: $vol_id"); 222 210 223 211 eval { … … 225 213 my $query = $db->prepare_cached( $sql->update_instance_uri ); 226 214 # vol_id, uri, ins_id 227 $query->execute($vol_id, $uri, $ins_id);215 $query->execute($vol_id, "$uri", $ins_id); 228 216 } 229 217 230 218 $db->commit; 219 $log->debug("commit"); 231 220 }; 232 221 if ($@) { … … 315 304 316 305 $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 }325 306 326 307 my $ins_id; … … 361 342 } 362 343 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. 368 347 369 348 # 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); 372 352 }; 373 353 if ($@) { … … 1107 1087 my $name = shift; 1108 1088 1109 my ($vol_id, $vol_path );1089 my ($vol_id, $vol_path, $xattr); 1110 1090 eval { 1111 1091 # TODO cache this? … … 1137 1117 1138 1118 my $free; 1139 ($vol_id, $vol_path, $ free) = $query->fetchrow_array;1119 ($vol_id, $vol_path, $xattr, $free) = $query->fetchrow_array; 1140 1120 $query->finish; 1141 1121 }; … … 1150 1130 $log->debug( "leaving" ); 1151 1131 1152 return ($vol_id, $vol_path );1132 return ($vol_id, $vol_path, $xattr); 1153 1133 } 1154 1134 … … 1210 1190 } 1211 1191 1192 sub _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 1229 sub _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 1252 sub _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 1212 1265 1213 1266 sub DESTROY
Note:
See TracChangeset
for help on using the changeset viewer.
