some progress
This commit is contained in:
parent
098984744e
commit
c87651fd99
@ -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"));
|
||||
|
||||
}
|
||||
|
23
disk.c
23
disk.c
@ -23,14 +23,18 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#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;
|
||||
}
|
||||
*/
|
||||
|
2
disk.h
2
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;
|
||||
};
|
||||
|
20
util.c
20
util.c
@ -27,7 +27,9 @@
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
//#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user