Handle -h and -V

This commit is contained in:
наб 2020-10-18 04:00:41 +02:00
parent 2860520271
commit fb3eb012b6
No known key found for this signature in database
GPG Key ID: BCFD0B018D2658F1
5 changed files with 23 additions and 11 deletions

View File

@ -14,7 +14,7 @@ Plus it's a pretty good annoyed sigh onomatopoeia.
### Building
You'll need `pkg-config`, `libzfslinux-dev`, `libtss2-dev`, and `make` should hopefully Just Work™ if you have a C++17-capable compiler.
You'll need `pkg-config`, `ronn`, `libzfslinux-dev`, `libtss2-dev`, and `make` should hopefully Just Work™ if you have a C++17-capable compiler.
The output binaries are trimmed of extraneous dependencies, so they're all just libc + libzfs and friends + TPM back-end.
### Installation

View File

@ -44,7 +44,7 @@ slice_iter<uint8_t> end(TPM2B_DIGEST & dg) {
int main(int argc, char ** argv) {
const char * backup{};
return do_main(
argc, argv, "b:", [&](auto) { backup = optarg; },
argc, argv, "b:", "[-b backup-file]", [&](auto) { backup = optarg; },
[&](auto dataset) {
REQUIRE_KEY_LOADED(dataset);

View File

@ -15,7 +15,7 @@
int main(int argc, char ** argv) {
return do_main(
argc, argv, "", [&](auto) {},
argc, argv, "", "", [&](auto) {},
[&](auto dataset) {
REQUIRE_KEY_LOADED(dataset);

View File

@ -19,7 +19,7 @@
int main(int argc, char ** argv) {
auto noop = B_FALSE;
return do_main(
argc, argv, "n", [&](auto) { noop = B_TRUE; },
argc, argv, "n", "[-n]", [&](auto) { noop = B_TRUE; },
[&](auto dataset) {
TPMI_DH_PERSISTENT handle{};
TRY_MAIN(parse_key_props(dataset, THIS_BACKEND, handle));

View File

@ -19,7 +19,7 @@
template <class G, class M>
int do_main(int argc, char ** argv, const char * getoptions, G && getoptfn, M && main) {
int do_main(int argc, char ** argv, const char * getoptions, const char * usage, G && getoptfn, M && main) {
const auto libz = TRY_PTR("initialise libzfs", libzfs_init());
quickscope_wrapper libz_deleter{[=] { libzfs_fini(libz); }};
@ -28,14 +28,26 @@ int do_main(int argc, char ** argv, const char * getoptions, G && getoptfn, M &&
#if __GLIBC__
setenv("POSIXLY_CORRECT", "1", true);
#endif
for(int opt; (opt = getopt(argc, argv, getoptions)) != -1;)
if(opt == '?')
return __LINE__;
else
getoptfn(opt);
auto gopts = reinterpret_cast<char *>(TRY_PTR("allocate options string", alloca(strlen(getoptions) + 2 + 1)));
snprintf(gopts, strlen(getoptions) + 2 + 1, "%shV", getoptions);
for(int opt; (opt = getopt(argc, argv, gopts)) != -1;)
switch(opt) {
case '?':
case 'h':
fprintf(opt == 'h' ? stdout : stderr, "Usage: %s [-hV] %s%s<dataset>\n", argv[0], usage, strlen(usage) ? " " : "");
return opt == 'h' ? 0 : __LINE__;
case 'V':
printf("tzpfms version %s\n", TZPFMS_VERSION);
return 0;
default:
getoptfn(opt);
}
if(optind >= argc) {
fprintf(stderr, "No dataset to act on?\n");
fprintf(stderr,
"No dataset to act on?\n"
"Usage: %s [-hV] %s%s<dataset>\n",
argv[0], usage, strlen(usage) ? " " : "");
return __LINE__;
}
auto dataset = TRY_PTR(nullptr, zfs_open(libz, argv[optind], ZFS_TYPE_FILESYSTEM));