Skip to content

Commit

Permalink
file_utils: add same_device() helper
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Oct 14, 2021
1 parent 7381a5d commit 0f9f5ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lxc/file_utils.c
Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/lxc/file_utils.h
Expand Up @@ -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 */

0 comments on commit 0f9f5ec

Please sign in to comment.