Skip to content

Support XDG Base directory specification for Linux - #264

Open
WhiredPlanck wants to merge 1 commit into
TypesettingTools:masterfrom
WhiredPlanck:xdg-base
Open

Support XDG Base directory specification for Linux#264
WhiredPlanck wants to merge 1 commit into
TypesettingTools:masterfrom
WhiredPlanck:xdg-base

Conversation

@WhiredPlanck

@WhiredPlanck WhiredPlanck commented Jan 3, 2025

Copy link
Copy Markdown

This PR will fix Issue Aegisub#226. Adapted from aria2/aria2@8bc1d37.

The basic idea just is:

  • If $HOME/.aegisub exists, then the program will still read/write config/cache/data/state from there;
  • If $HOME/.aegisub doesn't exist, then the program will read/write config/cache/data/state from corresponding XDG base Directory.

The original PR is here: wangqr#132

@witchymary

witchymary commented Jan 4, 2025

Copy link
Copy Markdown
Contributor

I'm pretty sure this will still run into the issues I fixed here: arch1t3cht@b5a071b

Also worth noting that this will break regardless due to this line in Dependency Control (https://github.com/TypesettingTools/DependencyControl/blob/9b8d615f18e295f4777ea606a7173465bdaf140d/modules/DependencyControl/Record.moon#L44). It needs to be changed in there as well

Comment thread libaegisub/unix/path.cpp Outdated
@WhiredPlanck

Copy link
Copy Markdown
Author

I'm pretty sure this will still run into the issues I fixed here: arch1t3cht@b5a071b

Also worth noting that this will break regardless due to this line in Dependency Control (https://github.com/TypesettingTools/DependencyControl/blob/9b8d615f18e295f4777ea606a7173465bdaf140d/modules/DependencyControl/Record.moon#L44). It needs to be changed in there as well

You're right, I will fix it.

Comment thread libaegisub/unix/path.cpp Outdated
throw agi::EnvironmentError("Could not get home directory. Make sure HOME is set.");
}

std::string xdg_dir(std::string_view token, agi::fs::path const& fallback) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token is not the right name for the parameter... (env_name would be better)

Comment thread libaegisub/unix/path.cpp
SetToken("?state", old_root);
} else {
SetToken("?user", xdg_config/"aegisub");
SetToken("?local", xdg_cache/"aegisub");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling a cache dir ?local is somewhat misleading, but I guess we have to keep it for compatibility. ?cache would be a better name though.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling a cache dir ?local is somewhat misleading, but I guess we have to keep it for compatibility. ?cache would be a better name though.

This is because the data originally stored in ?local is suppose to be store in the xdg cache dir. I think it would be better to keep this currently for compatibility.

Comment thread libaegisub/unix/path.cpp

std::string xdg_dir(std::string_view token, agi::fs::path const& fallback) {
const char *env = getenv(token.data());
if (env) return env;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable must be ignored if it is empty or doesn't contain an absolute path

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable must be ignored if it is empty or doesn't contain an absolute path

agi::Path::SetToken will treat the path like this:

void Path::SetToken(std::string_view token_name, fs::path const& token_value) {
	int idx = checked_find_token(token_name);

	if (token_value.empty())
		paths[idx] = token_value;
	else if (!token_value.is_absolute())
		paths[idx].clear();
	else {
		paths[idx] = token_value;
		paths[idx].make_preferred();
		if (fs::FileExists(paths[idx]))
			paths[idx] = paths[idx].parent_path();
	}
}

I think I don't need to do anymore.

@filip-hejsek

Copy link
Copy Markdown
Contributor

This PR causes a crash on startup when the XDG dirs don't exist. This part of the spec is relevent:

If, when attempting to write a file, the destination directory is non-existent an attempt should be made to create it with permission 0700

@line0

line0 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Also worth noting that this will break regardless due to this line in Dependency Control (https://github.com/TypesettingTools/DependencyControl/blob/9b8d615f18e295f4777ea606a7173465bdaf140d/modules/DependencyControl/Record.moon#L44). It needs to be changed in there as well

Support added in TypesettingTools/DependencyControl#36.

Ref:
1. https://specifications.freedesktop.org/basedir-spec/latest
2. aria2/aria2@8bc1d37

Fix compile issue caused by token to path mapping

Co-authored-by: witchymary <63314339+witchymary@users.noreply.github.com>
@WhiredPlanck

Copy link
Copy Markdown
Author

Make some refactors and rebase on the latest commit on current branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants