Skip to content

Commit 2708160

Browse files
committed
fileio: optionally, return discovered path of file in search_and_fopen()
1 parent ac2c088 commit 2708160

File tree

9 files changed

+146
-71
lines changed

9 files changed

+146
-71
lines changed

src/basic/fileio.c

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,19 @@ int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **r
914914
return 0;
915915
}
916916

917-
static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
917+
static int search_and_fopen_internal(
918+
const char *path,
919+
const char *mode,
920+
const char *root,
921+
char **search,
922+
FILE **ret,
923+
char **ret_path) {
924+
918925
char **i;
919926

920927
assert(path);
921928
assert(mode);
922-
assert(_f);
929+
assert(ret);
923930

924931
if (!path_strv_resolve_uniq(search, root))
925932
return -ENOMEM;
@@ -934,7 +941,10 @@ static int search_and_fopen_internal(const char *path, const char *mode, const c
934941

935942
f = fopen(p, mode);
936943
if (f) {
937-
*_f = f;
944+
if (ret_path)
945+
*ret_path = path_simplify(TAKE_PTR(p), true);
946+
947+
*ret = f;
938948
return 0;
939949
}
940950

@@ -945,52 +955,84 @@ static int search_and_fopen_internal(const char *path, const char *mode, const c
945955
return -ENOENT;
946956
}
947957

948-
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
958+
int search_and_fopen(
959+
const char *filename,
960+
const char *mode,
961+
const char *root,
962+
const char **search,
963+
FILE **ret,
964+
char **ret_path) {
965+
949966
_cleanup_strv_free_ char **copy = NULL;
950967

951-
assert(path);
968+
assert(filename);
952969
assert(mode);
953-
assert(_f);
970+
assert(ret);
954971

955-
if (path_is_absolute(path)) {
956-
FILE *f;
972+
if (path_is_absolute(filename)) {
973+
_cleanup_fclose_ FILE *f = NULL;
957974

958-
f = fopen(path, mode);
959-
if (f) {
960-
*_f = f;
961-
return 0;
975+
f = fopen(filename, mode);
976+
if (!f)
977+
return -errno;
978+
979+
if (ret_path) {
980+
char *p;
981+
982+
p = strdup(filename);
983+
if (!p)
984+
return -ENOMEM;
985+
986+
*ret_path = path_simplify(p, true);
962987
}
963988

964-
return -errno;
989+
*ret = TAKE_PTR(f);
990+
return 0;
965991
}
966992

967993
copy = strv_copy((char**) search);
968994
if (!copy)
969995
return -ENOMEM;
970996

971-
return search_and_fopen_internal(path, mode, root, copy, _f);
997+
return search_and_fopen_internal(filename, mode, root, copy, ret, ret_path);
972998
}
973999

974-
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1000+
int search_and_fopen_nulstr(
1001+
const char *filename,
1002+
const char *mode,
1003+
const char *root,
1004+
const char *search,
1005+
FILE **ret,
1006+
char **ret_path) {
1007+
9751008
_cleanup_strv_free_ char **s = NULL;
9761009

977-
if (path_is_absolute(path)) {
978-
FILE *f;
1010+
if (path_is_absolute(filename)) {
1011+
_cleanup_fclose_ FILE *f = NULL;
9791012

980-
f = fopen(path, mode);
981-
if (f) {
982-
*_f = f;
983-
return 0;
1013+
f = fopen(filename, mode);
1014+
if (!f)
1015+
return -errno;
1016+
1017+
if (ret_path) {
1018+
char *p;
1019+
1020+
p = strdup(filename);
1021+
if (!p)
1022+
return -ENOMEM;
1023+
1024+
*ret_path = path_simplify(p, true);
9841025
}
9851026

986-
return -errno;
1027+
*ret = TAKE_PTR(f);
1028+
return 0;
9871029
}
9881030

9891031
s = strv_split_nulstr(search);
9901032
if (!s)
9911033
return -ENOMEM;
9921034

993-
return search_and_fopen_internal(path, mode, root, s, _f);
1035+
return search_and_fopen_internal(filename, mode, root, s, ret, ret_path);
9941036
}
9951037

9961038
int chase_symlinks_and_fopen_unlocked(

src/basic/fileio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ int get_proc_field(const char *filename, const char *pattern, const char *termin
8080
DIR *xopendirat(int dirfd, const char *name, int flags);
8181
int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **ret);
8282

83-
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
84-
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
83+
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret, char **ret_path);
84+
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret, char **ret_path);
8585

8686
int chase_symlinks_and_fopen_unlocked(
8787
const char *path,

src/binfmt/binfmt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,28 @@ static int apply_rule(const char *rule) {
6565

6666
static int apply_file(const char *path, bool ignore_enoent) {
6767
_cleanup_fclose_ FILE *f = NULL;
68+
_cleanup_free_ char *pp = NULL;
6869
int r;
6970

7071
assert(path);
7172

72-
r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f);
73+
r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f, &pp);
7374
if (r < 0) {
7475
if (ignore_enoent && r == -ENOENT)
7576
return 0;
7677

7778
return log_error_errno(r, "Failed to open file '%s': %m", path);
7879
}
7980

80-
log_debug("apply: %s", path);
81+
log_debug("apply: %s", pp);
8182
for (;;) {
8283
_cleanup_free_ char *line = NULL;
8384
char *p;
8485
int k;
8586

8687
k = read_line(f, LONG_LINE_MAX, &line);
8788
if (k < 0)
88-
return log_error_errno(k, "Failed to read file '%s': %m", path);
89+
return log_error_errno(k, "Failed to read file '%s': %m", pp);
8990
if (k == 0)
9091
break;
9192

src/home/homed-manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ static int manager_load_key_pair(Manager *m) {
13271327
m->private_key = NULL;
13281328
}
13291329

1330-
r = search_and_fopen_nulstr("local.private", "re", NULL, KEY_PATHS_NULSTR, &f);
1330+
r = search_and_fopen_nulstr("local.private", "re", NULL, KEY_PATHS_NULSTR, &f, NULL);
13311331
if (r == -ENOENT)
13321332
return 0;
13331333
if (r < 0)

src/modules-load/modules-load.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,29 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
6262

6363
static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
6464
_cleanup_fclose_ FILE *f = NULL;
65+
_cleanup_free_ char *pp = NULL;
6566
int r;
6667

6768
assert(ctx);
6869
assert(path);
6970

70-
r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
71+
r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp);
7172
if (r < 0) {
7273
if (ignore_enoent && r == -ENOENT)
7374
return 0;
7475

7576
return log_error_errno(r, "Failed to open %s: %m", path);
7677
}
7778

78-
log_debug("apply: %s", path);
79+
log_debug("apply: %s", pp);
7980
for (;;) {
8081
_cleanup_free_ char *line = NULL;
8182
char *l;
8283
int k;
8384

8485
k = read_line(f, LONG_LINE_MAX, &line);
8586
if (k < 0)
86-
return log_error_errno(k, "Failed to read file '%s': %m", path);
87+
return log_error_errno(k, "Failed to read file '%s': %m", pp);
8788
if (k == 0)
8889
break;
8990

src/sysctl/sysctl.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,21 @@ static int apply_all(OrderedHashmap *sysctl_options) {
182182

183183
static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent) {
184184
_cleanup_fclose_ FILE *f = NULL;
185+
_cleanup_free_ char *pp = NULL;
185186
unsigned c = 0;
186187
int r;
187188

188189
assert(path);
189190

190-
r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f);
191+
r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("sysctl.d"), &f, &pp);
191192
if (r < 0) {
192193
if (ignore_enoent && r == -ENOENT)
193194
return 0;
194195

195196
return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
196197
}
197198

198-
log_debug("Parsing %s", path);
199+
log_debug("Parsing %s", pp);
199200
for (;;) {
200201
_cleanup_(option_freep) Option *new_option = NULL;
201202
_cleanup_free_ char *l = NULL;
@@ -208,7 +209,7 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
208209
if (k == 0)
209210
break;
210211
if (k < 0)
211-
return log_error_errno(k, "Failed to read file '%s', ignoring: %m", path);
212+
return log_error_errno(k, "Failed to read file '%s', ignoring: %m", pp);
212213

213214
c++;
214215

@@ -235,7 +236,7 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
235236
/* We have a "negative match" option. Let's continue with value==NULL. */
236237
p++;
237238
else {
238-
log_syntax(NULL, LOG_WARNING, path, c, 0,
239+
log_syntax(NULL, LOG_WARNING, pp, c, 0,
239240
"Line is not an assignment, ignoring: %s", p);
240241
if (r == 0)
241242
r = -EINVAL;
@@ -261,7 +262,7 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
261262
continue;
262263
}
263264

264-
log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
265+
log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, pp, c);
265266
option_free(ordered_hashmap_remove(*sysctl_options, p));
266267
}
267268

src/sysusers/sysusers.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
17281728

17291729
static int read_config_file(const char *fn, bool ignore_enoent) {
17301730
_cleanup_fclose_ FILE *rf = NULL;
1731+
_cleanup_free_ char *pp = NULL;
17311732
FILE *f = NULL;
17321733
unsigned v = 0;
17331734
int r = 0;
@@ -1737,7 +1738,7 @@ static int read_config_file(const char *fn, bool ignore_enoent) {
17371738
if (streq(fn, "-"))
17381739
f = stdin;
17391740
else {
1740-
r = search_and_fopen(fn, "re", arg_root, (const char**) CONF_PATHS_STRV("sysusers.d"), &rf);
1741+
r = search_and_fopen(fn, "re", arg_root, (const char**) CONF_PATHS_STRV("sysusers.d"), &rf, &pp);
17411742
if (r < 0) {
17421743
if (ignore_enoent && r == -ENOENT)
17431744
return 0;
@@ -1746,6 +1747,7 @@ static int read_config_file(const char *fn, bool ignore_enoent) {
17461747
}
17471748

17481749
f = rf;
1750+
fn = pp;
17491751
}
17501752

17511753
for (;;) {

0 commit comments

Comments
 (0)