From: Christian Brauner Date: Thu, 11 Feb 2021 13:40:41 +0000 (+0100) Subject: file_utils: add same_file_lax() X-Git-Tag: lxc-5.0.0~291^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e40762dfdc8baf697934f16057689958c053efb;p=thirdparty%2Flxc.git file_utils: add same_file_lax() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/file_utils.c b/src/lxc/file_utils.c index e046c2dd0..78fa85ed7 100644 --- a/src/lxc/file_utils.c +++ b/src/lxc/file_utils.c @@ -725,3 +725,26 @@ char *read_file_at(int dfd, const char *fnam, return move_ptr(buf); } + +bool same_file_lax(int fda, int fdb) +{ + struct stat st_fda, st_fdb; + + + if (fda == fdb) + return true; + + if (fstat(fda, &st_fda) < 0) + return false; + + if (fstat(fdb, &st_fdb) < 0) + return false; + + errno = EINVAL; + if ((st_fda.st_mode & S_IFMT) != (st_fdb.st_mode & S_IFMT)) + return false; + + errno = EINVAL; + return (st_fda.st_dev == st_fdb.st_dev) && + (st_fda.st_ino == st_fdb.st_ino); +} diff --git a/src/lxc/file_utils.h b/src/lxc/file_utils.h index 76a48edb3..36bd9b0c2 100644 --- a/src/lxc/file_utils.h +++ b/src/lxc/file_utils.h @@ -97,4 +97,12 @@ __hidden extern char *read_file_at(int dfd, const char *fnam, __hidden extern ssize_t lxc_read_try_buf_at(int dfd, const char *path, void *buf, size_t count); +/* + * Check if two fds refer to the same file. + * The function is "lax" in so far, as it doesn't care whether fda and fdb have + * the same flags or whether they share the same device context when they refer + * to devices. + */ +__hidden extern bool same_file_lax(int fda, int fdb); + #endif /* __LXC_FILE_UTILS_H */