Dietpi's file_manager uses a df command to return the files system description in human readable form and saves it in a temporary file. The code then uses MAWK to process each line of this file in turn, treating each line as a record and splitting each record into fields using space as the delimiter. Each 'record' normally returns six fields but when the share source name has spaces it results in more than six fields being returned, one extra field for each space.
The orginal code looks like this:
Code: Select all
#----------------------------------------------------------------
# PHYSICAL DRIVES
#----------------------------------------------------------------" > $fp_fstab_tmp
# Detect mounted drives
G_DIETPI-NOTIFY 2 'Detecting drives, please wait...'
# - Only detect mounts with valid source path (word 1 contains "/")
mawk '$1~/\//{print}' <<< $(df -Pha) > .df_out_tmp
# Remove misc items from list
# - bind: https://github.com/MichaIng/DietPi/issues/2013#issuecomment-416394374
[[ $misc_mounts ]] && while read line
do
local input_mount_target=$(mawk '{print $2}' <<< $line)
sed -i "\#[[:blank:]]$input_mount_target$#d" .df_out_tmp
# - target + source removal via $6 required for bind mounts: https://github.com/MichaIng/DietPi/issues/2013#issuecomment-417413867
local input_mount_source=$(mawk '{print $1}' <<< $line)
sed -i "\#[[:blank:]]$input_mount_source$#d" .df_out_tmp
[[ $G_DEBUG == 1 ]] && G_DIETPI-NOTIFY 0 " - Detected misc mount and removed from df scrape: $input_mount_source > $input_mount_target"
done <<< "$misc_mounts"
# Process final df result
while read line
do
Init_New_Device
aDRIVE_ISMOUNTED[$index]=1
aDRIVE_MOUNT_SOURCE[$index]=$(mawk '{print $1}' <<< $line)
aDRIVE_SIZE_TOTAL[$index]=$(mawk '{print $2}' <<< $line)
aDRIVE_SIZE_USED[$index]=$(mawk '{print $3}' <<< $line)
aDRIVE_SIZE_FREE[$index]=$(mawk '{print $4}' <<< $line)
aDRIVE_SIZE_PERCENTUSED[$index]=$(mawk '{print $5}' <<< $line)
aDRIVE_MOUNT_TARGET[$index]=$(mawk '{print $6}' <<< $line)
# Workaround for /dev/root under RPi, force physical location
[[ ${aDRIVE_MOUNT_TARGET[$index]} == '/' ]] && aDRIVE_MOUNT_SOURCE[$index]=$FP_ROOTFS_SOURCE
aDRIVE_SOURCE_DEVICE[$index]=$(Return_Drive_Without_Partitions ${aDRIVE_MOUNT_SOURCE[$index]})
[[ ${aDRIVE_MOUNT_SOURCE[$index]} == /dev/${aDRIVE_SOURCE_DEVICE[$index]} ]] || aDRIVE_ISPARTITIONTABLE[$index]=1
aDRIVE_UUID[$index]=$(blkid ${aDRIVE_MOUNT_SOURCE[$index]} -s UUID -o value)
(( ${aDRIVE_ISPARTITIONTABLE[$index]} )) && aDRIVE_PART_UUID[$index]=$(blkid ${aDRIVE_MOUNT_SOURCE[$index]} -s PARTUUID -o value)
I've used the AWK built in variable $NF to get the number of fields for each line and if there are more then six I intercept it and use a loop to reconstruct the proper source share name.
My code looks like this:
Code: Select all
#----------------------------------------------------------------
# PHYSICAL DRIVES
#----------------------------------------------------------------" > $fp_fstab_tmp
# Detect mounted drives
G_DIETPI-NOTIFY 2 'Detecting drives, please wait...'
# - Only detect mounts with valid source path (word 1 contains "/")
mawk '$1~/\//{print}' <<< $(df -Pha) > .df_out_tmp
# Remove misc items from list
# - bind: https://github.com/MichaIng/DietPi/issues/2013#issuecomment-416394374
[[ $misc_mounts ]] && while read line
do
local input_mount_target=$(mawk '{print $2}' <<< $line)
sed -i "\#[[:blank:]]$input_mount_target$#d" .df_out_tmp
# - target + source removal via $6 required for bind mounts: https://github.com/MichaIng/DietPi/issues/2013#issuecomment-417413867
local input_mount_source=$(mawk '{print $1}' <<< $line)
sed -i "\#[[:blank:]]$input_mount_source$#d" .df_out_tmp
[[ $G_DEBUG == 1 ]] && G_DIETPI-NOTIFY 0 " - Detected misc mount and removed from df scrape: $input_mount_source > $input_mount_target"
done <<< "$misc_mounts"
# Process final df result
while read line
do
Init_New_Device
# Get the total number of fields retured for each line
local number_fields=$(mawk '{print NF}' <<< $line)
aDRIVE_ISMOUNTED[$index]=1
# If there are more than 6 fields in the record then the source name has spaces!
if [[ $number_fields -ge 7 ]]; then
# 7 or more fields, source name has spaces
# Get the first part of the share source name
local source_parts=$(mawk '{print $1}' <<< $line)
# Now add the remaining parts
for (( n=2; n<=($number_fields - 5); n++ ))
do
source_parts="$source_parts "$(mawk "{print \$($n)}" <<< $line)
done
aDRIVE_MOUNT_SOURCE[$index]=$source_parts
else
# Record must have 6 fields and is normal
aDRIVE_MOUNT_SOURCE[$index]=$(mawk '{print $1}' <<< $line)
fi
aDRIVE_SIZE_TOTAL[$index]=$(mawk '{print $(2+NF-6)}' <<< $line)
aDRIVE_SIZE_USED[$index]=$(mawk '{print $(3+NF-6)}' <<< $line)
aDRIVE_SIZE_FREE[$index]=$(mawk '{print $(4+NF-6)}' <<< $line)
aDRIVE_SIZE_PERCENTUSED[$index]=$(mawk '{print $(5+NF-6)}' <<< $line)
aDRIVE_MOUNT_TARGET[$index]=$(mawk '{print $(6+NF-6)}' <<< $line)
# Workaround for /dev/root under RPi, force physical location
[[ ${aDRIVE_MOUNT_TARGET[$index]} == '/' ]] && aDRIVE_MOUNT_SOURCE[$index]=$FP_ROOTFS_SOURCE
aDRIVE_SOURCE_DEVICE[$index]=$(Return_Drive_Without_Partitions ${aDRIVE_MOUNT_SOURCE[$index]})
[[ ${aDRIVE_MOUNT_SOURCE[$index]} == /dev/${aDRIVE_SOURCE_DEVICE[$index]} ]] || aDRIVE_ISPARTITIONTABLE[$index]=1
aDRIVE_UUID[$index]=$(blkid ${aDRIVE_MOUNT_SOURCE[$index]} -s UUID -o value)
(( ${aDRIVE_ISPARTITIONTABLE[$index]} )) && aDRIVE_PART_UUID[$index]=$(blkid ${aDRIVE_MOUNT_SOURCE[$index]} -s PARTUUID -o value)
I'm sure this can be done much more elegantly if I had a better understanding of AWK than I have garnered but, hey, it works.
My changes to the code are highlighted and explained here, changes highlighted in orange:
# Get the total number of fields returned for each line
local number_fields=$(mawk '{print NF}' <<< $line)
aDRIVE_ISMOUNTED[$index]=1
# If there are more than 6 fields in the record then the sourece name has spaces!
if [[ $number_fields -ge 7 ]]; then
# 7 or more fields, source name has spaces
# Get the first part of the share source name
local source_parts=$(mawk '{print $1}' <<< $line)
# Now add the remaining parts
for (( n=2; n<=($number_fields - 5); n++ ))
do
source_parts="$source_parts "$(mawk "{print \$($n)}" <<< $line)
done
aDRIVE_MOUNT_SOURCE[$index]=$source_parts
else
# Record must have 6 fields and is normal
aDRIVE_MOUNT_SOURCE[$index]=$(mawk '{print $1}' <<< $line)
fi
# The following change forces the allocations to be last five fields regardless
aDRIVE_SIZE_TOTAL[$index]=$(mawk '{print $(2+NF-6)}' <<< $line)
aDRIVE_SIZE_USED[$index]=$(mawk '{print $(3+NF-6)}' <<< $line)
aDRIVE_SIZE_FREE[$index]=$(mawk '{print $(4+NF-6)}' <<< $line)
aDRIVE_SIZE_PERCENTUSED[$index]=$(mawk '{print $(5+NF-6)}' <<< $line)
aDRIVE_MOUNT_TARGET[$index]=$(mawk '{print $(6+NF-6)}' <<< $line)
Regards
SNG