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

Commit

Permalink
kdb-cli: rewrite rm
Browse files Browse the repository at this point in the history
... and fix #3742
  • Loading branch information
hannes99 committed Mar 2, 2023
1 parent dc5154d commit ca36161
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 128 deletions.
2 changes: 0 additions & 2 deletions src/tools/kdb/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <plugininfo.hpp>
#include <pluginlist.hpp>
#include <remount.hpp>
#include <rm.hpp>
#include <sget.hpp>
#include <shell.hpp>
#include <specmount.hpp>
Expand Down Expand Up @@ -77,7 +76,6 @@ class Factory
Factory () : m_factory ()
{
// TODO: to add a new command, 2.) add a line here -> and you are done
m_factory.insert (std::make_pair ("rm", std::make_shared<Cnstancer<RemoveCommand>> ()));
m_factory.insert (std::make_pair ("cache", std::make_shared<Cnstancer<CacheCommand>> ()));
m_factory.insert (std::make_pair ("complete", std::make_shared<Cnstancer<CompleteCommand>> ()));
m_factory.insert (std::make_pair ("cp", std::make_shared<Cnstancer<CpCommand>> ()));
Expand Down
2 changes: 2 additions & 0 deletions src/tools/kdb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mountpoint.h>
#include <mv.h>
#include <namespace.h>
#include <rm.h>
#include <set.h>

#include <command.h>
Expand All @@ -39,6 +40,7 @@ command subcommands[] = {
{ "mountpoint", addMountpointSpec, execMountpoint },
{ "mv", addMvSpec, execMv },
{ "namespace", addNamespaceSpec, execNamespace },
{ "rm", addRmSpec, execRm },
{ "set", addSetSpec, execSet },
};

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

#include <command.h>
#include <rm.h>

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

#define COMMAND_NAME "rm"

#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 addRmSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Remove a key.", KEY_META, "command",
COMMAND_NAME, KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/recursive", KEY_META, "description", "Work in recursive mode.",
KEY_META, "opt", "r", KEY_META, "opt/long", "recursive", KEY_META, "opt/arg", "none", KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/name", KEY_META, "description", "The key name", KEY_META, "args",
"indexed", KEY_META, "args/index", "0", KEY_END));

ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execRm (KeySet * options, Key * errorKey)
{
int ret = 0;
GET_BASIC_OPTIONS

bool recursive = false;
tmp = GET_OPTION_KEY (options, "recursive");
if (tmp != NULL)
{
elektraKeyToBoolean (GET_OPTION_KEY (options, "recursive"), &recursive);
}

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

Key * key = keyNew (name, KEY_END);

if (keyGetNamespace (key) == KEY_NS_NONE || keyGetNamespace (key) == KEY_NS_CASCADING)
{
ret = 1;
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, "source key does not specify a namespace");
elektraFree ((void *) name);
keyDel (key);
return ret;
}

Key * root = keyNew ("/", KEY_END);

KeySet * conf = ksNew (0, KS_END);
KDB * handle = kdbOpen (NULL, errorKey);
if (kdbGet (handle, conf, root) == -1)
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load '%s': %s", name, GET_ERR (root));
ret = 1;
goto cleanup;
}

Key * cur = NULL;

size_t keyNameLen = elektraStrLen (keyName (key));

long count = 0;
KeySet * newConf = ksNew (ksGetSize (conf), KS_END);
for (elektraCursor it = 0; it < ksGetSize (conf); ++it)
{
cur = ksAtCursor (conf, it);

bool startsWithSrc = !elektraStrNCmp (keyName (cur), keyName (key), keyNameLen - 1);
bool equalsSrc = !elektraStrCmp (keyName (cur), keyName (key));

// starts-with if recursive, or equals if !recursive
if ((recursive && startsWithSrc) || equalsSrc)
{
count++;
CLI_PRINT (CLI_LOG_VERBOSE, "removing '%s\n", BOLD(keyName (cur)));
continue;
}
ksAppendKey (newConf, cur);
}

if (kdbSet (handle, newConf, root) == -1)
{
ret = 1;
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not save keyset after moving: %s", GET_ERR (root));
}
CLI_PRINT (CLI_LOG_VERBOSE, "\nremoved %ld keys", count);


cleanup:
if (!noNewLine)
{
printf ("\n");
}
elektraFree ((void *) name);
keyDel (root);
elektraFree ((void *) fmtBuffer);
ksDel (conf);
kdbClose (handle, errorKey);
return ret;
}
79 changes: 0 additions & 79 deletions src/tools/kdb/rm.cpp

This file was deleted.

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

#ifndef ELEKTRA_KDB_RM_H
#define ELEKTRA_KDB_RM_H

#include <kdb.h>

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

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

// helper functions
int getKeyNameDepth (const char * name);

#endif // ELEKTRA_KDB_RM_H
47 changes: 0 additions & 47 deletions src/tools/kdb/rm.hpp

This file was deleted.

0 comments on commit ca36161

Please sign in to comment.