diff --git a/automount.c b/automount.c index 4cbef2a..b7e3925 100644 --- a/automount.c +++ b/automount.c @@ -34,7 +34,8 @@ #define DEV_BLOCK_ROOT "/dev/block/" - +// FIXME: this is all very inefficient code, everything gets called multiple +// times and it is not at all minimal int main (int argc, char *argv[]) { struct pollfd watchpoll = {0, POLLIN, 0}; @@ -83,7 +84,11 @@ int main (int argc, char *argv[]) continue; strcpy(strbuf, DEV_BLOCK_ROOT); strcat(strbuf, event->name); - printf(yellow("%s: %s -"), event->mask & IN_CREATE ? "Created" : "Removed", get_path(strbuf)); + + printf(yellow("%s: %s - %s"), + event->mask & IN_CREATE ? "Created" : "Removed", + get_path(strbuf), + path_is_disk(get_path(strbuf)) > 0 ? "disk" : "part"); printf(yellow("\n")); } diff --git a/disk.c b/disk.c index 2f2f6c1..9cf6556 100644 --- a/disk.c +++ b/disk.c @@ -23,14 +23,18 @@ */ #include +#include #include #include #include #include #include +#include #include "disk.h" #include "util.h" +#define SYS_DEV_BLOCK_PATH "/sys/dev/block/" + struct disk_list * disk_get_by_name (struct disk_list *head, const char *s) { @@ -73,15 +77,27 @@ disk_list_free (struct disk_list **head) int path_is_disk(const char *path) { struct stat stbuf = {0}; + char buf[PATH_MAX] = {0}; + + printf("%s\n", path); if (stat(path, &stbuf) < 0 || !S_ISBLK(stbuf.st_mode)) return 0; - if (minor(stbuf.st_rdev)) + + if (snprintf(buf, PATH_MAX, SYS_DEV_BLOCK_PATH "%d:%d", + major(stbuf.st_rdev), minor(stbuf.st_rdev)) >= PATH_MAX) + return -1; + // FIXME: possible race condition here + snprintf(buf, PATH_MAX, "%s", get_path(buf)); + strcat(buf, "/partition"); + if (file_exists(buf) > 0) + return 1; + else return 0; - return 1; } /* The do-all function */ -int disk_add_disk (struct disk_list *head, const char *path) + +/*int disk_add_disk (struct disk_list *head, const char *path) { struct disk_list dt = {0}; // verify if it is a disk @@ -96,3 +112,4 @@ int disk_add_disk (struct disk_list *head, const char *path) disk_list_add(head, &dt); return 1; } +*/ diff --git a/disk.h b/disk.h index 00a40cc..bc62c95 100644 --- a/disk.h +++ b/disk.h @@ -35,7 +35,7 @@ struct partition_list { struct disk_list { const char *name; - const char *block_path + const char *block_path; struct partition_list *parts; struct disk_list *next; }; diff --git a/util.c b/util.c index aa52015..adf25d6 100644 --- a/util.c +++ b/util.c @@ -27,7 +27,9 @@ #include #include #include -//#include +#include +#include +#include #include #include "util.h" @@ -74,3 +76,19 @@ const char * get_path (const char *sym) return buf; } + +/* Check if file exists using stat, returns 0 if file does not exist or st_mode + * if the file (or folder) exists, this way one could evaluate the type of file + * without a second stat call, on error -1 is returned and errno is set + */ +int file_exists (const char *path) +{ + static struct stat st; + if (stat(path, &st) < 0) { + if (errno == ENOENT) + return 0; + else + return -1; + } + return (int)st.st_mode; +} diff --git a/util.h b/util.h index f43fd07..700314b 100644 --- a/util.h +++ b/util.h @@ -40,5 +40,6 @@ void die (const char *, ...); void usage (void); const char * get_path (const char *); +int file_exists (const char *); #endif