| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "escape.h" |
| 5 |
#include "extract-word.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "open-file.h" |
| 8 |
#include "path-util.h" |
| 9 |
#include "string-table.h" |
| 10 |
#include "string-util.h" |
| 11 |
|
| 12 |
int open_file_parse(const char *v, OpenFile **ret) { |
| 13 |
_cleanup_free_ char *options = NULL; |
| 14 |
_cleanup_(open_file_freep) OpenFile *of = NULL; |
| 15 |
int r; |
| 16 |
|
| 17 |
assert(v); |
| 18 |
assert(ret); |
| 19 |
|
| 20 |
of = new0(OpenFile, 1); |
| 21 |
if (!of) |
| 22 |
return -ENOMEM; |
| 23 |
|
| 24 |
r = extract_many_words(&v, ":", EXTRACT_DONT_COALESCE_SEPARATORS|EXTRACT_CUNESCAPE, &of->path, &of->fdname, &options); |
| 25 |
if (r < 0) |
| 26 |
return r; |
| 27 |
if (r == 0) |
| 28 |
return -EINVAL; |
| 29 |
|
| 30 |
/* Enforce that at most 3 colon-separated words are present */ |
| 31 |
if (!isempty(v)) |
| 32 |
return -EINVAL; |
| 33 |
|
| 34 |
for (const char *p = options;;) { |
| 35 |
OpenFileFlag flag; |
| 36 |
_cleanup_free_ char *word = NULL; |
| 37 |
|
| 38 |
r = extract_first_word(&p, &word, ",", 0); |
| 39 |
if (r < 0) |
| 40 |
return r; |
| 41 |
if (r == 0) |
| 42 |
break; |
| 43 |
|
| 44 |
flag = open_file_flags_from_string(word); |
| 45 |
if (flag < 0) |
| 46 |
return flag; |
| 47 |
|
| 48 |
if ((flag & of->flags) != 0) |
| 49 |
return -EINVAL; |
| 50 |
|
| 51 |
of->flags |= flag; |
| 52 |
} |
| 53 |
|
| 54 |
if (isempty(of->fdname)) { |
| 55 |
of->fdname = mfree(of->fdname); |
| 56 |
r = path_extract_filename(of->path, &of->fdname); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
} |
| 60 |
|
| 61 |
r = open_file_validate(of); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
*ret = TAKE_PTR(of); |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
int open_file_validate(const OpenFile *of) { |
| 71 |
assert(of); |
| 72 |
|
| 73 |
if (!path_is_valid(of->path) || !path_is_absolute(of->path)) |
| 74 |
return -EINVAL; |
| 75 |
|
| 76 |
if (!fdname_is_valid(of->fdname)) |
| 77 |
return -EINVAL; |
| 78 |
|
| 79 |
if (FLAGS_SET(of->flags, OPENFILE_READ_ONLY) + |
| 80 |
FLAGS_SET(of->flags, OPENFILE_APPEND) + |
| 81 |
FLAGS_SET(of->flags, OPENFILE_TRUNCATE) > 1) |
| 82 |
return -EINVAL; |
| 83 |
|
| 84 |
if ((of->flags & ~_OPENFILE_MASK_PUBLIC) != 0) |
| 85 |
return -EINVAL; |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int open_file_to_string(const OpenFile *of, char **ret) { |
| 91 |
_cleanup_free_ char *options = NULL, *fname = NULL, *s = NULL; |
| 92 |
bool has_fdname = false; |
| 93 |
int r; |
| 94 |
|
| 95 |
assert(of); |
| 96 |
assert(ret); |
| 97 |
|
| 98 |
s = xescape(of->path, ":"); |
| 99 |
if (!s) |
| 100 |
return -ENOMEM; |
| 101 |
|
| 102 |
r = path_extract_filename(of->path, &fname); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
has_fdname = !streq(fname, of->fdname); |
| 107 |
if (has_fdname) |
| 108 |
if (!strextend(&s, ":", of->fdname)) |
| 109 |
return -ENOMEM; |
| 110 |
|
| 111 |
for (OpenFileFlag flag = OPENFILE_READ_ONLY; flag < _OPENFILE_MAX; flag <<= 1) |
| 112 |
if (FLAGS_SET(of->flags, flag) && !strextend_with_separator(&options, ",", open_file_flags_to_string(flag))) |
| 113 |
return -ENOMEM; |
| 114 |
|
| 115 |
if (options) |
| 116 |
if (!(has_fdname ? strextend(&s, ":", options) : strextend(&s, "::", options))) |
| 117 |
return -ENOMEM; |
| 118 |
|
| 119 |
*ret = TAKE_PTR(s); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
OpenFile* open_file_free(OpenFile *of) { |
| 125 |
if (!of) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
free(of->path); |
| 129 |
free(of->fdname); |
| 130 |
|
| 131 |
return mfree(of); |
| 132 |
} |
| 133 |
|
| 134 |
static const char * const open_file_flags_table[_OPENFILE_MAX] = { |
| 135 |
[OPENFILE_READ_ONLY] = "read-only", |
| 136 |
[OPENFILE_APPEND] = "append", |
| 137 |
[OPENFILE_TRUNCATE] = "truncate", |
| 138 |
[OPENFILE_GRACEFUL] = "graceful", |
| 139 |
}; |
| 140 |
|
| 141 |
DEFINE_STRING_TABLE_LOOKUP(open_file_flags, OpenFileFlag); |