<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># dfs-lib.pl
# Common functions for managing dfstab files

BEGIN { push(@INC, ".."); };
use WebminCore;
&amp;init_config();
%access = &amp;get_module_acl();

$default_type = 'nfs';
if ($config{'fstypes_file'} &amp;&amp; open(TYPES, $config{'fstypes_file'})) {
	if (&lt;TYPES&gt; =~ /^(\S+)/) {
		$default_type = $1;
		}
	close(TYPES);
	}
%access = &amp;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(&lt;DFS&gt;) {
	s/\r|\n//g; s/#.*$//;
	if (/^\s*\S*share\s+(.*)/) {
		# Found a share line
		local $share = { 'line' =&gt; $lnum,
				 'index' =&gt; scalar(@rv) };
		local $line = $1;
		while($line =~ /\\$/) {
			$_ = &lt;DFS&gt;;
			s/\r|\n//g; s/#.*$//;
			$line =~ s/\\$//;
			$line .= $_;
			$lnum++;
			}
		$share-&gt;{'eline'} = $lnum;
		if ($line =~ /\s(\/\S+)/) {
			$share-&gt;{'dir'} = $1;
			}
		if ($line =~ /-d\s+"([^"]+)"/) { $share-&gt;{'desc'} = $1; }
		elsif ($line =~ /-d\s+(\S+)/) { $share-&gt;{'desc'} = $1; }
		if ($line =~ /-o\s+"([^"]+)"/) { $share-&gt;{'opts'} = $1; }
		elsif ($line =~ /-o\s+(\S+)/) { $share-&gt;{'opts'} = $1; }
		if ($line =~ /\s-F\s+(\S+)/) { $share-&gt;{'type'} = $1; }
		else { $share-&gt;{'type'} = $default_type; }
		push(@rv, $share);
		}
	$lnum++;
	}
close(DFS);
return @rv;
}

# create_share(&amp;share)
# Add a new share to the dfstab file
sub create_share
{
&amp;open_tempfile(DFS, "&gt;&gt; $config{dfstab_file}");
&amp;print_tempfile(DFS, &amp;share_line($_[0]),"\n");
&amp;close_tempfile(DFS);
}

# modify_share(&amp;share)
# Modify an existing share
sub modify_share
{
local $lref = &amp;read_file_lines($config{'dfstab_file'});
splice(@$lref, $_[0]-&gt;{'line'}, $_[0]-&gt;{'eline'} - $_[0]-&gt;{'line'} + 1,
       &amp;share_line($_[0]));
&amp;flush_file_lines();
}

# share_line(&amp;share)
sub share_line
{
local $s = "share";
$s .= " -d \"$_[0]-&gt;{'desc'}\"" if ($_[0]-&gt;{'desc'});
$s .= " -o $_[0]-&gt;{'opts'}" if ($_[0]-&gt;{'opts'});
$s .= " -F $_[0]-&gt;{'type'}" if ($_[0]-&gt;{'type'} &amp;&amp;
				$_[0]-&gt;{'type'} ne $default_type);
$s .= " $_[0]-&gt;{'dir'}";
return $s;
}

# delete_share(&amp;share)
# Delete the share for a particular directory
sub delete_share
{
local $lref = &amp;read_file_lines($config{'dfstab_file'});
splice(@$lref, $_[0]-&gt;{'line'}, $_[0]-&gt;{'eline'} - $_[0]-&gt;{'line'} + 1);
&amp;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([&amp;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-&gt;{$k} eq "") {
		push(@list, $k);
		}
	else {
		push(@list, "$k=$o-&gt;{$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 = &amp;transname();
&amp;system_logged("$config{unshare_all_command} &gt;/dev/null 2&gt;&amp;1");
&amp;system_logged("$config{share_all_command} &gt;/dev/null 2&gt;$temp");
local $why = `/bin/cat $temp`;
unlink($temp);
if ($why =~ /\S+/) {
	return $why;
	}
return undef;
}

1;

</pre></body></html>