Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for XDG_STATE_HOME. #121

Merged
merged 1 commit into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions System/Directory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,7 @@ getXdgDirectory xdgDir suffix =
XdgData -> "XDG_DATA_HOME"
XdgConfig -> "XDG_CONFIG_HOME"
XdgCache -> "XDG_CACHE_HOME"
XdgState -> "XDG_STATE_HOME"
case env of
Just path | isAbsolute path -> pure path
_ -> getXdgDirectoryFallback getHomeDirectory xdgDir
Expand Down
8 changes: 8 additions & 0 deletions System/Directory/Internal/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ data XdgDirectory
-- On Windows, the default is @%LOCALAPPDATA%@
-- (e.g. @C:\/Users\//\<user\>/\/AppData\/Local@).
-- Can be considered as the user-specific equivalent of @\/var\/cache@.
| XdgState
-- ^ For data that should persist between (application) restarts,
-- but that is not important or portable enough to the user that it
-- should be stored in 'XdgData'.
-- It uses the @XDG_STATE_HOME@ environment variable.
-- On non-Windows sytems, the default is @~\/.local\/state@. On
-- Windows, the default is @%LOCALAPPDATA%@
-- (e.g. @C:\/Users\//\<user\>/\/AppData\/Local@).
deriving (Bounded, Enum, Eq, Ord, Read, Show)

-- | Search paths for various application data, as specified by the
Expand Down
1 change: 1 addition & 0 deletions System/Directory/Internal/Posix.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ getXdgDirectoryFallback getHomeDirectory xdgDir = do
XdgData -> ".local/share"
XdgConfig -> ".config"
XdgCache -> ".cache"
XdgState -> ".local/state"

getXdgDirectoryListFallback :: XdgDirectoryList -> IO [FilePath]
getXdgDirectoryListFallback xdgDirs =
Expand Down
1 change: 1 addition & 0 deletions System/Directory/Internal/Windows.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ getXdgDirectoryFallback _ xdgDir = do
XdgData -> getFolderPath Win32.cSIDL_APPDATA
XdgConfig -> getFolderPath Win32.cSIDL_APPDATA
XdgCache -> getFolderPath win32_cSIDL_LOCAL_APPDATA
XdgState -> getFolderPath win32_cSIDL_LOCAL_APPDATA

getXdgDirectoryListFallback :: XdgDirectoryList -> IO [FilePath]
getXdgDirectoryListFallback _ =
Expand Down
1 change: 1 addition & 0 deletions tests/GetHomeDirectory001.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ main _t = do
_ <- getXdgDirectory XdgCache "test"
_ <- getXdgDirectory XdgConfig "test"
_ <- getXdgDirectory XdgData "test"
_ <- getXdgDirectory XdgCache "test"
_ <- getUserDocumentsDirectory
_ <- getTemporaryDirectory
return ()
4 changes: 4 additions & 0 deletions tests/Xdg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ main _t = do
unsetEnv "XDG_DATA_HOME"
unsetEnv "XDG_CONFIG_HOME"
unsetEnv "XDG_CACHE_HOME"
unsetEnv "XDG_STATE_HOME"
xdgData <- getXdgDirectory XdgData "ff"
xdgConfig <- getXdgDirectory XdgConfig "oo"
xdgCache <- getXdgDirectory XdgCache "rk"
xdgState <- getXdgDirectory XdgState "aa"

-- non-absolute paths are ignored, and the fallback is used
setEnv "XDG_DATA_HOME" "ar"
setEnv "XDG_CONFIG_HOME" "aw"
setEnv "XDG_CACHE_HOME" "ba"
setEnv "XDG_STATE_HOME" "uw"
T(expectEq) () xdgData =<< getXdgDirectory XdgData "ff"
T(expectEq) () xdgConfig =<< getXdgDirectory XdgConfig "oo"
T(expectEq) () xdgCache =<< getXdgDirectory XdgCache "rk"
T(expectEq) () xdgState =<< getXdgDirectory XdgState "aa"

unsetEnv "XDG_CONFIG_DIRS"
unsetEnv "XDG_DATA_DIRS"
Expand Down