summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-06-17 12:37:16 -0400
committerSven Eden <yamakuzure@gmx.net>2017-07-25 09:46:52 +0200
commit15f07f3172e69e87b1c6d3d03911e3dc38b0110c (patch)
tree40d4b08aec2bf4b7da65968c609ec47517eabf02
parent1ea8dbd9cd30a46d8f410a7f651bd3e730d2378a (diff)
basic/path-util: allow flags for path_equal_or_files_same
No functional change, just a new parameters and the tests that AT_SYMLINK_NOFOLLOW works as expected.
-rw-r--r--src/basic/path-util.c4
-rw-r--r--src/basic/path-util.h2
-rw-r--r--src/basic/process-util.c2
-rw-r--r--src/basic/rm-rf.c2
-rw-r--r--src/basic/socket-util.c2
-rw-r--r--src/basic/stat-util.c6
-rw-r--r--src/basic/stat-util.h2
-rw-r--r--src/basic/util.c2
-rw-r--r--src/basic/virt.c2
-rw-r--r--src/test/test-path-util.c30
-rw-r--r--src/test/test-stat-util.c6
11 files changed, 36 insertions, 24 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 0e4e76dc4..acd1007e3 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -444,8 +444,8 @@ bool path_equal(const char *a, const char *b) {
return path_compare(a, b) == 0;
}
-bool path_equal_or_files_same(const char *a, const char *b) {
- return path_equal(a, b) || files_same(a, b) > 0;
+bool path_equal_or_files_same(const char *a, const char *b, int flags) {
+ return path_equal(a, b) || files_same(a, b, flags) > 0;
}
char* path_join(const char *root, const char *path, const char *rest) {
diff --git a/src/basic/path-util.h b/src/basic/path-util.h
index 487ae1396..3c0dbb321 100644
--- a/src/basic/path-util.h
+++ b/src/basic/path-util.h
@@ -52,7 +52,7 @@ char* path_kill_slashes(char *path);
char* path_startswith(const char *path, const char *prefix) _pure_;
int path_compare(const char *a, const char *b) _pure_;
bool path_equal(const char *a, const char *b) _pure_;
-bool path_equal_or_files_same(const char *a, const char *b);
+bool path_equal_or_files_same(const char *a, const char *b, int flags);
char* path_join(const char *root, const char *path, const char *rest);
static inline bool path_equal_ptr(const char *a, const char *b) {
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 08ec66fb3..cb41fbf31 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -814,7 +814,7 @@ int pid_from_same_root_fs(pid_t pid) {
root = procfs_file_alloca(pid, "root");
- return files_same(root, "/proc/1/root");
+ return files_same(root, "/proc/1/root", 0);
}
#endif // 0
diff --git a/src/basic/rm-rf.c b/src/basic/rm-rf.c
index 87a7bd845..225a21296 100644
--- a/src/basic/rm-rf.c
+++ b/src/basic/rm-rf.c
@@ -184,7 +184,7 @@ int rm_rf(const char *path, RemoveFlags flags) {
/* We refuse to clean the root file system with this
* call. This is extra paranoia to never cause a really
* seriously broken system. */
- if (path_equal_or_files_same(path, "/")) {
+ if (path_equal_or_files_same(path, "/", 0)) {
log_error("Attempted to remove entire root file system, and we can't allow that.");
return -EPERM;
}
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index 23e960e02..c1312c1d2 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -412,7 +412,7 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
return false;
if (a->sockaddr.un.sun_path[0]) {
- if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path))
+ if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
return false;
} else {
if (a->size != b->size)
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index d262fe16a..0280afeec 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -176,16 +176,16 @@ int path_is_os_tree(const char *path) {
}
#endif // 0
-int files_same(const char *filea, const char *fileb) {
+int files_same(const char *filea, const char *fileb, int flags) {
struct stat a, b;
assert(filea);
assert(fileb);
- if (stat(filea, &a) < 0)
+ if (fstatat(AT_FDCWD, filea, &a, flags) < 0)
return -errno;
- if (stat(fileb, &b) < 0)
+ if (fstatat(AT_FDCWD, fileb, &b, flags) < 0)
return -errno;
return a.st_dev == b.st_dev &&
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h
index 32d09d014..c74d1d58e 100644
--- a/src/basic/stat-util.h
+++ b/src/basic/stat-util.h
@@ -57,7 +57,7 @@ int path_is_read_only_fs(const char *path);
int path_is_os_tree(const char *path);
#endif // 0
-int files_same(const char *filea, const char *fileb);
+int files_same(const char *filea, const char *fileb, int flags);
/* The .f_type field of struct statfs is really weird defined on
* different archs. Let's give its type a name. */
diff --git a/src/basic/util.c b/src/basic/util.c
index 88c996c55..cd7f0e42d 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -548,7 +548,7 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int
if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
return -ENOMEM;
- r = files_same(userns_fd_path, "/proc/self/ns/user");
+ r = files_same(userns_fd_path, "/proc/self/ns/user", 0);
if (r < 0)
return r;
if (r)
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 9ea18e63f..0060bb846 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -579,7 +579,7 @@ int running_in_chroot(void) {
return 0;
#endif // 0
- ret = files_same("/proc/1/root", "/");
+ ret = files_same("/proc/1/root", "/", 0);
if (ret < 0)
return ret;
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index 72b931568..c27624b43 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -117,23 +117,33 @@ static void test_path_equal_root(void) {
/* Make sure that files_same works as expected. */
- assert_se(files_same("/", "/") > 0);
- assert_se(files_same("/", "//") > 0);
+ assert_se(files_same("/", "/", 0) > 0);
+ assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
+ assert_se(files_same("/", "//", 0) > 0);
+ assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
- assert_se(files_same("/", "/./") > 0);
- assert_se(files_same("/", "/../") > 0);
+ assert_se(files_same("/", "/./", 0) > 0);
+ assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
+ assert_se(files_same("/", "/../", 0) > 0);
+ assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
- assert_se(files_same("/", "/.../") == -ENOENT);
+ assert_se(files_same("/", "/.../", 0) == -ENOENT);
+ assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
/* The same for path_equal_or_files_same. */
- assert_se(path_equal_or_files_same("/", "/"));
- assert_se(path_equal_or_files_same("/", "//"));
+ assert_se(path_equal_or_files_same("/", "/", 0));
+ assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
+ assert_se(path_equal_or_files_same("/", "//", 0));
+ assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
- assert_se(path_equal_or_files_same("/", "/./"));
- assert_se(path_equal_or_files_same("/", "/../"));
+ assert_se(path_equal_or_files_same("/", "/./", 0));
+ assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
+ assert_se(path_equal_or_files_same("/", "/../", 0));
+ assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
- assert_se(!path_equal_or_files_same("/", "/.../"));
+ assert_se(!path_equal_or_files_same("/", "/.../", 0));
+ assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
}
static void test_find_binary(const char *self) {
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
index 93d025dac..2825dbc52 100644
--- a/src/test/test-stat-util.c
+++ b/src/test/test-stat-util.c
@@ -38,8 +38,10 @@ static void test_files_same(void) {
assert_se(fd >= 0);
assert_se(symlink(name, name_alias) >= 0);
- assert_se(files_same(name, name));
- assert_se(files_same(name, name_alias));
+ assert_se(files_same(name, name, 0));
+ assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
+ assert_se(files_same(name, name_alias, 0));
+ assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
unlink(name);
unlink(name_alias);