olNGIb4NkK5r2x7x4oG3GpEzizVpnY6KNCck9cym

你知道在 Unix-Like 作業系統中,在開頭使用 "." 來標示隱藏文件,或者說 dotfile 可能是個錯誤嗎?

Rob Pike 曾經表示 Unix-Like 作業系統中那些 dotfile 或者說使用 "." 前綴表示隱藏文件是一種錯誤。

為了撰寫 英文口語中的 "handwavy" 是什麼意思? 這一篇文章,筆者昨天在搜尋 Rob Pike 過往的演講記錄時,發現了一篇意外之喜:Rob Pike on the Origin of Unix Dot File Names

這篇文章記錄並備份了 Rob Pike 早期在 Google Plus 上發表的一篇內容,文章中提及 Rob Pike 在 Unix 小組工作時,關於檔案系統(file system)設計,以及後續這個設計卻開創了一個不好的先例(bad precedent)以及意想不到的後果:出現了隱藏文件或者 dotfile 的概念

隨著網路服務的興衰,如今 Google Plus 早已被 Google 公司所摒棄並送進了 Google Graveyard,這樣有趣的軼事不再被人看見是很可惜的,遂筆者也將全文轉載如下:

A lesson in shortcuts.

Long ago, as the design of the Unix file system was being worked out, the entries . and .. appeared, to make navigation easier. I'm not sure but I believe .. went in during the Version 2 rewrite, when the file system became hierarchical (it had a very different structure early on). When one typed ls, however, these files appeared, so either Ken or Dennis added a simple test to the program. It was in assembler then, but the code in question was equivalent to something like this:

if (name[0] == '.') continue;

This statement was a little shorter than what it should have been, which is

if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;

but hey, it was easy.

Two things resulted.

First, a bad precedent was set. A lot of other lazy programmers introduced bugs by making the same simplification. Actual files beginning with periods are often skipped when they should be counted.

Second, and much worse, the idea of a “hidden” or “dot” file was created. As a consequence, more lazy programmers started dropping files into everyone's home directory. I don't have all that much stuff installed on the machine I'm using to type this, but my home directory has about a hundred dot files and I don't even know what most of them are or whether they're still needed. Every file name evaluation that goes through my home directory is slowed down by this accumulated sludge.

I'm pretty sure the concept of a hidden file was an unintended consequence. It was certainly a mistake.

How many bugs and wasted CPU cycles and instances of human frustration (not to mention bad design) have resulted from that one small shortcut about 40 years ago?

Keep that in mind next time you want to cut a corner in your code.

(For those who object that dot files serve a purpose, I don't dispute that but counter that it's the files that serve the purpose, not the convention for their names. They could just as easily be in $HOME/cfg or $HOME/lib, which is what we did in Plan 9, which had no dot files. Lessons can be learned.)

根據 Rob Pike 的說法,在開發 Unix 作業系統的過程中,檔案系統隨著設計的變更逐漸發展成了分層結構,並以 ... 作為標記以便導航(navigation),但是這麼一來會在使用 ls 命令陳列文件時出現於結果中,於是添加了一段簡單邏輯判斷程式來跳過並避免這樣的結果。

然而這樣無心插柳的做法,卻開創了一個不好的先例:有樣學樣的程式開發人員使用了同樣的簡化方式,於是以 . 開頭的文件在它們需要被計數時,卻被這樣的簡化給忽略了!更糟的是,因此延伸出了以 . 作為前綴來表示隱藏檔案,或者是 dotfile 的概念……

在 Rob Pike 看來 這無疑是個錯誤(it was certainly a mistake),因為人們開始在 $HOME 目錄中以 dotfile 的形式創建配置文件,從而引致了許多錯誤與資源浪費。許多人認為這樣約定成俗的 dotfile 慣例是有用的,他也在文章末段補充 真正起到作用的是文件本身,而不是這樣的命名約定

為了應對這樣的 "dotfile pollution" 狀況,筆者習慣遵循 XDG Base Directory 規則來放置文件,如果你懶得自己動手,或許還可以試試看 xdg-ninja 這款工具。

張貼留言