2021-06-03 09:28:10 -04:00
|
|
|
"""
|
|
|
|
Ported to Python 3.
|
|
|
|
"""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
2019-03-24 14:09:10 +01:00
|
|
|
from __future__ import print_function
|
2007-10-11 20:31:48 -07:00
|
|
|
|
2021-06-03 09:28:10 -04:00
|
|
|
from future.utils import PY2
|
|
|
|
if PY2:
|
|
|
|
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
|
2021-05-03 11:13:27 -04:00
|
|
|
|
2007-10-11 20:31:48 -07:00
|
|
|
import re
|
2021-04-26 09:46:21 -04:00
|
|
|
from urllib.parse import quote as url_quote
|
2017-01-19 15:39:53 -07:00
|
|
|
import json
|
2010-02-10 18:43:18 -08:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
|
|
|
|
UnknownAliasError
|
2010-06-06 18:02:15 -07:00
|
|
|
from allmydata.scripts.common_http import do_http, format_http_error
|
2020-08-14 13:49:48 -04:00
|
|
|
from allmydata.util.encodingutil import to_bytes
|
2007-10-11 20:31:48 -07:00
|
|
|
|
2008-05-20 13:30:31 -07:00
|
|
|
# this script is used for both 'mv' and 'ln'
|
|
|
|
|
2008-08-01 11:46:24 -07:00
|
|
|
def mv(options, mode="move"):
|
|
|
|
nodeurl = options['node-url']
|
|
|
|
aliases = options.aliases
|
|
|
|
from_file = options.from_file
|
|
|
|
to_file = options.to_file
|
|
|
|
stdout = options.stdout
|
|
|
|
stderr = options.stderr
|
|
|
|
|
2007-10-11 20:31:48 -07:00
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2010-02-10 18:43:18 -08:00
|
|
|
try:
|
|
|
|
rootcap, from_path = get_alias(aliases, from_file, DEFAULT_ALIAS)
|
2019-03-28 12:45:28 +01:00
|
|
|
except UnknownAliasError as e:
|
2010-06-06 18:02:15 -07:00
|
|
|
e.display(stderr)
|
2010-02-10 18:43:18 -08:00
|
|
|
return 1
|
2021-06-03 09:28:10 -04:00
|
|
|
from_path = str(from_path, "utf-8")
|
2021-04-26 09:46:21 -04:00
|
|
|
from_url = nodeurl + "uri/%s" % url_quote(rootcap)
|
2009-07-19 20:45:23 -07:00
|
|
|
if from_path:
|
|
|
|
from_url += "/" + escape_path(from_path)
|
2008-05-19 19:28:50 -07:00
|
|
|
# figure out the source cap
|
2010-11-09 16:53:36 -08:00
|
|
|
resp = do_http("GET", from_url + "?t=json")
|
|
|
|
if not re.search(r'^2\d\d$', str(resp.status)):
|
2019-03-24 14:09:10 +01:00
|
|
|
print(format_http_error("Error", resp), file=stderr)
|
2010-11-09 16:53:36 -08:00
|
|
|
return 1
|
|
|
|
data = resp.read()
|
2017-01-19 15:39:53 -07:00
|
|
|
nodetype, attrs = json.loads(data)
|
2020-08-14 13:49:48 -04:00
|
|
|
cap = to_bytes(attrs.get("rw_uri") or attrs["ro_uri"])
|
2008-05-19 19:28:50 -07:00
|
|
|
|
|
|
|
# now get the target
|
2010-02-10 18:43:18 -08:00
|
|
|
try:
|
|
|
|
rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
|
2019-03-28 12:45:28 +01:00
|
|
|
except UnknownAliasError as e:
|
2010-06-06 18:02:15 -07:00
|
|
|
e.display(stderr)
|
2010-02-10 18:43:18 -08:00
|
|
|
return 1
|
2021-04-26 09:46:21 -04:00
|
|
|
to_url = nodeurl + "uri/%s" % url_quote(rootcap)
|
2021-06-03 09:28:10 -04:00
|
|
|
path = str(path, "utf-8")
|
2008-05-19 19:28:50 -07:00
|
|
|
if path:
|
|
|
|
to_url += "/" + escape_path(path)
|
2009-07-19 20:45:23 -07:00
|
|
|
|
|
|
|
if to_url.endswith("/"):
|
2008-05-19 19:28:50 -07:00
|
|
|
# "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
|
2009-07-19 20:45:23 -07:00
|
|
|
to_url += escape_path(from_path[from_path.rfind("/")+1:])
|
|
|
|
|
|
|
|
to_url += "?t=uri&replace=only-files"
|
2008-05-19 19:28:50 -07:00
|
|
|
|
|
|
|
resp = do_http("PUT", to_url, cap)
|
2007-10-11 20:31:48 -07:00
|
|
|
status = resp.status
|
|
|
|
if not re.search(r'^2\d\d$', str(status)):
|
2009-07-19 20:45:23 -07:00
|
|
|
if status == 409:
|
2019-03-24 14:09:10 +01:00
|
|
|
print("Error: You can't overwrite a directory with a file", file=stderr)
|
2009-07-19 20:45:23 -07:00
|
|
|
else:
|
2019-03-24 14:09:10 +01:00
|
|
|
print(format_http_error("Error", resp), file=stderr)
|
2009-07-19 20:45:23 -07:00
|
|
|
if mode == "move":
|
2019-03-24 14:09:10 +01:00
|
|
|
print("NOT removing the original", file=stderr)
|
2010-06-06 18:02:15 -07:00
|
|
|
return 1
|
2007-10-11 20:31:48 -07:00
|
|
|
|
2008-05-20 13:30:31 -07:00
|
|
|
if mode == "move":
|
|
|
|
# now remove the original
|
|
|
|
resp = do_http("DELETE", from_url)
|
2010-11-09 17:09:16 -08:00
|
|
|
if not re.search(r'^2\d\d$', str(resp.status)):
|
2019-03-24 14:09:10 +01:00
|
|
|
print(format_http_error("Error deleting original after move", resp), file=stderr)
|
2010-06-06 18:02:15 -07:00
|
|
|
return 2
|
2007-10-11 20:31:48 -07:00
|
|
|
|
2019-03-24 14:09:10 +01:00
|
|
|
print("OK", file=stdout)
|
2010-06-06 18:02:15 -07:00
|
|
|
return 0
|