Menu="Share:1"
Title="Share Settings"
Tag="share-alt-square"
---
if (!empty($name) && !array_key_exists($name, $shares)) {
echo "";
exit;
}
$width = [123,300];
if ($name == "") {
/* default values when adding new share. */
$share = [
"nameOrig" => "",
"name" => "",
"comment" => "",
"allocator" => "highwater",
"floor" => "",
"splitLevel" => "",
"include" => "",
"exclude" => "",
"useCache" => "",
"cachePool" => "",
"cachePool2" => "",
"cow" => "auto"
];
} else {
/* edit existing share. */
$share = $shares[$name];
}
/* If the configuration is pools only, then no array disks are available. */
$poolsOnly = ((int)$var['SYS_ARRAY_SLOTS'] <= 2) ? true : false;
/* Remove subpools from the array of pools. */
$pools = array_filter($pools, function($pool) {
return !isSubpool($pool);
});
/* Check for cachePool2 only which is a situation where the primary device is the cachePool2 device. */
if ((! $share['cachePool']) && ($share['cachePool2'])) {
$share['cachePool'] = $share['cachePool2'];
$share['cachePool2'] = "";
}
/* Correct a situation in previous Unraid versions where an array only share has a useCache defined. */
if ((!$poolsOnly) && ($share['useCache'] == "no")) {
$share['cachePool'] = "";
}
/* Check for non existent pool device. */
if (($share['cachePool'] && !in_array($share['cachePool'], $pools))) {
$share['useCache'] = $share['cachePool2'] ? "yes" : ($poolsOnly ? "no" : "yes");
$poolDefined = false;
} else {
$share['useCache'] = $share['useCache'] ?: ($poolsOnly ? "only" : "no");
$poolDefined = true;
}
/* Check for pool 2 (or array) being defined. */
$poolDefined2 = true;
if ((($share['useCache'] == "yes") || ($share['useCache'] == "prefer")) && ($poolsOnly) && (!$share['cachePool2'])) {
$share['useCache'] = "only";
} else if ($share['cachePool2']) {
$poolDefined2 = in_array($share['cachePool2'], $pools);
}
$cachePoolCapitalized = compress(my_disk($share['cachePool'],$display['raw']));
$cachePoolCapitalized2 = $share['cachePool2'] ? compress(my_disk($share['cachePool2'],$display['raw'])) : _("Array");
function globalInclude($name) {
global $var;
return substr($name,0,4)=='disk' && (!$var['shareUserInclude'] || in_array($name,explode(',',$var['shareUserInclude'])));
}
function sanitize(&$val) {
$data = explode('.',str_replace([' ',','],['','.'],$val));
$last = array_pop($data);
$val = count($data) ? implode($data).".$last" : $last;
}
/**
* Preset space calculation and formatting.
*
* @param float|string $val Value to calculate preset space for.
* @return string|null Formatted space string or null.
*/
function presetSpace($val) {
global $disks, $shares, $name, $pools, $display;
/* Return if the value is invalid or NaN */
if (!$val || strcasecmp($val, 'NaN') == 0) return null;
/* Sanitize the value */
sanitize($val);
/* Prepare the largest array disk */
$large = [];
foreach (data_filter($disks) as $disk) {
$large[] = _var($disk, 'fsSize');
}
/* Prepare the fsSize array for each pool */
$fsSize = [];
foreach ($pools as $pool) {
$fsSize[$pool] = _var($disks[$pool], 'fsSize', 0);
}
/* Get the maximum value from the large array, filtering out non-numeric values */
if (!empty($large)) {
/* Filter non-numeric values */
$numericLarge = array_filter($large, 'is_numeric');
/* Set the maximum value */
$fsSize[''] = !empty($numericLarge) ? max($numericLarge) : 0;
} else if (!empty($fsSize)) {
/* Filter non-numeric values */
$numericFsSize = array_filter($fsSize, 'is_numeric');
/* Set the minimum value */
$fsSize[''] = !empty($numericFsSize) ? min($numericFsSize) : 0;
} else {
$fsSize[''] = 0;
}
/* Get the cache pool size */
$pool = _var($shares[$name], 'cachePool');
$size = _var($fsSize, $pool, 0);
/* Calculate the size */
$size = $size > 0 ? round(100 * $val / $size, 1) : 0;
/* Units for size formatting */
$units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$base = $val > 0 ? floor(log($val, 1000)) : 0;
$formattedSize = round($val / pow(1000, $base), 1);
$unit = _var($units, $base);
/* Get number format settings */
[$dot, $comma] = str_split(_var($display, 'number', '.,'));
/* Return the formatted size */
return $formattedSize > 0 ? number_format($formattedSize, $formattedSize - floor($formattedSize) ? 1 : 0, $dot, $comma) . ' ' . $unit : '';
}
/**
* Function to get enabled disks based on include and exclude rules.
*
* @global array $disks Array of disk names to check.
* @global array $share Array containing include and exclude rules.
*
* @return array Array of keys for enabled disk names.
*/
function enabledDisks() {
global $disks, $share;
/* Prepare the resultant array */
$trueKeys = [];
/* Process each disk in the array */
foreach ($disks as $key => $disk) {
$include = true; /* Default to true */
/* Check the include field */
if (!empty($share['include'])) {
$includedDisks = explode(',', $share['include']);
if (!in_array($key, $includedDisks)) {
$include = false;
}
}
/* Check the exclude field */
if (!empty($share['exclude'])) {
$excludedDisks = explode(',', $share['exclude']);
if (in_array($key, $excludedDisks)) {
$include = false;
}
}
/* Add to trueKeys array if the disk should be included */
if ($include) {
$trueKeys[] = $key;
}
}
return $trueKeys;
}
function fsSize() {
global $disks, $pools, $share;
/* Initialize an array to hold filesystem sizes */
$fsSize = [];
/* Initialize an array to hold sizes of enabled disks */
$small = [];
/* Get a list of enabled disks */
$enabledDisks = enabledDisks();
/* Iterate over the filtered disk data */
foreach (data_filter($disks) as $key => $disk) {
/* Check if the disk is in the list of enabled disks */
if (in_array($key, $enabledDisks)) {
/* Add the filesystem size of the enabled disk to the array */
$small[] = _var($disk, 'fsSize');
}
}
/* Set the minimum filesystem size from the array of enabled disks */
if (!empty($small)) {
/* Filter out non-numeric values and zeros */
$numericSmall = array_filter($small, function($value) {
return is_numeric($value) && (float)$value > 0;
});
/* Set the minimum value */
$fsSize[''] = !empty($numericSmall) ? min($numericSmall) : 0;
} else {
$fsSize[''] = 0;
}
/* Iterate over the pool names and add their filesystem sizes */
foreach ($pools as $pool) {
/* Set the filesystem size for each pool, defaulting to 0 if not set */
$fsSize[$pool] = _var($disks[$pool], 'fsSize', 0);
}
/* Return the filesystem sizes as a JSON encoded string */
return json_encode($fsSize);
}
function fsFree() {
global $disks, $pools;
/* Initialize an array to hold free filesystem space */
$fsFree = [];
/* Initialize an array to hold free space of enabled disks */
$large = [];
/* Get a list of enabled disks */
$enabledDisks = enabledDisks();
/* Iterate over the filtered disk data */
foreach (data_filter($disks) as $key => $disk) {
/* Check if the disk is in the list of enabled disks */
if (in_array($key, $enabledDisks)) {
/* Add the free filesystem space of the enabled disk to the array */
$large[] = _var($disk, 'fsFree');
}
}
/* Iterate over the pool names and add their free filesystem space */
foreach ($pools as $pool) {
/* Set the free filesystem space for each pool, defaulting to 0 if not set */
$fsFree[$pool] = _var($disks[$pool], 'fsFree', 0);
}
/* Determine the free filesystem space */
if (!empty($large)) {
/* Filter non-numeric values */
$numericLarge = array_filter($large, 'is_numeric');
/* Set the maximum free filesystem space from the array of enabled disks */
$fsFree[''] = !empty($numericLarge) ? max($numericLarge) : 0;
} elseif (!empty($fsFree)) {
/* Filter non-numeric values */
$numericFsFree = array_filter($fsFree, 'is_numeric');
/* Set the minimum free filesystem space from the pool array */
$fsFree[''] = !empty($numericFsFree) ? min($numericFsFree) : 0;
} else {
/* The minimum free space cannot be determined from the array or pools, so it is set to zero */
$fsFree[''] = 0;
}
/* Return the free filesystem space as a JSON encoded string */
return json_encode($fsFree);
}
function fsType() {
global $disks,$pools;
$fsType = [];
foreach ($pools as $pool) {
$fsType[] = '"'.$pool.'":"'.str_replace('luks:','',_var($disks[$pool],'fsType')).'"';
}
return implode(',',$fsType);
}
function primary() {
global $share;
return $share['useCache']=='no' ? '' : $share['cachePool'];
}
function secondary() {
global $share;
$rc = in_array($share['useCache'],['no','only']) ? '0' : '1';
return $rc;
}
function direction() {
global $share;
return $share['useCache']=='prefer' ? '1' : '0';
}
/* global shares include/exclude. */
$myDisks = array_filter(array_diff(array_keys(array_filter($disks,'my_disks')), explode(',',$var['shareUserExclude'])), 'globalInclude');
?>
:share_edit_global1_help:
:share_edit_global2_help: