Skip to content

Commit d95a54d

Browse files
author
Louis T. Getterman IV
committed
hash.bash bug fix: (nested) empty directories
* Temporary cache file now only created if file count > 0 * Updated verbosity messages * Variable name changes to try and clarify purpose * Additional source code descriptions relating to UTF-8 sorting issue across platforms (e.g. OSX compared to Ubuntu)
1 parent 957b93b commit d95a54d

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

hash.bash

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
: <<'!COMMENT'
33
4-
GGCOM - Bash - Utils - Hash v201507101140
4+
GGCOM - Bash - Utils - Hash v201507111357
55
Louis T. Getterman IV (@LTGIV)
66
www.GotGetLLC.com | www.opensour.cc/ggcom/hashbash
77
@@ -43,6 +43,9 @@ http://unix.stackexchange.com/questions/34325/sorting-the-output-of-find
4343
python - case-insensitive list sorting, without lowercasing the result? - Stack Overflow
4444
http://stackoverflow.com/questions/10269701/case-insensitive-list-sorting-without-lowercasing-the-result
4545
46+
Recursive sub folder search and return files in a list python - Stack Overflow
47+
http://stackoverflow.com/questions/18394147/recursive-sub-folder-search-and-return-files-in-a-list-python
48+
4649
!COMMENT
4750

4851
################################################################################
@@ -227,46 +230,66 @@ else # FILE OR DIRECTORY
227230
# List individual hashes of all files (except operating system files) in Key:Value CSV format
228231
# Hash the cumulative output
229232
elif [ -d "$valTarget" ]; then
230-
TMPSTRINP=`mktemp 2>/dev/null || mktemp -t 'hash'`
231233
ORIGPWD="$PWD"
232234
cd "$valTarget"
233235

234-
if [ "$valVerbose" = True ]; then
235-
echo -e "${ggcLightPurple}Cumulative hash cache:${ggcNC} ${ggcLightBlue}${TMPSTRINP}${ggcNC}"
236-
echo;
237-
fi
238-
239236
# Create array of files to work through, and more importantly, show when the verbose flag is set
240-
if [ "$valVerbose" = True ]; then
241-
echo -e "${ggcLightPurple}Scanning files:${ggcNC}"
242-
fi
237+
if [ "$valVerbose" = True ]; then echo -n -e "${ggcLightPurple}Scanning for entries:${ggcNC} "; fi
243238

244-
# Hideous sorting technique has to be done due to the way that OSX differs from Linux on UTF-8
245-
unset FILEHASHLIST i f
239+
# Hideous sorting technique has to be done due to the way that -nix systems (e.g. OSX and Linux) differ from each other on UTF-8 and sorting
240+
# Check git history to see the various attempts I was making with combinations of `find` piped to `sort`
241+
unset fileItems i f
246242
while IFS= read -r -d $'\0' f; do
247-
FILEHASHLIST[i++]="$f"
243+
244+
# Skip: invalid file entry
245+
if [ -z "${f}" ] || [ ! -f "${f}" ]; then continue; fi
246+
247+
fileItems[i++]="$f"
248248
done < <( python -c "import os, sys; fdl=[os.path.join(dp, f) for dp, dn, filenames in os.walk('.') for f in filenames]; sys.stdout.write('\0'.join( sorted( [ x for x in fdl if os.path.basename(x) not in [ '.DS_Store', 'Icon\r' ] ], key=lambda s: s.lower() ) )+'\0' )" )
249249
unset i f
250250

251-
fileItems=${#FILEHASHLIST[@]}
251+
# Total size of array
252+
fileItemSize=${#fileItems[@]}
253+
254+
# Display count
255+
if [ "$valVerbose" = True ]; then echo -e "${ggcLightBlue}${fileItemSize}${ggcNC} "; echo; fi
256+
257+
# Directory contains 0 (nested) files.
258+
if [ "$fileItemSize" -eq 0 ]; then
259+
echo -e "${ggcLightRed}There are no (nested) files in directory '${valTarget}' to check. Exiting.${ggcNC}" >&2
260+
exit 1
261+
fi
262+
263+
TMPSTRINP=`mktemp 2>/dev/null || mktemp -t 'hash'`
264+
if [ "$valVerbose" = True ]; then
265+
echo -e "${ggcLightPurple}Cumulative hash cache:${ggcNC} ${ggcLightBlue}${TMPSTRINP}${ggcNC}"
266+
echo;
267+
fi
268+
269+
if [ "$valVerbose" = True ]; then echo -e "${ggcLightPurple}Processing queue:${ggcNC} "; fi
270+
252271
countFile=1
253-
for fileItem in "${FILEHASHLIST[@]}"; do
272+
for fileItem in "${fileItems[@]}"; do
254273

255-
if [ "$valVerbose" = True ]; then printf "($countFile/$fileItems = $(bc <<< "scale=1; ($countFile*100/$fileItems)") %%) ${fileItem}:"; fi
274+
if [ "$valVerbose" = True ]; then printf "($countFile/$fileItemSize = $(bc <<< "scale=1; ($countFile*100/$fileItemSize)") %%) ${fileItem}:"; fi
256275

257276
hashItem="$( "${SCRIPTPATH}/${SCRIPTNAME}" "$valAlg" "$fileItem" )"
277+
if [ $? -ne 0 ]; then
278+
(( fileItemSize-- ))
279+
continue
280+
fi
258281

259282
if [ "$valVerbose" = True ]; then printf "${hashItem}\n"; fi
260283

261284
printf "${fileItem}:${hashItem}" >> "$TMPSTRINP"
262285

263286
# Comma separator if not last entry
264-
if [ $countFile -lt $fileItems ]; then printf "," >> "$TMPSTRINP"; fi
287+
if [ $countFile -lt $fileItemSize ]; then printf "," >> "$TMPSTRINP"; fi
265288

266289
(( countFile++ ))
267290

268-
done; # END: for fileItem in "${FILEHASHLIST[@]}"
269-
unset FILEHASHLIST fileItems countFile fileItem
291+
done; # END: for fileItem in "${fileItems[@]}"
292+
unset fileItems fileItemSize countFile fileItem
270293

271294
# Dealing with OSX-based issue in erroneously adding newline, thus messing up the hash
272295
# cache="$( cat "$TMPSTRINP" | tr -d '\r' | tr -d '\n' | sed 's/,$//' )" # Can be used to clean-up the cache file
@@ -278,7 +301,7 @@ else # FILE OR DIRECTORY
278301
cat "$TMPSTRINP"
279302
echo;
280303
echo;
281-
echo -e "${ggcLightPurple}Directory Cache Hash:${ggcNC}"
304+
echo -e "${ggcLightPurple}Directory '${valTarget}' Cache Hash:${ggcNC}"
282305
echo -e "${ggcLightBlue}${hashCache}${ggcNC}"
283306
else
284307
echo "${hashCache}"

0 commit comments

Comments
 (0)