v-copy-fs-file 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. # info: copy file
  3. # options: USER SRC_FILE DST_FLE
  4. #
  5. # The function copies file on the file system
  6. user=$1
  7. src_file=$2
  8. dst_file=$3
  9. # Checking arguments
  10. if [ -z "$dst_file" ]; then
  11. echo "Usage: USER SRC_FILE DST_FILE"
  12. exit 1
  13. fi
  14. # Checking vesta user
  15. if [ ! -e "$VESTA/data/users/$user" ]; then
  16. echo "Error: vesta user $user doesn't exist"
  17. exit 3
  18. fi
  19. # Checking user homedir
  20. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  21. if [ -z $homedir ]; then
  22. echo "Error: user home directory doesn't exist"
  23. exit 12
  24. fi
  25. # Checking source file
  26. if [ ! -f "$src_file" ]; then
  27. echo "Error: $src_file doesn't exist"
  28. exit 3
  29. fi
  30. # Checking source path
  31. rpath=$(readlink -f "$src_file")
  32. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  33. echo "Error: invalid source path $src_file"
  34. exit 2
  35. fi
  36. # Checking destination path
  37. rpath=$(readlink -f "$dst_file")
  38. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  39. echo "Error: ivalid destination path $dst_file"
  40. exit 2
  41. fi
  42. # Copying file
  43. sudo -u $user cp "$src_file" "$dst_file" >/dev/null 2>&1
  44. if [ $? -ne 0 ]; then
  45. echo "Error: file $src_file was not copied"
  46. exit 3
  47. fi
  48. # Exiting
  49. exit