Skip to content

Commit 004cafd

Browse files
committed
Move the check for subjects_dir (that is repeated across different files) into a function in functions.
This check uses `stat`, which works in different ways for macOS and linux. Make the function (stat) work for both linux (GNU coreutils) and macOS (unclear version).
1 parent 992016d commit 004cafd

5 files changed

+35
-51
lines changed

long_fastsurfer.sh

+1-14
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,7 @@ if [ "${#tpids[@]}" -ne "${#t1s[@]}" ]
223223
fi
224224

225225
# check that SUBJECTS_DIR exists
226-
if [[ -z "${sd}" ]]
227-
then
228-
echo "ERROR: No subject directory defined via --sd. This is required!"
229-
exit 1
230-
elif [[ ! -d "${sd}" ]]
231-
then
232-
echo "INFO: The subject directory did not exist, creating it now."
233-
if ! mkdir -p "$sd" ; then echo "ERROR: directory creation failed" ; exit 1; fi
234-
elif [[ "$(stat -c "%u:%g" "$sd")" == "0:0" ]] && [[ "$(id -u)" != "0" ]] && [[ "$(stat -c "%a" "$sd" | tail -c 2)" -lt 6 ]]
235-
then
236-
echo "ERROR: The subject directory ($sd) is owned by root and is not writable. FastSurfer cannot write results! "
237-
echo " This can happen if the directory is created by docker. Make sure to create the directory before invoking docker!"
238-
exit 1
239-
fi
226+
check_create_subjects_dir_properties "$sd"
240227

241228
if [[ -z "$LF" ]] ; then LF="$sd/$tid/scripts/long_fastsurfer.log" ; fi
242229
# make sure the directory for the logfile exists, create automatically if the directory is not in $sd

recon_surf/functions.sh

+31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@ if [ "${PIPESTATUS[0]}" -ne 0 ] ; then
1818
fi
1919
export timecmd
2020

21+
function check_create_subjects_dir_properties()
22+
{
23+
# 1: subjects_dir
24+
if [[ -z "$1" ]]
25+
then
26+
echo "ERROR: No subject directory defined via --sd. This is required!"
27+
exit 1
28+
elif [[ ! -d "$1" ]]
29+
then
30+
echo "INFO: The subject directory did not exist, creating it now."
31+
if [[ "$(id -u)" == 0 ]] ; then echo "WARNING: Creating as root!" ; fi
32+
if ! mkdir -p "$1" ; then echo "ERROR: directory creation failed" ; exit 1; fi
33+
else
34+
if [[ "$(stat --version | head -n 1)" =~ GNU[[:space:]]coreutils ]] ; then # linux
35+
user_group=$(stat -c "%u:%g" "$1")
36+
world_access=$(stat -c "%a" "$1" | tail -c 2)
37+
elif [[ "$(stat -- 2>&1 | tail -n 1)" =~ ^usage:[[:space:]]stat.*-f[[:space:]]format ]] ; then # macOS
38+
user_group=$(stat -f "%u:%g" "$1")
39+
world_access=$(stat -f "%p" "$1" | tail -c 2)
40+
else echo "ERROR: Cannot detect stat interface!" ; exit 1
41+
fi
42+
if [[ "$user_group" == "0:0" ]] && [[ "$(id -u)" != "0" ]] && [[ "$world_access" -lt 6 ]]
43+
then
44+
echo "ERROR: The subject directory ($1) is owned by root and is not writable."
45+
echo " FastSurfer cannot write results! This can happen if the directory is created"
46+
echo " by docker. Make sure to create the directory before invoking docker!"
47+
exit 1
48+
fi
49+
fi
50+
}
51+
2152
function RunIt()
2253
{
2354
# parameters

recon_surf/long_prepare_template.sh

+1-19
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,7 @@ then
220220
fi
221221

222222
# check that SUBJECTS_DIR exists
223-
if [[ -z "$SUBJECTS_DIR" ]]
224-
then
225-
echo "ERROR: No subject directory defined via --sd. This is required!"
226-
exit 1;
227-
fi
228-
if [[ ! -d "${sd}" ]]
229-
then
230-
echo "INFO: The subject directory did not exist, creating it now."
231-
if ! mkdir -p "$SUBJECTS_DIR" ; then echo "ERROR: directory creation failed" ; exit 1; fi
232-
fi
233-
if [[ "$(stat -c "%u:%g" "$SUBJECTS_DIR")" == "0:0" ]] && [[ "$(id -u)" != "0" ]] && \
234-
[[ "$(stat -c "%a" "$SUBJECTS_DIR" | tail -c 2)" -lt 6 ]]
235-
then
236-
echo "ERROR: The subject directory ($SUBJECTS_DIR) is owned by root and is not writable."
237-
echo " FastSurfer cannot write results! This can happen if the directory is created by"
238-
echo " docker. Make sure to create the directory before invoking docker!"
239-
exit 1;
240-
fi
241-
223+
check_create_subjects_dir_properties "$SUBJECTS_DIR"
242224

243225
################################## SETUP and LOGFILE ##############################
244226

run_fastsurfer.sh

+1-18
Original file line numberDiff line numberDiff line change
@@ -563,24 +563,7 @@ if [[ "$legacy_parallel_hemi" == 1 ]] ; then
563563
fi
564564
fi
565565

566-
if [[ -z "${sd}" ]]
567-
then
568-
echo "ERROR: No subject directory defined via --sd. This is required!"
569-
exit 1
570-
fi
571-
if [[ ! -d "${sd}" ]]
572-
then
573-
echo "INFO: The subject directory did not exist, creating it now." | tee -a "$tmpLF"
574-
if ! mkdir -p "$sd" ; then echo "ERROR: Subject directory creation failed" ; exit 1 ; fi
575-
fi
576-
if [[ "$(stat -c "%u:%g" "$sd")" == "0:0" ]] && [[ "$(id -u)" != "0" ]] && \
577-
[[ "$(stat -c "%a" "$sd" | tail -c 2)" -lt 6 ]]
578-
then
579-
echo "ERROR: The subject directory ($sd) is owned by root and is not writable."
580-
echo " FastSurfer cannot write results! This can happen if the directory is created"
581-
echo " by docker. Make sure to create the directory before invoking docker!"
582-
exit 1
583-
fi
566+
check_create_subjects_dir_properties "$sd"
584567

585568
if [[ -z "$subject" ]]
586569
then

srun_fastsurfer.sh

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ do
383383
fi
384384
done
385385
debugf "$newline\n"
386+
# the following stat command is not compatible with macOS, but srun_fastsurfer is not expected to be
386387
shell=$(stat -c %N "/proc/$$/exe" | cut -d">" -f2 | tail -c +3 | head -c -2)
387388
debug "Running in shell $shell: $($shell --version 2>/dev/null | head -n 1)"
388389
debug ""

0 commit comments

Comments
 (0)