Skip to content

Commit

Permalink
Safer caching.
Browse files Browse the repository at this point in the history
By verifying that the cache file is referring to the
correct file path, in case of hash coliision.
  • Loading branch information
mythmon committed Jul 7, 2010
1 parent 3742f36 commit bdf57e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
18 changes: 12 additions & 6 deletions playlistsync.py
Expand Up @@ -5,6 +5,7 @@
from progress import ProgressBar
from optparse import OptionParser
from mutagen.mp3 import HeaderNotFoundError
import json
import unicodedata

cacheDir = os.path.join(os.getcwdu(), 'cache')
Expand All @@ -22,9 +23,14 @@ def getMetaData(songPath):

try:
f = open(cachePath, 'r')
jsonData = f.read()
song.fromJson(jsonData)
jsonData = json.load(f)
if jsonData['path'] == songPath:
song.fromJson(jsonData)
else:
print "Hash collision!"
raise LookupError()
except Exception as e:
print e
try:
song.load(songPath)
f = open(cachePath,'w')
Expand All @@ -36,7 +42,7 @@ def getMetaData(songPath):

return song

def main(plistpath, dest):
def main(plistpath, dest, flat=False, quiet=False, verbose=False):
plist = open(plistpath)
srcset = set()
destset = set()
Expand Down Expand Up @@ -125,7 +131,7 @@ def main(plistpath, dest):
data['album'] = sanitize(data['album'])
data['title'] = sanitize(data['title'])
newPath = ""
if options.flat == False:
if flat == False:
artistDir = u"{0[root]}/{0[artist]}".format(data)
albumDir = artistDir + u"/{0[album]}".format(data)
newPath = albumDir + u"/{0[track]:0>2} {0[title]}.mp3".format(data)
Expand Down Expand Up @@ -159,7 +165,7 @@ def main(plistpath, dest):
data['album'] = sanitize(data['album'])
data['title'] = sanitize(data['title'])
newFile = ""
if options.flat == False:
if flat == False:
artistDir = u"{0[root]}/{0[artist]}".format(data)
albumDir = artistDir + u"/{0[album]}".format(data)
newFile = albumDir + u"/{0[track]:0>2} {0[title]}.mp3".format(data)
Expand Down Expand Up @@ -194,4 +200,4 @@ def main(plistpath, dest):
plistpath = args[0]
dest = args[1]

main(plistpath,dest)
main(plistpath,dest,flat=options.flat)
20 changes: 9 additions & 11 deletions songfile.py
Expand Up @@ -44,18 +44,16 @@ def toJson(self):
return j

# From json
def fromJson(self, jsonString):
data = json.loads(jsonString)
def fromJson(self, jsonData):
if int(jsonData['cache_version']) != CACHE_VERSION:
print jsonData['cache_version'], CACHE_VERSION
raise CacheVersionError(jsonData['cache_version'], CACHE_VERSION)

if int(data['cache_version']) != CACHE_VERSION:
print data['cache_version'], CACHE_VERSION
raise CacheVersionError(data['cache_version'], CACHE_VERSION)

self.mp3path = data['path']
self.artist = [data['artist']]
self.album = [data['album']]
self.title = [data['title']]
self.track = [data['track']]
self.mp3path = jsonData['path']
self.artist = [jsonData['artist']]
self.album = [jsonData['album']]
self.title = [jsonData['title']]
self.track = [jsonData['track']]

# ignore the path, just worry about the artist,album,title, etc
def __eq__(self, other):
Expand Down

0 comments on commit bdf57e5

Please sign in to comment.