Skip to content

Instantly share code, notes, and snippets.

@zhangyoufu
Created August 25, 2022 10:47
Show Gist options
  • Save zhangyoufu/825e14d69c82464a16688977f791a3c8 to your computer and use it in GitHub Desktop.
Save zhangyoufu/825e14d69c82464a16688977f791a3c8 to your computer and use it in GitHub Desktop.
create symlink on exFAT filesystem (macOS way)
#!/usr/bin/env python3
import argparse
import hashlib
import os
SMB_SYMHDRLEN = (4+1)+(4+1)+(32+1)
MAXPATHLEN = 0x400
# see smbfs_create_windows_symlink_data
def symlink(target: str, link: str) -> None:
if os.path.exists(link):
raise FileExistsError
body = target.encode()
if len(body) > MAXPATHLEN:
raise ValueError
hdr = f'Xsym\n{len(body):04d}\n{hashlib.md5(body).hexdigest()}\n'.encode()
assert len(hdr) == SMB_SYMHDRLEN
if len(body) < MAXPATHLEN:
body += b'\n'
body = body.ljust(MAXPATHLEN, b' ')
with open(link, 'wb') as f:
f.write(hdr + body)
def main() -> None:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('target')
parser.add_argument('link')
args = parser.parse_args()
symlink(args.target, args.link)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment