#include "alloc-util.h"
#include "chase-symlinks.h"
#include "fd-util.h"
+#include "fileio.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "log.h"
return 1;
}
+
+int chase_symlinks_and_fopen_unlocked(
+ const char *path,
+ const char *root,
+ unsigned chase_flags,
+ const char *open_flags,
+ char **ret_path,
+ FILE **ret_file) {
+
+ _cleanup_free_ char *final_path = NULL;
+ _cleanup_close_ int fd = -1;
+ int mode_flags, r;
+
+ assert(path);
+ assert(open_flags);
+ assert(ret_file);
+
+ mode_flags = fopen_mode_to_flags(open_flags);
+ if (mode_flags < 0)
+ return mode_flags;
+
+ fd = chase_symlinks_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
+ if (fd < 0)
+ return fd;
+
+ r = take_fdopen_unlocked(&fd, open_flags, ret_file);
+ if (r < 0)
+ return r;
+
+ if (ret_path)
+ *ret_path = TAKE_PTR(final_path);
+
+ return 0;
+}
int chase_symlinks_and_open(const char *path, const char *root, unsigned chase_flags, int open_flags, char **ret_path);
int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chase_flags, char **ret_path, DIR **ret_dir);
int chase_symlinks_and_stat(const char *path, const char *root, unsigned chase_flags, char **ret_path, struct stat *ret_stat, int *ret_fd);
+
+int chase_symlinks_and_fopen_unlocked(const char *path, const char *root, unsigned chase_flags, const char *open_flags, char **ret_path, FILE **ret_file);
return d;
}
-static int mode_to_flags(const char *mode) {
+int fopen_mode_to_flags(const char *mode) {
const char *p;
int flags;
+ assert(mode);
+
if ((p = startswith(mode, "r+")))
flags = O_RDWR;
else if ((p = startswith(mode, "r")))
} else {
int fd, mode_flags;
- mode_flags = mode_to_flags(mode);
+ mode_flags = fopen_mode_to_flags(mode);
if (mode_flags < 0)
return mode_flags;
return search_and_fopen_internal(filename, mode, root, s, ret, ret_path);
}
-int chase_symlinks_and_fopen_unlocked(
- const char *path,
- const char *root,
- unsigned chase_flags,
- const char *open_flags,
- FILE **ret_file,
- char **ret_path) {
-
- _cleanup_close_ int fd = -1;
- _cleanup_free_ char *final_path = NULL;
- int mode_flags, r;
-
- assert(path);
- assert(open_flags);
- assert(ret_file);
-
- mode_flags = mode_to_flags(open_flags);
- if (mode_flags < 0)
- return mode_flags;
-
- fd = chase_symlinks_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
- if (fd < 0)
- return fd;
-
- r = take_fdopen_unlocked(&fd, open_flags, ret_file);
- if (r < 0)
- return r;
-
- if (ret_path)
- *ret_path = TAKE_PTR(final_path);
- return 0;
-}
-
int fflush_and_check(FILE *f) {
assert(f);
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret, char **ret_path);
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret, char **ret_path);
-int chase_symlinks_and_fopen_unlocked(
- const char *path,
- const char *root,
- unsigned chase_flags,
- const char *open_flags,
- FILE **ret_file,
- char **ret_path);
-
int fflush_and_check(FILE *f);
int fflush_sync_and_check(FILE *f);
int safe_fgetc(FILE *f, char *ret);
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);
+
+int fopen_mode_to_flags(const char *mode);
assert(directory);
assert(name || uid_is_valid(uid));
- r = chase_symlinks_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", &f, NULL);
+ r = chase_symlinks_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
if (r == -ENOENT)
return 0; /* no user database? then no user, hence no collision */
if (r < 0)
assert(directory);
assert(name || gid_is_valid(gid));
- r = chase_symlinks_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", &f, NULL);
+ r = chase_symlinks_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
if (r == -ENOENT)
return 0; /* no group database? then no group, hence no collision */
if (r < 0)
if (!path)
path = "/etc/login.defs";
- r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", &f, NULL);
+ r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
if (r == -ENOENT)
goto assign;
if (r < 0)