From 098984744e5332ca99b45bad9c11b100cc384147 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 4 Oct 2020 13:43:35 +0200 Subject: [PATCH] sync time! --- automount.c | 6 +++-- disk.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ disk.h | 14 +++++----- tests/stat.c | 21 +++++++++++++++ util.c | 7 ++--- 5 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 tests/stat.c diff --git a/automount.c b/automount.c index 080a4d0..4cbef2a 100644 --- a/automount.c +++ b/automount.c @@ -77,13 +77,15 @@ int main (int argc, char *argv[]) } for (ptr = buf; ptr < buf + len; - ptr += sizeof(struct inotify_event) + event->len) { + ptr += sizeof(struct inotify_event) + event->len) { event = (const struct inotify_event *) ptr; if(!event->len) continue; strcpy(strbuf, DEV_BLOCK_ROOT); strcat(strbuf, event->name); - printf(yellow("%s: %s\n"), event->mask & IN_CREATE ? "Created" : "Removed", get_path(strbuf)); + printf(yellow("%s: %s -"), event->mask & IN_CREATE ? "Created" : "Removed", get_path(strbuf)); + printf(yellow("\n")); + } } die("polling interrupted"); diff --git a/disk.c b/disk.c index c8185db..2f2f6c1 100644 --- a/disk.c +++ b/disk.c @@ -22,5 +22,77 @@ * SOFTWARE. */ +#include +#include +#include +#include +#include +#include #include "disk.h" #include "util.h" + +struct disk_list * +disk_get_by_name (struct disk_list *head, const char *s) +{ + for (; head; head = head->next) { + if (!strcmp(s, head->name)) + break; + } + return head; +} + +void +disk_rm_by_name (struct disk_list **head, const char *s) +{ + struct disk_list *prev = NULL, *tmp = NULL; + for (tmp = *head; tmp; prev = tmp, tmp = tmp->next) { + if (!strcmp(s, tmp->name)) { + if (prev) + prev->next = tmp->next; + else + *head = tmp->next; + free(tmp); + break; + } + } + return; +} + +void +disk_list_free (struct disk_list **head) +{ + for (struct disk_list *tmp = *head; tmp;) { + *head = (*head)->next; + free(tmp); + tmp = *head; + } + return; +} + +/* return true if the path is the main disk (sda, sdb, etc not sda2, sdb1) */ +int path_is_disk(const char *path) +{ + struct stat stbuf = {0}; + if (stat(path, &stbuf) < 0 || !S_ISBLK(stbuf.st_mode)) + return 0; + if (minor(stbuf.st_rdev)) + return 0; + return 1; +} + +/* The do-all function */ +int disk_add_disk (struct disk_list *head, const char *path) +{ + struct disk_list dt = {0}; + // verify if it is a disk + if (!path_is_disk(path)) + return 0; + + // get info (real name, etc) + + // get partitions + // add partitions + + disk_list_add(head, &dt); + return 1; +} diff --git a/disk.h b/disk.h index 02dbef3..00a40cc 100644 --- a/disk.h +++ b/disk.h @@ -26,10 +26,8 @@ #define _DISK_H /* Block info data type */ -// TODO: add this stupid data type. Maybe have a list of parents and have -// pending "strings" of children attached to them like -// truct parent_l { struct child_l { }; }; - +// TODO: maybe split this into three structs, one generic list, one just disk +// and one just for partitions, idk struct partition_list { const char *name; struct partition_list *next; @@ -37,13 +35,17 @@ struct partition_list { struct disk_list { const char *name; + const char *block_path struct partition_list *parts; struct disk_list *next; }; +int disk_add_disk (struct disk_list *, const char *); +int path_is_disk (const char *); + struct disk_list * disk_get_by_name (struct disk_list *, const char *); -void disk_rm_by_name (struct disk_list *, const char *); -void disk_list_free (struct disk_list *); +void disk_rm_by_name (struct disk_list **, const char *); +void disk_list_free (struct disk_list **); void disk_list_add (struct disk_list *, struct disk_list *); struct partition_list * disk_part_get_by_name (struct disk_list *, const char *); diff --git a/tests/stat.c b/tests/stat.c new file mode 100644 index 0000000..a424f4c --- /dev/null +++ b/tests/stat.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int main (void) +{ + struct stat st; + printf("sda:\n"); + stat("/dev/nvme1n1", &st); + printf("st_dev: %d\t%d\n", major(st.st_dev), minor(st.st_dev)); + printf("st_rdev: %d\t%d\n", major(st.st_rdev), minor(st.st_rdev)); + + printf("sda1:\n"); + stat("/dev/nvme1n1p2", &st); + printf("st_dev: %d\t%d\n", major(st.st_dev), minor(st.st_dev)); + printf("st_rdev: %d\t%d\n", major(st.st_rdev), minor(st.st_rdev)); + + return 0; +} diff --git a/util.c b/util.c index 98ff977..aa52015 100644 --- a/util.c +++ b/util.c @@ -27,7 +27,8 @@ #include #include #include -#include +//#include +#include #include "util.h" /* Exit printing an error message and error string based on errno */ @@ -63,10 +64,10 @@ void usage (void) */ const char * get_path (const char *sym) { - static char buf[256]; + static char buf[PATH_MAX]; memset(buf, 0, 256); - if (readlink(sym, buf, 256) < 0 || *buf == '0') + if (!realpath(sym, buf) || *buf == '0') return sym; if (buf[255]) return NULL;