*: convert amenable fdopen calls to take_fdopen

Mechanical change to eliminate some cruft by using the
new take_fdopen{_unlocked}() wrappers where trivial.
This commit is contained in:
Vito Caputo 2020-03-31 01:07:21 -07:00
parent 3ebbb6cb39
commit 4fa744a35c
12 changed files with 16 additions and 37 deletions

View file

@ -48,14 +48,12 @@ int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) {
/* This assumes that returned FILE object is short-lived and used within the same single-threaded
* context and never shared externally, hence locking is not necessary. */
r = fdopen_unlocked(fd, "w", &f);
r = take_fdopen_unlocked(&fd, "w", &f);
if (r < 0) {
(void) unlink(t);
return r;
}
TAKE_FD(fd);
if (ret_f)
*ret_f = TAKE_PTR(f);

View file

@ -360,12 +360,10 @@ static int home_parse_worker_stdout(int _fd, UserRecord **ret) {
if (lseek(fd, SEEK_SET, 0) == (off_t) -1)
return log_error_errno(errno, "Failed to seek to beginning of memfd: %m");
f = fdopen(fd, "r");
f = take_fdopen(&fd, "r");
if (!f)
return log_error_errno(errno, "Failed to reopen memfd: %m");
TAKE_FD(fd);
if (DEBUG_LOGGING) {
_cleanup_free_ char *text = NULL;

View file

@ -279,12 +279,10 @@ static int read_identity_file(int root_fd, JsonVariant **ret) {
if (r < 0)
return log_error_errno(r, "Embedded identity file is not a regular file, refusing: %m");
identity_file = fdopen(identity_fd, "r");
identity_file = take_fdopen(&identity_fd, "r");
if (!identity_file)
return log_oom();
identity_fd = -1;
r = json_parse_file(identity_file, ".identity", JSON_PARSE_SENSITIVE, ret, &line, &column);
if (r < 0)
return log_error_errno(r, "[.identity:%u:%u] Failed to parse JSON data: %m", line, column);
@ -318,14 +316,12 @@ static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) {
if (identity_fd < 0)
return log_error_errno(errno, "Failed to create .identity file in home directory: %m");
identity_file = fdopen(identity_fd, "w");
identity_file = take_fdopen(&identity_fd, "w");
if (!identity_file) {
r = log_oom();
goto fail;
}
identity_fd = -1;
json_variant_dump(normalized, JSON_FORMAT_PRETTY, identity_file, NULL);
r = fflush_and_check(identity_file);

View file

@ -20,6 +20,7 @@
#include "env-file.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "in-addr-util.h"
@ -399,12 +400,10 @@ int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, s
pair[1] = safe_close(pair[1]);
f = fdopen(pair[0], "r");
f = take_fdopen(&pair[0], "r");
if (!f)
return -errno;
pair[0] = -1;
r = load_env_file_pairs(f, "/etc/os-release", &l);
if (r < 0)
return r;

View file

@ -620,12 +620,10 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1)
return -errno;
f = fdopen(operation->extra_fd, "r");
f = take_fdopen(&operation->extra_fd, "r");
if (!f)
return -errno;
operation->extra_fd = -1;
/* The resulting temporary file starts with a boolean value that indicates success or not. */
errno = 0;
n = fread(&success, 1, sizeof(success), f);

View file

@ -118,10 +118,9 @@ int change_uid_gid(const char *user, char **_home) {
if (fd < 0)
return fd;
f = fdopen(fd, "r");
f = take_fdopen(&fd, "r");
if (!f)
return log_oom();
fd = -1;
r = read_line(f, LONG_LINE_MAX, &line);
if (r == 0)
@ -191,10 +190,9 @@ int change_uid_gid(const char *user, char **_home) {
if (fd < 0)
return fd;
f = fdopen(fd, "r");
f = take_fdopen(&fd, "r");
if (!f)
return log_oom();
fd = -1;
r = read_line(f, LONG_LINE_MAX, &line);
if (r == 0)

View file

@ -1092,10 +1092,9 @@ static int test_chroot_dropin(
return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
}
r = fdopen_unlocked(fd, "r", &f);
r = take_fdopen_unlocked(&fd, "r", &f);
if (r < 0)
return log_debug_errno(r, "Failed to convert file handle: %m");
TAKE_FD(fd);
r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0)

View file

@ -79,12 +79,10 @@ static int append_fd(sd_bus_message *m, PortableMetadata *d) {
assert(d);
assert(d->fd >= 0);
f = fdopen(d->fd, "r");
f = take_fdopen(&d->fd, "r");
if (!f)
return -errno;
d->fd = -1;
r = read_full_stream(f, &buf, &n);
if (r < 0)
return r;

View file

@ -791,14 +791,12 @@ int ask_password_agent(
(void) fchmod(fd, 0644);
f = fdopen(fd, "w");
f = take_fdopen(&fd, "w");
if (!f) {
r = -errno;
goto finish;
}
fd = -1;
signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
if (signal_fd < 0) {
r = -errno;

View file

@ -1541,14 +1541,12 @@ int dissected_image_acquire_metadata(DissectedImage *m) {
fds[2*k+1] = safe_close(fds[2*k+1]);
f = fdopen(fds[2*k], "r");
f = take_fdopen(&fds[2*k], "r");
if (!f) {
r = -errno;
goto finish;
}
fds[2*k] = -1;
switch (k) {
case META_HOSTNAME:

View file

@ -1280,10 +1280,9 @@ static int unit_file_load(
if (r < 0)
return r;
f = fdopen(fd, "r");
f = take_fdopen(&fd, "r");
if (!f)
return -errno;
fd = -1;
/* c is only needed if we actually load the file (it's referenced from items[] btw, in case you wonder.) */
assert(c);

View file

@ -3,6 +3,7 @@
#include "alloc-util.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "macro.h"
#include "os-util.h"
@ -76,10 +77,9 @@ int fopen_os_release(const char *root, char **ret_path, FILE **ret_file) {
if (r < 0)
return r;
f = fdopen(fd, "r");
f = take_fdopen(&fd, "r");
if (!f)
return -errno;
fd = -1;
*ret_file = f;