mirror of
https://github.com/elliotnunn/machfs.git
synced 2025-02-19 11:31:05 +00:00
Dump files to native FS more consistently
Specifically: - A data fork dump file will always exist - A resource fork dump file will exist iff there are any bytes in that fork (but this dump file may be empty) - A Finder info dump will exist if there are any nonzero bytes in the type or creator code
This commit is contained in:
parent
51a0f8e68e
commit
fb493a154f
@ -15,6 +15,12 @@ def _fuss_if_unsyncable(name):
|
|||||||
if _unsyncability(name):
|
if _unsyncability(name):
|
||||||
raise ValueError('Unsyncable name: %r' % name)
|
raise ValueError('Unsyncable name: %r' % name)
|
||||||
|
|
||||||
|
def _try_delete(name):
|
||||||
|
try:
|
||||||
|
os.remove(name)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AbstractFolder(MutableMapping):
|
class AbstractFolder(MutableMapping):
|
||||||
def __init__(self, from_dict=()):
|
def __init__(self, from_dict=()):
|
||||||
@ -234,34 +240,35 @@ class AbstractFolder(MutableMapping):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
nativepath = path.join(folder_path, *(comp.replace(path.sep, ':') for comp in p))
|
nativepath = path.join(folder_path, *(comp.replace(path.sep, ':') for comp in p))
|
||||||
|
info_path = nativepath + '.idump'
|
||||||
|
rsrc_path = nativepath + '.rdump'
|
||||||
|
|
||||||
if isinstance(obj, Folder):
|
if isinstance(obj, Folder):
|
||||||
os.makedirs(nativepath, exist_ok=True)
|
os.makedirs(nativepath, exist_ok=True)
|
||||||
|
|
||||||
elif obj.mddate != obj.bkdate or not any_exists(nativepath):
|
elif obj.mddate != obj.bkdate or not any_exists(nativepath):
|
||||||
|
# always write the data fork
|
||||||
data = obj.data
|
data = obj.data
|
||||||
if obj.type in TEXT_TYPES:
|
if obj.type in TEXT_TYPES:
|
||||||
data = data.decode('mac_roman').replace('\r', os.linesep).encode('utf8')
|
data = data.decode('mac_roman').replace('\r', os.linesep).encode('utf8')
|
||||||
|
with open(nativepath, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
rsrc = obj.rsrc
|
# write a resource dump iff that fork has any bytes (dump may still be empty)
|
||||||
if rsrc:
|
if obj.rsrc:
|
||||||
rsrc = parse_file(rsrc)
|
with open(rsrc_path, 'wb') as f:
|
||||||
rsrc = make_rez_code(rsrc, ascii_clean=True)
|
rdump = make_rez_code(parse_file(obj.rsrc), ascii_clean=True)
|
||||||
|
f.write(rdump)
|
||||||
info = obj.type + obj.creator
|
|
||||||
if info == b'????????': info = b''
|
|
||||||
|
|
||||||
for thing, suffix in ((data, ''), (rsrc, '.rdump'), (info, '.idump')):
|
|
||||||
wholepath = nativepath + suffix
|
|
||||||
if thing or (suffix == '' and not rsrc):
|
|
||||||
written.append(wholepath)
|
|
||||||
with open(written[-1], 'wb') as f:
|
|
||||||
f.write(thing)
|
|
||||||
else:
|
else:
|
||||||
try:
|
_try_delete(rsrc_path)
|
||||||
os.remove(wholepath)
|
|
||||||
except FileNotFoundError:
|
# write an info dump iff either field is non-null
|
||||||
pass
|
idump = obj.type + obj.creator
|
||||||
|
if any(idump):
|
||||||
|
with open(info_path, 'wb') as f:
|
||||||
|
f.write(idump)
|
||||||
|
else:
|
||||||
|
_try_delete(info_path)
|
||||||
|
|
||||||
if written:
|
if written:
|
||||||
t = path.getmtime(written[-1])
|
t = path.getmtime(written[-1])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user