master
Alessandro Mauri 4 years ago
parent d190b90e42
commit 098984744e
  1. 4
      automount.c
  2. 72
      disk.c
  3. 14
      disk.h
  4. 21
      tests/stat.c
  5. 7
      util.c

@ -83,7 +83,9 @@ int main (int argc, char *argv[])
continue; continue;
strcpy(strbuf, DEV_BLOCK_ROOT); strcpy(strbuf, DEV_BLOCK_ROOT);
strcat(strbuf, event->name); 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"); die("polling interrupted");

@ -22,5 +22,77 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include "disk.h" #include "disk.h"
#include "util.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;
}

@ -26,10 +26,8 @@
#define _DISK_H #define _DISK_H
/* Block info data type */ /* Block info data type */
// TODO: add this stupid data type. Maybe have a list of parents and have // TODO: maybe split this into three structs, one generic list, one just disk
// pending "strings" of children attached to them like // and one just for partitions, idk
// truct parent_l { struct child_l { }; };
struct partition_list { struct partition_list {
const char *name; const char *name;
struct partition_list *next; struct partition_list *next;
@ -37,13 +35,17 @@ struct partition_list {
struct disk_list { struct disk_list {
const char *name; const char *name;
const char *block_path
struct partition_list *parts; struct partition_list *parts;
struct disk_list *next; 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 *); struct disk_list * disk_get_by_name (struct disk_list *, const char *);
void disk_rm_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_list_free (struct disk_list **);
void disk_list_add (struct disk_list *, 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 *); struct partition_list * disk_part_get_by_name (struct disk_list *, const char *);

@ -0,0 +1,21 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
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;
}

@ -27,7 +27,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> //#include <unistd.h>
#include <limits.h>
#include "util.h" #include "util.h"
/* Exit printing an error message and error string based on errno */ /* 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) const char * get_path (const char *sym)
{ {
static char buf[256]; static char buf[PATH_MAX];
memset(buf, 0, 256); memset(buf, 0, 256);
if (readlink(sym, buf, 256) < 0 || *buf == '0') if (!realpath(sym, buf) || *buf == '0')
return sym; return sym;
if (buf[255]) if (buf[255])
return NULL; return NULL;

Loading…
Cancel
Save