| 1 | package Unix::AutomountFile; | ||
| 2 | |||
| 3 | # $Id: AutomountFile.pm,v 1.4 2000/05/02 15:50:36 ssnodgra Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||
| 7 | use Unix::ConfigFile; | ||
| 8 | |||
| 9 | require Exporter; | ||
| 10 | |||
| 11 | @ISA = qw(Unix::ConfigFile Exporter); | ||
| 12 | # Items to export into callers namespace by default. Note: do not export | ||
| 13 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 14 | # Do not simply export all your public functions/methods/constants. | ||
| 15 | @EXPORT = qw( | ||
| 16 | |||
| 17 | ); | ||
| 18 | $VERSION = '0.06'; | ||
| 19 | |||
| 20 | # Implementation Notes | ||
| 21 | # | ||
| 22 | # This module adds 2 new fields to the basic ConfigFile object. The fields | ||
| 23 | # are 'mount' and 'options'. Both of these fields are hashes. The mount | ||
| 24 | # field is a hash of lists, where each list contains the possible server | ||
| 25 | # mount points for the key, and the options field contains any options | ||
| 26 | # associated with the key. The options field may not be defined if no | ||
| 27 | # options were present. | ||
| 28 | |||
| 29 | # Preloaded methods go here. | ||
| 30 | |||
| 31 | # Read in the data structures from the supplied file | ||
| 32 | sub read { | ||
| 33 | my ($this, $fh) = @_; | ||
| 34 | |||
| 35 | while (<$fh>) { | ||
| 36 | chop; | ||
| 37 | # Currently we nuke comments and blank lines. This may change. | ||
| 38 | next if /^#/; | ||
| 39 | next if /^$/; | ||
| 40 | my @fields = split; | ||
| 41 | my $key = shift @fields; | ||
| 42 | my $options = undef; | ||
| 43 | if ($fields[0] =~ /^-/) { | ||
| 44 | $options = shift @fields; | ||
| 45 | } | ||
| 46 | $this->automount($key, @fields); | ||
| 47 | $this->options($key, $options); | ||
| 48 | } | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | # Add, modify, or get an automount point | ||
| 54 | sub automount { | ||
| 55 | my $this = shift; | ||
| 56 | my $key = shift; | ||
| 57 | |||
| 58 | # If no more parameters, we return automount info | ||
| 59 | unless (@_) { | ||
| 60 | return undef unless defined $this->{mount}{$key}; | ||
| 61 | return @{$this->{mount}{$key}} unless wantarray; | ||
| 62 | return sort @{$this->{mount}{$key}}; | ||
| 63 | } | ||
| 64 | $this->{mount}{$key} = [ @_ ]; | ||
| 65 | $this->{options}{$key} = undef; | ||
| 66 | return @{$this->{mount}{$key}} unless wantarray; | ||
| 67 | return sort @{$this->{mount}{$key}}; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | # Delete an automount entry | ||
| 72 | sub delete { | ||
| 73 | my ($this, $key) = @_; | ||
| 74 | |||
| 75 | return 0 unless defined $this->{mount}{$key}; | ||
| 76 | delete $this->{mount}{$key}; | ||
| 77 | delete $this->{options}{$key}; | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | # Renames an automount entry | ||
| 83 | sub rename { | ||
| 84 | my ($this, $oldname, $newname) = @_; | ||
| 85 | |||
| 86 | return 0 unless exists $this->{mount}{$oldname}; | ||
| 87 | $this->{mount}{$newname} = $this->{mount}{$oldname}; | ||
| 88 | $this->{options}{$newname} = $this->{options}{$oldname}; | ||
| 89 | $this->delete($oldname); | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | # Add servers to an existing automount entry | ||
| 95 | sub add_server { | ||
| 96 | my $this = shift; | ||
| 97 | my $key = shift; | ||
| 98 | |||
| 99 | return 0 unless defined $this->{mount}{$key}; | ||
| 100 | push @{$this->{mount}{$key}}, @_; | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | # Return the list of automount entries | ||
| 106 | sub automounts { | ||
| 107 | my $this = shift; | ||
| 108 | |||
| 109 | return keys %{$this->{mount}} unless wantarray; | ||
| 110 | return sort keys %{$this->{mount}}; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | # Output file to disk | ||
| 115 | sub write { | ||
| 116 | my ($this, $fh) = @_; | ||
| 117 | |||
| 118 | foreach my $key ($this->automounts) { | ||
| 119 | print $fh "$key\t" or return 0; | ||
| 120 | if (defined $this->options($key)) { | ||
| 121 | print $fh $this->options($key), "\t" or return 0; | ||
| 122 | } | ||
| 123 | print $fh join(" ", $this->automount($key)), "\n" or return 0; | ||
| 124 | } | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | # Set or return mount options | ||
| 130 | sub options { | ||
| 131 | my $this = shift; | ||
| 132 | my $key = shift; | ||
| 133 | return undef unless defined $this->{mount}{$key}; | ||
| 134 | @_ ? $this->{options}{$key} = shift : $this->{options}{$key}; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||
| 138 | |||
| 139 | 1; | ||
| 140 | __END__ |