1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-01 05:47:04 +03:00

util: add files_same() helper function

files_same() returns
     1, if the files are the same
     0, if the files have different inode/dev numbers
 errno, for any stat error

(cherry picked from commit 9d9951a460a90ef0e1e0384742cefdcf85193f8c)
This commit is contained in:
Harald Hoyer 2014-03-06 09:12:57 +01:00 committed by Zbigniew Jędrzejewski-Szmek
parent 1762262e61
commit ac0c2a0e3c
2 changed files with 20 additions and 10 deletions

View File

@ -3204,19 +3204,27 @@ bool on_tty(void) {
return cached_on_tty; return cached_on_tty;
} }
int files_same(const char *filea, const char *fileb) {
struct stat a, b;
if (stat(filea, &a) < 0)
return -errno;
if (stat(fileb, &b) < 0)
return -errno;
return a.st_dev == b.st_dev &&
a.st_ino == b.st_ino;
}
int running_in_chroot(void) { int running_in_chroot(void) {
struct stat a = {}, b = {}; int ret;
/* Only works as root */ ret = files_same("/proc/1/root", "/");
if (stat("/proc/1/root", &a) < 0) if (ret < 0)
return -errno; return ret;
if (stat("/", &b) < 0) return ret == 0;
return -errno;
return
a.st_dev != b.st_dev ||
a.st_ino != b.st_ino;
} }
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) { static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {

View File

@ -445,6 +445,8 @@ static inline const char *ansi_highlight_off(void) {
return on_tty() ? ANSI_HIGHLIGHT_OFF : ""; return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
} }
int files_same(const char *filea, const char *fileb);
int running_in_chroot(void); int running_in_chroot(void);
char *ellipsize(const char *s, size_t length, unsigned percent); char *ellipsize(const char *s, size_t length, unsigned percent);