Skip to content

Commit d710e34

Browse files
onnozweershailihu
andauthored
To check if parent dir exists, use /namespace call instead of /tape/locality (#106)
* To check if parent dir exists, use /namespace call instead of /tape/locality Fixes #105 * Fix unit test * New function is_dir() interfered with the output of create_path() * Typo was fixed in ada but should be fixed in unit test as well, to match * Removing `get_locality` function * The function used the /tape API call to check whether an object existed. This is not the best call to do that; the namespace call is better for that. Also, there has been a recent change in dCache that affects how this API call works, breaking Ada. * The last remaining call to this function was not really necessary, because the `case` statement below it already catches the case where a path does not exist. * Not using `is_dir` because the path might be a file... `is_dir` only returns true for a directory. * Fix integration test: use is_dir in test_ada_mkdir instead of get_locality --------- Co-authored-by: Haili Hu <hailihu@gmail.com>
1 parent c899e42 commit d710e34

3 files changed

Lines changed: 36 additions & 38 deletions

File tree

ada/ada

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,22 @@ 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+
result=$(
1108+
$debug && set -x
1109+
curl "${curl_authorization[@]}" \
1110+
-H "accept: application/json" \
1111+
--silent \
1112+
-X GET "$api/namespace/$encoded_path" 2>/dev/null
1113+
)
1114+
# The exit code of the grep will be passed on to the calling statement.
1115+
echo "$result" | jq -r .fileType | grep --silent 'DIR'
1116+
}
1117+
1118+
11031119
get_pnfsid () {
11041120
local path=$(urlencode "$1")
11051121
command='curl "${curl_authorization[@]}" \
@@ -1313,19 +1329,21 @@ create_path () {
13131329
local path="$1"
13141330
local recursive="$2"
13151331
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"
1332+
if ! is_dir "$parent" ; then
1333+
# The parent dir does not exist yet. Should we create it?
1334+
if $recursive ; then
1335+
if [ "${#parent}" -gt 1 ]; then
1336+
echo 1>&2 "Warning: parent dir '$parent' does not exist. Will attempt to create it."
1337+
create_path "$parent" "$recursive"
1338+
else
1339+
# We reached the root dir. Something most be wrong.
1340+
echo 1>&2 "ERROR: Unable to create dirs. Check the specified path."
1341+
exit 1
1342+
fi
13221343
else
1323-
echo 1>&2 "ERROR: Unable to create dirs. Check the specified path."
1344+
echo 1>&2 "ERROR: parent dir '$parent' does not exist. To recursively create dirs, add --recursive."
13241345
exit 1
13251346
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
13291347
fi
13301348
parent=$(urlencode "$(dirname "$path")")
13311349
name=$(basename "$path")
@@ -1393,36 +1411,12 @@ delete_path () {
13931411
}
13941412

13951413

1396-
get_locality () {
1397-
local path="$1"
1398-
locality="$( (\
1399-
$debug && set -x # If --debug is specified, show curl & jq command
1400-
curl "${curl_authorization[@]}" \
1401-
"${curl_options_common[@]}" \
1402-
"${curl_options_post[@]}" \
1403-
-X POST "$api/tape/archiveinfo" \
1404-
-d "{\"paths\":[\"/${path}\"]}" \
1405-
) | jq . | grep locality)"
1406-
if [ -z "$locality" ] ; then
1407-
return 1
1408-
else
1409-
return 0
1410-
fi
1411-
}
1412-
1413-
14141414
bulk_request() {
14151415
local activity="$1"
14161416
local pathlist="$2"
14171417
local recursive="$3"
14181418
if [ "$from_file" == false ] ; then
14191419
local filepath="$2"
1420-
get_locality "$filepath"
1421-
error=$?
1422-
if [ "$error" == 1 ] ; then
1423-
echo 1>&2 "Error: '$filepath' does not exist."
1424-
exit 1
1425-
fi
14261420
type=$(pathtype "$filepath")
14271421
case $type in
14281422
DIR )
@@ -2317,6 +2311,10 @@ api_call () {
23172311
)
23182312
;;
23192313
mkdir )
2314+
if is_dir "$path" ; then
2315+
echo 1>&2 "Directory '$path' already exists."
2316+
exit 0
2317+
fi
23202318
create_path "$path" "$recursive"
23212319
;;
23222320
mv )

tests/integration_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ test_ada_mkdir() {
3030
assertEquals "ada returned error code ${result}" 0 ${result} || return
3131
grep "success" "${stdoutF}" >/dev/null
3232
assertTrue "ada could not create the directory" $? || return
33-
get_locality "/${disk_path}/${dirname}/${testdir}/${subdir}"
34-
assertTrue "could not get locality" $?
33+
is_dir "/${disk_path}/${dirname}/${testdir}/${subdir}"
34+
assertTrue "is not a directory" $?
3535
}
3636

3737

tests/unit_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test_create_path() {
3737
( create_path "/test/a" false >${stdoutF} 2>${stderrF} )
3838
result=$?
3939
assertFalse "expecting return code of 1 (false)" ${result}
40-
grep "ERROR: parent dir '/test' does not exist. To recursivly create dirs, add --recursive" "${stderrF}" >/dev/null
40+
grep "ERROR: parent dir '/test' does not exist. To recursively create dirs, add --recursive" "${stderrF}" >/dev/null
4141
assertTrue 'STDERR message incorrect' $?
4242

4343
# Check error handling when max number of directories is exceeded
@@ -147,4 +147,4 @@ oneTimeSetUp() {
147147

148148

149149
# Load and run shunit2
150-
. shunit2
150+
. shunit2

0 commit comments

Comments
 (0)