/indra/llvfs/lldir_linux.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 284 lines · 208 code · 39 blank · 37 comment · 38 complexity · 85471ade645365ac22630e0916544089 MD5 · raw file

  1. /**
  2. * @file lldir_linux.cpp
  3. * @brief Implementation of directory utilities for linux
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "lldir_linux.h"
  28. #include "llerror.h"
  29. #include "llrand.h"
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #include <glob.h>
  34. #include <pwd.h>
  35. static std::string getCurrentUserHome(char* fallback)
  36. {
  37. const uid_t uid = getuid();
  38. struct passwd *pw;
  39. char *result_cstr = fallback;
  40. pw = getpwuid(uid);
  41. if ((pw != NULL) && (pw->pw_dir != NULL))
  42. {
  43. result_cstr = (char*) pw->pw_dir;
  44. }
  45. else
  46. {
  47. llinfos << "Couldn't detect home directory from passwd - trying $HOME" << llendl;
  48. const char *const home_env = getenv("HOME"); /* Flawfinder: ignore */
  49. if (home_env)
  50. {
  51. result_cstr = (char*) home_env;
  52. }
  53. else
  54. {
  55. llwarns << "Couldn't detect home directory! Falling back to " << fallback << llendl;
  56. }
  57. }
  58. return std::string(result_cstr);
  59. }
  60. LLDir_Linux::LLDir_Linux()
  61. {
  62. mDirDelimiter = "/";
  63. mCurrentDirIndex = -1;
  64. mCurrentDirCount = -1;
  65. mDirp = NULL;
  66. char tmp_str[LL_MAX_PATH]; /* Flawfinder: ignore */
  67. if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
  68. {
  69. strcpy(tmp_str, "/tmp");
  70. llwarns << "Could not get current directory; changing to "
  71. << tmp_str << llendl;
  72. if (chdir(tmp_str) == -1)
  73. {
  74. llerrs << "Could not change directory to " << tmp_str << llendl;
  75. }
  76. }
  77. mExecutableFilename = "";
  78. mExecutablePathAndName = "";
  79. mExecutableDir = tmp_str;
  80. mWorkingDir = tmp_str;
  81. #ifdef APP_RO_DATA_DIR
  82. mAppRODataDir = APP_RO_DATA_DIR;
  83. #else
  84. mAppRODataDir = tmp_str;
  85. #endif
  86. std::string::size_type build_dir_pos = mExecutableDir.rfind("/build-linux-");
  87. if (build_dir_pos != std::string::npos)
  88. {
  89. // ...we're in a dev checkout
  90. mSkinBaseDir = mExecutableDir.substr(0, build_dir_pos) + "/indra/newview/skins";
  91. llinfos << "Running in dev checkout with mSkinBaseDir "
  92. << mSkinBaseDir << llendl;
  93. }
  94. else
  95. {
  96. // ...normal installation running
  97. mSkinBaseDir = mAppRODataDir + mDirDelimiter + "skins";
  98. }
  99. mOSUserDir = getCurrentUserHome(tmp_str);
  100. mOSUserAppDir = "";
  101. mLindenUserDir = "";
  102. char path [32]; /* Flawfinder: ignore */
  103. // *NOTE: /proc/%d/exe doesn't work on FreeBSD. But that's ok,
  104. // because this is the linux implementation.
  105. snprintf (path, sizeof(path), "/proc/%d/exe", (int) getpid ());
  106. int rc = readlink (path, tmp_str, sizeof (tmp_str)-1); /* Flawfinder: ignore */
  107. if ( (rc != -1) && (rc <= ((int) sizeof (tmp_str)-1)) )
  108. {
  109. tmp_str[rc] = '\0'; //readlink() doesn't 0-terminate the buffer
  110. mExecutablePathAndName = tmp_str;
  111. char *path_end;
  112. if ((path_end = strrchr(tmp_str,'/')))
  113. {
  114. *path_end = '\0';
  115. mExecutableDir = tmp_str;
  116. mWorkingDir = tmp_str;
  117. mExecutableFilename = path_end+1;
  118. }
  119. else
  120. {
  121. mExecutableFilename = tmp_str;
  122. }
  123. }
  124. mLLPluginDir = mExecutableDir + mDirDelimiter + "llplugin";
  125. // *TODO: don't use /tmp, use $HOME/.secondlife/tmp or something.
  126. mTempDir = "/tmp";
  127. }
  128. LLDir_Linux::~LLDir_Linux()
  129. {
  130. }
  131. // Implementation
  132. void LLDir_Linux::initAppDirs(const std::string &app_name,
  133. const std::string& app_read_only_data_dir)
  134. {
  135. // Allow override so test apps can read newview directory
  136. if (!app_read_only_data_dir.empty())
  137. {
  138. mAppRODataDir = app_read_only_data_dir;
  139. mSkinBaseDir = mAppRODataDir + mDirDelimiter + "skins";
  140. }
  141. mAppName = app_name;
  142. std::string upper_app_name(app_name);
  143. LLStringUtil::toUpper(upper_app_name);
  144. char* app_home_env = getenv((upper_app_name + "_USER_DIR").c_str()); /* Flawfinder: ignore */
  145. if (app_home_env)
  146. {
  147. // user has specified own userappdir i.e. $SECONDLIFE_USER_DIR
  148. mOSUserAppDir = app_home_env;
  149. }
  150. else
  151. {
  152. // traditionally on unixoids, MyApp gets ~/.myapp dir for data
  153. mOSUserAppDir = mOSUserDir;
  154. mOSUserAppDir += "/";
  155. mOSUserAppDir += ".";
  156. std::string lower_app_name(app_name);
  157. LLStringUtil::toLower(lower_app_name);
  158. mOSUserAppDir += lower_app_name;
  159. }
  160. // create any directories we expect to write to.
  161. int res = LLFile::mkdir(mOSUserAppDir);
  162. if (res == -1)
  163. {
  164. if (errno != EEXIST)
  165. {
  166. llwarns << "Couldn't create app user dir " << mOSUserAppDir << llendl;
  167. llwarns << "Default to base dir" << mOSUserDir << llendl;
  168. mOSUserAppDir = mOSUserDir;
  169. }
  170. }
  171. res = LLFile::mkdir(getExpandedFilename(LL_PATH_LOGS,""));
  172. if (res == -1)
  173. {
  174. if (errno != EEXIST)
  175. {
  176. llwarns << "Couldn't create LL_PATH_LOGS dir " << getExpandedFilename(LL_PATH_LOGS,"") << llendl;
  177. }
  178. }
  179. res = LLFile::mkdir(getExpandedFilename(LL_PATH_USER_SETTINGS,""));
  180. if (res == -1)
  181. {
  182. if (errno != EEXIST)
  183. {
  184. llwarns << "Couldn't create LL_PATH_USER_SETTINGS dir " << getExpandedFilename(LL_PATH_USER_SETTINGS,"") << llendl;
  185. }
  186. }
  187. res = LLFile::mkdir(getExpandedFilename(LL_PATH_CACHE,""));
  188. if (res == -1)
  189. {
  190. if (errno != EEXIST)
  191. {
  192. llwarns << "Couldn't create LL_PATH_CACHE dir " << getExpandedFilename(LL_PATH_CACHE,"") << llendl;
  193. }
  194. }
  195. mCAFile = getExpandedFilename(LL_PATH_APP_SETTINGS, "CA.pem");
  196. }
  197. U32 LLDir_Linux::countFilesInDir(const std::string &dirname, const std::string &mask)
  198. {
  199. U32 file_count = 0;
  200. glob_t g;
  201. std::string tmp_str;
  202. tmp_str = dirname;
  203. tmp_str += mask;
  204. if(glob(tmp_str.c_str(), GLOB_NOSORT, NULL, &g) == 0)
  205. {
  206. file_count = g.gl_pathc;
  207. globfree(&g);
  208. }
  209. return (file_count);
  210. }
  211. std::string LLDir_Linux::getCurPath()
  212. {
  213. char tmp_str[LL_MAX_PATH]; /* Flawfinder: ignore */
  214. if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
  215. {
  216. llwarns << "Could not get current directory" << llendl;
  217. tmp_str[0] = '\0';
  218. }
  219. return tmp_str;
  220. }
  221. BOOL LLDir_Linux::fileExists(const std::string &filename) const
  222. {
  223. struct stat stat_data;
  224. // Check the age of the file
  225. // Now, we see if the files we've gathered are recent...
  226. int res = stat(filename.c_str(), &stat_data);
  227. if (!res)
  228. {
  229. return TRUE;
  230. }
  231. else
  232. {
  233. return FALSE;
  234. }
  235. }
  236. /*virtual*/ std::string LLDir_Linux::getLLPluginLauncher()
  237. {
  238. return gDirUtilp->getExecutableDir() + gDirUtilp->getDirDelimiter() +
  239. "SLPlugin";
  240. }
  241. /*virtual*/ std::string LLDir_Linux::getLLPluginFilename(std::string base_name)
  242. {
  243. return gDirUtilp->getLLPluginDir() + gDirUtilp->getDirDelimiter() +
  244. "lib" + base_name + ".so";
  245. }