file_utils: add same_file_lax()

parent 00c5cdf0
......@@ -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);
}
......@@ -96,4 +96,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 */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment