From 86daaa976b3efc12b1c47a988637dd0633f6d562 Mon Sep 17 00:00:00 2001 From: smitsohu Date: Thu, 11 Mar 2021 00:31:20 +0100 Subject: simplify is_link function --- src/firejail/util.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/firejail/util.c b/src/firejail/util.c index 53c671794..2ad85acd6 100644 --- a/src/firejail/util.c +++ b/src/firejail/util.c @@ -441,35 +441,22 @@ int is_dir(const char *fname) { return 0; } - // return 1 if the file is a link int is_link(const char *fname) { assert(fname); if (*fname == '\0') return 0; - char *dup = NULL; - struct stat s; - if (lstat(fname, &s) == 0) { - if (S_ISLNK(s.st_mode)) - return 1; - if (S_ISDIR(s.st_mode)) { - // remove trailing slashes and single dots and try again - dup = strdup(fname); - if (!dup) - errExit("strdup"); - trim_trailing_slash_or_dot(dup); - if (lstat(dup, &s) == 0) { - if (S_ISLNK(s.st_mode)) { - free(dup); - return 1; - } - } - } - } + char *dup = strdup(fname); + if (!dup) + errExit("strdup"); + trim_trailing_slash_or_dot(dup); + + char c; + ssize_t rv = readlink(dup, &c, 1); free(dup); - return 0; + return (rv != -1); } // remove all slashes and single dots from the end of a path -- cgit v1.2.3-54-g00ecf