Handle hostnames with upper-case letters
[webmin.git] / hpuxexports / hpuxexports-lib.pl
1 # hpuxexports-lib.pl
2 # Common functions for managing exports files
3
4 do '../web-lib.pl';
5 &init_config();
6 do '../ui-lib.pl';
7 use Socket;
8
9 # list_exports()
10 # Return a list of all the directories currently being exported
11 sub list_exports
12 {
13 local(@rv);
14 open(EXP, $config{exports_file});
15 while(<EXP>) {
16         chop; s/#.*//g;
17         if (!/\S/) { next; }
18         /(\/\S*)\s*/; push(@rv, $1);
19         }
20 close(EXP);
21 return @rv;
22 }
23
24
25 # get_exports(directory)
26 # Return an array containing the following for some directory
27 #  directory, options
28 sub get_exports
29 {
30 local(@rv);
31 open(EXP, $config{exports_file});
32 while(<EXP>) {
33         chop; s/#.*//g;
34         if (!/\S/) { next; }
35         if (/(\/\S*)\s+-(.*)/ && $1 eq $_[0]) {
36                 # found matching exports with options
37                 $rv[0] = $1;
38                 $rv[1] = $2;
39                 }
40         elsif (/(\/\S*)\s+-(.*)/ && $1 eq $_[0]) {
41                 # found matching exports with options
42                 $rv[0] = $1;
43                 $rv[1] = $2;
44                 }
45         }
46 close(EXP);
47 return @rv;
48 }
49
50
51 # create_export(directory, options)
52 # Add a new exports to the exports file
53 sub create_export
54 {
55 &open_tempfile(EXP, ">> $config{exports_file}");
56 &print_tempfile(EXP, "$_[0] ");
57 if ($_[1]) { &print_tempfile(EXP, "-$_[1]\n"); };
58 &close_tempfile(EXP);
59 }
60
61
62 # modify_export(old_directory, directory, options)
63 # Modify an existing exports
64 sub modify_export
65 {
66 local(@exp);
67 open(EXP, $config{exports_file});
68 @exp = <EXP>;
69 close(EXP);
70 &open_tempfile(EXP, "> $config{exports_file}");
71 foreach (@exp) {
72         chop; ($line = $_) =~ s/#.*//g;
73         if ($line =~ /(\/\S+)\s*/ && $1 eq $_[0]) {
74                 # found exports to change..
75                 /\s*(\S+)/;
76                 &print_tempfile(EXP, "$_[1] ");
77                 if ($_[2]) { &print_tempfile(EXP, "-$_[2]\n"); };
78                 }
79         else {
80                 # leave this line alone
81                 &print_tempfile(EXP, "$_\n");
82                 }
83         }
84 &close_tempfile(EXP);
85 }
86
87
88 # delete_export(directory)
89 # Delete the export for a particular directory
90 sub delete_export
91 {
92 local(@exp);
93 open(EXP, $config{exports_file});
94 @exp = <EXP>;
95 close(EXP);
96 &open_tempfile(EXP, "> $config{exports_file}");
97 foreach (@exp) {
98         chop; ($line = $_) =~ s/#.*//g;
99         if ($line !~ /(\/\S+)\s*/ || $1 ne $_[0]) {
100                 # Leave this line alone
101                 &print_tempfile(EXP, "$_\n");
102                 }
103         }
104 &close_tempfile(EXP);
105 }
106
107
108 # parse_options(string)
109 # Parse a mount options string like rw=foo,nosuid,... into the associative
110 # array %options. Parts with no value are given an empty string as the value
111 sub parse_options
112 {
113 local($opt);
114 undef(%options);
115 foreach $opt (split(/,/, $_[0])) {
116         if ($opt =~ /^([^=]+)=(.*)$/) {
117                 $options{$1} = $2;
118                 }
119         else {
120                 $options{$opt} = "";
121                 }
122         }
123 }
124
125 # join_options()
126 # Returns a list of options from the %options array, in the form used in
127 # the exports file
128 sub join_options
129 {
130 local(@list, $k);
131 foreach $k (keys %options) {
132         if ($options{$k} eq "") {
133                 push(@list, $k);
134                 }
135         else {
136                 push(@list, "$k=$options{$k}");
137                 }
138         }
139 return join(',', @list);
140 }
141
142 1;
143