calibre-mount-helper: A nicer mount point location check

This commit is contained in:
Kovid Goyal 2011-11-05 00:24:11 +05:30
parent b660c352a4
commit e148d61412
2 changed files with 41 additions and 4 deletions

View File

@ -8,6 +8,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <fcntl.h> #include <fcntl.h>
#include <strings.h>
#define MARKER ".created_by_calibre_mount_helper" #define MARKER ".created_by_calibre_mount_helper"
#define DEV "/dev/" #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) int main(int argc, char** argv)
{ {
char *action, *dev, *mp, *temp; char *action, *dev, *mp, *temp;
@ -303,8 +335,7 @@ int main(int argc, char** argv)
fprintf(stderr, "Failed to resolve device node.\n"); fprintf(stderr, "Failed to resolve device node.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
temp = realpath(mp, NULL); mp = get_real_mount_point(mp);
if (temp != NULL) mp = temp;
check_dev(dev); check_mount_point(mp); check_dev(dev); check_mount_point(mp);
status = do_mount(dev, mp); status = do_mount(dev, mp);
} else if (strncmp(action, "eject", 5) == 0) { } else if (strncmp(action, "eject", 5) == 0) {

View File

@ -622,8 +622,11 @@ class Device(DeviceConfig, DevicePlugin):
if getattr(sys, 'frozen', False): if getattr(sys, 'frozen', False):
cmd = os.path.join(sys.executables_location, 'bin', cmd) cmd = os.path.join(sys.executables_location, 'bin', cmd)
cmd = [cmd, 'mount'] cmd = [cmd, 'mount']
mlabel = label
if mlabel.endswith('/'):
mlabel = mlabel[:-1]
try: try:
p = subprocess.Popen(cmd + [node, '/media/'+label]) p = subprocess.Popen(cmd + [node, '/media/'+mlabel])
except OSError: except OSError:
raise DeviceError( raise DeviceError(
_('Could not find mount helper: %s.')%cmd[0]) _('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 # try all the nodes to see what we can mount
for dev in devs[i].split(): for dev in devs[i].split():
mp='/media/'+label+'-'+dev mp='/media/'+label+'-'+dev
mmp = mp
if mmp.endswith('/'):
mmp = mmp[:-1]
#print "trying ", dev, "on", mp #print "trying ", dev, "on", mp
try: try:
p = subprocess.Popen(cmd + ["/dev/"+dev, mp]) p = subprocess.Popen(cmd + ["/dev/"+dev, mmp])
except OSError: except OSError:
raise DeviceError(_('Could not find mount helper: %s.')%cmd[0]) raise DeviceError(_('Could not find mount helper: %s.')%cmd[0])
while p.poll() is None: while p.poll() is None: