Changeset 18451 for trunk/Nebulous-Server/lib/Nebulous/Keys.pm
- Timestamp:
- Jul 9, 2008, 1:32:35 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/Nebulous-Server/lib/Nebulous/Keys.pm (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Nebulous-Server/lib/Nebulous/Keys.pm
r17072 r18451 1 1 # Copyright (c) 2004 Joshua Hoblitt 2 2 # 3 # $Id: Keys.pm,v 1. 1 2008-03-20 21:13:28jhoblitt Exp $3 # $Id: Keys.pm,v 1.2 2008-07-09 23:32:35 jhoblitt Exp $ 4 4 5 5 package Nebulous::Keys; … … 10 10 our $VERSION = '0.01'; 11 11 12 use base qw( Exporter );13 14 use File::Spec ::Functions;12 use base qw( Exporter Class::Accessor::Fast ); 13 14 use File::Spec; 15 15 use URI::file; 16 16 use URI; 17 18 our @EXPORT_OK = qw( parse_neb_key ); 17 use overload '""' => \&_stringify_key; 18 19 our @EXPORT_OK = qw( 20 parse_neb_key 21 parse_neb_volume 22 ); 23 24 __PACKAGE__->mk_ro_accessors(qw( path volume soft_volume )); 19 25 20 26 sub parse_neb_key 21 27 { 22 my $text = shift;23 return unless defined $ text;28 my ($key, $volume) = @_; 29 return unless defined $key; 24 30 25 31 # white space is not allowed 26 if ($ text=~ qr/\s+/) {32 if ($key =~ qr/\s+/) { 27 33 die "keys and URIs may not contain whitespace"; 28 34 } 29 35 30 my $uri = URI->new($ text);36 my $uri = URI->new($key); 31 37 my $scheme = $uri->scheme; 32 38 my $path = $uri->opaque; 33 39 34 my $volume; 40 my $volume_name; 41 my $soft_volume; 35 42 # if this is a valid uri 36 43 if (defined $scheme) { … … 47 54 # strip off volume component if it exists 48 55 $path =~ s|^//([^/]+)||; 49 $volume = $1; 56 57 # ignore key supplied volume name if one is passed as a parameter 58 $volume ||= $1; 59 my $volume_info = parse_neb_volume($volume); 60 61 # magically eat the "any" volume 62 if (defined $volume_info->{volume} and $volume_info->{volume} ne 'any') { 63 $volume_name = $volume_info->{volume}; 64 $soft_volume = $volume_info->{soft_volume}; 65 } 50 66 51 67 # require a leading slash if there is no volume name 52 if ((not defined $volume ) and (not $path =~ m|^/|)) {68 if ((not defined $volume_name) and (not $path =~ m|^/|)) { 53 69 die "neb URI scheme requires a leading slash, eg. neb:/"; 54 70 } 71 } else { 72 my $volume_info = parse_neb_volume($volume); 73 74 # magically eat the "any" volume 75 if (defined $volume_info->{volume} and $volume_info->{volume} ne 'any') { 76 $volume_name = $volume_info->{volume}; 77 $soft_volume = $volume_info->{soft_volume}; 78 } 55 79 } 56 80 … … 59 83 60 84 # remove multiple /'s and trailing slashes 61 $path = canonpath($path); 62 63 return ($volume, $path); 85 $path = File::Spec->canonpath($path); 86 87 return __PACKAGE__->new({ 88 volume => $volume_name, 89 soft_volume => $soft_volume, 90 path => $path, 91 }); 92 } 93 94 sub parse_neb_volume 95 { 96 my $volume = shift; 97 return unless defined $volume; 98 99 my $soft_volume; 100 # check to see if there is a tilde and remove it if found 101 if (defined $volume and $volume =~ s/^~//) { 102 $soft_volume = 1; 103 } 104 105 return({ volume => $volume, soft_volume => $soft_volume }); 106 } 107 108 sub _stringify_key 109 { 110 my $self = shift; 111 112 my $path = $self->path; 113 my $volume = $self->volume || ""; 114 my $soft_volume = $self->soft_volume ? '~' : ""; 115 116 return "neb://${soft_volume}${volume}/$path"; 64 117 } 65 118
Note:
See TracChangeset
for help on using the changeset viewer.
