| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
/** |
| 5 |
* @file |
| 6 |
* |
| 7 |
* Input/output helpers. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef DRGN_IO_H |
| 11 |
#define DRGN_IO_H |
| 12 |
|
| 13 |
#include <stddef.h> |
| 14 |
#include <sys/types.h> |
| 15 |
|
| 16 |
/** |
| 17 |
* Wrapper around \manpage{read,2} that never returns less bytes than requested unless it |
| 18 |
* hits end-of-file. |
| 19 |
*/ |
| 20 |
ssize_t read_all(int fd, void *buf, size_t count); |
| 21 |
|
| 22 |
/** |
| 23 |
* Wrapper around \manpage{write,2} that never writes less bytes than requested. |
| 24 |
* |
| 25 |
* @return 0 on success, -1 on error. |
| 26 |
*/ |
| 27 |
int write_all(int fd, const void *buf, size_t count); |
| 28 |
|
| 29 |
/** |
| 30 |
* Wrapper around \manpage{pread,2} that never returns less bytes than requested unless |
| 31 |
* it hits end-of-file. |
| 32 |
*/ |
| 33 |
ssize_t pread_all(int fd, void *buf, size_t count, off_t offset); |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the canonical path of a file descriptor. |
| 37 |
* |
| 38 |
* This returns the first of the following that succeeds: |
| 39 |
* |
| 40 |
* 1. `readlink("/proc/self/fd/{fd}")` |
| 41 |
* 2. `realpath(path)` if @p path is not @c NULL |
| 42 |
* 3. `"/proc/self/fd/{fd}"` if @p path is @c NULL, `path` otherwise |
| 43 |
* |
| 44 |
* @return Returned path, or @c NULL if memory could not be allocated. On |
| 45 |
* success, must be freed with @c free(). |
| 46 |
*/ |
| 47 |
char *fd_canonical_path(int fd, const char *path); |
| 48 |
|
| 49 |
#endif /* DRGN_IO_H */ |