From: Christian Brauner Date: Tue, 17 Aug 2021 08:38:44 +0000 (+0200) Subject: file_utils: add same_device() helper X-Git-Tag: lxc-5.0.0~108^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d06abe2f9c47c2a355b3ea1a5194abe3fa8d7cb5;p=thirdparty%2Flxc.git file_utils: add same_device() helper Signed-off-by: Christian Brauner --- diff --git a/src/lxc/file_utils.c b/src/lxc/file_utils.c index a361d188e..eaa9a5a40 100644 --- a/src/lxc/file_utils.c +++ b/src/lxc/file_utils.c @@ -766,3 +766,35 @@ bool same_file_lax(int fda, int fdb) return (st_fda.st_dev == st_fdb.st_dev) && (st_fda.st_ino == st_fdb.st_ino); } + +bool same_device(int fda, const char *patha, int fdb, const char *pathb) +{ + int ret; + mode_t modea, modeb; + struct stat st_fda, st_fdb; + + if (fda == fdb) + return true; + + if (is_empty_string(patha)) + ret = fstat(fda, &st_fda); + else + ret = fstatat(fda, patha, &st_fda, 0); + if (ret) + return false; + + if (is_empty_string(pathb)) + ret = fstat(fdb, &st_fdb); + else + ret = fstatat(fdb, pathb, &st_fdb, 0); + if (ret) + return false; + + errno = EINVAL; + modea = (st_fda.st_mode & S_IFMT); + modeb = (st_fdb.st_mode & S_IFMT); + if (modea != modeb || !IN_SET(modea, S_IFCHR, S_IFBLK)) + return false; + + return (st_fda.st_rdev == st_fdb.st_rdev); +} diff --git a/src/lxc/file_utils.h b/src/lxc/file_utils.h index faf7454dd..c9765da50 100644 --- a/src/lxc/file_utils.h +++ b/src/lxc/file_utils.h @@ -123,4 +123,7 @@ static inline int dup_cloexec(int fd) return move_fd(fd_dup); } +__hidden extern bool same_device(int fda, const char *patha, int fdb, + const char *pathb); + #endif /* __LXC_FILE_UTILS_H */