From 84e3dd19e1394d4d696869dc33e1855e0dc4b1c8 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Sun, 5 Feb 2023 15:06:16 +0300 Subject: [PATCH] feat(dbms): introduce TDBMS CLI client app --- tdbms/cli/tdbms-cli.c | 41 +++++++++++++++++++++++++++++++++++++++++ tdbms/install.sh | 8 ++++++++ 2 files changed, 49 insertions(+) create mode 100644 tdbms/cli/tdbms-cli.c diff --git a/tdbms/cli/tdbms-cli.c b/tdbms/cli/tdbms-cli.c new file mode 100644 index 0000000..d28e9fc --- /dev/null +++ b/tdbms/cli/tdbms-cli.c @@ -0,0 +1,41 @@ +#include +#include + +#include "../../include/tdbms-client.h" + +int main(int argc, char **argv) { + char *db_name, request_code, *request_body; + if (argc < 4) { + request_body = ""; + } else { + request_body = argv[3]; + } + if (argc < 3) { + request_code = 0; + } else { + char *endptr; + request_code = (char) strtol(argv[2], &endptr, 0); + if (*endptr != 0) { + fprintf(stderr, "FATAL: invalid request code '%s'\n", argv[2]); + return 1; + } + } + if (argc < 2) { + db_name = ""; + } else { + db_name = argv[1]; + } + int socket_fd = tdbms_connect("UNIX", "/tmp/tdbms.sock"); + if (socket_fd < 0) { + fprintf(stderr, "FATAL: failed to connect to TDBMS server\n"); + return 1; + } + char *response = tdb_query(socket_fd, db_name, request_code, request_body); + if (response == NULL) { + fprintf(stderr, "FATAL: failed to execute request\n"); + return 1; + } + printf("%s\n", response); + tdbms_close(socket_fd); + return 0; +} diff --git a/tdbms/install.sh b/tdbms/install.sh index 0bc41fe..2aff0a8 100644 --- a/tdbms/install.sh +++ b/tdbms/install.sh @@ -80,5 +80,13 @@ fi chown 0:0 /etc/systemd/system/tdbms.service chmod 0644 /etc/systemd/system/tdbms.service +if ! (cmake -S .. -B ../build && cmake --build ../build --target tdb); then + echo "FATAL: failed to build TDB CLI client" + exit 1 +fi +mv -f ../build/tdb /usr/bin/ +chown 42776 /usr/bin/tdb +chmod 4755 /usr/bin/tdb + echo "TDBMS server successfully installed." echo "Start it with 'systemctl start tdbms'"