getopt() -> getopt_long()

Kill all remaining getopt() uses - all options can now be passed as
longopts.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2025-07-04 12:34:24 -04:00
parent 7d69a303d1
commit e7cc6bb9cf
6 changed files with 79 additions and 21 deletions

View File

@ -26,9 +26,13 @@ static void data_rereplicate_usage(void)
static int cmd_data_rereplicate(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL }
};
int opt;
while ((opt = getopt(argc, argv, "h")) != -1)
while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (opt) {
case 'h':
data_rereplicate_usage();
@ -262,16 +266,23 @@ static void data_job_usage(void)
"job: one of scrub, rereplicate, migrate, rewrite_old_nodes, or drop_extra_replicas\n"
"\n"
"Options:\n"
" -b btree btree to operate on\n"
" -s inode:offset start position\n"
" -e inode:offset end position\n"
" -h, --help display this help and exit\n"
" -b, --btree btree btree to operate on\n"
" -s, --start inode:offset start position\n"
" -e, --end inode:offset end position\n"
" -h, --help display this help and exit\n"
"Report bugs to <linux-bcachefs@vger.kernel.org>");
exit(EXIT_SUCCESS);
}
static int cmd_data_job(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "btree", required_argument, NULL, 'b' },
{ "start", required_argument, NULL, 's' },
{ "end", required_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
struct bch_ioctl_data op = {
.start_btree = 0,
.start_pos = POS_MIN,
@ -280,7 +291,7 @@ static int cmd_data_job(int argc, char *argv[])
};
int opt;
while ((opt = getopt(argc, argv, "s:e:h")) != -1)
while ((opt = getopt_long(argc, argv, "b:s:e:h", longopts, NULL)) != -1)
switch (opt) {
case 'b':
op.start_btree = read_string_list_or_die(optarg,
@ -290,8 +301,8 @@ static int cmd_data_job(int argc, char *argv[])
case 's':
op.start_pos = bpos_parse(optarg);
break;
op.end_pos = bpos_parse(optarg);
case 'e':
op.end_pos = bpos_parse(optarg);
break;
case 'h':
data_job_usage();

View File

@ -212,9 +212,13 @@ static void device_online_usage(void)
static int cmd_device_online(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL }
};
int opt;
while ((opt = getopt(argc, argv, "h")) != -1)
while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (opt) {
case 'h':
device_online_usage();
@ -293,9 +297,13 @@ static void device_evacuate_usage(void)
static int cmd_device_evacuate(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL }
};
int opt;
while ((opt = getopt(argc, argv, "h")) != -1)
while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (opt) {
case 'h':
device_evacuate_usage();

View File

@ -47,6 +47,24 @@ static int do_splice(int rfd, int wfd)
return 1;
do {
ssize_t w = write(wfd, b, r);
/*
* Ugly, but we have no way of doing nonblocking reads and
* blocking writes.
*
* Yes, this means that if one thread has stopped reading (or
* isn't keeping up) we block traffic on the other direction of
* the pipe. No, I don't care.
*/
if (w < 0 && errno == EAGAIN) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(wfd, &fds);
if (select(wfd + 1, NULL, &fds, NULL, NULL) < 0)
die("select error: %m");
continue;
}
if (w < 0)
die("%s: write error: %m", __func__);
r -= w;

View File

@ -1,5 +1,6 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <uuid/uuid.h>
@ -15,16 +16,23 @@ static void unlock_usage(void)
"Usage: bcachefs unlock [OPTION] device\n"
"\n"
"Options:\n"
" -c Check if a device is encrypted\n"
" -k (session|user|user_session)\n"
" -c, --check Check if a device is encrypted\n"
" -k, --keyring (session|user|user_session)\n"
" Keyring to add to (default: user)\n"
" -f Passphrase file to read from (disables passphrase prompt)\n"
" -h Display this help and exit\n"
" -f, --file Passphrase file to read from (disables passphrase prompt)\n"
" -h, --help Display this help and exit\n"
"Report bugs to <linux-bcachefs@vger.kernel.org>");
}
int cmd_unlock(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "check", no_argument, NULL, 'c' },
{ "keyring", required_argument, NULL, 'k' },
{ "file", required_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
const char *keyring = "user";
bool check = false;
const char *passphrase_file_path = NULL;
@ -32,7 +40,7 @@ int cmd_unlock(int argc, char *argv[])
int opt;
while ((opt = getopt(argc, argv, "cf:k:h")) != -1)
while ((opt = getopt_long(argc, argv, "cf:k:h", longopts, NULL)) != -1)
switch (opt) {
case 'c':
check = true;

View File

@ -1,4 +1,5 @@
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -20,8 +21,8 @@ static void kill_btree_node_usage(void)
"Usage: bcachefs kill_btree_node [OPTION]... <devices>\n"
"\n"
"Options:\n"
" -n btree:level:idx Node to kill\n"
" -d dev Device index (default: kill all replicas)\n"
" -n, --node btree:level:idx Node to kill\n"
" -d, --dev dev Device index (default: kill all replicas)\n"
" -h Display this help and exit\n"
"Report bugs to <linux-bcachefs@vger.kernel.org>");
}
@ -34,13 +35,19 @@ struct kill_node {
int cmd_kill_btree_node(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "node", required_argument, NULL, 'n' },
{ "dev", required_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
struct bch_opts opts = bch2_opts_empty();
DARRAY(struct kill_node) kill_nodes = {};
int opt, dev_idx = -1;
opt_set(opts, read_only, true);
while ((opt = getopt(argc, argv, "n:d:h")) != -1)
while ((opt = getopt_long(argc, argv, "n:d:h", longopts, NULL)) != -1)
switch (opt) {
case 'n': {
char *p = optarg;

View File

@ -370,19 +370,25 @@ static void migrate_superblock_usage(void)
"Usage: bcachefs migrate-superblock [OPTION]...\n"
"\n"
"Options:\n"
" -d device Device to create superblock for\n"
" -o offset Offset of existing superblock\n"
" -h Display this help and exit\n"
" -d, --dev device Device to create superblock for\n"
" -o, --offset offset Offset of existing superblock\n"
" -h, --help Display this help and exit\n"
"Report bugs to <linux-bcachefs@vger.kernel.org>");
}
int cmd_migrate_superblock(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "dev", required_argument, NULL, 'd' },
{ "offset", required_argument, NULL, 'o' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
darray_const_str devs = {};
u64 sb_offset = 0;
int opt, ret;
while ((opt = getopt(argc, argv, "d:o:h")) != -1)
while ((opt = getopt_long(argc, argv, "d:o:h", longopts, NULL)) != -1)
switch (opt) {
case 'd':
darray_push(&devs, optarg);