5 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
7 from allmydata.scripts.common_http import do_http, format_http_error
8 from allmydata.util.encodingutil import to_str
10 # this script is used for both 'mv' and 'ln'
12 def mv(options, mode="move"):
13 nodeurl = options['node-url']
14 aliases = options.aliases
15 from_file = options.from_file
16 to_file = options.to_file
17 stdout = options.stdout
18 stderr = options.stderr
20 if nodeurl[-1] != "/":
23 rootcap, from_path = get_alias(aliases, from_file, DEFAULT_ALIAS)
24 except UnknownAliasError, e:
27 from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
29 from_url += "/" + escape_path(from_path)
30 # figure out the source cap
31 resp = do_http("GET", from_url + "?t=json")
32 if not re.search(r'^2\d\d$', str(resp.status)):
33 print >>stderr, format_http_error("Error", resp)
36 nodetype, attrs = simplejson.loads(data)
37 cap = to_str(attrs.get("rw_uri") or attrs["ro_uri"])
41 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
42 except UnknownAliasError, e:
45 to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
47 to_url += "/" + escape_path(path)
49 if to_url.endswith("/"):
50 # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
51 to_url += escape_path(from_path[from_path.rfind("/")+1:])
53 to_url += "?t=uri&replace=only-files"
55 resp = do_http("PUT", to_url, cap)
57 if not re.search(r'^2\d\d$', str(status)):
59 print >>stderr, "Error: You can't overwrite a directory with a file"
61 print >>stderr, format_http_error("Error", resp)
63 print >>stderr, "NOT removing the original"
67 # now remove the original
68 resp = do_http("DELETE", from_url)
69 if not re.search(r'^2\d\d$', str(resp.status)):
70 print >>stderr, format_http_error("Error deleting original after move", resp)