From 7ed7cc48edc2daa8c2eb05cb76b028a01b14c9f0 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Thu, 2 Jan 2020 16:14:45 +0100 Subject: [PATCH] nux.fs: Migrated functionality from nuxfs Signed-off-by: Tony Tkacik --- bin/mark | 43 +++++------------------------ bin/vfs | 28 +++++++++++-------- inc/nux/fs.nuxsh.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 inc/nux/fs.nuxsh.sh diff --git a/bin/mark b/bin/mark index 33e1cfa..a8ffa1e 100755 --- a/bin/mark +++ b/bin/mark @@ -1,59 +1,30 @@ #!/usr/bin/env nuxr-nuxsh -nux.use nuxfs +nux.use nux/fs +@prefix fs nux.fs. MARK_DIR_NAME=.by MARK_PREFIX="" -# FIXME: This should be probably in nux/fs -@namespace nuxfs. { - function :path.relative base target { - realpath -Lms --relative-to="$base" "$target" - } - - function :path.display target { - echo $NC_LightPurple$(nuxfs.relative-to-pwd "$target")$NC_No - } - - function :symlink target dir name { - relative=$(nuxfs.path.relative "$dir" "$target") - nux.log debug "Relative path is: $relative" - :stage mkdir -p "$dir" - if [ -n "$name" ]; then - :stage ln -sf "$relative" "$dir/$name" - else - :stage ln -sf "$relative" "$dir" - fi - } - - function :stage { - if [ -n "$NUXFS_STAGE" ]; then - echo "[stage]" "$@" - else - "$@" - fi - } -} - @namespace mark. { function :dir item { if [ -n "$MARK_DIR" ]; then echo $MARK_DIR; else - nuxfs.closest "$MARK_DIR_NAME" "$item" + fs:closest "$MARK_DIR_NAME" "$item" fi } function :mark root item mark { name="" if [ -e "$root/.path-names" ]; then - rel_path=$(nuxfs.path.relative "$root/.." "$item"); + rel_path=$(nux.fs.path.relative "$root/.." "$item"); name=${rel_path//\//-} - nuxfs.info "$item" Creating symlink: $(nuxfs.path.display "$root/$mark/$name") + fs:info "$item" Creating symlink: $(nux.fs.path.display "$root/$mark/$name") else - nuxfs.info "$item" Creating symlink in $(nuxfs.path.display "$root/$mark") + fs:info "$item" Creating symlink in $(nux.fs.path.display "$root/$mark") fi - nuxfs.symlink "$item" "$root/$mark" "$name" + fs:symlink "$item" "$root/$mark" "$name" } } diff --git a/bin/vfs b/bin/vfs index 77ffc2a..260fdf0 100755 --- a/bin/vfs +++ b/bin/vfs @@ -1,11 +1,13 @@ #!/usr/bin/env nuxr-nuxsh -nux.use nuxfs +nux.use nux/fs +@prefix fs nux.fs. VFS_SOURCES_FILE=".vfs.sources" -CURRENT_SOURCES_FILE=$(nuxfs.closest "$VFS_SOURCES_FILE"); -if [ -f "$CURRENT_SOURCES_FILE" ] { +CURRENT_SOURCES_FILE=$(nux.fs.closest "$VFS_SOURCES_FILE"); + +if fs:exists "$CURRENT_SOURCES_FILE" { CURRENT_MOUNT="${CURRENT_SOURCES_FILE%/*}"; declare -gA CURRENT_SOURCES nux.log debug "Current VFS mount: ${CURRENT_MOUNT%/*}"; @@ -22,10 +24,10 @@ function vfs.path name { function vfs.path.real file { for root in "${CURRENT_SOURCES[@]}" ; do nux.log trace "Testing $root$file" - if [ -e "${root}${file}" -o -h "${root}${file}" ]; then + if fs:exists "${root}${file}" { echo ${root}${file}; return 0; - fi + } done } @@ -47,7 +49,6 @@ function vfs.path.real file { done } - } ## switch:: ## Moves specified **paths** to named **storage** for particular **vfs**. ## This is ideal for marking files as keep or migrating them to remote, @@ -63,6 +64,9 @@ function vfs.path.real file { @command switch storage { target="$(vfs.path "$storage")" + if [ -z "$target" ] { + nux.fatal "$storage does not exists." + } nux.log debug "Target path $target" for arg_path in "$@" ; do rooted_path="/$(realpath -m --relative-to="$CURRENT_MOUNT" ${arg_path%/})"; @@ -73,10 +77,10 @@ function vfs.path.real file { nux.log trace "Target dir: $target_dir"; if [ -n "$real_file" ]; then mkdir -p "$target_dir"; - nuxfs.info $arg_path moving from "$NC_LightPurple${real_file}$NC_No" to "$NC_LightPurple$target_dir" + fs:info "$arg_path" moving from "$NC_LightPurple${real_file}$NC_No" to "$NC_LightPurple$target_dir" mv "$real_file" "$target_dir"; else - nuxfs.error $arg_path does not exists. + fs:error $arg_path does not exists. fi done } @@ -101,16 +105,16 @@ function vfs.path.real file { done echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE=" - mergerfs_options="$source_tempfs:${mount_paths}" - nux.log debug "MergerFS command:" $mergerfs_options; - mergerfs "$mergerfs_options" "$target" + mergerfs_mounts="$source_tempfs:${mount_paths}" + nux.log debug "MergerFS command:" $mergerfs_mounts; + mergerfs "$mergerfs_mounts" "$target" (cd $target; vfs info ) } ## unmount:: ## Unmounts target VFS filesystem. @command :unmount target { fusermount -u "$target" - nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata. + fs:info "${CURRENT_SOURCES[temp]}" removing temporary metadata. rm -rf "${CURRENT_SOURCES[temp]}" } diff --git a/inc/nux/fs.nuxsh.sh b/inc/nux/fs.nuxsh.sh new file mode 100644 index 0000000..5b948d1 --- /dev/null +++ b/inc/nux/fs.nuxsh.sh @@ -0,0 +1,67 @@ +nux.use nux/check +@prefix check nux.check. + +@namespace nux.fs. { + + function :exists target { + check:file.exists "$target"; + } + + function :closest target { + cdir="${2:-$(pwd)}"; + nux.log trace "Searching in: " $cdir; + until [ -e "$cdir/$target" -o "$cdir" == "/" ]; do + cdir=$(dirname "$cdir"); + nux.log trace "Searching in: " $cdir; + done; + if [ -e "$cdir/$target" ]; then + echo "$cdir/$target"; + fi + } + + function :path.relative.pwd target { + realpath -Lms --relative-to="$(pwd)" "$target" + } + + function :path.relative base target { + realpath -Lms --relative-to="$base" "$target" + } + + function :path.display target { + echo $NC_LightPurple$(nux.fs.path.relative.pwd "$target")$NC_No; + } + + function :symlink target dir name { + relative=$(nux.fs.path.relative "$dir" "$target") + nux.log debug "Relative path is: $relative" + :stage mkdir -p "$dir" + if [ -n "$name" ]; then + :stage ln -sf "$relative" "$dir/$name" + else + :stage ln -sf "$relative" "$dir" + fi + } + + function :stage { + if [ -n "$NUXFS_STAGE" ]; then + echo "[stage]" "$@" + else + "$@" + fi + } + + function :error file { + local filename=$(nux.fs.path.relative.pwd "$file"); + nux.echo.error "$filename$NC_No: $@$NC_No"; + } + + function :warning file { + local filename=$(nux.fs.path.relative.pwd "$file"); + nux.echo.warning "$filename$NC_No: $@$NC_No"; + } + + function :info file { + local filename=$(nux.fs.path.relative.pwd "$file"); + echo -e "$NC_White$filename$NC_No: $@$NC_No"; + } +}