chase: allow using chase() as mkdir_p() replacement
This allows using CHASE_MKDIR_0755 without CHASE_NONEXISTENT or
CHASE_PARENT, so that it will create the final component of the path
too should it be missing.
This is really useful as a mkdir_p() replacement that returns an fd to
the final component, and knows how to operate relative to a root fs.
Kinda reverts 4ea0bcb922 (which only
refused the flags combination which didn't work, instead of making it
work, which is what this commit does.)
This also corrects behaviour if CHASE_MKDIR_0755 is used in one more
way: we'll now always open the dir as O_PATH. This is generally the
better idea, but matters in particular once with allow using
CHASE_MKDIR_0755 to create the final component: we should uniformly
return an O_PATH dir that must be converted to a proper fd first before
using it.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include "id128-util.h"
|
||||
#include "mkdir.h"
|
||||
#include "path-util.h"
|
||||
#include "random-util.h"
|
||||
#include "rm-rf.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
@@ -754,6 +755,34 @@ TEST(trailing_dot_dot) {
|
||||
assert_se(path_equal(fdpath, expected2));
|
||||
}
|
||||
|
||||
TEST(use_chase_as_mkdir_p) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
ASSERT_OK_ERRNO(asprintf(&p, "/tmp/chasemkdir%" PRIu64 "/a/b/c", random_u64()));
|
||||
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
ASSERT_OK(chase(p, NULL, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd));
|
||||
|
||||
ASSERT_OK_EQ(inode_same_at(AT_FDCWD, p, fd, NULL, AT_EMPTY_PATH), 1);
|
||||
|
||||
_cleanup_close_ int fd2 = -EBADF;
|
||||
ASSERT_OK(chase(p, p, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd2));
|
||||
|
||||
_cleanup_free_ char *pp = ASSERT_PTR(path_join(p, p));
|
||||
|
||||
ASSERT_OK_EQ(inode_same_at(AT_FDCWD, pp, fd2, NULL, AT_EMPTY_PATH), 1);
|
||||
|
||||
_cleanup_free_ char *f = NULL;
|
||||
ASSERT_OK(path_extract_directory(p, &f));
|
||||
|
||||
_cleanup_free_ char *ff = NULL;
|
||||
ASSERT_OK(path_extract_directory(f, &ff));
|
||||
|
||||
_cleanup_free_ char *fff = NULL;
|
||||
ASSERT_OK(path_extract_directory(ff, &fff));
|
||||
|
||||
ASSERT_OK(rm_rf(fff, REMOVE_PHYSICAL));
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
arg_test_dir = saved_argv[1];
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user