xdg.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package xdg
  2. import (
  3. "github.com/adrg/xdg/internal/pathutil"
  4. "github.com/adrg/xdg/internal/userdirs"
  5. )
  6. // UserDirectories defines the locations of well known user directories.
  7. type UserDirectories = userdirs.Directories
  8. var (
  9. // Home contains the path of the user's home directory.
  10. Home string
  11. // DataHome defines the base directory relative to which user-specific
  12. // data files should be stored. This directory is defined by the
  13. // $XDG_DATA_HOME environment variable. If the variable is not set,
  14. // a default equal to $HOME/.local/share should be used.
  15. DataHome string
  16. // DataDirs defines the preference-ordered set of base directories to
  17. // search for data files in addition to the DataHome base directory.
  18. // This set of directories is defined by the $XDG_DATA_DIRS environment
  19. // variable. If the variable is not set, the default directories
  20. // to be used are /usr/local/share and /usr/share, in that order. The
  21. // DataHome directory is considered more important than any of the
  22. // directories defined by DataDirs. Therefore, user data files should be
  23. // written relative to the DataHome directory, if possible.
  24. DataDirs []string
  25. // ConfigHome defines the base directory relative to which user-specific
  26. // configuration files should be written. This directory is defined by
  27. // the $XDG_CONFIG_HOME environment variable. If the variable is
  28. // not set, a default equal to $HOME/.config should be used.
  29. ConfigHome string
  30. // ConfigDirs defines the preference-ordered set of base directories to
  31. // search for configuration files in addition to the ConfigHome base
  32. // directory. This set of directories is defined by the $XDG_CONFIG_DIRS
  33. // environment variable. If the variable is not set, a default equal
  34. // to /etc/xdg should be used. The ConfigHome directory is considered
  35. // more important than any of the directories defined by ConfigDirs.
  36. // Therefore, user config files should be written relative to the
  37. // ConfigHome directory, if possible.
  38. ConfigDirs []string
  39. // StateHome defines the base directory relative to which user-specific
  40. // state files should be stored. This directory is defined by the
  41. // $XDG_STATE_HOME environment variable. If the variable is not set,
  42. // a default equal to ~/.local/state should be used.
  43. StateHome string
  44. // CacheHome defines the base directory relative to which user-specific
  45. // non-essential (cached) data should be written. This directory is
  46. // defined by the $XDG_CACHE_HOME environment variable. If the variable
  47. // is not set, a default equal to $HOME/.cache should be used.
  48. CacheHome string
  49. // RuntimeDir defines the base directory relative to which user-specific
  50. // non-essential runtime files and other file objects (such as sockets,
  51. // named pipes, etc.) should be stored. This directory is defined by the
  52. // $XDG_RUNTIME_DIR environment variable. If the variable is not set,
  53. // applications should fall back to a replacement directory with similar
  54. // capabilities. Applications should use this directory for communication
  55. // and synchronization purposes and should not place larger files in it,
  56. // since it might reside in runtime memory and cannot necessarily be
  57. // swapped out to disk.
  58. RuntimeDir string
  59. // BinHome defines the base directory relative to which user-specific
  60. // binary files should be written. This directory is defined by
  61. // the non-standard $XDG_BIN_HOME environment variable. If the variable is
  62. // not set, a default equal to $HOME/.local/bin should be used.
  63. BinHome string
  64. // UserDirs defines the locations of well known user directories.
  65. UserDirs UserDirectories
  66. // FontDirs defines the common locations where font files are stored.
  67. FontDirs []string
  68. // ApplicationDirs defines the common locations of applications.
  69. ApplicationDirs []string
  70. // baseDirs defines the locations of base directories.
  71. baseDirs baseDirectories
  72. )
  73. func init() {
  74. Reload()
  75. }
  76. // Reload refreshes base and user directories by reading the environment.
  77. // Defaults are applied for XDG variables which are empty or not present
  78. // in the environment.
  79. func Reload() {
  80. // Initialize home directory.
  81. Home = pathutil.UserHomeDir()
  82. // Initialize base and user directories.
  83. initDirs(Home)
  84. // Set standard directories.
  85. DataHome = baseDirs.dataHome
  86. DataDirs = baseDirs.data
  87. ConfigHome = baseDirs.configHome
  88. ConfigDirs = baseDirs.config
  89. StateHome = baseDirs.stateHome
  90. CacheHome = baseDirs.cacheHome
  91. RuntimeDir = baseDirs.runtime
  92. // Set non-standard directories.
  93. BinHome = baseDirs.binHome
  94. FontDirs = baseDirs.fonts
  95. ApplicationDirs = baseDirs.applications
  96. }
  97. // DataFile returns a suitable location for the specified data file.
  98. // The relPath parameter must contain the name of the data file, and
  99. // optionally, a set of parent directories (e.g. appname/app.data).
  100. // If the specified directories do not exist, they will be created relative
  101. // to the base data directory. On failure, an error containing the
  102. // attempted paths is returned.
  103. func DataFile(relPath string) (string, error) {
  104. return baseDirs.dataFile(relPath)
  105. }
  106. // ConfigFile returns a suitable location for the specified config file.
  107. // The relPath parameter must contain the name of the config file, and
  108. // optionally, a set of parent directories (e.g. appname/app.yaml).
  109. // If the specified directories do not exist, they will be created relative
  110. // to the base config directory. On failure, an error containing the
  111. // attempted paths is returned.
  112. func ConfigFile(relPath string) (string, error) {
  113. return baseDirs.configFile(relPath)
  114. }
  115. // StateFile returns a suitable location for the specified state file. State
  116. // files are usually volatile data files, not suitable to be stored relative
  117. // to the $XDG_DATA_HOME directory.
  118. // The relPath parameter must contain the name of the state file, and
  119. // optionally, a set of parent directories (e.g. appname/app.state).
  120. // If the specified directories do not exist, they will be created relative
  121. // to the base state directory. On failure, an error containing the
  122. // attempted paths is returned.
  123. func StateFile(relPath string) (string, error) {
  124. return baseDirs.stateFile(relPath)
  125. }
  126. // CacheFile returns a suitable location for the specified cache file.
  127. // The relPath parameter must contain the name of the cache file, and
  128. // optionally, a set of parent directories (e.g. appname/app.cache).
  129. // If the specified directories do not exist, they will be created relative
  130. // to the base cache directory. On failure, an error containing the
  131. // attempted paths is returned.
  132. func CacheFile(relPath string) (string, error) {
  133. return baseDirs.cacheFile(relPath)
  134. }
  135. // RuntimeFile returns a suitable location for the specified runtime file.
  136. // The relPath parameter must contain the name of the runtime file, and
  137. // optionally, a set of parent directories (e.g. appname/app.pid).
  138. // If the specified directories do not exist, they will be created relative
  139. // to the base runtime directory. If the base runtime directory does not exist,
  140. // the operating system's temporary directory is used as a fallback. On failure,
  141. // an error containing the attempted paths is returned.
  142. func RuntimeFile(relPath string) (string, error) {
  143. return baseDirs.runtimeFile(relPath)
  144. }
  145. // SearchDataFile searches for specified file in the data search paths.
  146. // The relPath parameter must contain the name of the data file, and
  147. // optionally, a set of parent directories (e.g. appname/app.data). If the
  148. // file cannot be found, an error specifying the searched paths is returned.
  149. func SearchDataFile(relPath string) (string, error) {
  150. return baseDirs.searchDataFile(relPath)
  151. }
  152. // SearchConfigFile searches for the specified file in config search paths.
  153. // The relPath parameter must contain the name of the config file, and
  154. // optionally, a set of parent directories (e.g. appname/app.yaml). If the
  155. // file cannot be found, an error specifying the searched paths is returned.
  156. func SearchConfigFile(relPath string) (string, error) {
  157. return baseDirs.searchConfigFile(relPath)
  158. }
  159. // SearchStateFile searches for the specified file in the state search path.
  160. // The relPath parameter must contain the name of the state file, and
  161. // optionally, a set of parent directories (e.g. appname/app.state). If the
  162. // file cannot be found, an error specifying the searched path is returned.
  163. func SearchStateFile(relPath string) (string, error) {
  164. return baseDirs.searchStateFile(relPath)
  165. }
  166. // SearchCacheFile searches for the specified file in the cache search path.
  167. // The relPath parameter must contain the name of the cache file, and
  168. // optionally, a set of parent directories (e.g. appname/app.cache). If the
  169. // file cannot be found, an error specifying the searched path is returned.
  170. func SearchCacheFile(relPath string) (string, error) {
  171. return baseDirs.searchCacheFile(relPath)
  172. }
  173. // SearchRuntimeFile searches for the specified file in the runtime search path.
  174. // The relPath parameter must contain the name of the runtime file, and
  175. // optionally, a set of parent directories (e.g. appname/app.pid). The runtime
  176. // file is also searched in the operating system's temporary directory in order
  177. // to cover cases in which the runtime base directory does not exist or is not
  178. // accessible. If the file cannot be found, an error specifying the searched
  179. // paths is returned.
  180. func SearchRuntimeFile(relPath string) (string, error) {
  181. return baseDirs.searchRuntimeFile(relPath)
  182. }