shared: port various shared helpers basename() → path_extract_filename()

This commit is contained in:
Lennart Poettering
2022-12-21 16:51:05 +01:00
parent 7b2ffb593e
commit 03469b770b
5 changed files with 56 additions and 24 deletions

View File

@@ -51,20 +51,22 @@ static int validate_subvolume_name(const char *name) {
return 0;
}
static int extract_subvolume_name(const char *path, const char **subvolume) {
const char *fn;
static int extract_subvolume_name(const char *path, char **ret) {
_cleanup_free_ char *fn = NULL;
int r;
assert(path);
assert(subvolume);
assert(ret);
fn = basename(path);
r = path_extract_filename(path, &fn);
if (r < 0)
return r;
r = validate_subvolume_name(fn);
if (r < 0)
return r;
*subvolume = fn;
*ret = TAKE_PTR(fn);
return 0;
}
@@ -119,8 +121,8 @@ int btrfs_subvol_make_fd(int fd, const char *subvolume) {
}
int btrfs_subvol_make(const char *path) {
_cleanup_free_ char *subvolume = NULL;
_cleanup_close_ int fd = -EBADF;
const char *subvolume;
int r;
assert(path);
@@ -1180,8 +1182,8 @@ static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol
}
int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
_cleanup_free_ char *subvolume = NULL;
_cleanup_close_ int fd = -EBADF;
const char *subvolume;
int r;
assert(path);
@@ -1578,8 +1580,8 @@ int btrfs_subvol_snapshot_fd_full(
copy_progress_bytes_t progress_bytes,
void *userdata) {
_cleanup_free_ char *subvolume = NULL;
_cleanup_close_ int new_fd = -EBADF;
const char *subvolume;
int r;
assert(old_fd >= 0);

View File

@@ -18,6 +18,7 @@
#include "hashmap.h"
#include "macro.h"
#include "missing_syscall.h"
#include "path-util.h"
#include "process-util.h"
#include "rlimit-util.h"
#include "serialize.h"
@@ -131,7 +132,13 @@ static int do_execute(
return log_oom();
if (callbacks) {
fd = open_serialization_fd(basename(*path));
_cleanup_free_ char *bn = NULL;
r = path_extract_filename(*path, &bn);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from path '%s': %m", *path);
fd = open_serialization_fd(bn);
if (fd < 0)
return log_error_errno(fd, "Failed to open serialization file: %m");
}
@@ -199,15 +206,16 @@ int execute_directories(
ExecDirFlags flags) {
char **dirs = (char**) directories;
_cleanup_free_ char *name = NULL;
_cleanup_close_ int fd = -EBADF;
char *name;
int r;
pid_t executor_pid;
assert(!strv_isempty(dirs));
name = basename(dirs[0]);
assert(!isempty(name));
r = path_extract_filename(dirs[0], &name);
if (r < 0)
return log_error_errno(r, "Failed to extract file name from '%s': %m", dirs[0]);
if (callbacks) {
assert(callback_args);

View File

@@ -60,13 +60,19 @@ int generator_open_unit_file(
}
int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src) {
_cleanup_free_ char *bn = NULL;
const char *from, *to;
int r;
/* Adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute)
* or ../<src> (otherwise). */
const char *from, *to;
r = path_extract_filename(src, &bn);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from '%s': %m", src);
from = path_is_absolute(src) ? src : strjoina("../", src);
to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src));
to = strjoina(dir, "/", dst, ".", dep_type, "/", bn);
(void) mkdir_parents_label(to, 0755);
if (symlink(from, to) < 0)

View File

@@ -83,18 +83,23 @@ int make_lock_file(const char *p, int operation, LockFile *ret) {
}
int make_lock_file_for(const char *p, int operation, LockFile *ret) {
const char *fn;
char *t;
_cleanup_free_ char *fn = NULL, *dn = NULL, *t = NULL;
int r;
assert(p);
assert(ret);
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
r = path_extract_filename(p, &fn);
if (r < 0)
return r;
t = newa(char, strlen(p) + 2 + 4 + 1);
stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
r = path_extract_directory(p, &dn);
if (r < 0)
return r;
t = strjoin(dn, "/.#", fn, ".lck");
if (!t)
return -ENOMEM;
return make_lock_file(t, operation, ret);
}

View File

@@ -952,7 +952,7 @@ static int mount_in_namespace(
if (r < 0)
goto finish;
if (r == 0) {
const char *mount_inside;
_cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL;
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
@@ -965,8 +965,19 @@ static int mount_in_namespace(
}
/* Fifth, move the mount to the right place inside */
mount_inside = strjoina(incoming_path, basename(mount_outside));
r = mount_nofollow_verbose(LOG_ERR, mount_inside, dest, NULL, MS_MOVE, NULL);
r = path_extract_filename(mount_outside, &mount_outside_fn);
if (r < 0) {
log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
goto child_fail;
}
mount_inside = path_join(incoming_path, mount_outside_fn);
if (!mount_inside) {
r = log_oom_debug();
goto child_fail;
}
r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest, NULL, MS_MOVE, NULL);
if (r < 0)
goto child_fail;