Changeset 13459 for trunk/PS-IPP-Config/lib/PS/IPP/Config.pm
- Timestamp:
- May 21, 2007, 6:22:00 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/PS-IPP-Config/lib/PS/IPP/Config.pm (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/PS-IPP-Config/lib/PS/IPP/Config.pm
r13317 r13459 1 1 # Copyright (c) 2006 Paul Price, Joshua Hoblitt 2 2 # 3 # $Id: Config.pm,v 1.4 5 2007-05-10 02:35:36price Exp $3 # $Id: Config.pm,v 1.46 2007-05-22 04:22:00 price Exp $ 4 4 5 5 package PS::IPP::Config; … … 15 15 use PS::IPP::Metadata::Config 1.00; 16 16 use Getopt::Long 2.35 qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV 17 use URI 1.35;18 17 19 18 use base qw( Class::Accessor::Fast Exporter ); … … 253 252 my $name = shift; # File name to check 254 253 255 my $uri = URI->new( $name );# URI parser 256 my $scheme = $uri->scheme();# Scheme for file name 254 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 257 255 258 256 return $name unless defined $scheme; # Probably a file name instead of a URI 259 257 260 258 if ($scheme eq 'neb') { 261 $name = _strip_scheme( $name );259 $name =~ s|^(\S+):/*||; 262 260 $self->_neb_start(); 263 261 my $neb = $self->{nebulous}; # Nebulous handle … … 279 277 $self->file_prepare( $name ); 280 278 281 my $uri = URI->new( $name );# URI parser 282 my $scheme = $uri->scheme();# Scheme for file name 279 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 283 280 if (defined $scheme) { 284 281 if ($scheme eq 'neb') { 285 $name = _strip_scheme( $name );282 $name =~ s|^(\S+):/*||; 286 283 $self->_neb_start(); 287 284 return $self->{nebulous}->open_create( $name ); … … 313 310 $self->file_prepare( $name ); 314 311 315 my $uri = URI->new( $name );# URI parser 316 my $scheme = $uri->scheme();# Scheme for file name 312 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 317 313 if (defined $scheme and $scheme eq 'neb') { 318 $name = _strip_scheme( $name );314 $name =~ s|^(\S+):/*||; 319 315 $self->_neb_start(); 320 316 $name = $self->{nebulous}->create( $name ); … … 330 326 my $name = shift; # File name to check 331 327 332 my $uri = URI->new( $name );# URI parser 333 my $scheme = $uri->scheme();# Scheme for file name 328 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 334 329 if (defined $scheme and $scheme eq 'neb') { 335 $name = _strip_scheme( $name );330 $name =~ s|^(\S+):/*||; 336 331 $self->_neb_start(); 337 332 return (defined $self->{nebulous}->find_instances( $name ) ? 1 : 0); … … 350 345 $self->file_prepare( $target ); 351 346 352 my $uri = URI->new( $target );# URI parser 353 my $scheme = $uri->scheme();# Scheme for file name 347 my ($scheme) = $target =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 354 348 if (defined $scheme and $scheme eq 'neb') { 355 $target = _strip_scheme( $target );349 $target =~ s|^(\S+):/*||; 356 350 $self->_neb_start(); 357 351 $target = $self->{nebulous}->create( $target ); … … 361 355 362 356 system "cp $source $target"; 363 }364 365 # Strip the scheme (e.g., "neb://") off a URI name366 sub _strip_scheme367 {368 my $name = shift; # File name of interest369 $name =~ s|^\S+:/*||;370 return $name;371 357 } 372 358 … … 388 374 my $template = shift; # Template filename from which to get working directory if 389 375 390 my $resolved = $name; # Resolved version of file name391 376 if (defined $workdir) { 392 $ resolved= caturi( $workdir, $name );377 $name = caturi( $workdir, $name ); 393 378 } elsif (defined $template) { 394 379 # Take directory from template and apply to name 395 380 my $dir = _strip_filename( $template ); 396 $resolved = caturi( $dir, $name ); 397 } 398 399 my $uri = URI->new( $name ); # URI parser 400 my $scheme = $uri->scheme(); # Scheme for file name 381 $name = caturi( $dir, $name ); 382 } 383 384 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path:// 401 385 return $name if defined $scheme and $scheme eq 'neb'; # Nothing to be done: Nebulous handles it all 402 386 403 387 # Might need to create a directory 404 $resolved = $self->convert_filename_absolute( $resolved);388 my $resolved = $self->convert_filename_absolute( $name ); 405 389 my ( $vol, $dirs, $file ) = File::Spec->splitpath( $resolved ); 406 390 system "mkdir -p $dirs"; … … 421 405 } 422 406 423 my $uri = URI->new($name) or die "Unable to parse URI: $name\n"; # URI 424 my $scheme = $uri->scheme(); # Scheme, e.g., file, path 425 if (lc($scheme) eq 'file') { 426 my $host = $uri->host(); 427 my $path; 428 if (defined $host) { 429 # The user has file://some/directory/ but *probably* wants 430 # /some/directory/ i.e., the URI is technically wrong 431 # (should be one or three leading slashes, not two). We 432 # choose to fix it and give the user what we think they 433 # want, rather than what they asked for. 434 $path = File::Spec->catfile( undef, $host, $uri->path() ); 435 } else { 436 $path = $uri->path(); 437 } 438 439 return File::Spec->canonpath( $path ); 440 } 407 $name =~ s|/$||; 408 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file, path 409 $name =~ s|^\S+:/*||; 410 return $name if lc($scheme) eq 'file'; 441 411 442 412 if (lc($scheme) eq 'path') { 443 my $authority = $uri->authority(); 444 my $path = $self->datapath( $authority ); 445 my @segments = $uri->path_segments(); 446 return File::Spec->catfile( $path, @segments ); 413 my @dirs = split('/', $name); 414 my $pathName = shift @dirs; 415 my $path = $self->datapath( $pathName ); 416 $path =~ s|/*$||; 417 return File::Spec->catfile( $path, @dirs ); 447 418 } 448 419 … … 455 426 my $self = shift; # Configuration object 456 427 my $name = shift; # raw name 457 428 458 429 unless (defined $self and defined $name) { 459 430 carp "Programming error"; 460 431 exit($PS_EXIT_PROG_ERROR); 461 432 } 462 433 463 434 # First, check to see if it's already in a relative form 464 my $scheme = URI->new( $name )->scheme(); 465 if ($scheme eq 'path' or $scheme eq 'file') { 466 # We may as well search for a 'better' path 467 $name = $self->convert_filename_absolute( $name ); 468 } elsif ($scheme eq 'neb') { 469 # No chance of changing anything --- move along 470 return $name; 471 } 472 435 my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file, path 436 if (defined $scheme) { 437 if ($scheme eq 'path' or $scheme eq 'file') { 438 # We may as well search for a 'better' path 439 $name = $self->convert_filename_absolute( $name ); 440 } elsif ($scheme eq 'neb') { 441 # No chance of changing anything --- move along 442 return $name; 443 } 444 } 445 473 446 $name = File::Spec->canonpath( $name); # Clean up 474 my ($vol, $dir, $file) = File::Spec->splitpath( $name ); 475 my @dirs = File::Spec->splitdir( $dir ); 476 pop @dirs; # Get rid of filename 477 447 my @dirs = File::Spec->splitdir( $name ); 448 478 449 my $path_list = metadataLookupMD($self->{_ipprc}, 'DATAPATH'); # List of paths 479 450 my $best_path; … … 483 454 my $path_name = $path_item->{name}; # Name of the path 484 455 my $path = File::Spec->canonpath( $path_item->{value} ); # The path 456 $path =~ s|/*$||; 485 457 my @path_dirs = File::Spec->splitdir( $path ); 486 458 487 459 # Check if the path is suitable 488 460 next if scalar @path_dirs > scalar @dirs; … … 498 470 } 499 471 } 500 my $uri = URI->new(); # URI to return 472 473 $name =~ s|^/||; 474 $name =~ s|/$||; 501 475 if (defined $best_score) { 502 my $rel = File::Spec->abs2rel( $name, $best_path ); 503 my @segments = File::Spec->splitdir( $rel ); 504 $uri->scheme( 'path' ); 505 $uri->authority( $best_name ); 506 $uri->path_segments( @segments ); 507 } else { 508 $uri->scheme( 'file' ); 509 $uri->path( $name ); 510 } 511 512 return $uri->as_string(); 476 $best_path =~ s|^/||; 477 $best_path =~ s|/$||; 478 $name =~ s|^$best_path|$best_name|; 479 return 'path://' . $name; 480 } 481 return 'file://' . $name; 513 482 } 514 483
Note:
See TracChangeset
for help on using the changeset viewer.
