mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
g13: New command --find-device.
* common/status.h (STATUS_BLOCKDEV: New. * g13/call-syshelp.c: Include "call-syshelp.h". (finddevice_status_cb, call_syshelp_find_device): New. * g13/g13.c (aFindDevice): New. (opts): Add "--find-device". (main): Implement --find-device. * g13/sh-cmd.c (cmd_finddevice): New. (register_commands): Register new command. -- This might be useful for scripting. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
3a75ff65fb
commit
b57f553212
5 changed files with 162 additions and 0 deletions
77
g13/sh-cmd.c
77
g13/sh-cmd.c
|
@ -157,6 +157,82 @@ reset_notify (assuan_context_t ctx, char *line)
|
|||
}
|
||||
|
||||
|
||||
static const char hlp_finddevice[] =
|
||||
"FINDDEVICE <name>\n"
|
||||
"\n"
|
||||
"Find the device matching NAME. NAME be any identifier from\n"
|
||||
"g13tab permissable for the user. The corresponding block\n"
|
||||
"device is retruned using a status line.";
|
||||
static gpg_error_t
|
||||
cmd_finddevice (assuan_context_t ctx, char *line)
|
||||
{
|
||||
ctrl_t ctrl = assuan_get_pointer (ctx);
|
||||
gpg_error_t err = 0;
|
||||
tab_item_t ti;
|
||||
const char *s;
|
||||
const char *name;
|
||||
|
||||
name = skip_options (line);
|
||||
|
||||
/* Are we allowed to use the given device? We check several names:
|
||||
* 1. The full block device
|
||||
* 2. The label
|
||||
* 3. The final part of the block device if NAME does not have a slash.
|
||||
* 4. The mountpoint
|
||||
*/
|
||||
for (ti=ctrl->client.tab; ti; ti = ti->next)
|
||||
if (!strcmp (name, ti->blockdev))
|
||||
break;
|
||||
if (!ti)
|
||||
{
|
||||
for (ti=ctrl->client.tab; ti; ti = ti->next)
|
||||
if (ti->label && !strcmp (name, ti->label))
|
||||
break;
|
||||
}
|
||||
if (!ti && !strchr (name, '/'))
|
||||
{
|
||||
for (ti=ctrl->client.tab; ti; ti = ti->next)
|
||||
{
|
||||
s = strrchr (ti->blockdev, '/');
|
||||
if (s && s[1] && !strcmp (name, s+1))
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ti)
|
||||
{
|
||||
for (ti=ctrl->client.tab; ti; ti = ti->next)
|
||||
if (ti->mountpoint && !strcmp (name, ti->mountpoint))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ti)
|
||||
{
|
||||
err = set_error (GPG_ERR_NOT_FOUND, "device not configured for user");
|
||||
goto leave;
|
||||
}
|
||||
|
||||
/* Check whether we have permissions to open the device. */
|
||||
{
|
||||
estream_t fp = es_fopen (ti->blockdev, "rb");
|
||||
if (!fp)
|
||||
{
|
||||
err = gpg_error_from_syserror ();
|
||||
log_error ("error opening '%s': %s\n",
|
||||
ti->blockdev, gpg_strerror (err));
|
||||
goto leave;
|
||||
}
|
||||
es_fclose (fp);
|
||||
}
|
||||
|
||||
err = g13_status (ctrl, STATUS_BLOCKDEV, ti->blockdev, NULL);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
leave:
|
||||
return leave_cmd (ctx, err);
|
||||
}
|
||||
|
||||
|
||||
static const char hlp_device[] =
|
||||
"DEVICE <name>\n"
|
||||
"\n"
|
||||
|
@ -588,6 +664,7 @@ register_commands (assuan_context_t ctx, int fail_all)
|
|||
assuan_handler_t handler;
|
||||
const char * const help;
|
||||
} table[] = {
|
||||
{ "FINDDEVICE", cmd_finddevice, hlp_finddevice },
|
||||
{ "DEVICE", cmd_device, hlp_device },
|
||||
{ "CREATE", cmd_create, hlp_create },
|
||||
{ "MOUNT", cmd_mount, hlp_mount },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue