Przeglądaj źródła

symuid-import-cmus: parse&validate cache prefix, version & flags

Fabian Peter Hammerle 6 lat temu
rodzic
commit
bcabde1539
1 zmienionych plików z 21 dodań i 5 usunięć
  1. 21 5
      symuid-import-cmus

+ 21 - 5
symuid-import-cmus

@@ -8,19 +8,35 @@ import symuid
 # import symuid.library.cmus
 import sys
 
-CACHE_LINE_SEPARATOR = b'\xff'*56
-CACHE_COLUMN_SEPARATOR = b'\x00'
+FILE_PREFIX = b'CTC'
+VERSION_LENGTH = 1
+SUPPORTED_VERSION = b'\x0c'
+LINE_SEPARATOR = b'\xff' * 56
+COLUMN_SEPARATOR = b'\x00'
+# always big endian, see cache_init()
+FLAGS_BYTEORDER = 'big'
+FLAGS_LENGTH = 4
+FLAG_64_BIT = 0x01
 
 def symuid_import_cmus(cache_path):
-    with open(os.path.expanduser(cache_path), 'rb') as f:
-        cache_data = [l.split(CACHE_COLUMN_SEPARATOR) for l in f.read().split(CACHE_LINE_SEPARATOR)]
+    with open(os.path.expanduser(cache_path), 'rb') as c:
+        # see cache.c cache_init()
+        assert c.read(len(FILE_PREFIX)) == FILE_PREFIX
+        cache_version = c.read(VERSION_LENGTH)
+        assert cache_version == SUPPORTED_VERSION, cache_version
+        flags = int.from_bytes(c.read(FLAGS_LENGTH), byteorder=FLAGS_BYTEORDER)
+        # only support 64-bit flag
+        assert flags & ~FLAG_64_BIT == 0, flags
+        cache_data = [l.split(COLUMN_SEPARATOR)
+                      for l in c.read().split(LINE_SEPARATOR)]
     for track_data in cache_data[1:]:
-        track_path = track_data[0]
+        track_path, codec, codec_profile = track_data[0:3]
         if not os.path.exists(track_path):
             sys.stderr.write('{!r}: not found\n'.format(track_path))
         else:
             print(track_path)
 
+
 def _init_argparser():
     import argparse
     argparser = argparse.ArgumentParser(description=None)