5 from cStringIO import StringIO
6 from twisted.python.failure import Failure
7 from allmydata.scripts.common import get_alias, escape_path, \
8 DefaultAliasMarker, TahoeError
9 from allmydata.scripts.common_http import do_http, HTTPError
10 from allmydata import uri
11 from allmydata.util import fileutil
12 from allmydata.util.fileutil import abspath_expanduser_unicode, precondition_abspath
13 from allmydata.util.encodingutil import unicode_to_url, listdir_unicode, quote_output, \
14 quote_local_unicode_path, to_str
15 from allmydata.util.assertutil import precondition
18 class MissingSourceError(TahoeError):
19 def __init__(self, name, quotefn=quote_output):
20 TahoeError.__init__(self, "No such file or directory %s" % quotefn(name))
24 resp = do_http("GET", url)
25 if resp.status == 200:
27 raise HTTPError("Error during GET", resp)
29 def GET_to_string(url):
34 resp = do_http("PUT", url, data)
35 if resp.status in (200, 201):
37 raise HTTPError("Error during PUT", resp)
40 resp = do_http("POST", url, data)
41 if resp.status in (200, 201):
43 raise HTTPError("Error during POST", resp)
46 url = targeturl + "?t=mkdir"
47 resp = do_http("POST", url)
48 if resp.status in (200, 201):
49 return resp.read().strip()
50 raise HTTPError("Error during mkdir", resp)
52 def make_tahoe_subdirectory(nodeurl, parent_writecap, name):
53 url = nodeurl + "/".join(["uri",
54 urllib.quote(parent_writecap),
55 urllib.quote(unicode_to_url(name)),
57 resp = do_http("POST", url)
58 if resp.status in (200, 201):
59 return resp.read().strip()
60 raise HTTPError("Error during mkdir", resp)
63 class LocalFileSource:
64 def __init__(self, pathname, basename):
65 precondition_abspath(pathname)
66 self.pathname = pathname
67 self._basename = basename
72 def need_to_copy_bytes(self):
75 def open(self, caps_only):
76 return open(self.pathname, "rb")
78 class LocalFileTarget:
79 def __init__(self, pathname):
80 precondition_abspath(pathname)
81 self.pathname = pathname
83 def put_file(self, inf):
84 fileutil.put_file(self.pathname, inf)
86 class LocalMissingTarget:
87 def __init__(self, pathname):
88 precondition_abspath(pathname)
89 self.pathname = pathname
91 def put_file(self, inf):
92 fileutil.put_file(self.pathname, inf)
94 class LocalDirectorySource:
95 def __init__(self, progressfunc, pathname, basename):
96 precondition_abspath(pathname)
98 self.progressfunc = progressfunc
99 self.pathname = pathname
101 self._basename = basename
104 return self._basename
106 def populate(self, recurse):
107 if self.children is not None:
110 children = listdir_unicode(self.pathname)
111 for i,n in enumerate(children):
112 self.progressfunc("examining %d of %d" % (i+1, len(children)))
113 pn = os.path.join(self.pathname, n)
114 if os.path.isdir(pn):
115 child = LocalDirectorySource(self.progressfunc, pn, n)
116 self.children[n] = child
119 elif os.path.isfile(pn):
120 self.children[n] = LocalFileSource(pn, n)
122 # Could be dangling symlink; probably not copy-able.
123 # TODO: output a warning
126 class LocalDirectoryTarget:
127 def __init__(self, progressfunc, pathname):
128 precondition_abspath(pathname)
130 self.progressfunc = progressfunc
131 self.pathname = pathname
134 def populate(self, recurse):
135 if self.children is not None:
138 children = listdir_unicode(self.pathname)
139 for i,n in enumerate(children):
140 self.progressfunc("examining %d of %d" % (i+1, len(children)))
141 pn = os.path.join(self.pathname, n)
142 if os.path.isdir(pn):
143 child = LocalDirectoryTarget(self.progressfunc, pn)
144 self.children[n] = child
148 assert os.path.isfile(pn)
149 self.children[n] = LocalFileTarget(pn)
151 def get_child_target(self, name):
152 precondition(isinstance(name, unicode), name)
153 if self.children is None:
155 if name in self.children:
156 return self.children[name]
157 pathname = os.path.join(self.pathname, name)
158 os.makedirs(pathname)
159 return LocalDirectoryTarget(self.progressfunc, pathname)
161 def put_file(self, name, inf):
162 precondition(isinstance(name, unicode), name)
163 pathname = os.path.join(self.pathname, name)
164 fileutil.put_file(pathname, inf)
166 def set_children(self):
170 class TahoeFileSource:
171 def __init__(self, nodeurl, mutable, writecap, readcap, basename):
172 self.nodeurl = nodeurl
173 self.mutable = mutable
174 self.writecap = writecap
175 self.readcap = readcap
176 self._basename = basename # unicode, or None for raw filecaps
179 return self._basename
181 def need_to_copy_bytes(self):
186 def open(self, caps_only):
188 return StringIO(self.readcap)
189 url = self.nodeurl + "uri/" + urllib.quote(self.readcap)
190 return GET_to_file(url)
193 return self.writecap or self.readcap
195 class TahoeFileTarget:
196 def __init__(self, nodeurl, mutable, writecap, readcap, url):
197 self.nodeurl = nodeurl
198 self.mutable = mutable
199 self.writecap = writecap
200 self.readcap = readcap
203 def put_file(self, inf):
204 # We want to replace this object in-place.
206 # our do_http() call currently requires a string or a filehandle with
208 if not hasattr(inf, "seek"):
211 # TODO: this always creates immutable files. We might want an option
212 # to always create mutable files, or to copy mutable files into new
213 # mutable files. ticket #835
215 class TahoeDirectorySource:
216 def __init__(self, nodeurl, cache, progressfunc, basename):
217 self.nodeurl = nodeurl
219 self.progressfunc = progressfunc
220 self._basename = basename # unicode, or None for raw dircaps
223 return self._basename
225 def init_from_grid(self, writecap, readcap):
226 self.writecap = writecap
227 self.readcap = readcap
228 bestcap = writecap or readcap
229 url = self.nodeurl + "uri/%s" % urllib.quote(bestcap)
230 resp = do_http("GET", url + "?t=json")
231 if resp.status != 200:
232 raise HTTPError("Error examining source directory", resp)
233 parsed = simplejson.loads(resp.read())
235 assert nodetype == "dirnode"
236 self.mutable = d.get("mutable", False) # older nodes don't provide it
237 self.children_d = dict( [(unicode(name),value)
239 in d["children"].iteritems()] )
242 def init_from_parsed(self, parsed):
244 self.writecap = to_str(d.get("rw_uri"))
245 self.readcap = to_str(d.get("ro_uri"))
246 self.mutable = d.get("mutable", False) # older nodes don't provide it
247 self.children_d = dict( [(unicode(name),value)
249 in d["children"].iteritems()] )
252 def populate(self, recurse):
253 if self.children is not None:
256 for i,(name, data) in enumerate(self.children_d.items()):
257 self.progressfunc("examining %d of %d" % (i+1, len(self.children_d)))
258 if data[0] == "filenode":
259 mutable = data[1].get("mutable", False)
260 writecap = to_str(data[1].get("rw_uri"))
261 readcap = to_str(data[1].get("ro_uri"))
262 self.children[name] = TahoeFileSource(self.nodeurl, mutable,
263 writecap, readcap, name)
264 elif data[0] == "dirnode":
265 writecap = to_str(data[1].get("rw_uri"))
266 readcap = to_str(data[1].get("ro_uri"))
267 if writecap and writecap in self.cache:
268 child = self.cache[writecap]
269 elif readcap and readcap in self.cache:
270 child = self.cache[readcap]
272 child = TahoeDirectorySource(self.nodeurl, self.cache,
273 self.progressfunc, name)
274 child.init_from_grid(writecap, readcap)
276 self.cache[writecap] = child
278 self.cache[readcap] = child
281 self.children[name] = child
283 # TODO: there should be an option to skip unknown nodes.
284 raise TahoeError("Cannot copy unknown nodes (ticket #839). "
285 "You probably need to use a later version of "
286 "Tahoe-LAFS to copy this directory.")
288 class TahoeMissingTarget:
289 def __init__(self, url):
292 def put_file(self, inf):
293 # We want to replace this object in-place.
294 if not hasattr(inf, "seek"):
297 # TODO: this always creates immutable files. We might want an option
298 # to always create mutable files, or to copy mutable files into new
301 def put_uri(self, filecap):
302 # I'm not sure this will always work
303 return PUT(self.url + "?t=uri", filecap)
305 class TahoeDirectoryTarget:
306 def __init__(self, nodeurl, cache, progressfunc):
307 self.nodeurl = nodeurl
309 self.progressfunc = progressfunc
310 self.new_children = {}
312 def init_from_parsed(self, parsed):
314 self.writecap = to_str(d.get("rw_uri"))
315 self.readcap = to_str(d.get("ro_uri"))
316 self.mutable = d.get("mutable", False) # older nodes don't provide it
317 self.children_d = dict( [(unicode(name),value)
319 in d["children"].iteritems()] )
322 def init_from_grid(self, writecap, readcap):
323 self.writecap = writecap
324 self.readcap = readcap
325 bestcap = writecap or readcap
326 url = self.nodeurl + "uri/%s" % urllib.quote(bestcap)
327 resp = do_http("GET", url + "?t=json")
328 if resp.status != 200:
329 raise HTTPError("Error examining target directory", resp)
330 parsed = simplejson.loads(resp.read())
332 assert nodetype == "dirnode"
333 self.mutable = d.get("mutable", False) # older nodes don't provide it
334 self.children_d = dict( [(unicode(name),value)
336 in d["children"].iteritems()] )
339 def just_created(self, writecap):
340 self.writecap = writecap
341 self.readcap = uri.from_string(writecap).get_readonly().to_string()
346 def populate(self, recurse):
347 if self.children is not None:
350 for i,(name, data) in enumerate(self.children_d.items()):
351 self.progressfunc("examining %d of %d" % (i+1, len(self.children_d)))
352 if data[0] == "filenode":
353 mutable = data[1].get("mutable", False)
354 writecap = to_str(data[1].get("rw_uri"))
355 readcap = to_str(data[1].get("ro_uri"))
358 url = self.nodeurl + "/".join(["uri",
359 urllib.quote(self.writecap),
360 urllib.quote(unicode_to_url(name))])
361 self.children[name] = TahoeFileTarget(self.nodeurl, mutable,
362 writecap, readcap, url)
363 elif data[0] == "dirnode":
364 writecap = to_str(data[1].get("rw_uri"))
365 readcap = to_str(data[1].get("ro_uri"))
366 if writecap and writecap in self.cache:
367 child = self.cache[writecap]
368 elif readcap and readcap in self.cache:
369 child = self.cache[readcap]
371 child = TahoeDirectoryTarget(self.nodeurl, self.cache,
373 child.init_from_grid(writecap, readcap)
375 self.cache[writecap] = child
377 self.cache[readcap] = child
380 self.children[name] = child
382 # TODO: there should be an option to skip unknown nodes.
383 raise TahoeError("Cannot copy unknown nodes (ticket #839). "
384 "You probably need to use a later version of "
385 "Tahoe-LAFS to copy this directory.")
387 def get_child_target(self, name):
388 # return a new target for a named subdirectory of this dir
389 precondition(isinstance(name, unicode), name)
390 if self.children is None:
392 if name in self.children:
393 return self.children[name]
394 writecap = make_tahoe_subdirectory(self.nodeurl, self.writecap, name)
395 child = TahoeDirectoryTarget(self.nodeurl, self.cache,
397 child.just_created(writecap)
398 self.children[name] = child
401 def put_file(self, name, inf):
402 precondition(isinstance(name, unicode), name)
403 url = self.nodeurl + "uri"
404 if not hasattr(inf, "seek"):
407 if self.children is None:
410 # Check to see if we already have a mutable file by this name.
411 # If so, overwrite that file in place.
412 if name in self.children and self.children[name].mutable:
413 self.children[name].put_file(inf)
415 filecap = PUT(url, inf)
416 # TODO: this always creates immutable files. We might want an option
417 # to always create mutable files, or to copy mutable files into new
419 self.new_children[name] = filecap
421 def put_uri(self, name, filecap):
422 precondition(isinstance(name, unicode), name)
423 self.new_children[name] = filecap
425 def set_children(self):
426 if not self.new_children:
428 url = (self.nodeurl + "uri/" + urllib.quote(self.writecap)
431 for (name, filecap) in self.new_children.items():
432 # it just so happens that ?t=set_children will accept both file
433 # read-caps and write-caps as ['rw_uri'], and will handle either
434 # correctly. So don't bother trying to figure out whether the one
435 # we have is read-only or read-write.
436 # TODO: think about how this affects forward-compatibility for
438 set_data[name] = ["filenode", {"rw_uri": filecap}]
439 body = simplejson.dumps(set_data)
442 FileSources = (LocalFileSource, TahoeFileSource)
443 DirectorySources = (LocalDirectorySource, TahoeDirectorySource)
444 FileTargets = (LocalFileTarget, TahoeFileTarget)
445 DirectoryTargets = (LocalDirectoryTarget, TahoeDirectoryTarget)
446 MissingTargets = (LocalMissingTarget, TahoeMissingTarget)
450 def do_copy(self, options, progressfunc=None):
453 elif options['verbose']:
458 nodeurl = options['node-url']
459 if nodeurl[-1] != "/":
461 self.nodeurl = nodeurl
462 self.progressfunc = progressfunc
463 self.options = options
464 self.aliases = options.aliases
465 self.verbosity = verbosity
466 self.stdout = options.stdout
467 self.stderr = options.stderr
468 if verbosity >= 2 and not self.progressfunc:
469 def progress(message):
470 print >>self.stderr, message
471 self.progressfunc = progress
472 self.caps_only = options["caps-only"]
475 status = self.try_copy()
477 except TahoeError, te:
479 Failure().printTraceback(self.stderr)
481 te.display(self.stderr)
485 source_specs = self.options.sources
486 destination_spec = self.options.destination
487 recursive = self.options["recursive"]
489 target = self.get_target_info(destination_spec)
491 sources = [] # list of source objects
492 for ss in source_specs:
493 sources.append(self.get_source_info(ss))
495 have_source_dirs = any([isinstance(s, DirectorySources)
498 if have_source_dirs and not recursive:
499 self.to_stderr("cannot copy directories without --recursive")
502 if isinstance(target, FileTargets):
503 # cp STUFF foo.txt, where foo.txt already exists. This limits the
504 # possibilities considerably.
506 self.to_stderr("target %s is not a directory" % quote_output(destination_spec))
509 self.to_stderr("cannot copy directory into a file")
511 return self.copy_file_to_file(sources[0], target)
513 if isinstance(target, MissingTargets):
515 return self.copy_to_directory(sources, target)
517 # if we have -r, we'll auto-create the target directory. Without
518 # it, we'll only create a file.
519 self.to_stderr("cannot copy multiple files into a file without -r")
522 return self.copy_file_to_file(sources[0], target)
524 if isinstance(target, DirectoryTargets):
525 # We're copying to an existing directory -- make sure that we
526 # have target names for everything
527 for source in sources:
528 if source.basename() is None and isinstance(source, TahoeFileSource):
530 "error: you must specify a destination filename")
532 return self.copy_to_directory(sources, target)
534 self.to_stderr("unknown target")
537 def to_stderr(self, text):
538 print >>self.stderr, text
540 # FIXME reduce the amount of near-duplicate code between get_target_info
541 # and get_source_info.
543 def get_target_info(self, destination_spec):
544 precondition(isinstance(destination_spec, unicode), destination_spec)
545 rootcap, path_utf8 = get_alias(self.aliases, destination_spec, None)
546 path = path_utf8.decode("utf-8")
547 if rootcap == DefaultAliasMarker:
548 # no alias, so this is a local file
549 pathname = abspath_expanduser_unicode(path)
550 if not os.path.exists(pathname):
551 t = LocalMissingTarget(pathname)
552 elif os.path.isdir(pathname):
553 t = LocalDirectoryTarget(self.progress, pathname)
555 # TODO: should this be _assert? what happens if the target is
557 assert os.path.isfile(pathname), pathname
558 t = LocalFileTarget(pathname) # non-empty
560 # this is a tahoe object
561 url = self.nodeurl + "uri/%s" % urllib.quote(rootcap)
563 url += "/" + escape_path(path)
565 resp = do_http("GET", url + "?t=json")
566 if resp.status == 404:
568 t = TahoeMissingTarget(url)
569 elif resp.status == 200:
570 parsed = simplejson.loads(resp.read())
572 if nodetype == "dirnode":
573 t = TahoeDirectoryTarget(self.nodeurl, self.cache,
575 t.init_from_parsed(parsed)
577 writecap = to_str(d.get("rw_uri"))
578 readcap = to_str(d.get("ro_uri"))
579 mutable = d.get("mutable", False)
580 t = TahoeFileTarget(self.nodeurl, mutable,
581 writecap, readcap, url)
583 raise HTTPError("Error examining target %s"
584 % quote_output(destination_spec), resp)
587 def get_source_info(self, source_spec):
588 precondition(isinstance(source_spec, unicode), source_spec)
589 rootcap, path_utf8 = get_alias(self.aliases, source_spec, None)
590 path = path_utf8.decode("utf-8")
591 if rootcap == DefaultAliasMarker:
592 # no alias, so this is a local file
593 pathname = abspath_expanduser_unicode(path)
594 name = os.path.basename(pathname)
595 if not os.path.exists(pathname):
596 raise MissingSourceError(source_spec, quotefn=quote_local_unicode_path)
597 if os.path.isdir(pathname):
598 t = LocalDirectorySource(self.progress, pathname, name)
600 assert os.path.isfile(pathname)
601 t = LocalFileSource(pathname, name) # non-empty
603 # this is a tahoe object
604 url = self.nodeurl + "uri/%s" % urllib.quote(rootcap)
607 url += "/" + escape_path(path)
608 last_slash = path.rfind(u"/")
611 name = path[last_slash+1:]
613 resp = do_http("GET", url + "?t=json")
614 if resp.status == 404:
615 raise MissingSourceError(source_spec)
616 elif resp.status != 200:
617 raise HTTPError("Error examining source %s" % quote_output(source_spec),
619 parsed = simplejson.loads(resp.read())
621 if nodetype == "dirnode":
622 t = TahoeDirectorySource(self.nodeurl, self.cache,
624 t.init_from_parsed(parsed)
626 writecap = to_str(d.get("rw_uri"))
627 readcap = to_str(d.get("ro_uri"))
628 mutable = d.get("mutable", False) # older nodes don't provide it
630 last_slash = source_spec.rfind(u"/")
632 # TODO: this looks funny and redundant with the 'name'
633 # assignment above. cf #2329
634 name = source_spec[last_slash+1:]
636 t = TahoeFileSource(self.nodeurl, mutable, writecap, readcap, name)
640 def need_to_copy_bytes(self, source, target):
641 if source.need_to_copy_bytes:
642 # mutable tahoe files, and local files
644 if isinstance(target, (LocalFileTarget, LocalDirectoryTarget)):
648 def announce_success(self, msg):
649 if self.verbosity >= 1:
650 print >>self.stdout, "Success: %s" % msg
653 def copy_file_to_file(self, source, target):
654 precondition(isinstance(source, FileSources), source)
655 precondition(isinstance(target, FileTargets + MissingTargets), target)
656 if self.need_to_copy_bytes(source, target):
657 # if the target is a local directory, this will just write the
658 # bytes to disk. If it is a tahoe directory, it will upload the
659 # data, and stash the new filecap for a later set_children call.
660 f = source.open(self.caps_only)
662 return self.announce_success("file copied")
663 # otherwise we're copying tahoe to tahoe, and using immutable files,
664 # so we can just make a link. TODO: this probably won't always work:
665 # need to enumerate the cases and analyze them.
666 target.put_uri(source.bestcap())
667 return self.announce_success("file linked")
669 def copy_file_into_dir(self, source, name, target):
670 precondition(isinstance(source, FileSources), source)
671 precondition(isinstance(target, DirectoryTargets), target)
672 precondition(isinstance(name, unicode), name)
673 if self.need_to_copy_bytes(source, target):
674 # if the target is a local directory, this will just write the
675 # bytes to disk. If it is a tahoe directory, it will upload the
676 # data, and stash the new filecap for a later set_children call.
677 f = source.open(self.caps_only)
678 target.put_file(name, f)
680 # otherwise we're copying tahoe to tahoe, and using immutable files,
681 # so we can just make a link
682 target.put_uri(name, source.bestcap())
685 def progress(self, message):
687 if self.progressfunc:
688 self.progressfunc(message)
692 return Copier().do_copy(options)
694 # error cases that need improvement:
695 # local-file-in-the-way
697 # tahoe cp -r my:docs/proposed/denver.txt proposed/denver.txt
698 # handling of unknown nodes
700 # things that maybe should be errors but aren't
701 # local-dir-in-the-way
703 # tahoe cp -r my:docs/proposed/denver.txt denver.txt
704 # (creates denver.txt/denver.txt)
706 # error cases that look good:
707 # tahoe cp -r my:docs/missing missing
709 # tahoe cp -r my:docs/missing missing -> No JSON object could be decoded
710 # tahoe-file-in-the-way (when we want to make a directory)
711 # tahoe put README my:docs
712 # tahoe cp -r docs/proposed my:docs/proposed