Mac OS X 上的 XDG_CONFIG_HOME 和 XDG_DATA_HOME 等效吗?

发布于 2024-09-12 13:08:03 字数 705 浏览 6 评论 0原文

我计划开发一个跨平台脚本。在 Linux 和其他操作系统上,它将在 XDG_CONFIG_HOME 中存储配置,在 XDG_DATA_HOME 中存储数据文件(特别是下载的插件)。在 Windows 上,它将使用 APPDATA 来实现两者(除非有人有更好的主意)。然而,在 Mac OS X 上正确的做法是什么?

当我第一次浏览方便的 Macbook 的 ~/Library 目录时,我看到了 PreferencesApplication 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

×纯※雪 2024-09-19 13:08:03

比较 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 best XDG_ 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:

黎夕旧梦 2024-09-19 13:08:03

我同意 OJFord 的评论:如果您正在编写脚本(我认为这适用于所有仅限 CLI 的应用程序),只需遵循 XDG 基本目录会更好。

理由:

  1. XDG_CONFIG_HOME 中的名称通常是小写的裸名称,例如 git~/Library/Preferences/ 中的文件通常是以 反向域名命名的文件符号,例如 com.apple.AppStore.plist,或 标题大小写Blackmagic Design
  2. GUI 应用程序放置在 /Applications 中时一直遵循 macOS 约定。您没有将脚本放在 /Applications 中;您将它们放在 UNIX 特定目录。最好保持一致。
  3. ~/Library/Preferences/ 充满了 .plist。据我所知,没有跨平台脚本使用属性列表。
  4. 用户可能希望分别同步跨平台配置和仅限 macOS 的配置。

例如,Git 将其配置文件放置在 XDG_CONFIG_HOME/git/config 中,而不是 ~/Library/Preferences/Git/config 中。对我来说很有意义。

注意

我仅针对配置文件 ($XDG_CONFIG_HOME) 和数据文件 ($XDG_DATA_HOME) 指出这一点;对于缓存文件来说,情况会变得微妙。根据 How-To Geek~/Library/Caches 目录已从 时间机器。我不关心缓存文件夹,因为我无论如何都不备份这两个目录;但对于关心的人,我建议他们将 ~/.cache 链接到默认缓存文件夹中的某个位置,例如:

cd ~; mv .cache ~/Library/Caches/XDG-cache; ln -s ~/Library/Caches/XDG-cache .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:

  1. Names in XDG_CONFIG_HOME are typically small-case bare-names like git; the ones in ~/Library/Preferences/ are typically files named with reverse domain name notation like com.apple.AppStore.plist, or directories named in title case like Blackmagic Design.
  2. GUI applications have been following macOS conventions when they are placed in /Applications. You don't place your script in /Applications; you place them in UNIX-specific directories. Better to be consistent.
  3. ~/Library/Preferences/ is full of .plists. No cross-platform script that I know is using property lists.
  4. Users may want to sync cross-platform configuration and macOS-only one separately.

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:

cd ~; mv .cache ~/Library/Caches/XDG-cache; ln -s ~/Library/Caches/XDG-cache .cache
远山浅 2024-09-19 13:08:03

我会使用~/Library/Application Support/script_name/Application Support 内的子目录通常由各种应用程序使用,包括 Apple 自己的软件。但它不是由操作系统强制执行的,也不与 /Applications 内的应用程序绑定。因此您可以完全自由地在其中创建自己的目录。

有关 OS X 的一般目录结构,请参阅 这个苹果文档

I would use ~/Library/Application Support/script_name/. The subdirectories inside Application 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.

神回复 2024-09-19 13:08:03

以下是 中的 os 包所做的 选择 golang

  • XDG_CACHE_HOME$HOME/Library/Caches

  • XDG_CONFIG_HOME$HOME/Library/Application Support

Here are the choices made by the os package in golang:

  • XDG_CACHE_HOME$HOME/Library/Caches

  • XDG_CONFIG_HOME$HOME/Library/Application Support

心的憧憬 2024-09-19 13:08:03

MacOS 本身根本不设置这些变量,但您可以在脚本中设置它们,并且大多数 mac 开发人员无论如何都会在 bash/zsh 配置中手动设置它们。
但请不要理会 Library、AppSup 和这些其他奇怪的目录 :D

$XDG_CONFIG_HOME => $HOME/.config
$XDG_DATA_HOME => $HOME/.local/share
$XDG_STATE_HOME => $HOME/.local/state
$XDG_CACHE_HOME => $HOME/.cache

注意:我说的是基于 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

$XDG_CONFIG_HOME => $HOME/.config
$XDG_DATA_HOME => $HOME/.local/share
$XDG_STATE_HOME => $HOME/.local/state
$XDG_CACHE_HOME => $HOME/.cache

NOTE: I'm talking about arm64/apple silicon based macOS. Can be different on the old intel ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文