NTFS文件名Unicode私有使用字符映射规则及跨平台适配问询
跨系统文件名特殊字符映射问题处理
问题背景
Windows会使用Unicode私有区域(U+F000至U+F0FF)的字符,编码那些在Linux文件名中合法但Windows不允许的字符。这类文件通常来自Windows Subsystem for Linux(WSL),或是通过跨系统复制操作产生(比如从MacOSX使用过的FAT32磁盘复制文件)。当需要同时从WSL和Windows(例如通过Python)访问这些文件时,必须明确字符的映射关系,但目前未找到权威的映射表。
已验证的WSL→Windows映射
通过WSL实验,得到以下确定的字符映射关系:
< → U+F03C > → U+F03E : → U+F03A " → U+F022 \ → U+F05C | → U+F07C ? → U+F03F * → U+F02A
基于上下文猜测的映射
在Windows文件中发现更多该区间内的码点,目前只能通过上下文推测其映射关系:
U+F009 → 制表符(tab) U+F00A → 换行符(linefeed) U+F00D → 回车符(carriage return) U+F020 → 双引号(double quote) U+F021 → 竖线(vertical line) U+F022 → 冒号或斜杠(colon or solidus) U+F023 → 小于号(less than sign) U+F024 → 大于号(greater than sign) U+F025 → 问号(question mark) U+F026 → 反斜杠(reverse solidus) U+F027 → 竖线(vertical line) U+F028 → 文件名末尾的空格(space [at end of filename]) U+F029 → 文件名末尾的句点(full stop [at end of filename])
现有Python处理代码
计划明确映射规则后,将这些字符映射成全角等价字符以适配各文件系统,目前已编写的代码如下:
import sys map_dict = { 0xf009:'\uff3ct', # tab 0xf00a:'\uff3cn', # linefeed 0xf00d:'\uff3cr', # carriage return 0xf020:'\uff02', # double quote 0xf021:'\uff5c', # vertical line 0xf022:'\uff1a', # colon or solidus 0xf023:'\uff1c', # less than sign 0xf024:'\uff1e', # more than sign 0xf025:'\uff1f', # question mark 0xf026:'\uff3c', # reverse solidus 0xf027:'\uff5c', # vertical line 0xf028:' ', # this seems to be at the end of the filename 0xf029:'.', # this seems to be at the end of the filename 0xf03a:'\uff1a', # colon 0xf03f:'\uff1f', # question mark 0xf8ff:'Apple\u2122', # Apple trade mark } def map_filename(input_name, map_dict): output_name = "" mapped = False for c in str(input_name): code_point = ord(c) if code_point in map_dict: output_name += map_dict[code_point] mapped = True else: if ((code_point >= 0xe000 and code_point <= 0xf8ff) or (code_point >= 0xf0000 and code_point <= 0xfffff) or (code_point >= 0x100000 and code_point <= 0x10ffff)): print('Error "%s" contains unmapped private use character %04x' % (input_name, code_point)) sys.exit(1) output_name += c return (output_name, mapped)
注:上述代码基于猜测编写,存在大量缺陷。
需求
寻求以下信息:
- WSL的全量字符映射规则
- MacOS/Linux在FAT32等场景下的非Windows字符编码规则
内容的提问来源于stack exchange,提问作者Bill Sellers
相关产品推荐
相关产品推荐

