Update 2024-12-23 18:21 OpenBSD/amd64-t14

This commit is contained in:
c0dev0id 2024-12-23 18:21:09 +01:00
parent ac58f6baa9
commit 1bb57985ca

View File

@ -3,6 +3,10 @@
# manage files in a webroot # manage files in a webroot
# needs: sh (cp,rm), openssh (ssh,scp), gzip, tar, sed, tail, xclip # needs: sh (cp,rm), openssh (ssh,scp), gzip, tar, sed, tail, xclip
###
### CONFIGURATION
###
# remote patch uploaded files should go # remote patch uploaded files should go
_rpath="/home/www/htdocs/ptrace/paste" _rpath="/home/www/htdocs/ptrace/paste"
# web url where the uploaded files can be accessed # web url where the uploaded files can be accessed
@ -10,33 +14,56 @@ _weburi="https://ptrace.org"
# ssh user@host # ssh user@host
_sshhost="sdk@codevoid.de" _sshhost="sdk@codevoid.de"
###
### SET ARGUMENT SWITCHES
###
for arg in "$@" for arg in "$@"
do do
case "$arg" in case "$arg" in
# extensions
sh) _ext=".sh" ;; sh) _ext=".sh" ;;
ksh) _ext=".ksh" ;; ksh) _ext=".ksh" ;;
txt) _ext=".txt" ;; txt) _ext=".txt" ;;
log) _ext=".log" ;; log) _ext=".log" ;;
# archive mode
gz) _gz=1 ;; gz) _gz=1 ;;
tgz) _tgz=1 ;; tgz) _tgz=1 ;;
# commands
rm) _rm=1 ;; rm) _rm=1 ;;
rml) _rmlast=1 ;; rml) _rmlast=1 ;;
ls) _ls=1 ;; ls) _ls=1 ;;
l|last) _last="-1" ;; l|last) _last="-1" ;;
-[0-9]*) _last="$arg" ;; -[0-9]*) _last="$arg" ;;
fixl) _fixperm=1 ;;
ren|mv) _mv=1 ;; ren|mv) _mv=1 ;;
renl|mvl) _mvlast=1 ;; renl|mvl) _mvlast=1 ;;
-h) ;; # swallow
*) break; ;; *) break; ;;
esac esac
shift; shift;
done done
###
### FUNCTIONS
###
# fetches remote file list
remote_list() { remote_list() {
ssh $_sshhost \ ssh $_sshhost \
"cd $_rpath/ \ "cd $_rpath/ \
&& ls -1tr" && ls -1tr"
} }
# formats file list output
remote_list_print() {
while read f
do
echo "$_weburi/$(echo "$f" \
| sed 's/ /%20/g') ($f)"
done
}
copy_and_print() { copy_and_print() {
echo "$_weburi/$(basename "$1")" \ echo "$_weburi/$(basename "$1")" \
| sed 's/ /%20/g' \ | sed 's/ /%20/g' \
@ -44,7 +71,12 @@ copy_and_print() {
echo echo
} }
# handle stdin ###
### MAIN PROGRAM
###
# HANDLE CASE: "STDIN"
# do: upload with provided or generated filename
if [[ ! -t 0 ]] if [[ ! -t 0 ]]
then then
fname="$1" fname="$1"
@ -55,27 +87,24 @@ then
exit 0 exit 0
fi fi
# HANDLE CASE: "list all remote files"
if [ -n "$_ls" ] if [ -n "$_ls" ]
then then
remote_list \ remote_list \
| while read f | remote_list_print
do
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
done
exit 0 exit 0
fi fi
# HANDLE CASE: "list last N files"
if [ -n "$_last" ] if [ -n "$_last" ]
then then
remote_list \ remote_list \
| tail $_last \ | tail $_last \
| while read f | remote_list_print
do
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
done
exit 0 exit 0
fi fi
# HANDLE CASE: rename remote file
if [ -n "$_mv" ] if [ -n "$_mv" ]
then then
ssh $_sshhost \ ssh $_sshhost \
@ -84,6 +113,7 @@ then
exit 0 exit 0
fi fi
# HANDLE CASE: rename the last uploaded remote file
if [ -n "$_mvlast" ] if [ -n "$_mvlast" ]
then then
lastfile="$(remote_list | tail -1)" lastfile="$(remote_list | tail -1)"
@ -93,6 +123,7 @@ then
exit 0 exit 0
fi fi
# HANDLE CASE: delete remote file
if [ -n "$_rm" ] if [ -n "$_rm" ]
then then
for file in "$@" for file in "$@"
@ -105,50 +136,59 @@ then
exit 0 exit 0
fi fi
# HANDLE CASE: delete last uploaded remote file
if [ -n "$_rmlast" ] if [ -n "$_rmlast" ]
then then
lastfile="$(remote_list | tail -1)" lastfile="$(remote_list | tail -1)"
echo "rm $lastfile"
ssh $_sshhost \ ssh $_sshhost \
"cd $_rpath/ \ "cd $_rpath/ \
&& rm -v \"$lastfile\"" \ && rm -f \"$lastfile\"" \
|| true || true
exit 0 exit 0
fi fi
# HANDLE CASE: fix permission of last file
if [ -n "$_fixperm" ]
then
lastfile="$(remote_list | tail -1)"
echo "Fixing: $lastfile"
ssh $_sshhost "cd $_rpath/ \
&& chmod u+rw,g+r \"$lastfile\"; \
ls -lh \"$lastfile\""
exit 0
fi
# HANDLE CASE: upload of files and folders
if [ $# -gt 0 ] if [ $# -gt 0 ]
then then
for item in "$@" for item in "$@"
do do
# the whole handling shall run in a subshell so all variables # the whole handling shall run in a subshell so all variables
# are reset when we leave the scope # are reset when we leave the scope. Note, that this requires
# note, that this requires us to return, instead fo continue # us to "return", instead of "continue" skip to the next loop
# to leave the loop iteration # iteration
( (
[ ! -e "$item" ] \ [ ! -e "$item" ] \
&& echo "File or directory not found: $item" \ && echo "File or directory not found: $item" \
&& exit 1 && return
# target filename # target filename
file="$(basename "$item")$_ext" file="$(basename "$item")$_ext"
# handle file (plain), no archive mode # CASE "file" "no archive"
if [ -f "$item" ] && [ -z "$_tgz" ] && [ -z "$_gz" ] if [ -f "$item" ] && [ -z "$_tgz" ] && [ -z "$_gz" ]
then then
# make sure the file is least owner rw and group r
# the sticky bit in the destiation folder will assign user+group
chmod u+rw,g+r "$item"
echo "Uploading $item as $file" echo "Uploading $item as $file"
scp -q "$item" "$_sshhost:\"$_rpath/$file\"" scp -q "$item" "$_sshhost:\"$_rpath/$file\""
copy_and_print "$file" copy_and_print "$file"
return return
fi fi
# directories can only be handled as tgz archive # CASE "folder"
[ -d "$item" ] && _tgz=1 [ -d "$item" ] && _tgz=1
# handle file or directory (tgz mode) # CASE "tgz archive mode"
if [ -n "$_tgz" ] if [ -n "$_tgz" ]
then then
file="$file.tgz" file="$file.tgz"
@ -161,7 +201,7 @@ then
return return
fi fi
# handle file (gz mode) # CASE "gz archive mode"
if [ -f "$item" ] && [ -n "$_gz" ] if [ -f "$item" ] && [ -n "$_gz" ]
then then
file="$file.gz" file="$file.gz"
@ -179,24 +219,28 @@ then
done done
fi fi
# USAGE
if [ $# -eq 0 ] if [ $# -eq 0 ]
then then
echo "usage: upload [command] [ext] [<files>]" echo "usage: upload [command] [ext] [<files>]"
echo " upload [filename] < file" echo " upload [filename] < file"
echo " cat file | upload [filename]" echo " cat file | upload [filename]"
echo echo
echo " <files> - files or directories to upload" echo " <files> - files or directories to upload"
echo echo
echo " Commands:" echo " Commands:"
echo " rm <files> - remove files" echo " rm <files> - remove files"
echo " rml - remove last upload" echo " rml - remove last upload"
echo " ls - list files" echo " ls - list files"
echo " -0..N - list N last uploads" echo " l|last - list last file"
echo " ren <oldname> <newname> - rename file" echo " -0..N - list N last files"
echo " renl <newname> - rename last uploaded file" echo " mv|ren <oldname> <newname> - rename file"
echo " mvl|renl <newname> - rename last uploaded file"
echo " fixl - fix permission on last uploaded file"
echo echo
echo " Extensions:" echo " Extensions:"
echo " sh, ksh, txt, log - add extension to file" echo " sh, ksh, txt, log - add extension to file"
echo " gz, tgz - uploads file as archive (tgz is always used for folders)"
echo echo
echo "Special Case:" echo "Special Case:"
echo " When data is piped to the script it expects only one" echo " When data is piped to the script it expects only one"