diff options
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/archive.cc | 5 | ||||
-rw-r--r-- | src/libutil/util.cc | 14 | ||||
-rw-r--r-- | src/libutil/util.hh | 4 |
3 files changed, 19 insertions, 4 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index f605e8b61954..90a039164b58 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -122,11 +122,8 @@ static void dump(const Path & path, DumpSink & sink) else if (S_ISLNK(st.st_mode)) { writeString("type", sink); writeString("symlink", sink); - char buf[st.st_size]; - if (readlink(path.c_str(), buf, st.st_size) != st.st_size) - throw SysError("reading symbolic link " + path); writeString("target", sink); - writeString(string(buf, st.st_size), sink); + writeString(readLink(path), sink); } else throw Error("unknown file type: " + path); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 60b86b162c9c..28e276a329b3 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -109,6 +109,20 @@ bool pathExists(const Path & path) } +Path readLink(const Path & path) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting status of `%1%'") % path); + if (!S_ISLNK(st.st_mode)) + throw Error(format("`%1%' is not a symlink") % path); + char buf[st.st_size]; + if (readlink(path.c_str(), buf, st.st_size) != st.st_size) + throw SysError(format("reading symbolic link `%1%'") % path); + return string(buf, st.st_size); +} + + Strings readDirectory(const Path & path) { Strings names; diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 4126381d9e96..5d27ac1bddb4 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -73,6 +73,10 @@ string baseNameOf(const Path & path); /* Return true iff the given path exists. */ bool pathExists(const Path & path); +/* Read the contents (target) of a symbolic link. The result is not + in any way canonicalised. */ +Path readLink(const Path & path); + /* Read the contents of a directory. The entries `.' and `..' are removed. */ Strings readDirectory(const Path & path); |