}
int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) {
- int r;
+ int r;
assert(fd);
const struct timespec *ts) {
bool needs_nl;
- int r, fd;
+ int r, fd = -1;
assert(f);
assert(line);
needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
- /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
- * that the write goes out in one go, instead of two */
+ /* If STDIO buffering was disabled, then let's append the newline character to the string
+ * itself, so that the write goes out in one go, instead of two */
line = strjoina(line, "\n");
needs_nl = false;
if (ts) {
const struct timespec twice[2] = {*ts, *ts};
+ assert(fd >= 0);
if (futimens(fd, twice) < 0)
return -errno;
}
/* OK, the operation failed, but let's see if the right
* contents in place already. If so, eat up the error. */
- q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+ q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) || (flags & WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE));
if (q <= 0)
return r;
return 1;
}
-int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) {
+int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) {
_cleanup_free_ char *buf = NULL;
_cleanup_close_ int fd = -1;
- struct stat st;
size_t n, size;
int n_retries;
+ bool truncated = false;
assert(ret_contents);
- /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work
- * with two sorts of virtual files. One sort uses "seq_file", and the results of
- * the first read are buffered for the second read. The other sort uses "raw"
- * reads which always go direct to the device. In the latter case, the content of
- * the virtual file must be retrieved with a single read otherwise a second read
- * might get the new value instead of finding EOF immediately. That's the reason
- * why the usage of fread(3) is prohibited in this case as it always performs a
- * second call to read(2) looking for EOF. See issue 13585. */
+ /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work with two sorts of
+ * virtual files. One sort uses "seq_file", and the results of the first read are buffered for the
+ * second read. The other sort uses "raw" reads which always go direct to the device. In the latter
+ * case, the content of the virtual file must be retrieved with a single read otherwise a second read
+ * might get the new value instead of finding EOF immediately. That's the reason why the usage of
+ * fread(3) is prohibited in this case as it always performs a second call to read(2) looking for
+ * EOF. See issue #13585.
+ *
+ * max_size specifies a limit on the bytes read. If max_size is SIZE_MAX, the full file is read. If
+ * the the full file is too large to read, an error is returned. For other values of max_size,
+ * *partial contents* may be returned. (Though the read is still done using one syscall.)
+ * Returns 0 on partial success, 1 if untruncated contents were read. */
fd = open(filename, O_RDONLY|O_CLOEXEC);
if (fd < 0)
return -errno;
+ assert(max_size <= READ_FULL_BYTES_MAX || max_size == SIZE_MAX);
+
/* Limit the number of attempts to read the number of bytes returned by fstat(). */
n_retries = 3;
for (;;) {
+ struct stat st;
+
if (fstat(fd, &st) < 0)
return -errno;
/* Be prepared for files from /proc which generally report a file size of 0. */
assert_cc(READ_FULL_BYTES_MAX < SSIZE_MAX);
if (st.st_size > 0) {
- if (st.st_size > READ_FULL_BYTES_MAX)
+ if (st.st_size > SSIZE_MAX) /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
+ return -EFBIG;
+
+ size = MIN((size_t) st.st_size, max_size);
+ if (size > READ_FULL_BYTES_MAX)
return -EFBIG;
- size = st.st_size;
n_retries--;
} else {
- size = READ_FULL_BYTES_MAX;
+ size = MIN(READ_FULL_BYTES_MAX, max_size);
n_retries = 0;
}
if (!buf)
return -ENOMEM;
/* Use a bigger allocation if we got it anyway, but not more than the limit. */
- size = MIN(malloc_usable_size(buf) - 1, READ_FULL_BYTES_MAX);
+ size = MIN3(malloc_usable_size(buf) - 1, max_size, READ_FULL_BYTES_MAX);
for (;;) {
ssize_t k;
* processing, let's try again either with a bigger guessed size or the new
* file size. */
- if (n_retries <= 0)
- return st.st_size > 0 ? -EIO : -EFBIG;
+ if (n_retries <= 0) {
+ if (max_size == SIZE_MAX)
+ return st.st_size > 0 ? -EIO : -EFBIG;
+
+ /* Accept a short read, but truncate it appropropriately. */
+ n = MIN(n, max_size);
+ truncated = true;
+ break;
+ }
if (lseek(fd, 0, SEEK_SET) < 0)
return -errno;
buf[n] = 0;
*ret_contents = TAKE_PTR(buf);
- return 0;
+ return !truncated;
}
int read_full_stream_full(
return 0;
}
-static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
+static int search_and_fopen_internal(
+ const char *path,
+ const char *mode,
+ const char *root,
+ char **search,
+ FILE **ret,
+ char **ret_path) {
+
char **i;
assert(path);
assert(mode);
- assert(_f);
+ assert(ret);
if (!path_strv_resolve_uniq(search, root))
return -ENOMEM;
f = fopen(p, mode);
if (f) {
- *_f = f;
+ if (ret_path)
+ *ret_path = path_simplify(TAKE_PTR(p), true);
+
+ *ret = f;
return 0;
}
return -ENOENT;
}
-int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
+int search_and_fopen(
+ const char *filename,
+ const char *mode,
+ const char *root,
+ const char **search,
+ FILE **ret,
+ char **ret_path) {
+
_cleanup_strv_free_ char **copy = NULL;
- assert(path);
+ assert(filename);
assert(mode);
- assert(_f);
+ assert(ret);
- if (path_is_absolute(path)) {
- FILE *f;
+ if (path_is_absolute(filename)) {
+ _cleanup_fclose_ FILE *f = NULL;
- f = fopen(path, mode);
- if (f) {
- *_f = f;
- return 0;
+ f = fopen(filename, mode);
+ if (!f)
+ return -errno;
+
+ if (ret_path) {
+ char *p;
+
+ p = strdup(filename);
+ if (!p)
+ return -ENOMEM;
+
+ *ret_path = path_simplify(p, true);
}
- return -errno;
+ *ret = TAKE_PTR(f);
+ return 0;
}
copy = strv_copy((char**) search);
if (!copy)
return -ENOMEM;
- return search_and_fopen_internal(path, mode, root, copy, _f);
+ return search_and_fopen_internal(filename, mode, root, copy, ret, ret_path);
}
-int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
+int search_and_fopen_nulstr(
+ const char *filename,
+ const char *mode,
+ const char *root,
+ const char *search,
+ FILE **ret,
+ char **ret_path) {
+
_cleanup_strv_free_ char **s = NULL;
- if (path_is_absolute(path)) {
- FILE *f;
+ if (path_is_absolute(filename)) {
+ _cleanup_fclose_ FILE *f = NULL;
- f = fopen(path, mode);
- if (f) {
- *_f = f;
- return 0;
+ f = fopen(filename, mode);
+ if (!f)
+ return -errno;
+
+ if (ret_path) {
+ char *p;
+
+ p = strdup(filename);
+ if (!p)
+ return -ENOMEM;
+
+ *ret_path = path_simplify(p, true);
}
- return -errno;
+ *ret = TAKE_PTR(f);
+ return 0;
}
s = strv_split_nulstr(search);
if (!s)
return -ENOMEM;
- return search_and_fopen_internal(path, mode, root, s, _f);
+ return search_and_fopen_internal(filename, mode, root, s, ret, ret_path);
}
int chase_symlinks_and_fopen_unlocked(
_cleanup_close_ int fd = -1;
_cleanup_free_ char *final_path = NULL;
int mode_flags, r;
- FILE *f;
assert(path);
assert(open_flags);
if (fd < 0)
return fd;
- r = fdopen_unlocked(fd, open_flags, &f);
+ r = take_fdopen_unlocked(&fd, open_flags, ret_file);
if (r < 0)
return r;
- TAKE_FD(fd);
- *ret_file = f;
if (ret_path)
*ret_path = TAKE_PTR(final_path);
return 0;