-
Notifications
You must be signed in to change notification settings - Fork 81
Reimplement mkdir -p reporting where permissions break down
#228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -31,3 +31,71 @@ function check_exit_code { | |||||
| fatal_error "${fail_msg}" | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| # Reimplement 'mkdir -p' with reporting on where permissions break down | ||||||
| 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return 1 | ||||||
| fi | ||||||
|
|
||||||
| # set a persistent variable that knows the full structure | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| # (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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return 1 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return 1 | ||||||
| else | ||||||
| # Success! | ||||||
| return 0 | ||||||
| fi | ||||||
| else | ||||||
| echo "Attempt to create ${full_structure} failed," \ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "${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 | ||||||
| } | ||||||
There was a problem hiding this comment.
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 -pnot good enough, is it mainly to get better error reporting?Can we add a reference to an issue on something here with more info?