5 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
7 from allmydata.scripts.common_http import do_http
9 # this script is used for both 'mv' and 'ln'
11 def mv(options, mode="move"):
12 nodeurl = options['node-url']
13 aliases = options.aliases
14 from_file = options.from_file
15 to_file = options.to_file
16 stdout = options.stdout
17 stderr = options.stderr
19 if nodeurl[-1] != "/":
22 rootcap, from_path = get_alias(aliases, from_file, DEFAULT_ALIAS)
23 except UnknownAliasError, e:
24 print >>stderr, "error: %s" % e.args[0]
26 from_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
28 from_url += "/" + escape_path(from_path)
29 # figure out the source cap
30 data = urllib.urlopen(from_url + "?t=json").read()
31 nodetype, attrs = simplejson.loads(data)
32 cap = attrs.get("rw_uri") or attrs["ro_uri"]
33 # simplejson sometimes returns unicode, but we know that it's really just
39 rootcap, path = get_alias(aliases, to_file, DEFAULT_ALIAS)
40 except UnknownAliasError, e:
41 print >>stderr, "error: %s" % e.args[0]
43 to_url = nodeurl + "uri/%s" % urllib.quote(rootcap)
45 to_url += "/" + escape_path(path)
47 if to_url.endswith("/"):
48 # "mv foo.txt bar/" == "mv foo.txt bar/foo.txt"
49 to_url += escape_path(from_path[from_path.rfind("/")+1:])
51 to_url += "?t=uri&replace=only-files"
53 resp = do_http("PUT", to_url, cap)
55 if not re.search(r'^2\d\d$', str(status)):
57 print >>stderr, "Error: You can't overwrite a directory with a file"
59 print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
60 print >>stderr, resp.read()
62 print >>stderr, "NOT removing the original"
66 # now remove the original
67 resp = do_http("DELETE", from_url)
68 if not re.search(r'^2\d\d$', str(status)):
69 print >>stderr, "error, got %s %s" % (resp.status, resp.reason)
70 print >>stderr, resp.read()