Windows的XDG Basedir目录 [英] XDG Basedir directories for Windows

查看:77
本文介绍了Windows的XDG Basedir目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了方便访问

I have made a Racket library for convenience in accessing the XDG Basedir directories. As I want the library to be useable on Windows as well (for cross-platform programs), I have it use standard Windows directories as the defaults when the XDG environment variables are unset.

我当前正在使用以下内容:

I am currently using the following:

  • $XDG_DATA_HOME = %LOCALAPPDATA%
  • $XDG_DATA_DIRS = %APPDATA%
  • $XDG_CONFIG_HOME = %LOCALAPPDATA%
  • $XDG_CONFIG_DIRS = %APPDATA%
  • $XDG_CACHE_HOME = %TEMP%
  • $XDG_RUNTIME_DIR = %TEMP%
  • $XDG_DATA_HOME = %LOCALAPPDATA%
  • $XDG_DATA_DIRS = %APPDATA%
  • $XDG_CONFIG_HOME = %LOCALAPPDATA%
  • $XDG_CONFIG_DIRS = %APPDATA%
  • $XDG_CACHE_HOME = %TEMP%
  • $XDG_RUNTIME_DIR = %TEMP%

我的问题是,是否有比这些更好的默认设置.我知道%TEMP%作为$XDG_RUNTIME_DIR是错误的,因为它实际上应该位于像/tmp这样的ramfs上,但是我不知道Windows上的任何类似目录.在Windows中,似乎没有很好的选择将数据目录和配置目录分开,所以我对它们使用相同的目录.我的直觉是,对于可写的$XDG_*_HOME变量,%LOCALAPPDATA%是更好的选择,并且在$XDG_*_DIRS列表中具有漫游"配置可以读取并且通常不会被覆盖.但是具有漫游配置的企业Windows用户会发现这种奇怪并且不同意吗?

My question is whether there are better defaults than those. I know that %TEMP% as the $XDG_RUNTIME_DIR is wrong, since it really should be on a ramfs like /tmp, but I don't know of any directory on Windows that is like that. In Windows it seems that there is no good option to have data and configuration directories separate, so I am using the same directories for them. My gut feeling is that %LOCALAPPDATA% is a better choice for the writable $XDG_*_HOME variables and having the "roaming" configuration in the $XDG_*_DIRS lists to be read and generally not overwritten. But would corporate Windows users that have a roaming configuration find that strange and disagree?

推荐答案

我已经在 JVM Rust .这是我学到的:

I have implemented such functionality in libraries for the JVM and Rust. Here is what I learned:

提供用于计算配置,缓存等目录的 full 路径(包括应用程序名称!)的API.如果不这样做,将导致在3个主要平台中的至少2个上保证代码是错误的,因为约定大不相同.

Provide APIs that compute the full path (including the application name!) to configuration, cache, etc. directories. Not doing this will result in code that is guaranteed to be wrong on at least 2 of 3 major platforms, as the conventions differ dramatically.

考虑一个由 MegaCorp 公司(网址为 MegaCorp.co.uk )编写的名为 Foo App 的应用程序. 在Linux下,命名应用程序的路径段应为fooapp/(小写,无空格),在Windows上应为MegaCorp\Foo App\(请注意两个文件夹),而在macOS上应为为uk.co.MegaCorp.Foo-App(用-替换的无效字符).

Consider an application written by company MegaCorp (web address MegaCorp.co.uk) named Foo App. Under Linux, the path segment naming the application should be fooapp/ (lower-cased, no spaces), on Windows it should be MegaCorp\Foo App\ (note the two folders), and on macOS it should be uk.co.MegaCorp.Foo-App (invalid characters replaced with -).

例如,我的库在macOS或Windows上不提供runtimeDir,因为XDG_RUNTIME_DIR与e完全不同. G. %TEMP%在Windows上.

For instance, my library does not offer a runtimeDir on macOS or Windows, because XDG_RUNTIME_DIR is very different from e. g. %TEMP% on Windows.

这是安全问题的潜在根源,因为Linux上的运行时目录保证它只能由所有者访问,在用户注销时会被删除等.

This is a potential source of security issues, as the runtime dir on Linux guarantees that it can only be accessed by the owner, is deleted when the user logs off, etc.

此外,我仅在Linux和macOS上提供fontDir. Windows确实有一个字体目录,但是与Linux和macOS不同,它不是用户可写的.

Also, I only offer fontDir on Linux and macOS. Windows does have a font directory, but unlike on Linux and macOS, it is not user-writable.

另一方面,我在所有三个平台上同时提供了dataDir(%APPDATA%)和dataLocalDir(%LOCALAPPDATA%).在macOS和Linux上,这些目录返回相同的路径–这是一个明确的设计决定,要考虑到如果其中一个目录不可用,用户将如何编写代码:用户会忘记处理它,或者只是回退到另一个目录.使用选定的设计,它就可以立即使用,而无需用户考虑.

On the other hand, I offer both dataDir (%APPDATA%) and dataLocalDir (%LOCALAPPDATA%) across all three platforms. On macOS and Linux those directories return the same path – this is an explicit design decision, considering how users would write code if one of those directories would not be available: Users would either forget to handle it, or just fallback to the other directory. With the chosen design this just works out of the box, without users needing to think about it.

这就是为什么常规缓存,配置等目录路径返回%LOCALAPPDATA%的原因和%APPDATA%,但应用程序特定的缓存和配置目录路径返回%LOCALAPPDATA%\Company\Application\cache%APPDATA%\Company\Application\config.

This is why the general cache, config etc. directory paths return %LOCALAPPDATA% and %APPDATA%, but application-specific cache and config directory paths return %LOCALAPPDATA%\Company\Application\cache and %APPDATA%\Company\Application\config.

注意子目录!这是为了确保将应用程序的缓存,配置和数据目录完全分开,而不管用户可能具有什么奇怪的Windows设置.

Note the sub-directories! This is to guarantee a clean separation of an application's cache, config and data directory, regardless of what weird Windows settings a user might have.

我的库中有三个不同的模块,具有明确定义的用例:

There are three distinct modules in my library, with clearly defined, separated use cases:

BaseDirs ,该查询可查询用户-不可见的标准目录(缓存,配置,数据,可执行文件,运行时目录),强烈建议改用 ProjectDirs .

ProjectDirs ,用于计算缓存的位置,您自己的应用程序或项目的配置或数据目录,它们是从标准目录派生的.

ProjectDirs, which computes the location of cache, config or data directories for your own application or project, which are derived from the standard directories.

UserDirs ,该查询可查询用户-面向标准目录(音频,文档,下载等).

UserDirs, which queries the paths of user-facing standard directories (Audio, Documents, Downloads, etc.).

BaseDirsUserDirs的构造函数(new())不那么有趣,而ProjectDirs提供了以下工厂方法:

While BaseDirs and UserDirs have fairly uninteresting constructors (new()), ProjectDirs provides this factory method:

ProjectDirs::from(qualifier: &str, organization: &str, application: &str)

此方法可确保用户最终获得指向应用程序的缓存,配置等目录的正确且符合标准的路径-无需知道每个平台的所有复杂性.

This method ensures that users end up with correct, standards-compliant paths to their applications' cache, config, etc. directories – without them needing to be aware of all the intricacies of each individual platform.

最后一条建议:我将保留一个名为"XDG Basedir Library"的库,重点放在Linux上,并发布一个更通用的库,例如用于处理Linux,Windows等的"Standard Directory Library",以避免混淆

One last suggestion: I would keep a library named "XDG Basedir Library" focused on Linux, and publish a library with a more general name, like "Standard Directory Library" that deals with Linux, Windows, etc. to avoid confusion.

希望这很有帮助!

这篇关于Windows的XDG Basedir目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆