lldir_macos.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @file lldir_macos.cpp
  3. * @brief Implementation of macOS-specific directory related methods
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #if LL_DARWIN
  33. #include "linden_common.h"
  34. #include <dirent.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <pwd.h>
  38. #include <unistd.h>
  39. #include <uuid/uuid.h>
  40. #include "boost/filesystem.hpp"
  41. #include "lldir.h"
  42. #include "lldir_macos_objc.h"
  43. // Helper functions
  44. static bool macos_create_dir(const std::string& parent,
  45. const std::string& child,
  46. std::string* fullname = NULL)
  47. {
  48. boost::filesystem::path p(parent);
  49. p /= child;
  50. if (fullname)
  51. {
  52. *fullname = std::string(p.string());
  53. }
  54. if (!boost::filesystem::create_directory(p))
  55. {
  56. return boost::filesystem::is_directory(p);
  57. }
  58. return true;
  59. }
  60. static std::string get_user_home()
  61. {
  62. const uid_t uid = getuid();
  63. struct passwd* pw;
  64. char* result_cstr = (char*)"/Users";
  65. pw = getpwuid(uid);
  66. if (pw && pw->pw_dir)
  67. {
  68. result_cstr = (char*)pw->pw_dir;
  69. }
  70. else
  71. {
  72. llinfos << "Could not detect home directory from passwd; trying $HOME"
  73. << llendl;
  74. const char* const home_env = getenv("HOME");
  75. if (home_env)
  76. {
  77. result_cstr = (char*)home_env;
  78. }
  79. else
  80. {
  81. llwarns << "Could not detect home directory ! Falling back to: /Users "
  82. << llendl;
  83. }
  84. }
  85. return std::string(result_cstr);
  86. }
  87. void LLDir::init()
  88. {
  89. const std::string second_life_string = "SecondLife";
  90. const std::string tpv_string = "CoolVLViewer";
  91. std::string* executablepathstr = getSystemExecutableFolder();
  92. if (executablepathstr)
  93. {
  94. // mExecutablePathAndName
  95. mExecutablePathAndName = *executablepathstr;
  96. boost::filesystem::path executablepath(*executablepathstr);
  97. // mExecutableFilename && mExecutableDir
  98. mExecutableFilename = executablepath.filename().string();
  99. mExecutableDir = executablepath.parent_path().string();
  100. // mAppRODataDir
  101. std::string* resourcepath = getSystemResourceFolder();
  102. mAppRODataDir = *resourcepath;
  103. // mOSUserDir
  104. mOSUserDir = get_user_home();
  105. std::string* appdir = getSystemApplicationSupportFolder();
  106. std::string rootdir;
  107. // Create root directory
  108. if (macos_create_dir(*appdir, second_life_string, &rootdir))
  109. {
  110. // mOSUserAppDir
  111. mOSUserAppDir = rootdir;
  112. // Create our sub-dirs
  113. macos_create_dir(rootdir, "data");
  114. macos_create_dir(rootdir, "logs");
  115. macos_create_dir(rootdir, "user_settings");
  116. macos_create_dir(rootdir, "browser_profile");
  117. }
  118. else
  119. {
  120. // mOSUserAppDir
  121. mOSUserAppDir = mOSUserDir;
  122. }
  123. // mOSCacheDir
  124. std::string* cachedir = getSystemCacheFolder();
  125. if (cachedir)
  126. {
  127. mOSCacheDir = *cachedir;
  128. macos_create_dir(mOSCacheDir, tpv_string);
  129. }
  130. // mTempDir
  131. std::string* tmpdir = getSystemTempFolder();
  132. if (tmpdir)
  133. {
  134. macos_create_dir(*tmpdir, tpv_string, &mTempDir);
  135. if (tmpdir)
  136. {
  137. delete tmpdir;
  138. }
  139. }
  140. mWorkingDir = getCurPath();
  141. mLLPluginDir = mAppRODataDir + "/llplugin";
  142. }
  143. dumpCurrentDirectories();
  144. }
  145. void LLDir::initAppDirs(const std::string& app_name)
  146. {
  147. mAppName = app_name;
  148. mCRTFile = getFullPath(LL_PATH_APP_SETTINGS, "ca-bundle.crt");
  149. dumpCurrentDirectories();
  150. }
  151. std::string LLDir::getCurPath()
  152. {
  153. return boost::filesystem::path(boost::filesystem::current_path()).string();
  154. }
  155. std::string LLDir::getLLPluginLauncher()
  156. {
  157. return mAppRODataDir + "/SLPlugin.app/Contents/MacOS/SLPlugin";
  158. }
  159. std::string LLDir::getLLPluginFilename(std::string base_name)
  160. {
  161. return mLLPluginDir + "/" + base_name + ".dylib";
  162. }
  163. #endif // LL_DARWIN