Skip to content

Commit bbeae59

Browse files
committed
fs-path.bash: attempt fixing recursive validation
/ref 3cbcbce /ref a603aaa
1 parent a603aaa commit bbeae59

1 file changed

Lines changed: 17 additions & 40 deletions

File tree

commands/fs-path.bash

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function __process() (
7070
fi
7171
}
7272
# buble upwards until successful absolute or root
73-
local resolution bubble subpath='' accessible='' initial_iteration='yes'
73+
local resolution bubble subpath='' accessible='' initial_iteration='yes' if_missing_it_is_because_of_symlink='no'
7474
local -i symlink_status
7575
function __is_accessible {
7676
if [[ -z $accessible ]]; then
@@ -116,7 +116,7 @@ function __process() (
116116
fi
117117
break
118118
fi
119-
# `-<d|e|f|L>` do not operate correctly when inside a symlinked directory, and doing on `fs-path --resolve -- ../<symlink>`
119+
# `[[ -<d|e|f|L> ]]` do not operate correctly when inside a symlinked directory, and doing on `fs-path --resolve -- ../<symlink>`
120120
# so we must first resolve these synthetic paths before anything else, so those conditionals work correctly
121121
bubble='no'
122122
__enter || {
@@ -159,8 +159,8 @@ function __process() (
159159
return "$symlink_status"
160160
fi
161161
fi
162-
# reset lineage
163-
accessible=''
162+
# reset lineage: shouldn't be necessary
163+
# accessible=''
164164
# assign resolution
165165
path="$resolution"
166166
# done
@@ -174,44 +174,21 @@ function __process() (
174174
# on macos and fedora/GNU will not resolve with failure exit status (however we have already excluded this case earlier with -L)
175175
resolution="$(readlink -- "$path" || :)"
176176
if [[ -z $resolution ]]; then
177-
symlink_status=9 # EBADF 9 Bad file descriptor
178-
resolution="$path"
179-
elif [[ $validate == 'yes' && ! -e $resolution ]]; then # we support broken symlinks with successful resolution if not validating
180-
symlink_status=9 # EBADF 9 Bad file descriptor
181-
fi
182-
# check resolution, is fine that it runs on the pre-resolution old path, as long as it is absolute
183-
if [[ $symlink_status -ne 0 ]]; then
184-
__is_accessible || return $?
185-
# no need for validate check, as we have already done prior checks to ensure failure is only if desired
186-
return "$symlink_status"
177+
# success exit status, but still failed resolution, should never happen, but if it does, hard give up
178+
return 9 # EBADF 9 Bad file descriptor
187179
fi
188-
# reset lineage
189-
accessible=''
190-
# prevent future resolutions as we only wanted to resolve once
191-
resolve='no'
192-
# check resolution
193-
if [[ $symlink_status -eq 0 ]]; then
194-
# success, reiterate to complete the removal of synthetics and relatives
195-
if [[ ${resolution:0:1} == '/' ]]; then
196-
# absolute, replace
197-
path="$resolution"
198-
else
199-
# relative, append to the parent
200-
path="$(dirname -- "$path")"
201-
subpath="/$(basename -- "$resolution")$subpath"
202-
fi
203-
continue
180+
# reset lineage: shouldn't be necessary
181+
# accessible=''
182+
# reiterate on the resolved path, without further resolutions, to resolve synthetics and relatives, and validation
183+
if_missing_it_is_because_of_symlink='yes'
184+
if [[ ${resolution:0:1} == '/' ]]; then
185+
# absolute, replace
186+
path="$resolution"
204187
else
205-
# failed, so bubble to complete the removal of synthetics and relatives
206-
if [[ ${resolution:0:1} == '/' ]]; then
207-
# absolute, replace
208-
path="$resolution"
209-
else
210-
# relative, append to original parent
211-
path="$(dirname -- "$path")/$(basename -- "$resolution")"
212-
fi
213-
bubble='yes'
188+
# relative, append to original parent
189+
path="$(dirname -- "$path")/$(basename -- "$resolution")"
214190
fi
191+
continue
215192
fi
216193
elif [[ -e $path ]]; then
217194
# finally found something that does exist, so we are done
@@ -221,7 +198,7 @@ function __process() (
221198
bubble='yes'
222199
else
223200
__is_accessible || return $?
224-
if [[ -L $path ]]; then
201+
if [[ $if_missing_it_is_because_of_symlink == 'yes' || -L $path ]]; then
225202
# broken symlink
226203
return 9 # EBADF 9 Bad file descriptor
227204
else

0 commit comments

Comments
 (0)