Check if (un)mounted in sshfs-map.

This commit is contained in:
Érico Rolim 2020-06-03 14:31:41 -03:00
parent 4c7f8b18f6
commit 9030040f41
1 changed files with 52 additions and 7 deletions

View File

@ -1,10 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <mntent.h>
#include <limits.h>
#include <linux/limits.h>
#include "sshfs-map.h" #include "sshfs-map.h"
@ -49,20 +52,50 @@ int main(int argc, char **argv)
// guarantee null-terminated terms // guarantee null-terminated terms
if (line[result - 1] == '\n') line[result - 1] = '\0'; if (line[result - 1] == '\n') line[result - 1] = '\0';
char *source, *dist;
source = dist = NULL; char *source, *dest, *full_dest;
sscanf(line, "%ms %ms", &source, &dist); source = dest = full_dest = NULL;
sscanf(line, "%ms %ms", &source, &dest);
printf("map #%02d: %s\n", i, line); printf("map #%02d: %s\n", i, line);
if (dest[0] == '/') {
full_dest = dest;
} else {
full_dest = realpath(dest, NULL);
if (full_dest == NULL) {
puts("couldn't find path");
exit(1);
}
}
// check path mount status
FILE *mtab = setmntent("/etc/mtab", "r");
bool is_mounted = false;
while (true) {
struct mntent *mounts = getmntent(mtab);
if (mounts == NULL) {
break;
}
if (strcmp(full_dest, mounts->mnt_dir) == 0) {
is_mounted = true;
break;
}
}
endmntent(mtab);
switch (mode) { switch (mode) {
pid_t p; pid_t p;
case SSHFS_MAP: case SSHFS_MAP:
if (is_mounted) {
printf("%s is already mounted\n", full_dest);
continue;
}
p = fork(); p = fork();
if (p == 0) { if (p == 0) {
printf("%s <=> %s\n", source, dist); printf("%s <=> %s\n", source, full_dest);
fflush(stdout); fflush(stdout);
char *args[] = {"sshfs", source, dist, NULL}; char *args[] = {"sshfs", source, full_dest, NULL};
execvp("sshfs", args); execvp("sshfs", args);
} else { } else {
int wstatus; int wstatus;
@ -71,9 +104,13 @@ int main(int argc, char **argv)
break; break;
case SSHFS_UNMAP: case SSHFS_UNMAP:
if (!is_mounted) {
printf("%s isn't mounted\n", full_dest);
continue;
}
p = fork(); p = fork();
if (p == 0) { if (p == 0) {
char *args[] = {"umount", dist, NULL}; char *args[] = {"umount", full_dest, NULL};
execvp("umount", args); execvp("umount", args);
} else { } else {
int wstatus; int wstatus;
@ -81,7 +118,15 @@ int main(int argc, char **argv)
} }
break; break;
} }
free(line); free(line);
free(source);
free(dest);
if (full_dest != dest) free(full_dest);
i++; // update count i++; // update count
} }
fclose(map);
return 0;
} }