Improved file manager API functions

This commit is contained in:
Serghey Rodin 2015-08-20 18:01:27 +03:00
parent 7bc8af40f3
commit 15d1f4f4c9
13 changed files with 292 additions and 82 deletions

59
bin/v-add-fs-archive Executable file
View file

@ -0,0 +1,59 @@
#!/bin/bash
# info: archive directory
# options: USER ARCHIVE DIRECTORY [DIRECTORY_N]
#
# The function creates tar archive
user=$1
archive=$2
src1=$3
src2=$4
src3=$5
src4=$6
src5=$7
src6=$8
src7=$9
# Checking arguments
if [ -z "$src1" ]; then
echo "Usage: USER ARCHIVE DIRECTORY [DIRECTORY_N]"
exit 1
fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking archive
if [ -e "$archive.tar.gz" ]; then
echo "Error: archive already exist $archive.tar.gz"
exit 1
fi
# Checking source path
for src_path in $src1 $src2 $src3 $src4 $src5 $src6 $src7; do
rpath=$(readlink -f "$src_path")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
echo "Error: invalid source path $src_path"
exit 1
fi
done
# Creating tar.gz archive
sudo -u $user tar -czf "$archive.tar.gz" \
$src1 $src2 $src3 $src4 $src5 $src6 $src7 > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Error: archive $archive.tar.gz was not created"
exit 3
fi
exit

View file

@ -15,23 +15,30 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking destination path
rpath=$(readlink -f "$dst_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_dir"
exit 2
fi
# Adding directory
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: directory $dst_dir was not created"
exit 3
fi
# Extiging
exit $?
exit

View file

@ -15,23 +15,30 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking destination path
rpath=$(readlink -f "$dst_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_dir"
exit 2
fi
# Creating file
sudo -u $user touch $dst_file >/dev/null 2>&1
sudo -u $user touch "$dst_file" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: file $dst_file was not created"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,28 +16,36 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source file
if [ ! -f "$src_file" ]; then
exit 1
echo "Error: source file doesn't exist $src_file"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_file"
exit 2
fi
# Changing file permissions
sudo -u $user chmod $permisions $src_file >/dev/null 2>&1
sudo -u $user chmod $permisions "$src_file" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: access permission on $src_file was not changed"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,34 +16,43 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source dir
if [ ! -e "$src_dir" ]; then
exit 1
echo "Error: source directory $src_dir doesn't exist"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_dir"
exit 2
fi
# Checking destination path
rpath=$(readlink -f "$dst_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_dir"
exit 2
fi
# Copying directory
sudo -u $user cp -r $src_dir $dst_dir >/dev/null 2>&1
sudo -u $user cp -r "$src_dir" "$dst_dir" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: directory $src_dir was not copied"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,34 +16,43 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source file
if [ ! -f "$src_file" ]; then
exit 1
echo "Error: $src_file doesn't exist"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_file"
exit 2
fi
# Checking destination path
rpath=$(readlink -f "$dst_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: ivalid destination path $dst_file"
exit 2
fi
# Copying file
sudo -u $user cp $src_file $dst_file >/dev/null 2>&1
sudo -u $user cp "$src_file" "$dst_file" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: file $src_file was not copied"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,23 +16,30 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking destination path
rpath=$(readlink -f "$dst_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
echo "Error: invalid destination path $dst_dir"
exit 1
fi
# Deleting directory
sudo -u $user rm -rf $dst_dir >/dev/null 2>&1
sudo -u $user rm -rf "$dst_dir" # >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: directory $dst_dir was not deleted"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,23 +16,30 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking destination path
rpath=$(readlink -f "$dst_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_file"
exit 2
fi
# Deleting file
sudo -u $user rm -f $dst_file >/dev/null 2>&1
sudo -u $user rm -f "$dst_file" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: file $dst_file was not deleted"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -16,85 +16,104 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source dir
if [ ! -e "$src_file" ]; then
exit 1
echo "Error: source file $src_file doesn't exist"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_file"
exit 2
fi
# Checking destination path
rpath=$(readlink -f "$dst_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_dir"
exit 2
fi
# Extracting gziped archive
if [ ! -z "$(echo $src_file |egrep -i '.tgz|.tar.gz')" ]; then
x='yes'
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user tar -xzf $src_file -C $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user tar -xzf "$src_file" -C "$dst_dir" >/dev/null 2>&1
rc=$?
fi
# Extracting bziped archive
if [ ! -z "$(echo $src_file |egrep -i '.tbz|.tar.bz')" ]; then
x='yes'
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user tar -xjf $src_file -C $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user tar -xjf "$src_file" -C "$dst_dir" >/dev/null 2>&1
rc=$?
fi
# Extracting gziped file
if [ ! -z "$(echo $src_file |grep -i '.gz')" ] && [ -z "$x" ]; then
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user mv $src_file $dst_dir >/dev/null 2>&1
sudo -u $user gzip -d $dst_dir/$(basename $src_file) >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user mv "$src_file" "$dst_dir" >/dev/null 2>&1
sudo -u $user gzip -d "$dst_dir/$(basename $src_file)" >/dev/null 2>&1
rc=$?
fi
# Extracting bziped file
if [ ! -z "$(echo $src_file |grep -i '.bz')" ] && [ -z "$x" ]; then
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user mv $src_file $dst_dir >/dev/null 2>&1
sudo -u $user bzip2 -d $dst_dir/$(basename $src_file) >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user mv "$src_file" "$dst_dir"# >/dev/null 2>&1
sudo -u $user bzip2 -d "$dst_dir/$(basename $src_file)" >/dev/null 2>&1
rc=$?
fi
# Extracting ziped archive
if [ ! -z "$(echo $src_file |grep -i '.zip')" ]; then
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user unzip $src_file -d $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user unzip "$src_file" -d "$dst_dir" >/dev/null 2>&1
rc=$?
fi
# Extracting ziped archive
if [ ! -z "$(echo $src_file |grep -i '.7z')" ]; then
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user mv "$src_file" "$dst_dir" >/dev/null 2>&1
sudo -u $user p7zip -d "$src_file" >/dev/null 2>&1
rc=$?
fi
# Extracting tared archive
if [ ! -z "$(echo $src_file |grep -i '.tar')" ] && [ -z "$x" ]; then
x='yes'
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user tar -xf $src_file -C $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user tar -xf "$src_file" -C "$dst_dir" >/dev/null 2>&1
rc=$?
fi
# Extracting rared archive
if [ ! -z "$(echo $src_file |grep -i '.rar')" ]; then
sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1
sudo -u $user unrar $src_file $dst_dir >/dev/null 2>&1
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
sudo -u $user unrar "$src_file" "$dst_dir" >/dev/null 2>&1
rc=$?
fi
# Checking result
if [ $rc -ne 0 ]; then
echo "Error: $src_file was not extracted"
exit 3
fi
# Exiting
exit $rc
exit

View file

@ -15,20 +15,23 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking path
if [ ! -z "$path" ]; then
rpath=$(readlink -f "$path")
if [ -z "$(echo $rpath |grep $homedir)" ]; then
exit 1
echo "Error: invalid path $dst_dir"
exit 2
fi
else
path=$homedir

59
bin/v-move-fs-directory Executable file
View file

@ -0,0 +1,59 @@
#!/bin/bash
# info: move file
# options: USER SRC_DIRECTORY DST_DIRECTORY
#
# The function moved file or directory on the file system. This function
# can also be used to rename files just like normal mv command.
user=$1
src_dir=$2
dst_dir=$3
# Checking arguments
if [ -z "$dst_dir" ]; then
echo "Usage: USER SRC_DIRECTORY DST_DIRECTORY"
exit 1
fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source file
if [ ! -d "$src_dir" ]; then
echo "Error: source directory $src_dir doesn't exist"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
echo "Error: invalid source path $src_dir"
exit 2
fi
# Checking destination path
rpath=$(readlink -f "$dst_dir")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
echo "Error: invalid destination path $dst_dir"
exit 2
fi
# Moving directory
sudo -u $user mv "$src_dir" "$dst_dir" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: directory $src_dir was not moved"
exit 3
fi
# Exiting
exit

View file

@ -17,34 +17,43 @@ fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking source file
if [ ! -f "$src_file" ]; then
exit 1
echo "Error: source file $src_file doesn't exist"
exit 3
fi
# Checking source path
rpath=$(readlink -f "$src_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_file"
exit 2
fi
# Checking destination path
rpath=$(readlink -f "$dst_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid destination path $dst_file"
exit 2
fi
# Copying file
sudo -u $user mv $src_file $dst_file >/dev/null 2>&1
# Moving file
sudo -u $user mv "$src_file" "$dst_file" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: file $src_file was not moved"
exit 3
fi
# Exiting
exit $?
exit

View file

@ -5,35 +5,42 @@
# The function opens/reads files on the file system
user=$1
path=$2
src_file=$2
# Checking arguments
if [ -z "$path" ]; then
echo "Usage: USER PATH"
if [ -z "$src_file" ]; then
echo "Usage: USER FILE"
exit 1
fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
exit 1
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
exit 1
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking path
if [ ! -z "$path" ]; then
rpath=$(readlink -f "$path")
if [ ! -z "$src_file" ]; then
rpath=$(readlink -f "$src_file")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
exit 1
echo "Error: invalid source path $src_file"
exit 2
fi
fi
# Reading file
sudo -u $user cat "$path"
sudo -u $user cat "$src_file" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: file $src_file was not opened"
exit 3
fi
# Exiting
exit $?
exit