Handle hostnames with upper-case letters
[webmin.git] / hpuxexports / exports-lib.pl
1 # export-lib.pl
2 # Common functions for exports file
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 do "hpux-lib.pl";
8 %access = &get_module_acl();
9
10 # parse_options($options, \%options)
11 # Parse a mount options string like rw=foo,nosuid,... into a associative
12 # options array. Parts with no value are given an empty string as the value
13 # usefull for HPUX and DFS - perhaps for linux, too
14 sub parse_options
15 {
16 local($opt);
17 foreach $opt (split(/,/, $_[0])) {
18         if ($opt =~ /^([^=]+)=(.*)$/) {
19                 $_[1]->{$1} = $2;
20                 }
21         else {
22                 $_[1]->{$opt} = "";
23                 }
24         }
25 }
26
27
28 # join_options(\%options)
29 # Returns a list of options from a options array, in the form used in
30 # the exports file
31 # usefull for HPUX and DFS - perhaps for linux, too
32 sub join_options
33 {
34 local $o = $_[0];
35 local(@list, $k);
36 foreach $k (keys %$o) {
37         if ($_[0]->{$k} eq "") {
38                 push(@list, $k);
39                 }
40         else {
41                 push(@list, "$k=$_[0]->{$k}");
42                 }
43         }
44 return join(',', @list);
45 }
46
47 # create_export(&export)
48 sub create_export
49 {
50 open(EXP, ">>$config{'exports_file'}");
51 print EXP &make_exports_line($_[0]),"\n";
52 close(EXP);
53 }
54
55
56 # modify_export(&export, &old)
57 sub modify_export
58 {
59 local @exps = &list_exports();
60 local @same = grep { $_->{'line'} eq $_[1]->{'line'} } @exps;
61 local $lref = &read_file_lines($config{'exports_file'});
62 if ($_[0]->{'dir'} eq $_[1]->{'dir'} &&
63     $_[0]->{'active'} == $_[1]->{'active'} || @same == 1) {
64         # directory not changed, or on a line of it's own
65         splice(@same, &indexof($_[1],@same), 1, $_[0]);
66         splice(@$lref, $_[1]->{'line'}, $_[1]->{'eline'}-$_[1]->{'line'}+1,
67                &make_exports_line(@same));
68         }
69 else {
70         # move to a line of it's own
71         splice(@same, &indexof($_[1],@same), 1);
72         splice(@$lref, $_[1]->{'line'}, $_[1]->{'eline'}-$_[1]->{'line'}+1,
73                &make_exports_line(@same));
74         push(@$lref, &make_exports_line($_[0]));
75         }
76 &flush_file_lines();
77 }
78
79
80 # delete_export(&export)
81 # Delete an existing export
82 sub delete_export
83 {
84 local @exps = &list_exports();
85 local @same = grep { $_ ne $_[0] && $_->{'line'} eq $_[0]->{'line'} } @exps;
86 local $lref = &read_file_lines($config{'exports_file'});
87 if (@same) {
88         # other exports on the same line.. cannot totally delete
89         splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'}-$_[0]->{'line'}+1,
90                &make_exports_line(@same));
91         map { $_->{'line'} = $_->{'eline'} = $_[0]->{'line'} } @same;
92         }
93 else {
94         # remove export line
95         splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'}-$_[0]->{'line'}+1);
96         }
97 @list_exports_cache = grep { $_ ne $_[0] } @list_exports_cache;
98 &flush_file_lines();
99 }
100
101
102 # check_hosts(option, hostlist)
103 # Die if any of the listed hosts does not exist
104 # or no hosts listed for specified option
105 sub check_hosts
106 {
107 local @h = split(/\s+/, $_[1]);
108
109 if (!@h) {
110         &error(&text('save_enohost', $_[0]));
111         }
112
113 foreach (@h) {
114         if (!&to_ipaddress($_)) { &error(&text('save_ehost', $_, $_[0])); }
115         }
116 }
117
118 sub has_nfs_commands
119 {
120 return !&has_command("rpc.nfsd") && !&has_command("nfsd") &&
121        !&has_command("rpc.knfsd") ? 0 : 1;
122 }
123
124 1;
125