From e148d614122d038760b1ed90278956a5d577f3df Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 5 Nov 2011 00:24:11 +0530 Subject: [PATCH] calibre-mount-helper: A nicer mount point location check --- src/calibre/devices/linux_mount_helper.c | 35 ++++++++++++++++++++++-- src/calibre/devices/usbms/device.py | 10 +++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/calibre/devices/linux_mount_helper.c b/src/calibre/devices/linux_mount_helper.c index dfeaa7985e..eefbd1ffcc 100644 --- a/src/calibre/devices/linux_mount_helper.c +++ b/src/calibre/devices/linux_mount_helper.c @@ -8,6 +8,7 @@ #include #include #include +#include #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) { diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index e6120f337f..0e899517a9 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -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: