Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
kdb-cli: add mountpoint command structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes99 committed Jan 4, 2023
1 parent 7914fdc commit 71c7c18
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/tools/kdb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dirname.h>
#include <get.h>
#include <ls.h>
#include <mountpoint.h>
#include <namespace.h>

#include <command.h>
Expand All @@ -31,6 +32,7 @@ command subcommands[] = {
{ "dirname", addDirnameSpec, execDirname },
{ "get", addGetSpec, execGet },
{ "ls", addLsSpec, execLs },
{ "mountpoint", addMountpointSpec, execMountpoint },
{ "namespace", addNamespaceSpec, execNamespace },
};

Expand Down
101 changes: 101 additions & 0 deletions src/tools/kdb/mountpoint-mount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @file
*
* @brief Implementation of kdb mount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <cmerge.h>
#include <mountpoint.h>
#include <mountpoint-mount.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbmerge.h>
#include <stdio.h>
#include <string.h>

#define COMMAND_NAME "mountpoint/mount"

#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)
#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)

void addMountSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Mount a new backend.", KEY_META, "command",
"mount", KEY_END));

ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/path", KEY_META, "description",
"A filename (absolute for system, relative for cascading or user)", KEY_META, "args", "indexed",
KEY_META, "args/index", "0", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/mountpoint", KEY_META, "description",
"where to mount the backend, start with / for cascading mp", KEY_META, "args", "indexed", KEY_META,
"args/index", "1", KEY_END));

ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/plugins/#", KEY_META, "description",
"Plugin and its config, <PLUGIN>[key1=val1,key2=val2,...]", KEY_META, "args", "remaining", KEY_END));

ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/strategy", KEY_META, "description",
"Specify the strategy to resolve conflicts.", KEY_META, "opt", "s", KEY_META, "opt/arg/help", "STRATEGY",
KEY_META, "opt/long", "strategy", KEY_META, "opt/arg", "required", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/resolver", KEY_META, "description",
"Specify the resolver plugin to use.", KEY_META, "opt", "R", KEY_META, "opt/arg/help", "NAME", KEY_META,
"opt/long", "resolver", KEY_META, "opt/arg", "required", KEY_END));

ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/force", KEY_META, "description", "Force the action to be done.",
KEY_META, "opt", "f", KEY_META, "opt/long", "force", KEY_META, "opt/arg", "none", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/addrecommended", KEY_META, "description", "Add recommended plugins.",
KEY_META, "opt", "W", KEY_META, "opt/long", "with-recommends", KEY_META, "opt/arg", "none", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/nullterm", KEY_META, "description", "Use binary 0 termination.",
KEY_META, "opt", "0", KEY_META, "opt/long", "null", KEY_META, "opt/arg", "none", KEY_END));

ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/supfirst", KEY_META, "description", "Suppress the first column.",
KEY_META, "opt", "1", KEY_META, "opt/long", "first", KEY_META, "opt/arg", "none", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/supsecond", KEY_META, "description", "Suppress the second column.",
KEY_META, "opt", "2", KEY_META, "opt/long", "second", KEY_META, "opt/arg", "none", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/supthird", KEY_META, "description", "Suppress the third column.",
KEY_META, "opt", "3", KEY_META, "opt/long", "third", KEY_META, "opt/arg", "none", KEY_END));

ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execMount (KeySet * options, Key * errorKey)
{
int ret = 0;
bool verbose = false;
Key * tmp = GET_OPTION_KEY (options, "verbose");
if (tmp != NULL)
{
elektraKeyToBoolean (GET_OPTION_KEY (options, "verbose"), &verbose);
}

const char * path = GET_OPTION (options, "path");

const char * mountpoint = getKeyNameFromOptions (options, GET_OPTION (options, "mountpoint"), errorKey, verbose);
if (mountpoint == NULL) return 1;

Key * arrayParent = GET_OPTION_KEY (options, "plugins");
KeySet * plugins = elektraArrayGet (arrayParent, options);

ksRewind (plugins);
Key * cur = NULL;
while ((cur = ksNext (plugins)) != NULL)
{
printf ("PLUGIN -> %s\n", keyString (cur));
}
printf ("\n");

KDB * handle = kdbOpen (NULL, errorKey);

KeySet * currentMountConfig = getMountConfig (handle, errorKey);

Key * mountpointKey = keyNew (mountpoint, KEY_END);

printKsNames (currentMountConfig);

printf ("MOUNT");
}
33 changes: 33 additions & 0 deletions src/tools/kdb/mountpoint-mount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file
*
* @brief Header for mount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_MOUNT_H
#define ELEKTRA_KDB_MOUNT_H

#include <kdb.h>

/**
* Adds options specification of mount command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addMountSpec (KeySet * spec);

/**
* Executes the mount command
*
* @param options cli options and arguments as specified in @addMountSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 ls command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execMount (KeySet * options, Key * errorKey);

#endif // ELEKTRA_KDB_MOUNT_H
43 changes: 43 additions & 0 deletions src/tools/kdb/mountpoint-remount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file
*
* @brief Implementation of kdb remount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <mountpoint-mount.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbmerge.h>
#include <stdio.h>
#include <string.h>

#define COMMAND_NAME "mountpoint/remount"

#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)
#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)

void addRemountSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Remount an existing backend with a different filename.", KEY_META,
"command", "remount", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/filename", KEY_META, "description", "The new filename.", KEY_META,
"args", "indexed", KEY_META, "args/index", "0", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/path", KEY_META, "description", "The new path.", KEY_META,
"args", "indexed", KEY_META, "args/index", "1", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/mountpoint", KEY_META, "description", "The mountpoint that should be remounted", KEY_META,
"args", "indexed", KEY_META, "args/index", "2", KEY_END));


ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execRemount (KeySet * options, Key * errorKey)
{
printf ("RE-MOUNT");
}
33 changes: 33 additions & 0 deletions src/tools/kdb/mountpoint-remount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file
*
* @brief Header for remount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_REMOUNT_H
#define ELEKTRA_KDB_REMOUNT_H

#include <kdb.h>

/**
* Adds options specification of remount command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addRemountSpec (KeySet * spec);

/**
* Executes the remount command
*
* @param options cli options and arguments as specified in @addRemountSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 ls command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execRemount (KeySet * options, Key * errorKey);

#endif // ELEKTRA_KDB_REMOUNT_H
38 changes: 38 additions & 0 deletions src/tools/kdb/mountpoint-umount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file
*
* @brief Implementation of kdb umount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <mountpoint-mount.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbmerge.h>
#include <stdio.h>
#include <string.h>

#define COMMAND_NAME "mountpoint/umount"

#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)
#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)

void addUmountSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Unmount backend from key database.", KEY_META,
"command", "umount", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/name", KEY_META, "description", "The keyname to unmount.", KEY_META,
"args", "indexed", KEY_META, "args/index", "0", KEY_END));

ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execUmount (KeySet * options, Key * errorKey)
{
printf ("UN-MOUNT");
}
33 changes: 33 additions & 0 deletions src/tools/kdb/mountpoint-umount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file
*
* @brief Header for umount command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_UMOUNT_H
#define ELEKTRA_KDB_UMOUNT_H

#include <kdb.h>

/**
* Adds options specification of umount command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addUmountSpec (KeySet * spec);

/**
* Executes the umount command
*
* @param options cli options and arguments as specified in @addUmountSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 ls command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execUmount (KeySet * options, Key * errorKey);

#endif // ELEKTRA_KDB_UMOUNT_H
68 changes: 68 additions & 0 deletions src/tools/kdb/mountpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @file
*
* @brief Implementation of kdb mountpoint command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <mountpoint-mount.h>
#include <mountpoint-remount.h>
#include <mountpoint-umount.h>
#include <mountpoint.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbmerge.h>
#include <stdio.h>
#include <string.h>

#define COMMAND_NAME "mountpoint"

#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)
#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)

command mountSubcommands[] = {
{ "mount", addMountSpec, execMount },
{ "remount", addRemountSpec, execRemount },
{ "umount", addUmountSpec, execUmount },
};

void addMountpointSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Manage kdb mounts.", KEY_META, "command",
COMMAND_NAME, KEY_END));

for (unsigned long i = 0; i < sizeof (mountSubcommands) / sizeof (mountSubcommands[0]); ++i)
{
mountSubcommands[i].addSpec (spec);
}
}

int execMountpoint (KeySet * options, Key * errorKey)
{
const char * subcommand = keyString (ksLookupByName (options, CLI_BASE_KEY "/" COMMAND_NAME, 0));

for (unsigned long i = 0; i < sizeof (mountSubcommands) / sizeof (mountSubcommands[0]); ++i)
{
if (elektraStrCmp (subcommand, mountSubcommands[i].name) == 0)
{
return mountSubcommands[i].exec (options, errorKey);
}
}
}

KeySet * getMountConfig (KDB * handle, Key * errorKey)
{
Key * parent = keyNew (MOUNTPOINTS_PATH, KEY_END);
KeySet * mountInfo = ksNew (0, KS_END);
kdbGet (handle, mountInfo, parent);

// TODO: maybe print warnings(or add them to error key)

keyDel (parent);
return mountInfo;
}
Loading

0 comments on commit 71c7c18

Please sign in to comment.