mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
calibre-mount-helper: A nicer mount point location check
This commit is contained in:
parent
b660c352a4
commit
e148d61412
@ -8,6 +8,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <strings.h>
|
||||
|
||||
#define MARKER ".created_by_calibre_mount_helper"
|
||||
#define DEV "/dev/"
|
||||
@ -272,6 +273,37 @@ void check_dev(const char *dev) {
|
||||
|
||||
}
|
||||
|
||||
char *get_real_mount_point(const char *mp) {
|
||||
/* Resolve the mount point to a canonical path. Assumes that mp
|
||||
* either points to an existing directory, or only the leaf node
|
||||
* of mp does not exist. This is safe for the usage scenario of calibre-mount-helper. */
|
||||
char *dirname = NULL, *basename, *p, *buffer;
|
||||
buffer = calloc(PATH_MAX+1, sizeof(char));
|
||||
if (buffer == NULL) exit(EXIT_FAILURE);
|
||||
if (realpath(mp, buffer) != NULL) return buffer;
|
||||
|
||||
dirname = calloc(PATH_MAX+1, sizeof(char));
|
||||
if (dirname == NULL) exit(EXIT_FAILURE);
|
||||
|
||||
strncpy(dirname, mp, PATH_MAX);
|
||||
p = rindex(dirname, '/');
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "mountpoint must have atleast one /\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
basename = p+1;
|
||||
*p = 0;
|
||||
dirname = realpath(dirname, NULL);
|
||||
if (dirname == NULL) {
|
||||
fprintf(stderr, "parent directory of mount point cannot be resolved, ensure the mountpoint does not have a trailing slash.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
snprintf(buffer, PATH_MAX, "%s/%s", dirname, basename);
|
||||
free(dirname);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char *action, *dev, *mp, *temp;
|
||||
@ -303,8 +335,7 @@ int main(int argc, char** argv)
|
||||
fprintf(stderr, "Failed to resolve device node.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
temp = realpath(mp, NULL);
|
||||
if (temp != NULL) mp = temp;
|
||||
mp = get_real_mount_point(mp);
|
||||
check_dev(dev); check_mount_point(mp);
|
||||
status = do_mount(dev, mp);
|
||||
} else if (strncmp(action, "eject", 5) == 0) {
|
||||
|
@ -622,8 +622,11 @@ class Device(DeviceConfig, DevicePlugin):
|
||||
if getattr(sys, 'frozen', False):
|
||||
cmd = os.path.join(sys.executables_location, 'bin', cmd)
|
||||
cmd = [cmd, 'mount']
|
||||
mlabel = label
|
||||
if mlabel.endswith('/'):
|
||||
mlabel = mlabel[:-1]
|
||||
try:
|
||||
p = subprocess.Popen(cmd + [node, '/media/'+label])
|
||||
p = subprocess.Popen(cmd + [node, '/media/'+mlabel])
|
||||
except OSError:
|
||||
raise DeviceError(
|
||||
_('Could not find mount helper: %s.')%cmd[0])
|
||||
@ -777,9 +780,12 @@ class Device(DeviceConfig, DevicePlugin):
|
||||
# try all the nodes to see what we can mount
|
||||
for dev in devs[i].split():
|
||||
mp='/media/'+label+'-'+dev
|
||||
mmp = mp
|
||||
if mmp.endswith('/'):
|
||||
mmp = mmp[:-1]
|
||||
#print "trying ", dev, "on", mp
|
||||
try:
|
||||
p = subprocess.Popen(cmd + ["/dev/"+dev, mp])
|
||||
p = subprocess.Popen(cmd + ["/dev/"+dev, mmp])
|
||||
except OSError:
|
||||
raise DeviceError(_('Could not find mount helper: %s.')%cmd[0])
|
||||
while p.poll() is None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user