[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
240/376: Remove some duplicate code
From: |
Ludovic Courtès |
Subject: |
240/376: Remove some duplicate code |
Date: |
Wed, 28 Jan 2015 22:05:19 +0000 |
civodul pushed a commit to tag 1.8
in repository guix.
commit 3f8576a6abedfa7c16d6f13dbdbabaa695cf60bb
Author: Eelco Dolstra <address@hidden>
Date: Fri Oct 3 22:37:51 2014 +0200
Remove some duplicate code
---
src/libexpr/primops.cc | 23 ++++++++---------------
src/libstore/gc.cc | 8 ++------
src/libutil/util.cc | 10 ++++++++++
src/libutil/util.hh | 2 ++
4 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index ca4ccf4..461a547 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 481f5a7..e9db711 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 4365806..99d2b1e 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 4f9f742..b35e02d 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);
- 229/376: Remove bash requirement, (continued)
- 229/376: Remove bash requirement, Ludovic Courtès, 2015/01/28
- 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 <=
- 239/376: Add readDir primop, Ludovic Courtès, 2015/01/28
- 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