Skip to content

Commit

Permalink
Merge pull request #35 from sara-nl/mkdir-recursive
Browse files Browse the repository at this point in the history
Mkdir recursive
  • Loading branch information
hailihu authored Nov 27, 2024
2 parents c41af0a + 8f49570 commit 7f7bea0
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions ada/ada
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Latest version is available at: https://github.com/sara-nl/SpiderScripts
#
# Changes:
# 2024-11-26 - Haili - Add --recursive to --mkdir
# 2024-09-12 - Haili - Added bulk requests for staging and unstaging
# 2024-08-24 - Haili - Added option to use env var BEARER_TOKEN
# 2020-11-04 - Onno - Added link to Natalie's demo video
Expand Down Expand Up @@ -78,8 +79,11 @@ usage() {
--stat <file|directory>
Show all details of file or dir.
--mkdir <directory>
--mkdir <directory> [--recursive]
Create a directory.
To recursively create a directory and ALL of its
parents, add --recursive. For safety, the maximum number
of directories that can be created at once is 10.
--mv <file|directory> <destination>
Rename or move a file or directory.
Expand Down Expand Up @@ -203,6 +207,7 @@ certdir=${X509_CERT_DIR:-/etc/grid-security/certificates}
lifetime=7
lifetime_unit=D
from_file=false
counter=0

# Default options to curl for various activities;
# these can be overidden in configuration files, see below.
Expand Down Expand Up @@ -806,6 +811,43 @@ get_confirmation () {
}


create_path () {
let counter++
if [ $counter -gt 10 ] ; then
echo 1>&2 "ERROR: max number of directories that can be created at once is 10."
exit 1
fi
local path="$1"
local recursive="$2"
local parent="$(dirname "$path")"
get_locality "$parent"
error=$?
if [ $error == 1 ] && $recursive ; then
if [ "${#parent}" -gt 1 ]; then
echo 1>&2 "Warning: parent dir '$parent' does not exist. Will atempt to create it."
create_path $parent $recursive
else
echo 1>&2 "ERROR: Unable to create dirs. Check the specified path."
exit 1
fi
elif [ $error == 1 ]; then
echo 1>&2 "ERROR: parent dir '$parent' does not exist. To recursivly create dirs, add --recursive."
exit 1
fi
parent=$(urlencode "$(dirname "$path")")
name=$(basename "$path")
(
$debug && set -x # If --debug is specified, show (only) curl command
curl "${curl_authorization[@]}" \
"${curl_options_common[@]}" \
"${curl_options_post[@]}" \
-X POST "$api/namespace/$parent" \
-d "{\"action\":\"mkdir\",\"name\":\"$name\"}"
) \
| jq -r .status
}


delete_path () {
local path="$1"
local recursive="$2"
Expand Down Expand Up @@ -1408,17 +1450,7 @@ case $command in
| jq .
;;
mkdir )
parent=$(urlencode "$(dirname "$path")")
name=$(basename "$path")
(
$debug && set -x # If --debug is specified, show (only) curl command
curl "${curl_authorization[@]}" \
"${curl_options_common[@]}" \
"${curl_options_post[@]}" \
-X POST "$api/namespace/$parent" \
-d "{\"action\":\"mkdir\",\"name\":\"$name\"}"
) \
| jq -r .status
create_path $path $recursive
;;
mv )
# dCache may overwrite an empty directory.
Expand Down

0 comments on commit 7f7bea0

Please sign in to comment.