1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00

nux.fs: Migrated functionality from nuxfs

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
Tony Tkáčik 2020-01-02 16:14:45 +01:00
parent c1f365db70
commit 7ed7cc48ed
3 changed files with 90 additions and 48 deletions

View file

@ -1,59 +1,30 @@
#!/usr/bin/env nuxr-nuxsh #!/usr/bin/env nuxr-nuxsh
nux.use nuxfs nux.use nux/fs
@prefix fs nux.fs.
MARK_DIR_NAME=.by MARK_DIR_NAME=.by
MARK_PREFIX="" 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. { @namespace mark. {
function :dir item { function :dir item {
if [ -n "$MARK_DIR" ]; then if [ -n "$MARK_DIR" ]; then
echo $MARK_DIR; echo $MARK_DIR;
else else
nuxfs.closest "$MARK_DIR_NAME" "$item" fs:closest "$MARK_DIR_NAME" "$item"
fi fi
} }
function :mark root item mark { function :mark root item mark {
name="" name=""
if [ -e "$root/.path-names" ]; then if [ -e "$root/.path-names" ]; then
rel_path=$(nuxfs.path.relative "$root/.." "$item"); rel_path=$(nux.fs.path.relative "$root/.." "$item");
name=${rel_path//\//-} 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 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 fi
nuxfs.symlink "$item" "$root/$mark" "$name" fs:symlink "$item" "$root/$mark" "$name"
} }
} }

28
bin/vfs
View file

@ -1,11 +1,13 @@
#!/usr/bin/env nuxr-nuxsh #!/usr/bin/env nuxr-nuxsh
nux.use nuxfs nux.use nux/fs
@prefix fs nux.fs.
VFS_SOURCES_FILE=".vfs.sources" VFS_SOURCES_FILE=".vfs.sources"
CURRENT_SOURCES_FILE=$(nuxfs.closest "$VFS_SOURCES_FILE"); CURRENT_SOURCES_FILE=$(nux.fs.closest "$VFS_SOURCES_FILE");
if [ -f "$CURRENT_SOURCES_FILE" ] {
if fs:exists "$CURRENT_SOURCES_FILE" {
CURRENT_MOUNT="${CURRENT_SOURCES_FILE%/*}"; CURRENT_MOUNT="${CURRENT_SOURCES_FILE%/*}";
declare -gA CURRENT_SOURCES declare -gA CURRENT_SOURCES
nux.log debug "Current VFS mount: ${CURRENT_MOUNT%/*}"; nux.log debug "Current VFS mount: ${CURRENT_MOUNT%/*}";
@ -22,10 +24,10 @@ function vfs.path name {
function vfs.path.real file { function vfs.path.real file {
for root in "${CURRENT_SOURCES[@]}" ; do for root in "${CURRENT_SOURCES[@]}" ; do
nux.log trace "Testing $root$file" nux.log trace "Testing $root$file"
if [ -e "${root}${file}" -o -h "${root}${file}" ]; then if fs:exists "${root}${file}" {
echo ${root}${file}; echo ${root}${file};
return 0; return 0;
fi }
done done
} }
@ -47,7 +49,6 @@ function vfs.path.real file {
done done
} }
}
## switch:: <storage> <path+> ## switch:: <storage> <path+>
## Moves specified **paths** to named **storage** for particular **vfs**. ## Moves specified **paths** to named **storage** for particular **vfs**.
## This is ideal for marking files as keep or migrating them to remote, ## 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 { @command switch storage {
target="$(vfs.path "$storage")" target="$(vfs.path "$storage")"
if [ -z "$target" ] {
nux.fatal "$storage does not exists."
}
nux.log debug "Target path $target" nux.log debug "Target path $target"
for arg_path in "$@" ; do for arg_path in "$@" ; do
rooted_path="/$(realpath -m --relative-to="$CURRENT_MOUNT" ${arg_path%/})"; 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"; nux.log trace "Target dir: $target_dir";
if [ -n "$real_file" ]; then if [ -n "$real_file" ]; then
mkdir -p "$target_dir"; 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"; mv "$real_file" "$target_dir";
else else
nuxfs.error $arg_path does not exists. fs:error $arg_path does not exists.
fi fi
done done
} }
@ -101,16 +105,16 @@ function vfs.path.real file {
done done
echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE=" echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE="
mergerfs_options="$source_tempfs:${mount_paths}" mergerfs_mounts="$source_tempfs:${mount_paths}"
nux.log debug "MergerFS command:" $mergerfs_options; nux.log debug "MergerFS command:" $mergerfs_mounts;
mergerfs "$mergerfs_options" "$target" mergerfs "$mergerfs_mounts" "$target"
(cd $target; vfs info ) (cd $target; vfs info )
} }
## unmount:: <target> ## unmount:: <target>
## Unmounts target VFS filesystem. ## Unmounts target VFS filesystem.
@command :unmount target { @command :unmount target {
fusermount -u "$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]}" rm -rf "${CURRENT_SOURCES[temp]}"
} }

67
inc/nux/fs.nuxsh.sh Normal file
View file

@ -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";
}
}