forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs-lib.pl
executable file
·150 lines (138 loc) · 3.32 KB
/
dfs-lib.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# dfs-lib.pl
# Common functions for managing dfstab files
BEGIN { push(@INC, ".."); };
use WebminCore;
&init_config();
%access = &get_module_acl();
$default_type = 'nfs';
if ($config{'fstypes_file'} && open(TYPES, "<".$config{'fstypes_file'})) {
if (<TYPES> =~ /^(\S+)/) {
$default_type = $1;
}
close(TYPES);
}
%access = &get_module_acl();
# list_shares()
# Returns a list of structures containing share details
sub list_shares
{
local $lnum = 0;
local @rv;
open(DFS, "<".$config{'dfstab_file'});
while(<DFS>) {
s/\r|\n//g; s/#.*$//;
if (/^\s*\S*share\s+(.*)/) {
# Found a share line
local $share = { 'line' => $lnum,
'index' => scalar(@rv) };
local $line = $1;
while($line =~ /\\$/) {
$_ = <DFS>;
s/\r|\n//g; s/#.*$//;
$line =~ s/\\$//;
$line .= $_;
$lnum++;
}
$share->{'eline'} = $lnum;
if ($line =~ /\s(\/\S+)/) {
$share->{'dir'} = $1;
}
if ($line =~ /-d\s+"([^"]+)"/) { $share->{'desc'} = $1; }
elsif ($line =~ /-d\s+(\S+)/) { $share->{'desc'} = $1; }
if ($line =~ /-o\s+"([^"]+)"/) { $share->{'opts'} = $1; }
elsif ($line =~ /-o\s+(\S+)/) { $share->{'opts'} = $1; }
if ($line =~ /\s-F\s+(\S+)/) { $share->{'type'} = $1; }
else { $share->{'type'} = $default_type; }
push(@rv, $share);
}
$lnum++;
}
close(DFS);
return @rv;
}
# create_share(&share)
# Add a new share to the dfstab file
sub create_share
{
&open_tempfile(DFS, ">> $config{dfstab_file}");
&print_tempfile(DFS, &share_line($_[0]),"\n");
&close_tempfile(DFS);
}
# modify_share(&share)
# Modify an existing share
sub modify_share
{
local $lref = &read_file_lines($config{'dfstab_file'});
splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
&share_line($_[0]));
&flush_file_lines();
}
# share_line(&share)
sub share_line
{
local $s = "share";
$s .= " -d \"$_[0]->{'desc'}\"" if ($_[0]->{'desc'});
$s .= " -o $_[0]->{'opts'}" if ($_[0]->{'opts'});
$s .= " -F $_[0]->{'type'}" if ($_[0]->{'type'} &&
$_[0]->{'type'} ne $default_type);
$s .= " $_[0]->{'dir'}";
return $s;
}
# delete_share(&share)
# Delete the share for a particular directory
sub delete_share
{
local $lref = &read_file_lines($config{'dfstab_file'});
splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
&flush_file_lines();
}
# parse_options(string)
# Parse a mount options string like rw=foo,nosuid,... into the associative
# array %options. Parts with no value are given an empty string as the value
sub parse_options
{
local($opt);
undef(%options);
foreach $opt (split(/,/, $_[0])) {
if ($opt =~ /^([^=]+)=(.*)$/) {
$options{$1} = $2;
}
else {
$options{$opt} = "";
}
}
return \%options;
}
# join_options([&options])
# Returns a list of options from the %options array, in the form used in
# the dfstab file
sub join_options
{
local $o = $_[0] ? $_[0] : \%options;
local(@list, $k);
foreach $k (keys %$o) {
if ($o->{$k} eq "") {
push(@list, $k);
}
else {
push(@list, "$k=$o->{$k}");
}
}
return join(',', @list);
}
# apply_configuration()
# Apply the NFS configuration, returning undef on success or an error message
# on failure
sub apply_configuration
{
local $temp = &transname();
&system_logged("$config{unshare_all_command} >/dev/null 2>&1");
&system_logged("$config{share_all_command} >/dev/null 2>$temp");
local $why = `/bin/cat $temp`;
unlink($temp);
if ($why =~ /\S+/) {
return $why;
}
return undef;
}
1;