break out mount search

this function was getting a bit long, so break it out before we add
more gunk in to it.
This commit is contained in:
Dave Hansen 2011-08-07 11:05:29 -07:00
parent 312d60ce78
commit c7d01cc896

View file

@ -65,21 +65,55 @@ int fd_flush(int fd)
return ret;
}
int fs_is(char *fs, char *fs_name)
{
return (strcmp(fs, fs_name) == 0);
}
#define LINEBUFSZ 1024
char *locate_eyefi_mount(void)
static char *check_mount_line(int line_nr, char *line)
{
static char eyefi_mount[PATHNAME_MAX]; // PATH_MAX anyone?
char line[LINEBUFSZ];
FILE *mounts;
char dev[LINEBUFSZ];
char mnt[LINEBUFSZ];
char fs[LINEBUFSZ];
char opt[LINEBUFSZ];
int garb1;
int garb2;
int read;
read = sscanf(&line[0], "%s %s %s %s %d %d",
&dev[0], &mnt[0], &fs[0], &opt[0],
&garb1, &garb2);
// only look at fat filesystems:
if (!fs_is(fs, "msdos") && !fs_is(fs, "vfat")) {
debug_printf(4, "fs[%d] at '%s' is not fat, skipping...\n",
line_nr, mnt);
return NULL;
}
// Linux's /proc/mounts has spaces like this \040
replace_escapes(&mnt[0]);
char *file = eyefi_file_on(REQM, &mnt[0]);
debug_printf(4, "looking for EyeFi file here: '%s'\n", file);
struct stat statbuf;
int statret;
statret = stat(file, &statbuf);
free(file);
if (statret) {
debug_printf(3, "fs[%d] at: %s is not an Eye-Fi card, skipping...\n",
line_nr, &mnt[0]);
debug_printf(4, "statret: %d\n", statret);
return NULL;
}
return strdup(&mnt[0]);
}
char *locate_eyefi_mount(void)
{
static char eyefi_mount[PATHNAME_MAX]; // PATH_MAX anyone?
FILE *mounts;
char line[LINEBUFSZ];
int fs_nr = -1;
int foo;
int bar;
if (strlen(eyefi_mount))
return &eyefi_mount[0];
@ -87,33 +121,11 @@ char *locate_eyefi_mount(void)
mounts = fopen("/proc/mounts", "r");
while (fgets(&line[0], 1023, mounts)) {
int read;
fs_nr++;
read = sscanf(&line[0], "%s %s %s %s %d %d",
&dev[0], &mnt[0], &fs[0], &opt[0],
&foo, &bar);
// only look at fat filesystems:
if (strcmp(fs, "msdos") && strcmp(fs, "vfat")) {
debug_printf(4, "fs[%d] at '%s' is not fat, skipping...\n",
fs_nr, mnt);
char *mnt = check_mount_line(fs_nr++, line);
if (!mnt)
continue;
}
// Linux's /proc/mounts has spaces like this \040
replace_escapes(&mnt[0]);
char *file = eyefi_file_on(REQM, &mnt[0]);
debug_printf(4, "looking for EyeFi file here: '%s'\n", file);
struct stat statbuf;
int statret;
statret = stat(file, &statbuf);
free(file);
if (statret) {
debug_printf(3, "fs[%d] at: %s is not an Eye-Fi card, skipping...\n",
fs_nr, &mnt[0]);
debug_printf(4, "statret: %d\n", statret);
continue;
}
strcpy(&eyefi_mount[0], &mnt[0]);
strcpy(&eyefi_mount[0], mnt);
free(mnt);
debug_printf(1, "located EyeFi card at: '%s'\n", eyefi_mount);
break;
}
@ -139,7 +151,7 @@ void eject_card(void)
{
char cmd[PATHNAME_MAX];
sprintf(cmd, "umount '%s'", locate_eyefi_mount());
debug_printf("ejecting card: '%s'\n", cmd);
debug_printf(1, "ejecting card: '%s'\n", cmd);
system(cmd);
exit(0);
}