From 98163086fc67b137f8dc805ad48195b0394d10b5 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Tue, 18 May 2010 06:23:38 +0100 Subject: [PATCH 1/3] First pass at winutil.c to associate usb vendor ids with drives --- src/calibre/utils/windows/winutil.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/calibre/utils/windows/winutil.c b/src/calibre/utils/windows/winutil.c index 2f176043b2..42b6462313 100644 --- a/src/calibre/utils/windows/winutil.c +++ b/src/calibre/utils/windows/winutil.c @@ -560,6 +560,7 @@ get_device_ancestors(HDEVINFO hDevInfo, DWORD index, PyObject *candidates, BOOL return NULL; } interfaceDetailData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA); + devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); status = SetupDiGetDeviceInterfaceDetail ( hDevInfo, // Interface Device info handle From 12374868318c6b39971ce4c6c54310f4ffc6e03a Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 19 May 2010 09:41:09 +0100 Subject: [PATCH 2/3] Fix prefix to be normalized. Apparently the python file dialog returns front-slashed filenames, even on windows. --- src/calibre/devices/folder_device/driver.py | 1 + src/calibre/devices/usbms/driver.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/devices/folder_device/driver.py b/src/calibre/devices/folder_device/driver.py index 792de9ee0a..bb3c684099 100644 --- a/src/calibre/devices/folder_device/driver.py +++ b/src/calibre/devices/folder_device/driver.py @@ -54,6 +54,7 @@ class FOLDER_DEVICE(USBMS): def __init__(self, path): if not os.path.isdir(path): raise IOError, 'Path is not a folder' + path = USBMS.normalize_path(path) if path.endswith(os.sep): self._main_prefix = path else: diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 5273ffe579..f519e8ce22 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -90,7 +90,6 @@ class USBMS(CLI, Device): #print 'update_metadata_item returned true' changed = True else: - #print "adding new book", lpath if bl.add_book(self.book_from_path(prefix, lpath), replace_metadata=False): changed = True From 9fcab76112491ee3a5b552390ec9254cc60648b7 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 19 May 2010 11:31:08 +0100 Subject: [PATCH 3/3] Ensure that normalized paths and paths that are found during a directory walk are unicode. In addition, fix matching of prefixes when adding metadata to books on cards on windows machines. --- src/calibre/devices/usbms/driver.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index f519e8ce22..c75fc9f6a1 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -112,7 +112,8 @@ class USBMS(CLI, Device): for path, dirs, files in os.walk(ebook_dir): for filename in files: if filename != self.METADATA_CACHE: - flist.append({'filename':filename, 'path': path}) + flist.append({'filename':unicode(filename), + 'path':unicode(path)}) for i, f in enumerate(flist): self.report_progress(i/float(len(flist)), _('Getting list of books on device...')) changed = update_booklist(f['filename'], f['path'], prefix) @@ -122,7 +123,7 @@ class USBMS(CLI, Device): paths = os.listdir(ebook_dir) for i, filename in enumerate(paths): self.report_progress((i+1) / float(len(paths)), _('Getting list of books on device...')) - changed = update_booklist(filename, ebook_dir, prefix) + changed = update_booklist(unicode(filename), ebook_dir, prefix) if changed: need_sync = True @@ -188,20 +189,22 @@ class USBMS(CLI, Device): for i, location in enumerate(locations): self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...')) info = metadata.next() - path = location[0] blist = 2 if location[1] == 'cardb' else 1 if location[1] == 'carda' else 0 + # Extract the correct prefix from the pathname. To do this correctly, + # we must ensure that both the prefix and the path are normalized + # so that the comparison will work. Book's __init__ will fix up + # lpath, so we don't need to worry about that here. + path = self.normalize_path(location[0]) if self._main_prefix: - # Normalize path and prefix - if self._main_prefix.find('\\') >= 0: - path = path.replace('/', '\\') - else: - path = path.replace('\\', '/') - prefix = self._main_prefix if path.startswith(self._main_prefix) else None + prefix = self._main_prefix if \ + path.startswith(self.normalize_path(self._main_prefix)) else None if not prefix and self._card_a_prefix: - prefix = self._card_a_prefix if path.startswith(self._card_a_prefix) else None + prefix = self._card_a_prefix if \ + path.startswith(self.normalize_path(self._card_a_prefix)) else None if not prefix and self._card_b_prefix: - prefix = self._card_b_prefix if path.startswith(self._card_b_prefix) else None + prefix = self._card_b_prefix if \ + path.startswith(self.normalize_path(self._card_b_prefix)) else None if prefix is None: prints('in add_books_to_metadata. Prefix is None!', path, self._main_prefix) @@ -273,7 +276,7 @@ class USBMS(CLI, Device): path = path.replace('/', '\\') else: path = path.replace('\\', '/') - return path + return unicode(path) @classmethod def parse_metadata_cache(cls, bl, prefix, name):