diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-11-23 16:23:35 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-11-26 11:28:05 +0900 |
commit | f8cf3d19d2013d8e152720a1115e47a5b2b2fb7d (patch) | |
tree | 42beb93e324619cb67c7f1a926bf3a31534f8445 /src/shared/fdisk-util.c | |
parent | f329a41be44894a72ea71de1a96db3b50051ad24 (diff) | |
download | systemd-f8cf3d19d2013d8e152720a1115e47a5b2b2fb7d.tar.gz |
fdisk: introduce common fdisk_new_context_fd() helper
We do the same thing over and over again and it's a bit ugly, hence
let's unify the code for it at one common place.
Diffstat (limited to 'src/shared/fdisk-util.c')
-rw-r--r-- | src/shared/fdisk-util.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/shared/fdisk-util.c b/src/shared/fdisk-util.c new file mode 100644 index 0000000000..1cdf09b18d --- /dev/null +++ b/src/shared/fdisk-util.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "fd-util.h" +#include "fdisk-util.h" + +#if HAVE_LIBFDISK + +int fdisk_new_context_fd(int fd, bool read_only, struct fdisk_context **ret) { + _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; + int r; + + assert(ret); + + if (fd < 0) + return -EBADF; + + c = fdisk_new_context(); + if (!c) + return -ENOMEM; + + r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), read_only); + if (r < 0) + return r; + + *ret = TAKE_PTR(c); + return 0; +} + +#endif |