From 070f39fc2b033bcf53797480c4b1ce27a0eb0f91 Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 11 Jun 2026 17:26:52 +0200 Subject: feat: added utils/fs.h for filesystem operations and made a function to create necessary directories --- analyzer/utils/fs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ analyzer/utils/fs.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 analyzer/utils/fs.c create mode 100644 analyzer/utils/fs.h (limited to 'analyzer/utils') diff --git a/analyzer/utils/fs.c b/analyzer/utils/fs.c new file mode 100644 index 0000000..76fb581 --- /dev/null +++ b/analyzer/utils/fs.c @@ -0,0 +1,43 @@ +#include "utils.h" + +#include +#include +#include + +void get_home_dir(char *home_dir) { + strcpy(home_dir, getenv("HOME")); +} + +void get_main_dir(char *main_dir) { + char home_dir[256]; + get_home_dir(home_dir); + sprintf(main_dir, "%s/.local/share/branal", home_dir); +} + +void get_capture_dir(char *capture_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(capture_dir, "%s/captures", main_dir); +} + +void get_saves_dir(char *saves_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(saves_dir, "%s/saves", main_dir); +} + +void create_necessary_directories() { + char main_dir[256], capture_dir[256], saves_dir[256]; + get_main_dir(main_dir); + get_capture_dir(capture_dir); + get_saves_dir(saves_dir); + mkdir(main_dir, 0755); + mkdir(capture_dir, 0755); + mkdir(saves_dir, 0755); +} + +void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions) { + int fd = open(path, flags, permissions); + write(fd, contents, contents_len); + close(fd); +} diff --git a/analyzer/utils/fs.h b/analyzer/utils/fs.h new file mode 100644 index 0000000..6335799 --- /dev/null +++ b/analyzer/utils/fs.h @@ -0,0 +1,31 @@ +// utils/fs.h +// functions for operating on the file system, i.e. creating directories, writing files, etc. + +#include + +// get main directory used by the program +// params: +// - main_dir - buffer to save the directory to +extern void get_main_dir(char *main_dir); + +// get directory to save the .pcap files to +// params: +// - capture_dir - buffer to save the directory to +extern void get_capture_dir(char *capture_dir); + +// get directory of save data of the program +// params: +// - saves_dir - buffer to save the directory to +extern void get_saves_dir(char *saves_dir); + +// create directories that will be used by the program +extern void create_necessary_directories(); + +// writes file to the specified path +// params: +// - path - path to write the file to +// - contents - contents of the file +// - contents_len - amount of bytes of contents to write +// - flags - flags to open the file with +// - permissions - permissions of the written file +extern void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions); -- cgit v1.2.3 From 07bd987537275b23904f41f4795bf081d2421d0a Mon Sep 17 00:00:00 2001 From: Mosder Date: Sat, 13 Jun 2026 20:08:47 +0200 Subject: feat: created utils/time.h for helper functions regarding date and time --- analyzer/utils/time.c | 10 ++++++++++ analyzer/utils/time.h | 7 +++++++ 2 files changed, 17 insertions(+) create mode 100644 analyzer/utils/time.c create mode 100644 analyzer/utils/time.h (limited to 'analyzer/utils') diff --git a/analyzer/utils/time.c b/analyzer/utils/time.c new file mode 100644 index 0000000..72c0bc7 --- /dev/null +++ b/analyzer/utils/time.c @@ -0,0 +1,10 @@ +#include "time.h" + +#include +#include + +void get_date_time_string(char *date_time) { + time_t now = time(NULL); + struct tm *t = localtime(&now); + sprintf(date_time, "%02d%02d%02d_%02d%02d%02d", t->tm_year - 100, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); +} diff --git a/analyzer/utils/time.h b/analyzer/utils/time.h new file mode 100644 index 0000000..0bd0df1 --- /dev/null +++ b/analyzer/utils/time.h @@ -0,0 +1,7 @@ +// utils/time.h +// functions regarding date and time + +// gets a string _