Mac OS X 上的 XDG_CONFIG_HOME 和 XDG_DATA_HOME 等效吗?
我计划开发一个跨平台脚本。在 Linux 和其他操作系统上,它将在 XDG_CONFIG_HOME
中存储配置,在 XDG_DATA_HOME
中存储数据文件(特别是下载的插件)。在 Windows 上,它将使用 APPDATA 来实现两者(除非有人有更好的主意)。然而,在 Mac OS X 上正确的做法是什么?
当我第一次浏览方便的 Macbook 的 ~/Library
目录时,我看到了 Preferences
和 Application Support
文件夹。我原本打算使用这些,但是 Preferences
似乎只包含具有反向域名的 plist,例如 com.apple.foo.bar.baz.plist
,以及中的每个文件夹Application Support
对应于 /Applications
中的捆绑包,因此我不确定系统对不符合其标准的文件的反应如何。将它们直接存储在 ~/Library
中可能是一种选择,但如果有更好的地方的话,我不想用杂散的 myscript.conf
文件污染它。
我应该将这些文件存储在哪里? (请不要只说 ~/.myscript
。我知道这是 Unix 传统,但在主目录中看到随机的点文件让我很恼火。)
I am planning to develop a cross-platform script. On Linux and other operating systems, it will store configuration in XDG_CONFIG_HOME
and data files (specifically, downloaded plugins) in XDG_DATA_HOME
. On Windows, it will use APPDATA
for both (unless someone has a better idea). However, what would be the proper thing to do on Mac OS X?
On my first glance through a handy Macbook's ~/Library
directory, I saw Preferences
and Application Support
folders. I was originally planning to use those, but Preferences
seems to just contain plists with reverse domain names like com.apple.foo.bar.baz.plist
, and every folder in Application Support
corresponds to a bundle in /Applications
, so I'm not sure how well the system would react to files that don't match its standards. Storing them directly in ~/Library
might be an option, but I don't want to pollute it with a stray myscript.conf
file if there's a better place for it.
Where should I store these files? (And please don't say just ~/.myscript
. I know it's the Unix tradition, but it annoys me to see random dotfiles in the home directory.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
比较 Apple 文档中 XDG 基本目录规范的各种路径,大致位于以下位置:
XDG_CONFIG_HOME
▶︎~/Library/Preferences/
XDG_DATA_HOME
▶︎~/Library/
XDG_CACHE_HOME
▶︎~/Library/Caches/
在 Mac OS X 上映射“我的应用程序”的 XDG 基本目录规范位置可以查看像这样:
XDG_CONFIG_HOME
▶︎~/Library/Preferences/name.often.with.domain.myapp.plist
XDG_DATA_HOME
▶︎~/ Library/My App/
XDG_CACHE_HOME
▶︎~/Library/Caches/My App/
这些映射看起来很合理,但并不准确。某些类型的缓存或数据可能适合
~/Library/Application Support/My App
,而其他类型的缓存或数据可能最适合临时位置或应用程序包。所有这些都是按照惯例,使用最佳XDG_
位置的原因同样适用于在 Mac OS X 系统上使用最佳位置。您对
~/.myscript
的烦恼符合 Apple 的准则:“不要污染用户空间”。参考文献:
Comparing Apple's documentation for the various paths to the XDG Base Directory specifications approximates to the following locations:
XDG_CONFIG_HOME
▶︎~/Library/Preferences/
XDG_DATA_HOME
▶︎~/Library/
XDG_CACHE_HOME
▶︎~/Library/Caches/
Mapping XDG Base Directory Specification locations for "My App" on Mac OS X could look like this:
XDG_CONFIG_HOME
▶︎~/Library/Preferences/name.often.with.domain.myapp.plist
XDG_DATA_HOME
▶︎~/Library/My App/
XDG_CACHE_HOME
▶︎~/Library/Caches/My App/
These mappings seem pretty reasonable but they aren't exact. Some kinds of cache or data may be appropriate for
~/Library/Application Support/My App
, and other may be best in the temp locations or the App bundle. All of it is by convention and the same reasons for using the bestXDG_
locations apply to using the best locations on the Mac OS X system.Your annoyance at
~/.myscript
is in line with Apple's guidelines: "Don't pollute user space".References:
我同意 OJFord 的评论:如果您正在编写脚本(我认为这适用于所有仅限 CLI 的应用程序),只需遵循 XDG 基本目录会更好。
理由:
XDG_CONFIG_HOME
中的名称通常是小写的裸名称,例如git
;~/Library/Preferences/
中的文件通常是以 反向域名命名的文件符号,例如com.apple.AppStore.plist
,或 标题大小写如Blackmagic Design
。/Applications
中时一直遵循 macOS 约定。您没有将脚本放在/Applications
中;您将它们放在 UNIX 特定目录。最好保持一致。~/Library/Preferences/
充满了.plist
。据我所知,没有跨平台脚本使用属性列表。例如,Git 将其配置文件放置在
XDG_CONFIG_HOME/git/config
中,而不是~/Library/Preferences/Git/config
中。对我来说很有意义。注意
我仅针对配置文件 (
$XDG_CONFIG_HOME
) 和数据文件 ($XDG_DATA_HOME
) 指出这一点;对于缓存文件来说,情况会变得微妙。根据 How-To Geek,~/Library/Caches
目录已从 时间机器。我不关心缓存文件夹,因为我无论如何都不备份这两个目录;但对于关心的人,我建议他们将~/.cache
链接到默认缓存文件夹中的某个位置,例如:I agree with OJFord's comment: if you are writing a script (I think this applies to all CLI-only application), simply follow the XDG Base Directory would be better.
Rationales:
XDG_CONFIG_HOME
are typically small-case bare-names likegit
; the ones in~/Library/Preferences/
are typically files named with reverse domain name notation likecom.apple.AppStore.plist
, or directories named in title case likeBlackmagic Design
./Applications
. You don't place your script in/Applications
; you place them in UNIX-specific directories. Better to be consistent.~/Library/Preferences/
is full of.plist
s. No cross-platform script that I know is using property lists.For example, Git places their config file in
XDG_CONFIG_HOME/git/config
, not in~/Library/Preferences/Git/config
. Makes sense to me.note
I made the point only for configuration files (
$XDG_CONFIG_HOME
) and data files ($XDG_DATA_HOME
); for cache files it gets subtle. According to How-To Geek,~/Library/Caches
directory is excluded from Time Machine by default. I don’t care about cache folder since I back up neither directories anyway; but for ones who care, I recommend them to link~/.cache
to somewhere in the default cache folder, such as:我会使用
~/Library/Application Support/script_name/
。Application Support
内的子目录通常由各种应用程序使用,包括 Apple 自己的软件。但它不是由操作系统强制执行的,也不与/Applications
内的应用程序绑定。因此您可以完全自由地在其中创建自己的目录。有关 OS X 的一般目录结构,请参阅 这个苹果文档。
I would use
~/Library/Application Support/script_name/
. The subdirectories insideApplication Support
are used conventionally by various apps, including Apple's own softwares. But it's not enforced by the OS and not tied to apps inside/Applications
. So you're perfectly free to create your own directory in it.For the directory structure of OS X in general, see this Apple document.
以下是
中的
:os
包所做的 选择 golangXDG_CACHE_HOME
→$HOME/Library/Caches
XDG_CONFIG_HOME
→$HOME/Library/Application Support
Here are the choices made by the
os
package ingolang
:XDG_CACHE_HOME
→$HOME/Library/Caches
XDG_CONFIG_HOME
→$HOME/Library/Application Support
MacOS 本身根本不设置这些变量,但您可以在脚本中设置它们,并且大多数 mac 开发人员无论如何都会在 bash/zsh 配置中手动设置它们。
但请不要理会 Library、AppSup 和这些其他奇怪的目录 :D
注意:我说的是基于 ARM64/Apple Silicon 的 macOS。旧的英特尔版本可能有所不同。
MacOS itself is not setting these variables at all, but you can set them in your script and most mac devs set them in their bash/zsh config manually anyway.
But please leave the Library, AppSup, and these other weird directories alone :D
NOTE: I'm talking about arm64/apple silicon based macOS. Can be different on the old intel ones.