Skip to content

Commit 88e253c

Browse files
committed
To check if parent dir exists, use /namespace call instead of /tape/locality
Fixes #105
1 parent c899e42 commit 88e253c

1 file changed

Lines changed: 35 additions & 10 deletions

File tree

ada/ada

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,25 @@ pathtype () {
11001100
}
11011101

11021102

1103+
is_dir () {
1104+
# Check whether a path is a directory. If the path doesn't exist, just continue.
1105+
# Similar to pathtype(), but that one quits with error if the path doesn't exist.
1106+
encoded_path=$(urlencode "$1")
1107+
command='$debug && set -x
1108+
curl "${curl_authorization[@]}" \
1109+
-H "accept: application/json" \
1110+
--silent \
1111+
-X GET "$api/namespace/$encoded_path"'
1112+
if $dry_run ; then
1113+
echo "$command"
1114+
else
1115+
result=$(eval "$command")
1116+
# The result of the grep will be passed on to the calling statement.
1117+
echo "$result" | jq -r .fileType | grep --silent 'DIR'
1118+
fi
1119+
}
1120+
1121+
11031122
get_pnfsid () {
11041123
local path=$(urlencode "$1")
11051124
command='curl "${curl_authorization[@]}" \
@@ -1313,19 +1332,21 @@ create_path () {
13131332
local path="$1"
13141333
local recursive="$2"
13151334
local parent="$(dirname "$path")"
1316-
get_locality "$parent"
1317-
error=$?
1318-
if [ $error == 1 ] && $recursive ; then
1319-
if [ "${#parent}" -gt 1 ]; then
1320-
echo 1>&2 "Warning: parent dir '$parent' does not exist. Will atempt to create it."
1321-
create_path "$parent" "$recursive"
1335+
if ! is_dir "$parent" ; then
1336+
# The parent dir does not exist yet. Should we create it?
1337+
if $recursive ; then
1338+
if [ "${#parent}" -gt 1 ]; then
1339+
echo 1>&2 "Warning: parent dir '$parent' does not exist. Will attempt to create it."
1340+
create_path "$parent" "$recursive"
1341+
else
1342+
# We reached the root dir. Something most be wrong.
1343+
echo 1>&2 "ERROR: Unable to create dirs. Check the specified path."
1344+
exit 1
1345+
fi
13221346
else
1323-
echo 1>&2 "ERROR: Unable to create dirs. Check the specified path."
1347+
echo 1>&2 "ERROR: parent dir '$parent' does not exist. To recursively create dirs, add --recursive."
13241348
exit 1
13251349
fi
1326-
elif [ $error == 1 ]; then
1327-
echo 1>&2 "ERROR: parent dir '$parent' does not exist. To recursivly create dirs, add --recursive."
1328-
exit 1
13291350
fi
13301351
parent=$(urlencode "$(dirname "$path")")
13311352
name=$(basename "$path")
@@ -2317,6 +2338,10 @@ api_call () {
23172338
)
23182339
;;
23192340
mkdir )
2341+
if is_dir "$path" ; then
2342+
echo 1>&2 "Directory '$path' already exists."
2343+
exit 0
2344+
fi
23202345
create_path "$path" "$recursive"
23212346
;;
23222347
mv )

0 commit comments

Comments
 (0)