/indra/llvfs/lldir_win32.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 330 lines · 201 code · 64 blank · 65 comment · 34 complexity · 386cca0829b070e73affbdbb3680c981 MD5 · raw file

  1. /**
  2. * @file lldir_win32.cpp
  3. * @brief Implementation of directory utilities for windows
  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. #if LL_WINDOWS
  27. #include "linden_common.h"
  28. #include "lldir_win32.h"
  29. #include "llerror.h"
  30. #include "llrand.h" // for gLindenLabRandomNumber
  31. #include "shlobj.h"
  32. #include <direct.h>
  33. #include <errno.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. // Utility stuff to get versions of the sh
  37. #define PACKVERSION(major,minor) MAKELONG(minor,major)
  38. DWORD GetDllVersion(LPCTSTR lpszDllName);
  39. LLDir_Win32::LLDir_Win32()
  40. {
  41. mDirDelimiter = "\\";
  42. WCHAR w_str[MAX_PATH];
  43. // Application Data is where user settings go
  44. SHGetSpecialFolderPath(NULL, w_str, CSIDL_APPDATA, TRUE);
  45. mOSUserDir = utf16str_to_utf8str(llutf16string(w_str));
  46. // We want cache files to go on the local disk, even if the
  47. // user is on a network with a "roaming profile".
  48. //
  49. // On XP this is:
  50. // C:\Docments and Settings\James\Local Settings\Application Data
  51. // On Vista this is:
  52. // C:\Users\James\AppData\Local
  53. //
  54. // We used to store the cache in AppData\Roaming, and the installer
  55. // cleans up that version on upgrade. JC
  56. SHGetSpecialFolderPath(NULL, w_str, CSIDL_LOCAL_APPDATA, TRUE);
  57. mOSCacheDir = utf16str_to_utf8str(llutf16string(w_str));
  58. if (GetTempPath(MAX_PATH, w_str))
  59. {
  60. if (wcslen(w_str)) /* Flawfinder: ignore */
  61. {
  62. w_str[wcslen(w_str)-1] = '\0'; /* Flawfinder: ignore */ // remove trailing slash
  63. }
  64. mTempDir = utf16str_to_utf8str(llutf16string(w_str));
  65. }
  66. else
  67. {
  68. mTempDir = mOSUserDir;
  69. }
  70. // fprintf(stderr, "mTempDir = <%s>",mTempDir);
  71. // Set working directory, for LLDir::getWorkingDir()
  72. GetCurrentDirectory(MAX_PATH, w_str);
  73. mWorkingDir = utf16str_to_utf8str(llutf16string(w_str));
  74. // Set the executable directory
  75. S32 size = GetModuleFileName(NULL, w_str, MAX_PATH);
  76. if (size)
  77. {
  78. w_str[size] = '\0';
  79. mExecutablePathAndName = utf16str_to_utf8str(llutf16string(w_str));
  80. S32 path_end = mExecutablePathAndName.find_last_of('\\');
  81. if (path_end != std::string::npos)
  82. {
  83. mExecutableDir = mExecutablePathAndName.substr(0, path_end);
  84. mExecutableFilename = mExecutablePathAndName.substr(path_end+1, std::string::npos);
  85. }
  86. else
  87. {
  88. mExecutableFilename = mExecutablePathAndName;
  89. }
  90. }
  91. else
  92. {
  93. fprintf(stderr, "Couldn't get APP path, assuming current directory!");
  94. mExecutableDir = mWorkingDir;
  95. // Assume it's the current directory
  96. }
  97. // mAppRODataDir = ".";
  98. // Determine the location of the App-Read-Only-Data
  99. // Try the working directory then the exe's dir.
  100. mAppRODataDir = mWorkingDir;
  101. // if (mExecutableDir.find("indra") == std::string::npos)
  102. // *NOTE:Mani - It is a mistake to put viewer specific code in
  103. // the LLDir implementation. The references to 'skins' and
  104. // 'llplugin' need to go somewhere else.
  105. // alas... this also gets called during static initialization
  106. // time due to the construction of gDirUtil in lldir.cpp.
  107. if(! LLFile::isdir(mAppRODataDir + mDirDelimiter + "skins"))
  108. {
  109. // What? No skins in the working dir?
  110. // Try the executable's directory.
  111. mAppRODataDir = mExecutableDir;
  112. }
  113. llinfos << "mAppRODataDir = " << mAppRODataDir << llendl;
  114. mSkinBaseDir = mAppRODataDir + mDirDelimiter + "skins";
  115. // Build the default cache directory
  116. mDefaultCacheDir = buildSLOSCacheDir();
  117. // Make sure it exists
  118. int res = LLFile::mkdir(mDefaultCacheDir);
  119. if (res == -1)
  120. {
  121. if (errno != EEXIST)
  122. {
  123. llwarns << "Couldn't create LL_PATH_CACHE dir " << mDefaultCacheDir << llendl;
  124. }
  125. }
  126. mLLPluginDir = mExecutableDir + mDirDelimiter + "llplugin";
  127. }
  128. LLDir_Win32::~LLDir_Win32()
  129. {
  130. }
  131. // Implementation
  132. void LLDir_Win32::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. mOSUserAppDir = mOSUserDir;
  143. mOSUserAppDir += "\\";
  144. mOSUserAppDir += app_name;
  145. int res = LLFile::mkdir(mOSUserAppDir);
  146. if (res == -1)
  147. {
  148. if (errno != EEXIST)
  149. {
  150. llwarns << "Couldn't create app user dir " << mOSUserAppDir << llendl;
  151. llwarns << "Default to base dir" << mOSUserDir << llendl;
  152. mOSUserAppDir = mOSUserDir;
  153. }
  154. }
  155. //dumpCurrentDirectories();
  156. res = LLFile::mkdir(getExpandedFilename(LL_PATH_LOGS,""));
  157. if (res == -1)
  158. {
  159. if (errno != EEXIST)
  160. {
  161. llwarns << "Couldn't create LL_PATH_LOGS dir " << getExpandedFilename(LL_PATH_LOGS,"") << llendl;
  162. }
  163. }
  164. res = LLFile::mkdir(getExpandedFilename(LL_PATH_USER_SETTINGS,""));
  165. if (res == -1)
  166. {
  167. if (errno != EEXIST)
  168. {
  169. llwarns << "Couldn't create LL_PATH_USER_SETTINGS dir " << getExpandedFilename(LL_PATH_USER_SETTINGS,"") << llendl;
  170. }
  171. }
  172. res = LLFile::mkdir(getExpandedFilename(LL_PATH_CACHE,""));
  173. if (res == -1)
  174. {
  175. if (errno != EEXIST)
  176. {
  177. llwarns << "Couldn't create LL_PATH_CACHE dir " << getExpandedFilename(LL_PATH_CACHE,"") << llendl;
  178. }
  179. }
  180. mCAFile = getExpandedFilename(LL_PATH_APP_SETTINGS, "CA.pem");
  181. }
  182. U32 LLDir_Win32::countFilesInDir(const std::string &dirname, const std::string &mask)
  183. {
  184. HANDLE count_search_h;
  185. U32 file_count;
  186. file_count = 0;
  187. WIN32_FIND_DATA FileData;
  188. llutf16string pathname = utf8str_to_utf16str(dirname);
  189. pathname += utf8str_to_utf16str(mask);
  190. if ((count_search_h = FindFirstFile(pathname.c_str(), &FileData)) != INVALID_HANDLE_VALUE)
  191. {
  192. file_count++;
  193. while (FindNextFile(count_search_h, &FileData))
  194. {
  195. file_count++;
  196. }
  197. FindClose(count_search_h);
  198. }
  199. return (file_count);
  200. }
  201. std::string LLDir_Win32::getCurPath()
  202. {
  203. WCHAR w_str[MAX_PATH];
  204. GetCurrentDirectory(MAX_PATH, w_str);
  205. return utf16str_to_utf8str(llutf16string(w_str));
  206. }
  207. BOOL LLDir_Win32::fileExists(const std::string &filename) const
  208. {
  209. llstat stat_data;
  210. // Check the age of the file
  211. // Now, we see if the files we've gathered are recent...
  212. int res = LLFile::stat(filename, &stat_data);
  213. if (!res)
  214. {
  215. return TRUE;
  216. }
  217. else
  218. {
  219. return FALSE;
  220. }
  221. }
  222. /*virtual*/ std::string LLDir_Win32::getLLPluginLauncher()
  223. {
  224. return gDirUtilp->getExecutableDir() + gDirUtilp->getDirDelimiter() +
  225. "SLPlugin.exe";
  226. }
  227. /*virtual*/ std::string LLDir_Win32::getLLPluginFilename(std::string base_name)
  228. {
  229. return gDirUtilp->getLLPluginDir() + gDirUtilp->getDirDelimiter() +
  230. base_name + ".dll";
  231. }
  232. #if 0
  233. // Utility function to get version number of a DLL
  234. #define PACKVERSION(major,minor) MAKELONG(minor,major)
  235. DWORD GetDllVersion(LPCTSTR lpszDllName)
  236. {
  237. HINSTANCE hinstDll;
  238. DWORD dwVersion = 0;
  239. hinstDll = LoadLibrary(lpszDllName); /* Flawfinder: ignore */
  240. if(hinstDll)
  241. {
  242. DLLGETVERSIONPROC pDllGetVersion;
  243. pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hinstDll, "DllGetVersion");
  244. /*Because some DLLs might not implement this function, you
  245. must test for it explicitly. Depending on the particular
  246. DLL, the lack of a DllGetVersion function can be a useful
  247. indicator of the version.
  248. */
  249. if(pDllGetVersion)
  250. {
  251. DLLVERSIONINFO dvi;
  252. HRESULT hr;
  253. ZeroMemory(&dvi, sizeof(dvi));
  254. dvi.cbSize = sizeof(dvi);
  255. hr = (*pDllGetVersion)(&dvi);
  256. if(SUCCEEDED(hr))
  257. {
  258. dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion);
  259. }
  260. }
  261. FreeLibrary(hinstDll);
  262. }
  263. return dwVersion;
  264. }
  265. #endif
  266. #endif