Handle hostnames with upper-case letters
[webmin.git] / sgiexports / sgiexports-lib.pl
1 # sgiexports-lib.pl
2 # Functions for reading and editing the SGI NFS exports file
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7
8 # get_exports()
9 # Parses the exports file into a list of structures, one per export
10 sub get_exports
11 {
12 local $lnum = 0;
13 local @rv;
14 open(EXPORTS, $config{'exports_file'});
15 while(<EXPORTS>) {
16         s/\r|\n//g;
17         s/#.*$//g;
18         local $slnum = $lnum;
19         while(/\\$/) {
20                 local $nl = <EXPORTS>;
21                 s/\\$//;
22                 $nl =~ s/^\s+//;
23                 $_ .= $nl;
24                 $lnum++;
25                 }
26         local @w = split(/\s+/, $_);
27         if (@w) {
28                 local $exp = { 'dir' => shift(@w),
29                                'opts' => { },
30                                'line' => $slnum,
31                                'eline' => $lnum,
32                                'index' => scalar(@rv) };
33                 if ($w[0] =~ /^-/) {
34                         # Has some options
35                         local $opts = shift(@w);
36                         $opts =~ s/^\-//;
37                         local $o;
38                         foreach $o (split(/,/, $opts)) {
39                                 if ($o =~ /^([^=]+)=(\S*)$/) {
40                                         $exp->{'opts'}->{$1} = $2;
41                                         }
42                                 else {
43                                         $exp->{'opts'}->{$o} = "";
44                                         }
45                                 }
46                         }
47                 $exp->{'hosts'} = \@w;
48                 push(@rv, $exp);
49                 }
50         $lnum++;
51         }
52 close(EXPORTS);
53 return @rv;
54 }
55
56 # create_export(&export)
57 sub create_export
58 {
59 open(EXPORTS, ">>$config{'exports_file'}");
60 print EXPORTS &export_line($_[0]),"\n";
61 close(EXPORTS);
62 }
63
64 # modify_export(&export)
65 sub modify_export
66 {
67 local $lref = &read_file_lines($config{'exports_file'});
68 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
69        &export_line($_[0]));
70 &flush_file_lines();
71 }
72
73 # delete_export(&export)
74 sub delete_export
75 {
76 local $lref = &read_file_lines($config{'exports_file'});
77 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
78 &flush_file_lines();
79 }
80
81 sub export_line
82 {
83 local @w = ( $_[0]->{'dir'} );
84 local @o;
85 foreach $o (keys %{$_[0]->{'opts'}}) {
86         if ($_[0]->{'opts'}->{$o} eq "") {
87                 push(@o, $o);
88                 }
89         else {
90                 push(@o, $o."=".$_[0]->{'opts'}->{$o});
91                 }
92         }
93 push(@w, "-".join(",", @o)) if (@o);
94 push(@w, @{$_[0]->{'hosts'}});
95 return join(" ", @w);
96 }
97
98 1;
99