2
0
mirror of git://github.com/lxc/lxc synced 2025-08-30 15:22:05 +00:00

file_utils: add same_device() helper

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner 2021-08-17 10:38:44 +02:00
parent 72c6d3a56d
commit d06abe2f9c
No known key found for this signature in database
GPG Key ID: 8EB056D53EECB12D
2 changed files with 35 additions and 0 deletions

View File

@ -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);
}

View File

@ -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 */