Add print_info.c example & fix deserialization

This commit is contained in:
thematdev 2023-06-15 21:18:19 +03:00
parent f713a18efa
commit 509dd9ec01
Signed by: thematdev
GPG Key ID: D12878639B090D90
3 changed files with 91 additions and 6 deletions

View File

@ -1,5 +1,4 @@
/* Reads text from stdio and creates comment using unix fs driver in current directory */ /* Reads text from stdio and creates comment using unix fs driver in current directory */
/* TODO: add path, displayname as arguments */
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
@ -24,9 +23,11 @@ get_username()
return ""; return "";
} }
void usage() { void
usage()
{
fprintf(stderr, "Simple utility to leave comment using unix_fs_driver\n"); fprintf(stderr, "Simple utility to leave comment using unix_fs_driver\n");
fprintf(stderr, "usage: <tool-name> [-p path] [-r reply_id] [-d displayname] [-i input_file]"); fprintf(stderr, "usage: <tool-name> [-p path] [-r reply_id] [-d displayname] [-i input_file]\n");
fprintf(stderr, "-p path:\t path to comment storage \n"); fprintf(stderr, "-p path:\t path to comment storage \n");
fprintf(stderr, "-r reply_id:\t ID of comment you are replying to, 0 if yours is top-level\n"); fprintf(stderr, "-r reply_id:\t ID of comment you are replying to, 0 if yours is top-level\n");
fprintf(stderr, "-d displayname:\t Displayname to attach to comment\n"); fprintf(stderr, "-d displayname:\t Displayname to attach to comment\n");

83
examples/print_info.c Normal file
View File

@ -0,0 +1,83 @@
/* Simple utility to get human-readable comment data */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "comment.h"
#include "driver.h"
#include "drivers/unix_fs/unix_fs_driver.h"
#define BUF_SZ 4096
void
usage()
{
fprintf(stderr, "Simple utility to get comment info\n");
fprintf(stderr, "usage: <tool-name> [-p path] id\n");
fprintf(stderr, "-p path: path to comment storage \n");
fprintf(stderr, "id : id of comment \n");
}
int
main(int argc, char *argv[])
{
const Driver *driver = &unix_fs_driver;
char *path = ".";
int id, opt;
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
path = optarg;
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
usage();
exit(EXIT_FAILURE);
}
id = atoi(argv[optind]);
if (id <= 0) {
usage();
exit(EXIT_FAILURE);
}
CommentHeader *header = malloc(sizeof(CommentHeader));
header->user_sid = malloc(BUF_SZ);
header->user_displayname = malloc(BUF_SZ);
UnixFsDriverData driver_data = { path };
if (driver->get_header(&driver_data, header, id) < 0) {
fprintf(stderr, "failed to get header of comment, are you sure it exists?\n");
exit(EXIT_FAILURE);
}
printf("Got a valid comment header\n");
printf("%-30s: %d\n", "Reply id", header->reply_id);
struct tm tm = *localtime(&header->creation_time);
printf("%-30s: %d-%02d-%02d %02d:%02d:%02d\n", "Creation time", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
printf("%-30s: %lu\n", "Text size in bytes", header->text_length);
printf("%-30s: %s\n", "User string ID", header->user_sid);
printf("%-30s: %s\n", "User displayname", header->user_displayname);
char *text_buffer = malloc(header->text_length + 1);
if (driver->get_text(&driver_data, text_buffer, id) < 0) {
fprintf(stderr, "failed to get text of comment\n");
exit(EXIT_FAILURE);
}
printf("Comment text:\n");
printf(text_buffer);
}

View File

@ -46,6 +46,8 @@ deserialize_header(int fd, CommentHeader *header)
int bytes_read; int bytes_read;
size_t len, dst_idx, i; size_t len, dst_idx, i;
i = 0;
dst_bufs[0] = header->user_sid; dst_bufs[0] = header->user_sid;
dst_bufs[1] = header->user_displayname; dst_bufs[1] = header->user_displayname;
dst_idx = 0; dst_idx = 0;
@ -56,9 +58,8 @@ deserialize_header(int fd, CommentHeader *header)
while ((bytes_read = read(fd, buf, READ_BUF_SZ))) { while ((bytes_read = read(fd, buf, READ_BUF_SZ))) {
for (; i < bytes_read; ++i) { for (; i < bytes_read; ++i) {
if (buf[i] != '\0') {
*(dst_bufs[dst_idx]++) = buf[i]; *(dst_bufs[dst_idx]++) = buf[i];
} else { if (buf[i] == '\0') {
dst_idx++; dst_idx++;
} }
} }