port APFS fix to 2.0

This commit is contained in:
Damien Elmes 2018-03-01 12:56:32 +10:00
parent 1de2f316e4
commit 53e730c801
2 changed files with 22 additions and 21 deletions

View file

@ -2,7 +2,7 @@
# Copyright: Damien Elmes <anki@ichi2.net> # Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import re, os, zipfile, shutil import re, os, zipfile, shutil, unicodedata
from anki.lang import _ from anki.lang import _
from anki.utils import ids2str, splitFields, json from anki.utils import ids2str, splitFields, json
from anki.hooks import runHook from anki.hooks import runHook
@ -272,7 +272,7 @@ class AnkiPackageExporter(AnkiExporter):
mpath = os.path.join(self.mediaDir, file) mpath = os.path.join(self.mediaDir, file)
if os.path.exists(mpath): if os.path.exists(mpath):
z.write(mpath, cStr, zipfile.ZIP_STORED) z.write(mpath, cStr, zipfile.ZIP_STORED)
media[cStr] = file media[cStr] = unicodedata.normalize("NFC", file)
runHook("exportedMediaFiles", c) runHook("exportedMediaFiles", c)
# tidy up intermediate files # tidy up intermediate files
os.unlink(colfile) os.unlink(colfile)

View file

@ -140,7 +140,7 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
def writeData(self, opath, data): def writeData(self, opath, data):
# if fname is a full path, use only the basename # if fname is a full path, use only the basename
fname = os.path.basename(opath) fname = os.path.basename(opath)
# make sure we write it in NFC form (on mac will autoconvert to NFD), # make sure we write it in NFC form (pre-APFS Macs will autoconvert to NFD)
# and return an NFC-encoded reference # and return an NFC-encoded reference
fname = unicodedata.normalize("NFC", fname) fname = unicodedata.normalize("NFC", fname)
# remove any dangerous characters # remove any dangerous characters
@ -275,8 +275,7 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
invalid.append(unicode(file, sys.getfilesystemencoding(), "replace")) invalid.append(unicode(file, sys.getfilesystemencoding(), "replace"))
continue continue
nfcFile = unicodedata.normalize("NFC", file) nfcFile = unicodedata.normalize("NFC", file)
# we enforce NFC fs encoding on non-macs; on macs we'll have gotten # we enforce NFC fs encoding on non-macs
# NFD so we use the above variable for comparing references
if not isMac and not local: if not isMac and not local:
if file != nfcFile: if file != nfcFile:
# delete if we already have the NFC form, otherwise rename # delete if we already have the NFC form, otherwise rename
@ -371,7 +370,9 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
self.cache = {} self.cache = {}
for (name, csum, mod) in self.db.execute( for (name, csum, mod) in self.db.execute(
"select fname, csum, mtime from media where csum is not null"): "select fname, csum, mtime from media where csum is not null"):
self.cache[name] = [csum, mod, False] # previous entries may not have been in NFC form
normname = unicodedata.normalize("NFC", name)
self.cache[normname] = [csum, mod, False]
added = [] added = []
removed = [] removed = []
# loop through on-disk files # loop through on-disk files
@ -393,25 +394,28 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
self.col.log("ignoring file over 100MB", f) self.col.log("ignoring file over 100MB", f)
continue continue
# check encoding # check encoding
normname = unicodedata.normalize("NFC", f)
if not isMac: if not isMac:
normf = unicodedata.normalize("NFC", f) if f != normname:
if f != normf:
# wrong filename encoding which will cause sync errors # wrong filename encoding which will cause sync errors
if os.path.exists(normf): if os.path.exists(normname):
os.unlink(f) os.unlink(f)
else: else:
os.rename(f, normf) os.rename(f, normname)
else:
# on Macs we can access the file using any normalization
pass
# newly added? # newly added?
if f not in self.cache: if normname not in self.cache:
added.append(f) added.append(normname)
else: else:
# modified since last time? # modified since last time?
if self._mtime(f) != self.cache[f][1]: if self._mtime(normname) != self.cache[normname][1]:
# and has different checksum? # and has different checksum?
if self._checksum(f) != self.cache[f][0]: if self._checksum(normname) != self.cache[normname][0]:
added.append(f) added.append(normname)
# mark as used # mark as used
self.cache[f][2] = True self.cache[normname][2] = True
# look for any entries in the cache that no longer exist on disk # look for any entries in the cache that no longer exist on disk
for (k, v) in self.cache.items(): for (k, v) in self.cache.items():
if not v[2]: if not v[2]:
@ -513,11 +517,8 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
name = meta[i.filename] name = meta[i.filename]
if not isinstance(name, unicode): if not isinstance(name, unicode):
name = unicode(name, "utf8") name = unicode(name, "utf8")
# normalize name for platform # normalize name
if isMac: name = unicodedata.normalize("NFC", name)
name = unicodedata.normalize("NFD", name)
else:
name = unicodedata.normalize("NFC", name)
# save file # save file
open(name, "wb").write(data) open(name, "wb").write(data)
# update db # update db