Merge pull request #500 from zonque/fileio
fileio: consolidate write_string_file*()
This commit is contained in:
@@ -204,7 +204,7 @@ static int drop_from_file(const char *fn, uint64_t drop) {
|
||||
if (asprintf(&p, "%u %u", lo, hi) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
r = write_string_file(fn, p);
|
||||
r = write_string_file(fn, p, WRITE_STRING_FILE_CREATE);
|
||||
free(p);
|
||||
|
||||
return r;
|
||||
|
||||
@@ -646,7 +646,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
|
||||
|
||||
snprintf(c, sizeof(c), PID_FMT"\n", pid);
|
||||
|
||||
return write_string_file_no_create(fs, c);
|
||||
return write_string_file(fs, c, 0);
|
||||
}
|
||||
|
||||
int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
|
||||
@@ -820,7 +820,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
|
||||
|
||||
sc = strstrip(contents);
|
||||
if (sc[0] == 0) {
|
||||
r = write_string_file_no_create(fs, agent);
|
||||
r = write_string_file(fs, agent, 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
} else if (!streq(sc, agent))
|
||||
@@ -840,7 +840,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
|
||||
|
||||
sc = strstrip(contents);
|
||||
if (streq(sc, "0")) {
|
||||
r = write_string_file_no_create(fs, "1");
|
||||
r = write_string_file(fs, "1", 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -861,7 +861,7 @@ int cg_uninstall_release_agent(const char *controller) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = write_string_file_no_create(fs, "0");
|
||||
r = write_string_file(fs, "0", 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -872,7 +872,7 @@ int cg_uninstall_release_agent(const char *controller) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = write_string_file_no_create(fs, "");
|
||||
r = write_string_file(fs, "", 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1708,7 +1708,7 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return write_string_file_no_create(p, value);
|
||||
return write_string_file(p, value, 0);
|
||||
}
|
||||
|
||||
int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
|
||||
|
||||
@@ -31,7 +31,7 @@ int write_string_file_atomic_label(const char *fn, const char *line) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = write_string_file_atomic(fn, line);
|
||||
r = write_string_file(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
|
||||
|
||||
mac_selinux_create_file_clear();
|
||||
|
||||
|
||||
+39
-39
@@ -27,14 +27,14 @@
|
||||
#include "ctype.h"
|
||||
#include "fileio.h"
|
||||
|
||||
int write_string_stream(FILE *f, const char *line) {
|
||||
int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
|
||||
assert(f);
|
||||
assert(line);
|
||||
|
||||
errno = 0;
|
||||
|
||||
fputs(line, f);
|
||||
if (!endswith(line, "\n"))
|
||||
if (enforce_newline && !endswith(line, "\n"))
|
||||
fputc('\n', f);
|
||||
|
||||
fflush(f);
|
||||
@@ -45,42 +45,7 @@ int write_string_stream(FILE *f, const char *line) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_string_file(const char *fn, const char *line) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
|
||||
assert(fn);
|
||||
assert(line);
|
||||
|
||||
f = fopen(fn, "we");
|
||||
if (!f)
|
||||
return -errno;
|
||||
|
||||
return write_string_stream(f, line);
|
||||
}
|
||||
|
||||
int write_string_file_no_create(const char *fn, const char *line) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
int fd;
|
||||
|
||||
assert(fn);
|
||||
assert(line);
|
||||
|
||||
/* We manually build our own version of fopen(..., "we") that
|
||||
* works without O_CREAT */
|
||||
fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
f = fdopen(fd, "we");
|
||||
if (!f) {
|
||||
safe_close(fd);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return write_string_stream(f, line);
|
||||
}
|
||||
|
||||
int write_string_file_atomic(const char *fn, const char *line) {
|
||||
static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *p = NULL;
|
||||
int r;
|
||||
@@ -94,7 +59,7 @@ int write_string_file_atomic(const char *fn, const char *line) {
|
||||
|
||||
fchmod_umask(fileno(f), 0644);
|
||||
|
||||
r = write_string_stream(f, line);
|
||||
r = write_string_stream(f, line, enforce_newline);
|
||||
if (r >= 0) {
|
||||
if (rename(p, fn) < 0)
|
||||
r = -errno;
|
||||
@@ -106,6 +71,41 @@ int write_string_file_atomic(const char *fn, const char *line) {
|
||||
return r;
|
||||
}
|
||||
|
||||
int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
|
||||
assert(fn);
|
||||
assert(line);
|
||||
|
||||
if (flags & WRITE_STRING_FILE_ATOMIC) {
|
||||
assert(flags & WRITE_STRING_FILE_CREATE);
|
||||
|
||||
return write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
|
||||
}
|
||||
|
||||
if (flags & WRITE_STRING_FILE_CREATE) {
|
||||
f = fopen(fn, "we");
|
||||
if (!f)
|
||||
return -errno;
|
||||
} else {
|
||||
int fd;
|
||||
|
||||
/* We manually build our own version of fopen(..., "we") that
|
||||
* works without O_CREAT */
|
||||
fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
f = fdopen(fd, "we");
|
||||
if (!f) {
|
||||
safe_close(fd);
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
|
||||
return write_string_stream(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
|
||||
}
|
||||
|
||||
int read_one_line_file(const char *fn, char **line) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
char t[LINE_MAX], *c;
|
||||
|
||||
+8
-4
@@ -25,10 +25,14 @@
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
int write_string_stream(FILE *f, const char *line);
|
||||
int write_string_file(const char *fn, const char *line);
|
||||
int write_string_file_no_create(const char *fn, const char *line);
|
||||
int write_string_file_atomic(const char *fn, const char *line);
|
||||
typedef enum {
|
||||
WRITE_STRING_FILE_CREATE = 1,
|
||||
WRITE_STRING_FILE_ATOMIC = 2,
|
||||
WRITE_STRING_FILE_AVOID_NEWLINE = 4,
|
||||
} WriteStringFileFlags;
|
||||
|
||||
int write_string_stream(FILE *f, const char *line, bool enforce_newline);
|
||||
int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags);
|
||||
|
||||
int read_one_line_file(const char *fn, char **line);
|
||||
int read_full_file(const char *fn, char **contents, size_t *size);
|
||||
|
||||
@@ -139,7 +139,7 @@ int mac_smack_apply_pid(pid_t pid, const char *label) {
|
||||
return 0;
|
||||
|
||||
p = procfs_file_alloca(pid, "attr/current");
|
||||
r = write_string_file(p, label);
|
||||
r = write_string_file(p, label, 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -4716,7 +4716,7 @@ int update_reboot_param_file(const char *param) {
|
||||
|
||||
if (param) {
|
||||
|
||||
r = write_string_file(REBOOT_PARAM_FILE, param);
|
||||
r = write_string_file(REBOOT_PARAM_FILE, param, WRITE_STRING_FILE_CREATE);
|
||||
if (r < 0)
|
||||
log_error("Failed to write reboot param to "
|
||||
REBOOT_PARAM_FILE": %s", strerror(-r));
|
||||
|
||||
Reference in New Issue
Block a user