[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
239/376: Add readDir primop
From: |
Ludovic Courtès |
Subject: |
239/376: Add readDir primop |
Date: |
Wed, 28 Jan 2015 22:05:19 +0000 |
civodul pushed a commit to tag 1.8
in repository guix.
commit c08c802bf31ce739e0de6d1fbfe4d58b808ae9bb
Author: Shea Levy <address@hidden>
Date: Wed Oct 1 10:17:50 2014 -0400
Add readDir primop
---
src/libexpr/primops.cc | 37 +++++++++++++++++++++++++++++++++++++
1 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 0ec39f5..ca4ccf4 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -800,6 +800,42 @@ static void prim_findFile(EvalState & state, const Pos &
pos, Value * * args, Va
mkPath(v, state.findFile(searchPath, path).c_str());
}
+/* Read a directory (without . or ..) */
+static void prim_readDir(EvalState & state, const Pos & pos, Value * * args,
Value & v)
+{
+ PathSet ctx;
+ Path path = state.coerceToPath(pos, *args[0], ctx);
+ try {
+ realiseContext(ctx);
+ } catch (InvalidPathError & e) {
+ throw EvalError(format("cannot read ‘%1%’, since path ‘%2%’ is not
valid, at %3%")
+ % path % e.path % pos);
+ }
+
+ DirEntries entries = readDirectory(path);
+ state.mkAttrs(v, entries.size());
+
+ for (const auto & ent : entries) {
+ Value * ent_val = state.allocAttr(v, state.symbols.create(ent.name));
+ if (ent.type == DT_UNKNOWN) {
+ struct stat st = lstat(path + "/" + ent.name);
+ mkString(*ent_val,
+ S_ISREG(st.st_mode) ? "regular" :
+ S_ISDIR(st.st_mode) ? "directory" :
+ S_ISLNK(st.st_mode) ? "symlink" :
+ "unknown");
+ } else {
+ mkString(*ent_val,
+ ent.type == DT_REG ? "regular" :
+ ent.type == DT_DIR ? "directory" :
+ ent.type == DT_LNK ? "symlink" :
+ "unknown");
+ }
+ }
+
+ v.attrs->sort();
+}
+
/*************************************************************
* Creating files
@@ -1460,6 +1496,7 @@ void EvalState::createBaseEnv()
addPrimOp("baseNameOf", 1, prim_baseNameOf);
addPrimOp("dirOf", 1, prim_dirOf);
addPrimOp("__readFile", 1, prim_readFile);
+ addPrimOp("__readDir", 1, prim_readDir);
addPrimOp("__findFile", 2, prim_findFile);
// Creating files
- 231/376: nix-daemon: Close unnecessary fd, (continued)
- 231/376: nix-daemon: Close unnecessary fd, Ludovic Courtès, 2015/01/28
- 230/376: Bindings: Remove copy constructor, Ludovic Courtès, 2015/01/28
- 222/376: Add missing static, Ludovic Courtès, 2015/01/28
- 223/376: manual: add a note that lists are strict in length, Ludovic Courtès, 2015/01/28
- 232/376: printMissing(): Print derivations in approximate build order, Ludovic Courtès, 2015/01/28
- 234/376: printValue(): Don't print <CYCLE> for repeated values, Ludovic Courtès, 2015/01/28
- 233/376: Support control characters in JSON output, Ludovic Courtès, 2015/01/28
- 235/376: createDirs(): Handle ‘path’ being a symlink, Ludovic Courtès, 2015/01/28
- 238/376: Add test for readDir primop, Ludovic Courtès, 2015/01/28
- 240/376: Remove some duplicate code, Ludovic Courtès, 2015/01/28
- 239/376: Add readDir primop,
Ludovic Courtès <=
- 224/376: add manpage for nix-generate-patches, Ludovic Courtès, 2015/01/28
- 246/376: Typo, Ludovic Courtès, 2015/01/28
- 245/376: Get rid of some unnecessary ExprConcatStrings nodes in dynamic attrs, Ludovic Courtès, 2015/01/28
- 243/376: Add primop ‘catAttrs’, Ludovic Courtès, 2015/01/28
- 249/376: nix-copy-closure: Use strict, Ludovic Courtès, 2015/01/28
- 241/376: Tweak, Ludovic Courtès, 2015/01/28
- 304/376: Combine introduction / quick start parts, Ludovic Courtès, 2015/01/28
- 250/376: Remove unused @sshOpts flag, Ludovic Courtès, 2015/01/28
- 305/376: Update quick start section, Ludovic Courtès, 2015/01/28
- 306/376: Update installation section, Ludovic Courtès, 2015/01/28