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.stringutils 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 data = urllib.urlopen(from_url + "?t=json").read()
32 nodetype, attrs = simplejson.loads(data)
33 cap = to_str(attrs.get("rw_uri") or attrs["ro_uri"])
37 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
38 except UnknownAliasError, e:
41 to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
43 to_url += "/" + escape_path(path)
45 if to_url.endswith("/"):
46 # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
47 to_url += escape_path(from_path[from_path.rfind("/")+1:])
49 to_url += "?t=uri&replace=only-files"
51 resp = do_http("PUT", to_url, cap)
53 if not re.search(r'^2\d\d$', str(status)):
55 print >>stderr, "Error: You can't overwrite a directory with a file"
57 print >>stderr, format_http_error("Error", resp)
59 print >>stderr, "NOT removing the original"
63 # now remove the original
64 resp = do_http("DELETE", from_url)
65 if not re.search(r'^2\d\d$', str(status)):
66 print >>stderr, format_http_error("Error deleting original after move", resp)