about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-10-03T20·37+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-10-03T20·37+0200
commit3f8576a6abedfa7c16d6f13dbdbabaa695cf60bb (patch)
treee1aad65d6748f23229b06768d61c21401de2a98f /src
parentc08c802bf31ce739e0de6d1fbfe4d58b808ae9bb (diff)
Remove some duplicate code
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/primops.cc23
-rw-r--r--src/libstore/gc.cc8
-rw-r--r--src/libutil/util.cc10
-rw-r--r--src/libutil/util.hh2
4 files changed, 22 insertions, 21 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index ca4ccf4495..461a547768 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -815,22 +815,15 @@ static void prim_readDir(EvalState & state, const Pos & pos, Value * * args, Val
     DirEntries entries = readDirectory(path);
     state.mkAttrs(v, entries.size());
 
-    for (const auto & ent : entries) {
+    for (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");
-        }
+        if (ent.type == DT_UNKNOWN)
+            ent.type = getFileType(path);
+        mkString(*ent_val,
+            ent.type == DT_REG ? "regular" :
+            ent.type == DT_DIR ? "directory" :
+            ent.type == DT_LNK ? "symlink" :
+            "unknown");
     }
 
     v.attrs->sort();
diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index 481f5a7c35..e9db711e7c 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -301,12 +301,8 @@ static void findRoots(StoreAPI & store, const Path & path, unsigned char type, R
 {
     try {
 
-        if (type == DT_UNKNOWN) {
-            struct stat st = lstat(path);
-            if (S_ISDIR(st.st_mode)) type = DT_DIR;
-            else if (S_ISLNK(st.st_mode)) type = DT_LNK;
-            else if (S_ISREG(st.st_mode)) type = DT_REG;
-        }
+        if (type == DT_UNKNOWN)
+            type = getFileType(path);
 
         if (type == DT_DIR) {
             for (auto & i : readDirectory(path))
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 436580651d..99d2b1e0ad 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -227,6 +227,16 @@ DirEntries readDirectory(const Path & path)
 }
 
 
+unsigned char getFileType(const Path & path)
+{
+    struct stat st = lstat(path);
+    if (S_ISDIR(st.st_mode)) return DT_DIR;
+    if (S_ISLNK(st.st_mode)) return DT_LNK;
+    if (S_ISREG(st.st_mode)) return DT_REG;
+    return DT_UNKNOWN;
+}
+
+
 string readFile(int fd)
 {
     struct stat st;
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 4f9f7422c0..b35e02dceb 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -77,6 +77,8 @@ typedef vector<DirEntry> DirEntries;
 
 DirEntries readDirectory(const Path & path);
 
+unsigned char getFileType(const Path & path);
+
 /* Read the contents of a file into a string. */
 string readFile(int fd);
 string readFile(const Path & path, bool drain = false);