some progress

master
Alessandro Mauri 4 years ago
parent 098984744e
commit c87651fd99
  1. 9
      automount.c
  2. 23
      disk.c
  3. 2
      disk.h
  4. 20
      util.c
  5. 1
      util.h

@ -34,7 +34,8 @@
#define DEV_BLOCK_ROOT "/dev/block/" #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[]) int main (int argc, char *argv[])
{ {
struct pollfd watchpoll = {0, POLLIN, 0}; struct pollfd watchpoll = {0, POLLIN, 0};
@ -83,7 +84,11 @@ 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 -"), 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")); printf(yellow("\n"));
} }

@ -23,14 +23,18 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h>
#include "disk.h" #include "disk.h"
#include "util.h" #include "util.h"
#define SYS_DEV_BLOCK_PATH "/sys/dev/block/"
struct disk_list * struct disk_list *
disk_get_by_name (struct disk_list *head, const char *s) 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) int path_is_disk(const char *path)
{ {
struct stat stbuf = {0}; struct stat stbuf = {0};
char buf[PATH_MAX] = {0};
printf("%s\n", path);
if (stat(path, &stbuf) < 0 || !S_ISBLK(stbuf.st_mode)) if (stat(path, &stbuf) < 0 || !S_ISBLK(stbuf.st_mode))
return 0; 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 0;
return 1;
} }
/* The do-all function */ /* 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}; struct disk_list dt = {0};
// verify if it is a disk // 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); disk_list_add(head, &dt);
return 1; return 1;
} }
*/

@ -35,7 +35,7 @@ struct partition_list {
struct disk_list { struct disk_list {
const char *name; const char *name;
const char *block_path const char *block_path;
struct partition_list *parts; struct partition_list *parts;
struct disk_list *next; struct disk_list *next;
}; };

@ -27,7 +27,9 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
//#include <unistd.h> #include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
@ -74,3 +76,19 @@ const char * get_path (const char *sym)
return buf; 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;
}

@ -40,5 +40,6 @@
void die (const char *, ...); void die (const char *, ...);
void usage (void); void usage (void);
const char * get_path (const char *); const char * get_path (const char *);
int file_exists (const char *);
#endif #endif

Loading…
Cancel
Save