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.
This commit is contained in:
Lennart Poettering
2022-11-26 11:28:05 +09:00
committed by Yu Watanabe
parent f329a41be4
commit f8cf3d19d2
6 changed files with 68 additions and 44 deletions
+29
View File
@@ -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
+2
View File
@@ -12,4 +12,6 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_partition*, fdisk_unref_partition,
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_parttype*, fdisk_unref_parttype, NULL);
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL);
int fdisk_new_context_fd(int fd, bool read_only, struct fdisk_context **ret);
#endif
+15 -1
View File
@@ -125,7 +125,6 @@ shared_sources = files(
'exit-status.h',
'extension-release.c',
'extension-release.h',
'fdisk-util.h',
'fdset.c',
'fdset.h',
'fileio-label.c',
@@ -493,3 +492,18 @@ libshared = shared_library(
dependencies : libshared_deps,
install : true,
install_dir : rootpkglibdir)
shared_fdisk_sources = files(
'fdisk-util.h',
'fdisk-util.c',
)
if get_option('fdisk') != 'false'
libshared_fdisk = static_library(
'shared-fdisk',
shared_fdisk_sources,
include_directories : includes,
dependencies : [libfdisk],
c_args : ['-fvisibility=default'],
build_by_default : false)
endif