Remove some duplicate code

This commit is contained in:
Eelco Dolstra 2014-10-03 22:37:51 +02:00
parent c08c802bf3
commit 3f8576a6ab
4 changed files with 22 additions and 21 deletions

View file

@ -815,22 +815,15 @@ static void prim_readDir(EvalState & state, const Pos & pos, Value * * args, Val
DirEntries entries = readDirectory(path); DirEntries entries = readDirectory(path);
state.mkAttrs(v, entries.size()); 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)); Value * ent_val = state.allocAttr(v, state.symbols.create(ent.name));
if (ent.type == DT_UNKNOWN) { if (ent.type == DT_UNKNOWN)
struct stat st = lstat(path + "/" + ent.name); ent.type = getFileType(path);
mkString(*ent_val, mkString(*ent_val,
S_ISREG(st.st_mode) ? "regular" : ent.type == DT_REG ? "regular" :
S_ISDIR(st.st_mode) ? "directory" : ent.type == DT_DIR ? "directory" :
S_ISLNK(st.st_mode) ? "symlink" : ent.type == DT_LNK ? "symlink" :
"unknown"); "unknown");
} else {
mkString(*ent_val,
ent.type == DT_REG ? "regular" :
ent.type == DT_DIR ? "directory" :
ent.type == DT_LNK ? "symlink" :
"unknown");
}
} }
v.attrs->sort(); v.attrs->sort();

View file

@ -301,12 +301,8 @@ static void findRoots(StoreAPI & store, const Path & path, unsigned char type, R
{ {
try { try {
if (type == DT_UNKNOWN) { if (type == DT_UNKNOWN)
struct stat st = lstat(path); type = getFileType(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_DIR) { if (type == DT_DIR) {
for (auto & i : readDirectory(path)) for (auto & i : readDirectory(path))

View file

@ -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) string readFile(int fd)
{ {
struct stat st; struct stat st;

View file

@ -77,6 +77,8 @@ typedef vector<DirEntry> DirEntries;
DirEntries readDirectory(const Path & path); DirEntries readDirectory(const Path & path);
unsigned char getFileType(const Path & path);
/* Read the contents of a file into a string. */ /* Read the contents of a file into a string. */
string readFile(int fd); string readFile(int fd);
string readFile(const Path & path, bool drain = false); string readFile(const Path & path, bool drain = false);