]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/web/common.py
Implementation, tests and docs for blacklists. This version allows listing directorie...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / common.py
index a9bfa8c2ef2e73685b2749d25a96be5df11791fb..0a14bf307ab926c2893e30932b51c307092eec53 100644 (file)
@@ -6,11 +6,15 @@ from zope.interface import Interface
 from nevow import loaders, appserver
 from nevow.inevow import IRequest
 from nevow.util import resource_filename
+from allmydata import blacklist
 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
      FileTooLargeError, NotEnoughSharesError, NoSharesError, \
-     NotDeepImmutableError
+     EmptyPathnameComponentError, MustBeDeepImmutableError, \
+     MustBeReadonlyError, MustNotBeUnknownRWError, SDMF_VERSION, MDMF_VERSION
 from allmydata.mutable.common import UnrecoverableFileError
-from allmydata.util import abbreviate # TODO: consolidate
+from allmydata.util import abbreviate
+from allmydata.util.encodingutil import to_str, quote_output
+
 
 class IOpHandleTable(Interface):
     pass
@@ -29,6 +33,32 @@ def parse_replace_arg(replace):
     else:
         return boolean_of_arg(replace)
 
+
+def parse_mutable_type_arg(arg):
+    if not arg:
+        return None # interpreted by the caller as "let the nodemaker decide"
+
+    arg = arg.lower()
+    if arg == "mdmf":
+        return MDMF_VERSION
+    elif arg == "sdmf":
+        return SDMF_VERSION
+
+    return "invalid"
+
+
+def parse_offset_arg(offset):
+    # XXX: This will raise a ValueError when invoked on something that
+    # is not an integer. Is that okay? Or do we want a better error
+    # message? Since this call is going to be used by programmers and
+    # their tools rather than users (through the wui), it is not
+    # inconsistent to return that, I guess.
+    if offset is not None:
+        offset = int(offset)
+
+    return offset
+
+
 def get_root(ctx_or_req):
     req = IRequest(ctx_or_req)
     # the addSlash=True gives us one extra (empty) segment
@@ -63,17 +93,14 @@ def convert_children_json(nodemaker, children_json):
     children = {}
     if children_json:
         data = simplejson.loads(children_json)
-        for (name, (ctype, propdict)) in data.iteritems():
-            name = unicode(name)
-            writecap = propdict.get("rw_uri")
-            if writecap is not None:
-                writecap = str(writecap)
-            readcap = propdict.get("ro_uri")
-            if readcap is not None:
-                readcap = str(readcap)
+        for (namex, (ctype, propdict)) in data.iteritems():
+            namex = unicode(namex)
+            writecap = to_str(propdict.get("rw_uri"))
+            readcap = to_str(propdict.get("ro_uri"))
             metadata = propdict.get("metadata", {})
-            childnode = nodemaker.create_from_cap(writecap, readcap)
-            children[name] = (childnode, metadata)
+            # name= argument is just for error reporting
+            childnode = nodemaker.create_from_cap(writecap, readcap, name=namex)
+            children[namex] = (childnode, metadata)
     return children
 
 def abbreviate_time(data):
@@ -86,10 +113,23 @@ def abbreviate_time(data):
     if s >= 1.0:
         return "%.2fs" % s
     if s >= 0.01:
-        return "%dms" % (1000*s)
+        return "%.0fms" % (1000*s)
     if s >= 0.001:
         return "%.1fms" % (1000*s)
-    return "%dus" % (1000000*s)
+    return "%.0fus" % (1000000*s)
+
+def compute_rate(bytes, seconds):
+    if bytes is None:
+      return None
+
+    if seconds is None or seconds == 0:
+      return None
+
+    # negative values don't make sense here
+    assert bytes > -1
+    assert seconds > 0
+
+    return 1.0 * bytes / seconds
 
 def abbreviate_rate(data):
     # 21.8kBps, 554.4kBps 4.37MBps
@@ -100,7 +140,7 @@ def abbreviate_rate(data):
         return "%1.2fMBps" % (r/1000000)
     if r > 1000:
         return "%.1fkBps" % (r/1000)
-    return "%dBps" % r
+    return "%.0fBps" % r
 
 def abbreviate_size(data):
     # 21.8kB, 554.4kB 4.37MB
@@ -113,7 +153,7 @@ def abbreviate_size(data):
         return "%1.2fMB" % (r/1000000)
     if r > 1000:
         return "%.1fkB" % (r/1000)
-    return "%dB" % r
+    return "%.0fB" % r
 
 def plural(sequence_or_length):
     if isinstance(sequence_or_length, int):
@@ -147,12 +187,15 @@ def should_create_intermediate_directories(req):
 
 def humanize_failure(f):
     # return text, responsecode
+    if f.check(EmptyPathnameComponentError):
+        return ("The webapi does not allow empty pathname components, "
+                "i.e. a double slash", http.BAD_REQUEST)
     if f.check(ExistingChildError):
         return ("There was already a child by that name, and you asked me "
                 "to not replace it.", http.CONFLICT)
     if f.check(NoSuchChildError):
-        name = f.value.args[0]
-        return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
+        quoted_name = quote_output(f.value.args[0], encoding="utf-8", quotemarks=False)
+        return ("No such child: %s" % quoted_name, http.NOT_FOUND)
     if f.check(NotEnoughSharesError):
         t = ("NotEnoughSharesError: This indicates that some "
              "servers were unavailable, or that shares have been "
@@ -178,10 +221,46 @@ def humanize_failure(f):
              "failure, or disk corruption. You should perform a filecheck on "
              "this object to learn more.")
         return (t, http.GONE)
-    if f.check(NotDeepImmutableError):
-        t = ("NotDeepImmutableError: a mkdir-immutable operation was given "
-             "a child that was not itself immutable: %s" % (f.value,))
+    if f.check(MustNotBeUnknownRWError):
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
+        immutable = f.value.args[2]
+        if immutable:
+            t = ("MustNotBeUnknownRWError: an operation to add a child named "
+                 "%s to a directory was given an unknown cap in a write slot.\n"
+                 "If the cap is actually an immutable readcap, then using a "
+                 "webapi server that supports a later version of Tahoe may help.\n\n"
+                 "If you are using the webapi directly, then specifying an immutable "
+                 "readcap in the read slot (ro_uri) of the JSON PROPDICT, and "
+                 "omitting the write slot (rw_uri), would also work in this "
+                 "case.") % quoted_name
+        else:
+            t = ("MustNotBeUnknownRWError: an operation to add a child named "
+                 "%s to a directory was given an unknown cap in a write slot.\n"
+                 "Using a webapi server that supports a later version of Tahoe "
+                 "may help.\n\n"
+                 "If you are using the webapi directly, specifying a readcap in "
+                 "the read slot (ro_uri) of the JSON PROPDICT, as well as a "
+                 "writecap in the write slot if desired, would also work in this "
+                 "case.") % quoted_name
+        return (t, http.BAD_REQUEST)
+    if f.check(MustBeDeepImmutableError):
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
+        t = ("MustBeDeepImmutableError: a cap passed to this operation for "
+             "the child named %s, needed to be immutable but was not. Either "
+             "the cap is being added to an immutable directory, or it was "
+             "originally retrieved from an immutable directory as an unknown "
+             "cap.") % quoted_name
+        return (t, http.BAD_REQUEST)
+    if f.check(MustBeReadonlyError):
+        quoted_name = quote_output(f.value.args[1], encoding="utf-8")
+        t = ("MustBeReadonlyError: a cap passed to this operation for "
+             "the child named '%s', needed to be read-only but was not. "
+             "The cap is being passed in a read slot (ro_uri), or was retrieved "
+             "from a read slot as an unknown cap.") % quoted_name
         return (t, http.BAD_REQUEST)
+    if f.check(blacklist.FileProhibited):
+        t = "Access Prohibited: %s" % quote_output(f.value.reason, encoding="utf-8", quotemarks=False)
+        return (t, http.FORBIDDEN)
     if f.check(WebError):
         return (f.value.text, f.value.code)
     if f.check(FileTooLargeError):