#@IgnoreInspection BashAddShebang # Filesystem functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2017, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Resolve a given filesystem path to an absolute path # # Input: # $1 - the path to resolve # # Output: # the resolved path proc k_fs_resolve { var path = $(1%/) var dir = ''"" var base = ''"" if test -d $(path) { set dir = $(path) } else { set dir = $[k_fs_dirname $(path)] set base = ""/$[k_fs_basename $(path)]"" } if test -z $(dir) { set dir = '".'" } printf "%s%s" $[cd $(dir) && pwd -P] $(base) } # Get the base name of a filesystem path, optionally with an extension removed # # Input: # $1 - the path to get the basename of # $2 - the extension to remove if found # # Output: # the base name of the given path proc k_fs_basename { var path = $(1%/) var extension = $(2:-) set path = $(path##*/) if test -n $(extension) { set path = $(path%$extension) } printf "%s" $(path:-/) } # Get the parent directory path of a filesystem path # # Input: # $1 - the path to get the parent directory path of # # Output: # the parent directory name of the given path proc k_fs_dirname { var path = $(1%/) var dirPath = $(path%/*) if test -n $(path) && test $(path) = $(dirPath) { set dirPath = '".'" } printf "%s" $(dirPath:-/) } # Get a temp directory # # Output: # the temp directory path proc k_fs_temp_dir { var tmpDir = $[mktemp -d] k_log_debug "Created temporary directory: '$(tmpDir)'" printf "%s\n" $(tmpDir) >> $[k_fs_predictable_file k-fs-temp-files] printf "%s" $(tmpDir) } # Get a temp file # # Output: # the temp file path proc k_fs_temp_file { var tmpFile = $[mktemp] k_log_debug "Created temporary file: '$(tmpFile)'" printf "%s\n" $(tmpFile) >> $[k_fs_predictable_file k-fs-temp-files] printf "%s" $(tmpFile) } # Get a 'predictable' temporary directory # The directory path is generated using constants for this process, so they can be retrieved within a trap callback # # Input: # $1 - the basename for the directory. If not specified, the output of (#k_tool_name) is used # # Output: # the temporary directory name proc k_fs_predictable_dir { var tmpDir = ""$[k_fs_dirname $[mktemp -u]]/$(1:-$(k_tool_name)).$Pid"" mkdir -p $(tmpDir) k_log_debug "Created predictable temporary directory: '$(tmpDir)'" printf "%s\n" $(tmpDir) >> $[k_fs_predictable_file k-fs-temp-files] printf "%s" $(tmpDir) } # Get a 'predictable' temporary file # The file path is generated using constants for this process, so they can be retrieved within a trap callback # # Input: # $1 - the basename for the file. If not specified, the output of (#k_tool_name) is used # # Output: # the temporary file name proc k_fs_predictable_file { var name = $(1:-$(k_tool_name)) var tmpFile = ""$[k_fs_dirname $[mktemp -u]]/$(name).$Pid"" k_log_debug "Created predictable temporary file: '$(tmpFile)'" if test ! $(name) = "k-fs-temp-files" { printf "%s\n" $(tmpFile) >> $[k_fs_predictable_file k-fs-temp-files] } printf "%s" $(tmpFile) } # Cleanup temp files and directories proc k_fs_temp_cleanup { var tempFilesFile = $[k_fs_predictable_file k-fs-temp-files] k_log_info "Cleaning up temp files" while env IFS= read -r file { k_log_debug "Removing '$(file)'" rm -rf $(file) } < "${tempFilesFile}" k_log_debug "Removing '$(tempFilesFile)'" rm -f $(tempFilesFile) }