Commit f2d727f6 authored by Vitaly Lipatov's avatar Vitaly Lipatov

eget: add --force/--allow-overwrite, error if file exists without flags

parent 9c9a4c19
...@@ -353,6 +353,8 @@ WGETNODIRECTORIES='' ...@@ -353,6 +353,8 @@ WGETNODIRECTORIES=''
WGETCONTINUE='' WGETCONTINUE=''
CURLCONTINUE='' CURLCONTINUE=''
ARIA2CONTINUE='' ARIA2CONTINUE=''
CONTINUE=''
FORCEOVERWRITE=''
WGETTIMEOUT='' WGETTIMEOUT=''
CURLMAXTIME='' CURLMAXTIME=''
WGETREADTIMEOUT='' WGETREADTIMEOUT=''
...@@ -431,6 +433,7 @@ Options: ...@@ -431,6 +433,7 @@ Options:
-nd|--no-directories - do not create a hierarchy of directories when retrieving recursively -nd|--no-directories - do not create a hierarchy of directories when retrieving recursively
--no-glob - turn off file name globbing --no-glob - turn off file name globbing
-c|--continue - continue getting a partially-downloaded file -c|--continue - continue getting a partially-downloaded file
--force|--allow-overwrite - force overwrite existing file
-N|--timestamping - only download if remote file is newer than local -N|--timestamping - only download if remote file is newer than local
-i|--input-file FILE - read URLs from FILE, one per line; each line can contain multiple mirror URLs (use - for stdin) -i|--input-file FILE - read URLs from FILE, one per line; each line can contain multiple mirror URLs (use - for stdin)
-T|--timeout=N - set the network timeout to N seconds -T|--timeout=N - set the network timeout to N seconds
...@@ -595,11 +598,15 @@ while [ -n "$1" ] ; do ...@@ -595,11 +598,15 @@ while [ -n "$1" ] ; do
NOGLOB="--no-glob" NOGLOB="--no-glob"
;; ;;
-c|--continue) -c|--continue)
CONTINUE=1
WGETCONTINUE="$1" WGETCONTINUE="$1"
CURLCONTINUE="-C -" CURLCONTINUE="-C -"
ARIA2CONTINUE="--continue=true" ARIA2CONTINUE="--continue=true"
AXELCONTINUE="" AXELCONTINUE=""
;; ;;
--force|--allow-overwrite)
FORCEOVERWRITE=1
;;
-N|--timestamping) -N|--timestamping)
TIMESTAMPING="1" TIMESTAMPING="1"
WGETTIMESTAMPING="-N" WGETTIMESTAMPING="-N"
...@@ -1862,6 +1869,21 @@ sget() ...@@ -1862,6 +1869,21 @@ sget()
return return
fi fi
# Check if target file exists (unless --force, -c, or -N is used)
if [ -n "$TARGET" ] && [ -f "$TARGET" ] ; then
if [ -n "$TIMESTAMPING" ] ; then
# -N: handled by backend timestamping logic
:
elif [ -n "$CONTINUE" ] ; then
# -c: pass to backend for resume
:
elif [ -n "$FORCEOVERWRITE" ] ; then
# --force: will overwrite
:
else
fatal "File '$TARGET' already exists. Use --force to overwrite, -c to continue, or -N for timestamping."
fi
fi
#if is_strange_url "$REALURL" ; then #if is_strange_url "$REALURL" ; then
# info "Just download strange URL $REALURL, skipping IPFS" # info "Just download strange URL $REALURL, skipping IPFS"
...@@ -1893,6 +1915,12 @@ sget() ...@@ -1893,6 +1915,12 @@ sget()
if [ -z "$TARGET" ] ; then if [ -z "$TARGET" ] ; then
TARGET="$CID" TARGET="$CID"
fi fi
# Check if target file exists (TARGET was just determined from CID)
if [ -f "$TARGET" ] ; then
if [ -z "$TIMESTAMPING" ] && [ -z "$CONTINUE" ] && [ -z "$FORCEOVERWRITE" ] ; then
fatal "File '$TARGET' already exists. Use --force to overwrite, -c to continue, or -N for timestamping."
fi
fi
fi fi
[ "$URL" = "$REALURL" ] && info "$URL -> $CID -> $TARGET" || info "$URL -> $REALURL -> $CID -> $TARGET" [ "$URL" = "$REALURL" ] && info "$URL -> $CID -> $TARGET" || info "$URL -> $REALURL -> $CID -> $TARGET"
ipfs_get "$CID" "$TARGET" && return ipfs_get "$CID" "$TARGET" && return
...@@ -1907,6 +1935,12 @@ sget() ...@@ -1907,6 +1935,12 @@ sget()
local FN="$(url_get_filename "$REALURL")" || return local FN="$(url_get_filename "$REALURL")" || return
if [ -z "$TARGET" ] ; then if [ -z "$TARGET" ] ; then
TARGET="$FN" TARGET="$FN"
# Check if target file exists (TARGET was just determined from URL)
if [ -f "$TARGET" ] ; then
if [ -z "$TIMESTAMPING" ] && [ -z "$CONTINUE" ] && [ -z "$FORCEOVERWRITE" ] ; then
fatal "File '$TARGET' already exists. Use --force to overwrite, -c to continue, or -N for timestamping."
fi
fi
fi fi
if [ -n "$GETIPFSCID" ] ; then if [ -n "$GETIPFSCID" ] ; then
...@@ -1968,16 +2002,42 @@ scat() ...@@ -1968,16 +2002,42 @@ scat()
sget() sget()
{ {
local URL="$1"
local TARGET="$2"
if [ -n "$GETFILENAME" ] ; then if [ -n "$GETFILENAME" ] ; then
get_filename "$1" get_filename "$URL"
exit exit
fi fi
if [ -n "$GETREALURL" ] ; then if [ -n "$GETREALURL" ] ; then
get_real_url "$1" get_real_url "$URL"
exit exit
fi fi
# Skip check for stdout
if [ "$TARGET" = "/dev/stdout" ] || [ "$TARGET" = "-" ] ; then
url_sget "$@"
return
fi
# Check if target file exists (unless --force, -c, or -N is used)
# When TARGET is not specified, determine it from URL
local TARGET_FROM_URL=""
if [ -z "$TARGET" ] ; then
TARGET_FROM_URL="$(url_get_filename "$URL")"
TARGET="$TARGET_FROM_URL"
fi
if [ -n "$TARGET" ] && [ -f "$TARGET" ] ; then
if [ -z "$TIMESTAMPING" ] && [ -z "$CONTINUE" ] && [ -z "$FORCEOVERWRITE" ] ; then
fatal "File '$TARGET' already exists. Use --force to overwrite, -c to continue, or -N for timestamping."
fi
# With --force and no explicit -O, delete file so wget won't create .1
if [ -n "$FORCEOVERWRITE" ] && [ -n "$TARGET_FROM_URL" ] ; then
rm -f "$TARGET"
fi
fi
url_sget "$@" url_sget "$@"
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment