mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
Merge pull request #26949 from DaanDeMeyer/xopenat-reopen
loop-util: Add loop_device_make_by_path_at()
This commit is contained in:
commit
d024c4d0e1
@ -1104,6 +1104,11 @@ int xopenat(int dir_fd, const char *path, int flags, mode_t mode) {
|
||||
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
|
||||
assert(path);
|
||||
|
||||
if (isempty(path)) {
|
||||
assert(!FLAGS_SET(flags, O_CREAT|O_EXCL));
|
||||
return fd_reopen(dir_fd, flags);
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, O_DIRECTORY|O_CREAT)) {
|
||||
r = RET_NERRNO(mkdirat(dir_fd, path, mode));
|
||||
if (r == -EEXIST) {
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "env-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "fileio.h"
|
||||
#include "loop-util.h"
|
||||
#include "missing_loop.h"
|
||||
@ -650,7 +651,8 @@ int loop_device_make(
|
||||
ret);
|
||||
}
|
||||
|
||||
int loop_device_make_by_path(
|
||||
int loop_device_make_by_path_at(
|
||||
int dir_fd,
|
||||
const char *path,
|
||||
int open_flags,
|
||||
uint32_t sector_size,
|
||||
@ -662,6 +664,7 @@ int loop_device_make_by_path(
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
bool direct = false;
|
||||
|
||||
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
|
||||
assert(path);
|
||||
assert(ret);
|
||||
assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY));
|
||||
@ -678,9 +681,9 @@ int loop_device_make_by_path(
|
||||
direct_flags = FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) ? O_DIRECT : 0;
|
||||
rdwr_flags = open_flags >= 0 ? open_flags : O_RDWR;
|
||||
|
||||
fd = open(path, basic_flags|direct_flags|rdwr_flags);
|
||||
fd = xopenat(dir_fd, path, basic_flags|direct_flags|rdwr_flags, 0);
|
||||
if (fd < 0 && direct_flags != 0) /* If we had O_DIRECT on, and things failed with that, let's immediately try again without */
|
||||
fd = open(path, basic_flags|rdwr_flags);
|
||||
fd = xopenat(dir_fd, path, basic_flags|rdwr_flags, 0);
|
||||
else
|
||||
direct = direct_flags != 0;
|
||||
if (fd < 0) {
|
||||
@ -690,9 +693,9 @@ int loop_device_make_by_path(
|
||||
if (open_flags >= 0 || !(ERRNO_IS_PRIVILEGE(r) || r == -EROFS))
|
||||
return r;
|
||||
|
||||
fd = open(path, basic_flags|direct_flags|O_RDONLY);
|
||||
fd = xopenat(dir_fd, path, basic_flags|direct_flags|O_RDONLY, 0);
|
||||
if (fd < 0 && direct_flags != 0) /* as above */
|
||||
fd = open(path, basic_flags|O_RDONLY);
|
||||
fd = xopenat(dir_fd, path, basic_flags|O_RDONLY, 0);
|
||||
else
|
||||
direct = direct_flags != 0;
|
||||
if (fd < 0)
|
||||
@ -709,7 +712,16 @@ int loop_device_make_by_path(
|
||||
direct ? "enabled" : "disabled",
|
||||
direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : "");
|
||||
|
||||
return loop_device_make_internal(path, fd, open_flags, 0, 0, sector_size, loop_flags, lock_op, ret);
|
||||
return loop_device_make_internal(
|
||||
dir_fd == AT_FDCWD ? path : NULL,
|
||||
fd,
|
||||
open_flags,
|
||||
/* offset = */ 0,
|
||||
/* size = */ 0,
|
||||
sector_size,
|
||||
loop_flags,
|
||||
lock_op,
|
||||
ret);
|
||||
}
|
||||
|
||||
int loop_device_make_by_path_memory(
|
||||
|
@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "sd-device.h"
|
||||
|
||||
#include "macro.h"
|
||||
@ -32,7 +34,10 @@ struct LoopDevice {
|
||||
#define LOOP_DEVICE_IS_FOREIGN(d) ((d)->nr < 0)
|
||||
|
||||
int loop_device_make(int fd, int open_flags, uint64_t offset, uint64_t size, uint32_t sector_size, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_make_by_path(const char *path, int open_flags, uint32_t sector_size, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_make_by_path_at(int dir_fd, const char *path, int open_flags, uint32_t sector_size, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
static inline int loop_device_make_by_path(const char *path, int open_flags, uint32_t sector_size, uint32_t loop_flags, int lock_op, LoopDevice **ret) {
|
||||
return loop_device_make_by_path_at(AT_FDCWD, path, open_flags, sector_size, loop_flags, lock_op, ret);
|
||||
}
|
||||
int loop_device_make_by_path_memory(const char *path, int open_flags, uint32_t sector_size, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_open(sd_device *dev, int open_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_open_from_fd(int fd, int open_flags, int lock_op, LoopDevice **ret);
|
||||
|
@ -1223,7 +1223,7 @@ TEST(openat_report_new) {
|
||||
|
||||
TEST(xopenat) {
|
||||
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
|
||||
_cleanup_close_ int tfd = -EBADF, fd = -EBADF;
|
||||
_cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF;
|
||||
|
||||
assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
|
||||
|
||||
@ -1244,6 +1244,11 @@ TEST(xopenat) {
|
||||
assert_se((fd = xopenat(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0644)) >= 0);
|
||||
assert_se(fd_verify_regular(fd) >= 0);
|
||||
fd = safe_close(fd);
|
||||
|
||||
/* Test that we can reopen an existing fd with xopenat() by specifying an empty path. */
|
||||
|
||||
assert_se((fd = xopenat(tfd, "def", O_PATH|O_CLOEXEC, 0)) >= 0);
|
||||
assert_se((fd2 = xopenat(fd, "", O_RDWR|O_CLOEXEC, 0644)) >= 0);
|
||||
}
|
||||
|
||||
TEST(xopenat_lock) {
|
||||
|
Loading…
Reference in New Issue
Block a user