about summary refs log tree commit diff
path: root/src/libstore/nar-accessor.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-11-14T13·23+0100
committerEelco Dolstra <edolstra@gmail.com>2017-11-14T13·23+0100
commitbac8055652964d9ad5202011befb6b199463ddef (patch)
treeb37165578b2e6b5bf0c0739af636acbfa63c0be9 /src/libstore/nar-accessor.cc
parentc0d93a01ee996427a1ec6b7602e655c8405624d9 (diff)
nix ls-{store,nar}: Add --json flag
Diffstat (limited to 'src/libstore/nar-accessor.cc')
-rw-r--r--src/libstore/nar-accessor.cc33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libstore/nar-accessor.cc b/src/libstore/nar-accessor.cc
index 2afdeb021a93..b2d15c395f10 100644
--- a/src/libstore/nar-accessor.cc
+++ b/src/libstore/nar-accessor.cc
@@ -1,5 +1,6 @@
 #include "nar-accessor.hh"
 #include "archive.hh"
+#include "json.hh"
 
 #include <map>
 #include <stack>
@@ -181,4 +182,36 @@ ref<FSAccessor> makeNarAccessor(ref<const std::string> nar)
     return make_ref<NarAccessor>(nar);
 }
 
+void listNar(JSONPlaceholder & res, ref<FSAccessor> accessor, const Path & path)
+{
+    auto st = accessor->stat(path);
+
+    auto obj = res.object();
+
+    switch (st.type) {
+    case FSAccessor::Type::tRegular:
+        obj.attr("type", "regular");
+        obj.attr("size", st.fileSize);
+        if (st.isExecutable)
+            obj.attr("executable", true);
+        break;
+    case FSAccessor::Type::tDirectory:
+        obj.attr("type", "directory");
+        {
+            auto res2 = obj.object("entries");
+            for (auto & name : accessor->readDirectory(path)) {
+                auto res3 = res2.placeholder(name);
+                listNar(res3, accessor, path + "/" + name);
+            }
+        }
+        break;
+    case FSAccessor::Type::tSymlink:
+        obj.attr("type", "symlink");
+        obj.attr("target", accessor->readLink(path));
+        break;
+    default:
+        abort();
+    }
+}
+
 }