Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,71 @@ function check_exit_code {
fatal_error "${fail_msg}"
fi
}

# Reimplement 'mkdir -p' with reporting on where permissions break down
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocaisa Maybe it makes sense to elaborate on the use case for this function a bit?
Why is mkdir -p not good enough, is it mainly to get better error reporting?

Can we add a reference to an issue on something here with more info?

function create_directory_structure() {
# Ensure we are given a single path argument
if [ $# -ne 1 ]; then
echo "Function requires a single (relative or absolute) path argument" >&2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function -> create_directory_structure

Suggested change
echo "Function requires a single (relative or absolute) path argument" >&2
echo "ERROR: create_directory_structure requires a single (relative or absolute) path argument" >&2

return 1
fi

# set a persistent variable that knows the full structure
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# set a persistent variable that knows the full structure
# set a persistent global variable that knows the full structure

# (i.e., retains the value upon recursive calls)
full_structure="${full_structure:="$1"}"

local directory_structure="$1"

# Check if directory exists and is writeable
if [ -d "${directory_structure}" ]; then
if [ "${directory_structure}" = "${full_structure}" ]; then
# release our (unneeded) global variable
unset full_structure
fi
if [ -w "${directory_structure}" ]; then
# Nothing to be done
return 0
else
echo "Directory ${directory_structure} exists but is not writeable" >&2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Directory ${directory_structure} exists but is not writeable" >&2
echo "ERROR: Directory ${directory_structure} exists but is not writeable" >&2

return 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want different exit codes for different types of failures?

fi
fi

local directory_structure_parent=$(dirname "${directory_structure}")

# If the parent doesn't exist we need to create it
if [ ! -d "${directory_structure_parent}" ]; then
# Create the parent via a recursive call to this function
# (if this doesn't succeed we need to return the error code)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not returning the error code of the recursive call at all?

if ! create_directory_structure "${directory_structure_parent}"; then
if [ "${directory_structure}" = "${full_structure}" ]; then
# release our (unneeded) global variable
unset full_structure
fi
return 1
fi
fi

# Check the parent is writeable, and create the new subdir
if [ -w "${directory_structure_parent}" ]; then
if [ "${directory_structure}" = "${full_structure}" ]; then
# release our (unneeded) global variable
unset full_structure
fi
if ! mkdir "${directory_structure}"; then
echo "'mkdir ${directory_structure}' failed for an unknown reason!" >&2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "'mkdir ${directory_structure}' failed for an unknown reason!" >&2
echo "'ERROR: mkdir ${directory_structure}' failed for an unknown reason!" >&2

return 1
else
# Success!
return 0
fi
else
echo "Attempt to create ${full_structure} failed," \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Attempt to create ${full_structure} failed," \
echo "ERROR: Attempt to create ${full_structure} failed," \

"${directory_structure_parent} exists but you don't have write permissions." >&2
if [ "${directory_structure}" = "${full_structure}" ]; then
# release our global variable
unset full_structure
fi
return 1
fi
}