Skip to content

Commit

Permalink
fix(mac): should normalize unicode strings from file system before us…
Browse files Browse the repository at this point in the history
…ed in string compare (#4841)

Co-authored-by: Oliver Poignant <[email protected]>
  • Loading branch information
olipo186 and olipo186 committed Apr 3, 2020
1 parent 9c43cc3 commit 37a17f2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions packages/dmg-builder/vendor/mac_alias/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os.path
import stat
import sys
from unicodedata import normalize

if sys.platform == 'darwin':
from . import osx
Expand All @@ -20,7 +21,7 @@
long = int

from .utils import *

ALIAS_KIND_FILE = 0
ALIAS_KIND_FOLDER = 1

Expand Down Expand Up @@ -56,21 +57,21 @@ def __init__(self, zone=None, server=None, user=None):

def __repr__(self):
return 'AppleShareInfo(%r,%r,%r)' % (self.zone, self.server, self.user)

class VolumeInfo (object):
def __init__(self, name, creation_date, fs_type, disk_type,
attribute_flags, fs_id, appleshare_info=None,
driver_name=None, posix_path=None, disk_image_alias=None,
dialup_info=None, network_mount_info=None):
#: The name of the volume on which the target resides
self.name = name

#: The creation date of the target's volume
self.creation_date = creation_date

#: The filesystem type (a two character code, e.g. ``b'H+'`` for HFS+)
self.fs_type = fs_type

#: The type of disk; should be one of
#:
#: * ALIAS_FIXED_DISK
Expand Down Expand Up @@ -116,23 +117,23 @@ def __repr__(self):
for a in args:
v = getattr(self, a)
values.append(repr(v))

kwargs = ['appleshare_info', 'driver_name', 'posix_path',
'disk_image_alias', 'dialup_info', 'network_mount_info']
for a in kwargs:
v = getattr(self, a)
if v is not None:
values.append('%s=%r' % (a, v))
return 'VolumeInfo(%s)' % ','.join(values)

class TargetInfo (object):
def __init__(self, kind, filename, folder_cnid, cnid, creation_date,
creator_code, type_code, levels_from=-1, levels_to=-1,
folder_name=None, cnid_path=None, carbon_path=None,
posix_path=None, user_home_prefix_len=None):
#: Either ALIAS_KIND_FILE or ALIAS_KIND_FOLDER
self.kind = kind

#: The filename of the target
self.filename = filename

Expand Down Expand Up @@ -197,7 +198,7 @@ def __repr__(self):
values.append('%s=%r' % (a, v))

return 'TargetInfo(%s)' % ','.join(values)

TAG_CARBON_FOLDER_NAME = 0
TAG_CNID_PATH = 1
TAG_CARBON_PATH = 2
Expand Down Expand Up @@ -230,7 +231,7 @@ def __init__(self, appinfo=b'\0\0\0\0', version=2, volume=None,

#: A :class:`VolumeInfo` object describing the target's volume
self.volume = volume

#: A :class:`TargetInfo` object describing the target
self.target = target

Expand All @@ -243,7 +244,7 @@ def _from_fd(cls, b):

if recsize < 150:
raise ValueError('Incorrect alias length')

if version != 2:
raise ValueError('Unsupported alias version %u' % version)

Expand All @@ -257,7 +258,7 @@ def _from_fd(cls, b):

alias = Alias()
alias.appinfo = appinfo

alias.volume = VolumeInfo (volname.replace('/',':'),
voldate, fstype, disktype,
volattrs, volfsid)
Expand All @@ -266,7 +267,7 @@ def _from_fd(cls, b):
crdate, creator_code, type_code)
alias.target.levels_from = levels_from
alias.target.levels_to = levels_to

tag = struct.unpack(b'>h', b.read(2))[0]

while tag != -1:
Expand Down Expand Up @@ -324,9 +325,9 @@ def _from_fd(cls, b):
alias.extra.append((tag, value))

tag = struct.unpack(b'>h', b.read(2))[0]

return alias

@classmethod
def from_bytes(cls, bytes):
"""Construct an :class:`Alias` object given binary Alias data."""
Expand All @@ -340,13 +341,17 @@ def for_file(cls, path):
raise Exception('Not implemented (requires special support)')

path = encode_utf8(path)

a = Alias()

# Find the filesystem
st = osx.statfs(path)
vol_path = st.f_mntonname


# File and folder names in HFS+ are normalized to a form similar to NFD.
# Must be normalized (NFD->NFC) before use to avoid unicode string comparison issues.
vol_path = normalize("NFC", vol_path.decode('utf-8')).encode('utf-8')

# Grab its attributes
attrs = [osx.ATTR_CMN_CRTIME,
osx.ATTR_VOL_NAME,
Expand All @@ -355,7 +360,7 @@ def for_file(cls, path):

vol_crtime = volinfo[0]
vol_name = encode_utf8(volinfo[1])

# Also grab various attributes of the file
attrs = [(osx.ATTR_CMN_OBJTYPE
| osx.ATTR_CMN_CRTIME
Expand All @@ -371,14 +376,14 @@ def for_file(cls, path):

cnid = info[3]
folder_cnid = info[4]

dirname, filename = os.path.split(path)

if dirname == b'' or dirname == b'.':
dirname = os.getcwd()

foldername = os.path.basename(dirname)

creation_date = info[1]

if kind == ALIAS_KIND_FILE:
Expand Down Expand Up @@ -425,7 +430,7 @@ def for_file(cls, path):
a.target.cnid_path = cnid_path

return a

def _to_fd(self, b):
# We'll come back and fix the length when we're done
pos = b.tell()
Expand Down Expand Up @@ -578,7 +583,7 @@ def _to_fd(self, b):
b.write(b'\0')

b.write(struct.pack(b'>hh', -1, 0))

blen = b.tell() - pos
b.seek(pos + 4, os.SEEK_SET)
b.write(struct.pack(b'>h', blen))
Expand Down

0 comments on commit 37a17f2

Please sign in to comment.