Skip to content

Commit

Permalink
Add support for XDG_STATE_HOME.
Browse files Browse the repository at this point in the history
On Windows, we treat XdgState the same as XdgCache.  This might not be
optimal.
  • Loading branch information
athas authored and Rufflewind committed Jul 3, 2021
1 parent 2eca0e3 commit 47e797d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 0 deletions.
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

0 comments on commit 47e797d

Please sign in to comment.