StandardPaths.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Platform.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/StandardPaths.h>
  11. #include <LibCore/System.h>
  12. #include <pwd.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. namespace Core {
  16. DeprecatedString StandardPaths::home_directory()
  17. {
  18. if (auto* home_env = getenv("HOME"))
  19. return LexicalPath::canonicalized_path(home_env);
  20. auto* pwd = getpwuid(getuid());
  21. DeprecatedString path = pwd ? pwd->pw_dir : "/";
  22. endpwent();
  23. return LexicalPath::canonicalized_path(path);
  24. }
  25. DeprecatedString StandardPaths::desktop_directory()
  26. {
  27. StringBuilder builder;
  28. builder.append(home_directory());
  29. builder.append("/Desktop"sv);
  30. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  31. }
  32. DeprecatedString StandardPaths::documents_directory()
  33. {
  34. StringBuilder builder;
  35. builder.append(home_directory());
  36. builder.append("/Documents"sv);
  37. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  38. }
  39. DeprecatedString StandardPaths::downloads_directory()
  40. {
  41. StringBuilder builder;
  42. builder.append(home_directory());
  43. builder.append("/Downloads"sv);
  44. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  45. }
  46. DeprecatedString StandardPaths::config_directory()
  47. {
  48. if (auto* config_directory = getenv("XDG_CONFIG_HOME"))
  49. return LexicalPath::canonicalized_path(config_directory);
  50. StringBuilder builder;
  51. builder.append(home_directory());
  52. #if defined(AK_OS_MACOS)
  53. builder.append("/Library/Preferences"sv);
  54. #else
  55. builder.append("/.config"sv);
  56. #endif
  57. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  58. }
  59. DeprecatedString StandardPaths::data_directory()
  60. {
  61. if (auto* data_directory = getenv("XDG_DATA_HOME"))
  62. return LexicalPath::canonicalized_path(data_directory);
  63. StringBuilder builder;
  64. builder.append(home_directory());
  65. #if defined(AK_OS_SERENITY)
  66. builder.append("/.data"sv);
  67. #elif defined(AK_OS_MACOS)
  68. builder.append("/Library/Application Support"sv);
  69. #else
  70. builder.append("/.local/share"sv);
  71. #endif
  72. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  73. }
  74. ErrorOr<DeprecatedString> StandardPaths::runtime_directory()
  75. {
  76. if (auto* data_directory = getenv("XDG_RUNTIME_DIR"))
  77. return LexicalPath::canonicalized_path(data_directory);
  78. StringBuilder builder;
  79. #if defined(AK_OS_SERENITY)
  80. auto sid = TRY(Core::System::getsid());
  81. builder.appendff("/tmp/session/{}", sid);
  82. #elif defined(AK_OS_MACOS)
  83. builder.append(home_directory());
  84. builder.append("/Library/Application Support"sv);
  85. #else
  86. auto uid = getuid();
  87. builder.appendff("/run/user/{}", uid);
  88. #endif
  89. return LexicalPath::canonicalized_path(builder.to_deprecated_string());
  90. }
  91. DeprecatedString StandardPaths::tempfile_directory()
  92. {
  93. return "/tmp";
  94. }
  95. }