TONT 33243 为什么(有时)UNC路径的开头允许有盘符?


A little-known quirk is that the file system accepts and ignores a drive letter in front of a UNC path. For example, if you have a directory called \\server\share\directory, you can say

有个鲜为人知的奇怪设计是,文件系统接受(并会忽略)UNC 路径的开头包含盘符。例如,如果你有个目录(的共享路径)是\\server\share\directory,你可以输入

dir P:\\server\share\directory

and the directory will be listed to the screen. The leading P: is ignored.

目录内容会成功列出,开头的 P: 被忽略了。

Why is that?


Rewind to 1984 and the upcoming release of MS-DOS 3.1, which added networking support. Up to this point, all fully-qualified file specifications consisted of three components: A drive letter, a path, and a file name. Many programs relied on this breakdown and did things like “helpfully” prepend a drive letter if it looks like you “forgot” one. For example, if you told it to save the results to \\server\share\file.txt it would say, “Oh dear, that’s not good, the user forgot the drive letter! I’ll put the current drive in front to make things better,” resulting in C:\\server\share\file.txt. Other programs would prompt you with “Please enter a drive letter”, and you couldn’t say “No, there’s no drive letter, just take the path and use it.” They insisted on a drive letter, and you darn sure better give them one.

回到 1984 年,MS-DOS 3.1 即将发布,该版本新增了网络支持。到这时为止,所有完全限定的文件规范包含三个组成部分:盘符、路径和文件名。很多应用程序都依赖这种组合方式,并且如果路径看上去是用户『忘了』录入盘符的话,还会在最前面『帮忙』加上一个。例如,如果用户指定将数据保存到 \\server\share\file.txt,程序就会认为:『哎呀,这怎么行,用户忘了写盘符了!让我在最前面加上当前驱动器的盘符,这样就好了』,路径就变成了 C:\\server\share\file.txt。另外也有程序会提示用户『请输入盘符』,而用户无法告诉程序『不不,这个路径里没有盘符,直接用这个路径就行了』。程序是一定要一个盘符的,而用户不给是不行的。

(Compare the Unix programs that “helpfully” rewrite //server/volume/file as /server/volume/file because they “know” that consecutive slashes collapse, unaware of the special exception for two leading slashes.)

(相较之下,UNIX 程序则会『帮忙』将 //server/volume/file 重写为 /server/volume/file,因为他们『知道』连续斜线应当进行合并,但却不知道两道斜线开头是个例外。)

In order to retain compatibility with programs that provided this sort of “unwanted help”, the designers of the networking support in MS-DOS decided to allow the strange syntax C:\\server\share\directory and treat it as if the drive letter simply weren’t there. Some (but not all) of this quirk of path parsing persists today.

为了保持与提供这种『毫无必要的帮助』的程序的兼容性,MS-DOS 网路支持功能的设计者们决定允许 C:\\server\share\directory 这种奇怪语法的存在,并将其视为开头的盘符


您的电子邮箱地址不会被公开。 必填项已用*标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.