PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 27#include "linden_common.h"
 28
 29#include "lldir_linux.h"
 30#include "llerror.h"
 31#include "llrand.h"
 32#include <sys/types.h>
 33#include <sys/stat.h>
 34#include <unistd.h>
 35#include <glob.h>
 36#include <pwd.h>
 37
 38
 39static std::string getCurrentUserHome(char* fallback)
 40{
 41	const uid_t uid = getuid();
 42	struct passwd *pw;
 43	char *result_cstr = fallback;
 44	
 45	pw = getpwuid(uid);
 46	if ((pw != NULL) && (pw->pw_dir != NULL))
 47	{
 48		result_cstr = (char*) pw->pw_dir;
 49	}
 50	else
 51	{
 52		llinfos << "Couldn't detect home directory from passwd - trying $HOME" << llendl;
 53		const char *const home_env = getenv("HOME");	/* Flawfinder: ignore */ 
 54		if (home_env)
 55		{
 56			result_cstr = (char*) home_env;
 57		}
 58		else
 59		{
 60			llwarns << "Couldn't detect home directory!  Falling back to " << fallback << llendl;
 61		}
 62	}
 63	
 64	return std::string(result_cstr);
 65}
 66
 67
 68LLDir_Linux::LLDir_Linux()
 69{
 70	mDirDelimiter = "/";
 71	mCurrentDirIndex = -1;
 72	mCurrentDirCount = -1;
 73	mDirp = NULL;
 74
 75	char tmp_str[LL_MAX_PATH];	/* Flawfinder: ignore */ 
 76	if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
 77	{
 78		strcpy(tmp_str, "/tmp");
 79		llwarns << "Could not get current directory; changing to "
 80				<< tmp_str << llendl;
 81		if (chdir(tmp_str) == -1)
 82		{
 83			llerrs << "Could not change directory to " << tmp_str << llendl;
 84		}
 85	}
 86
 87	mExecutableFilename = "";
 88	mExecutablePathAndName = "";
 89	mExecutableDir = tmp_str;
 90	mWorkingDir = tmp_str;
 91#ifdef APP_RO_DATA_DIR
 92	mAppRODataDir = APP_RO_DATA_DIR;
 93#else
 94	mAppRODataDir = tmp_str;
 95#endif
 96    std::string::size_type build_dir_pos = mExecutableDir.rfind("/build-linux-");
 97    if (build_dir_pos != std::string::npos)
 98    {
 99		// ...we're in a dev checkout
100		mSkinBaseDir = mExecutableDir.substr(0, build_dir_pos) + "/indra/newview/skins";
101		llinfos << "Running in dev checkout with mSkinBaseDir "
102		 << mSkinBaseDir << llendl;
103    }
104    else
105    {
106		// ...normal installation running
107		mSkinBaseDir = mAppRODataDir + mDirDelimiter + "skins";
108    }	
109
110	mOSUserDir = getCurrentUserHome(tmp_str);
111	mOSUserAppDir = "";
112	mLindenUserDir = "";
113
114	char path [32];	/* Flawfinder: ignore */ 
115
116	// *NOTE: /proc/%d/exe doesn't work on FreeBSD. But that's ok,
117	// because this is the linux implementation.
118
119	snprintf (path, sizeof(path), "/proc/%d/exe", (int) getpid ()); 
120	int rc = readlink (path, tmp_str, sizeof (tmp_str)-1);	/* Flawfinder: ignore */ 
121	if ( (rc != -1) && (rc <= ((int) sizeof (tmp_str)-1)) )
122	{
123		tmp_str[rc] = '\0'; //readlink() doesn't 0-terminate the buffer
124		mExecutablePathAndName = tmp_str;
125		char *path_end;
126		if ((path_end = strrchr(tmp_str,'/')))
127		{
128			*path_end = '\0';
129			mExecutableDir = tmp_str;
130			mWorkingDir = tmp_str;
131			mExecutableFilename = path_end+1;
132		}
133		else
134		{
135			mExecutableFilename = tmp_str;
136		}
137	}
138
139	mLLPluginDir = mExecutableDir + mDirDelimiter + "llplugin";
140
141	// *TODO: don't use /tmp, use $HOME/.secondlife/tmp or something.
142	mTempDir = "/tmp";
143}
144
145LLDir_Linux::~LLDir_Linux()
146{
147}
148
149// Implementation
150
151
152void LLDir_Linux::initAppDirs(const std::string &app_name,
153							  const std::string& app_read_only_data_dir)
154{
155	// Allow override so test apps can read newview directory
156	if (!app_read_only_data_dir.empty())
157	{
158		mAppRODataDir = app_read_only_data_dir;
159		mSkinBaseDir = mAppRODataDir + mDirDelimiter + "skins";
160	}
161	mAppName = app_name;
162
163	std::string upper_app_name(app_name);
164	LLStringUtil::toUpper(upper_app_name);
165
166	char* app_home_env = getenv((upper_app_name + "_USER_DIR").c_str());	/* Flawfinder: ignore */ 
167	if (app_home_env)
168	{
169		// user has specified own userappdir i.e. $SECONDLIFE_USER_DIR
170		mOSUserAppDir = app_home_env;
171	}
172	else
173	{
174		// traditionally on unixoids, MyApp gets ~/.myapp dir for data
175		mOSUserAppDir = mOSUserDir;
176		mOSUserAppDir += "/";
177		mOSUserAppDir += ".";
178		std::string lower_app_name(app_name);
179		LLStringUtil::toLower(lower_app_name);
180		mOSUserAppDir += lower_app_name;
181	}
182
183	// create any directories we expect to write to.
184
185	int res = LLFile::mkdir(mOSUserAppDir);
186	if (res == -1)
187	{
188		if (errno != EEXIST)
189		{
190			llwarns << "Couldn't create app user dir " << mOSUserAppDir << llendl;
191			llwarns << "Default to base dir" << mOSUserDir << llendl;
192			mOSUserAppDir = mOSUserDir;
193		}
194	}
195
196	res = LLFile::mkdir(getExpandedFilename(LL_PATH_LOGS,""));
197	if (res == -1)
198	{
199		if (errno != EEXIST)
200		{
201			llwarns << "Couldn't create LL_PATH_LOGS dir " << getExpandedFilename(LL_PATH_LOGS,"") << llendl;
202		}
203	}
204	
205	res = LLFile::mkdir(getExpandedFilename(LL_PATH_USER_SETTINGS,""));
206	if (res == -1)
207	{
208		if (errno != EEXIST)
209		{
210			llwarns << "Couldn't create LL_PATH_USER_SETTINGS dir " << getExpandedFilename(LL_PATH_USER_SETTINGS,"") << llendl;
211		}
212	}
213	
214	res = LLFile::mkdir(getExpandedFilename(LL_PATH_CACHE,""));
215	if (res == -1)
216	{
217		if (errno != EEXIST)
218		{
219			llwarns << "Couldn't create LL_PATH_CACHE dir " << getExpandedFilename(LL_PATH_CACHE,"") << llendl;
220		}
221	}
222	
223	mCAFile = getExpandedFilename(LL_PATH_APP_SETTINGS, "CA.pem");
224}
225
226U32 LLDir_Linux::countFilesInDir(const std::string &dirname, const std::string &mask)
227{
228	U32 file_count = 0;
229	glob_t g;
230
231	std::string tmp_str;
232	tmp_str = dirname;
233	tmp_str += mask;
234	
235	if(glob(tmp_str.c_str(), GLOB_NOSORT, NULL, &g) == 0)
236	{
237		file_count = g.gl_pathc;
238
239		globfree(&g);
240	}
241
242	return (file_count);
243}
244
245std::string LLDir_Linux::getCurPath()
246{
247	char tmp_str[LL_MAX_PATH];	/* Flawfinder: ignore */ 
248	if (getcwd(tmp_str, LL_MAX_PATH) == NULL)
249	{
250		llwarns << "Could not get current directory" << llendl;
251		tmp_str[0] = '\0';
252	}
253	return tmp_str;
254}
255
256
257BOOL LLDir_Linux::fileExists(const std::string &filename) const
258{
259	struct stat stat_data;
260	// Check the age of the file
261	// Now, we see if the files we've gathered are recent...
262	int res = stat(filename.c_str(), &stat_data);
263	if (!res)
264	{
265		return TRUE;
266	}
267	else
268	{
269		return FALSE;
270	}
271}
272
273
274/*virtual*/ std::string LLDir_Linux::getLLPluginLauncher()
275{
276	return gDirUtilp->getExecutableDir() + gDirUtilp->getDirDelimiter() +
277		"SLPlugin";
278}
279
280/*virtual*/ std::string LLDir_Linux::getLLPluginFilename(std::string base_name)
281{
282	return gDirUtilp->getLLPluginDir() + gDirUtilp->getDirDelimiter() +
283		"lib" + base_name + ".so";
284}