Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to check if a path in qubesdb exists #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ install: all
ln -s qubesdb-cmd $(DESTDIR)$(BINDIR)/qubesdb-multiread
ln -s qubesdb-cmd $(DESTDIR)$(BINDIR)/qubesdb-list
ln -s qubesdb-cmd $(DESTDIR)$(BINDIR)/qubesdb-watch
ln -s qubesdb-cmd $(DESTDIR)$(BINDIR)/qubesdb-exists
install qubesdb-read-bool $(DESTDIR)$(BINDIR)/

.PHONY: libqubesdb
Expand Down
29 changes: 29 additions & 0 deletions client/qubesdb-cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#ifdef WIN32
#include <windows.h>
#include <getopt.h>
Expand All @@ -25,6 +26,7 @@ enum {
DO_MULTIREAD,
DO_LIST,
DO_WATCH,
DO_EXISTS,
} qdb_cmd;

static int encode_and_print_value(char *val) {
Expand Down Expand Up @@ -105,6 +107,26 @@ static int cmd_read(qdb_handle_t h, int argc, char **args) {
return anything_failed;
}

static int cmd_exists(qdb_handle_t h, int argc, char **args) {
bool anything_failed = false;

for (int i=0; i < argc && !anything_failed; i++) {
char *value = qdb_read(h, args[i], NULL);
if (value) {
anything_failed = fwrite("true\n", 1, 5, stdout) != 5;
free(value);
} else if (errno == ENOENT) {
anything_failed = fwrite("false\n", 1, 6, stdout) != 6;
} else {
perror("qdb_read()");
anything_failed = true;
}
}

return anything_failed;
}


static int cmd_multiread(qdb_handle_t h, int argc, char **args) {
int i, j;
char **path_value;
Expand Down Expand Up @@ -238,6 +260,8 @@ static int parse_cmd(char *cmd_str) {
return DO_LIST;
else if (!strcmp(cmd_str, "watch"))
return DO_WATCH;
else if (!strcmp(cmd_str, "exists"))
return DO_EXISTS;
else
return 0;
}
Expand All @@ -261,6 +285,8 @@ static void usage(char *argv0) {
fprintf(stderr, " rm path [path...] - remove value(s)\n");
fprintf(stderr, " multiread path [path...] - read all entries matching given path\n");
fprintf(stderr, " list path - list paths mathing given argument\n");
fprintf(stderr, " exists path - check if path exists, and print \"true\" or "
"\"false\" accordingly\n");
fprintf(stderr, " watch [-n N] path [path...] - watch given path(s) for "
"modifications\n");
fprintf(stderr, " if -n given you can specify how many events should "
Expand Down Expand Up @@ -362,6 +388,9 @@ int main(int argc, char **argv) {
case DO_WATCH:
ret = cmd_watch(h, argc-optind, argv+optind);
break;
case DO_EXISTS:
ret = cmd_exists(h, argc-optind, argv+optind);
break;
default:
if (!opt_quiet)
fprintf(stderr, "Unknown command\n");
Expand Down
1 change: 1 addition & 0 deletions debian/qubesdb.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ usr/bin/qubesdb-rm
usr/bin/qubesdb-multiread
usr/bin/qubesdb-list
usr/bin/qubesdb-watch
usr/bin/qubesdb-exists
usr/sbin/qubesdb-daemon
1 change: 1 addition & 0 deletions rpm_spec/qubes-db.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ make install \
%{_bindir}/qubesdb-multiread
%{_bindir}/qubesdb-list
%{_bindir}/qubesdb-watch
%{_bindir}/qubesdb-exists
%{_sbindir}/qubesdb-daemon

%files libs
Expand Down