From c2b65dd197a1b2e14d517b0b5ff307b149538917 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 3 Oct 2014 22:37:51 +0200 Subject: Remove some duplicate code --- nix/libstore/gc.cc | 8 ++------ nix/libutil/util.cc | 10 ++++++++++ nix/libutil/util.hh | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) (limited to 'nix') diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc index 4babea4a50..05d332f2a1 100644 --- a/nix/libstore/gc.cc +++ b/nix/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/nix/libutil/util.cc b/nix/libutil/util.cc index 9af42a150c..ad3b3c387f 100644 --- a/nix/libutil/util.cc +++ b/nix/libutil/util.cc @@ -223,6 +223,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/nix/libutil/util.hh b/nix/libutil/util.hh index 7e538ea033..418d76daec 100644 --- a/nix/libutil/util.hh +++ b/nix/libutil/util.hh @@ -77,6 +77,8 @@ typedef vector 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); -- cgit 1.4.1