Skip to content

Commit

Permalink
file_utils: add same_file_lax()
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 Feb 11, 2021
1 parent 74f4638 commit 8e40762
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lxc/file_utils.c
Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions src/lxc/file_utils.h
Expand Up @@ -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 */

0 comments on commit 8e40762

Please sign in to comment.