POSIXFileio.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /* JMQ:
  23. Here's the scoop on unix file IO. The windows platform makes some
  24. assumptions about fileio: 1) the file system is case-insensitive, and
  25. 2) the platform can write to the directory in which
  26. the game is running. Both of these are usually false on linux. So, to
  27. compensate, we "route" created files and directories to the user's home
  28. directory (see GetPrefPath()). When a file is to be accessed, the code
  29. looks in the home directory first. If the file is not found there and the
  30. open mode is read only, the code will look in the game installation
  31. directory. Files are never created or modified in the game directory.
  32. For case-sensitivity, the MungePath code will test whether a given path
  33. specified by the engine exists. If not, it will use the ResolvePathCaseInsensitive function
  34. which will try to determine if an actual filesystem path matches the
  35. specified path case insensitive. If one is found, the actual path
  36. transparently (we hope) replaces the one requested by the engine.
  37. The preference directory is global to all torque executables with the same
  38. name. You should make sure you keep it clean if you build from multiple
  39. torque development trees.
  40. */
  41. #if defined(__FreeBSD__)
  42. #include <sys/types.h>
  43. #endif
  44. #include <utime.h>
  45. /* these are for reading directors, getting stats, etc. */
  46. #include <dirent.h>
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <errno.h>
  52. #include <stdlib.h>
  53. #include "core/fileio.h"
  54. #include "core/util/tVector.h"
  55. #include "core/stringTable.h"
  56. #include "console/console.h"
  57. #include "core/strings/stringFunctions.h"
  58. #include "util/tempAlloc.h"
  59. #include "cinterface/c_controlInterface.h"
  60. #include "core/volume.h"
  61. const int MaxPath = PATH_MAX;
  62. //------------------------------------------------------------------------------
  63. // munge the case of the specified pathName. This means try to find the actual
  64. // filename in with case-insensitive matching on the specified pathName, and
  65. // store the actual found name.
  66. bool ResolvePathCaseInsensitive(char* pathName, S32 pathNameSize, bool requiredAbsolute)
  67. {
  68. char tempBuf[MaxPath];
  69. dStrncpy(tempBuf, pathName, pathNameSize);
  70. // Check if we're an absolute path
  71. if (pathName[0] != '/')
  72. {
  73. AssertFatal(!requiredAbsolute, "PATH must be absolute");
  74. return false;
  75. }
  76. struct stat filestat;
  77. const int MaxPathEl = 200;
  78. char *currChar = pathName;
  79. char testPath[MaxPath];
  80. char pathEl[MaxPathEl];
  81. bool done = false;
  82. bool foundMatch = false;
  83. dStrncpy(tempBuf, "/", MaxPath);
  84. currChar++;
  85. while (!done)
  86. {
  87. char* termChar = dStrchr(currChar, '/');
  88. if (termChar == NULL)
  89. termChar = dStrchr(currChar, '\0');
  90. AssertFatal(termChar, "Can't find / or NULL terminator");
  91. S32 pathElLen = (termChar - currChar);
  92. dStrncpy(pathEl, currChar, pathElLen);
  93. pathEl[pathElLen] = '\0';
  94. dStrncpy(testPath, tempBuf, MaxPath);
  95. dStrcat(testPath, pathEl, MaxPath);
  96. if (stat(testPath, &filestat) != -1)
  97. {
  98. dStrncpy(tempBuf, testPath, MaxPath);
  99. }
  100. else
  101. {
  102. DIR *dir = opendir(tempBuf);
  103. struct dirent* ent;
  104. while (dir != NULL && (ent = readdir(dir)) != NULL)
  105. {
  106. if (dStricmp(pathEl, ent->d_name) == 0)
  107. {
  108. foundMatch = true;
  109. dStrcat(tempBuf, ent->d_name, MaxPath);
  110. break;
  111. }
  112. }
  113. if (!foundMatch)
  114. dStrncpy(tempBuf, testPath, MaxPath);
  115. if (dir)
  116. closedir(dir);
  117. }
  118. if (*termChar == '/')
  119. {
  120. dStrcat(tempBuf, "/", MaxPath);
  121. termChar++;
  122. currChar = termChar;
  123. }
  124. else
  125. done = true;
  126. }
  127. dStrncpy(pathName, tempBuf, pathNameSize);
  128. return foundMatch;
  129. }
  130. #ifndef __APPLE__
  131. // evil hack to get around insane X windows #define-happy header files
  132. #ifdef Status
  133. #undef Status
  134. #endif
  135. #include "platformX86UNIX/platformX86UNIX.h"
  136. extern int x86UNIXOpen(const char *path, int oflag);
  137. extern int x86UNIXClose(int fd);
  138. extern ssize_t x86UNIXRead(int fd, void *buf, size_t nbytes);
  139. extern ssize_t x86UNIXWrite(int fd, const void *buf, size_t nbytes);
  140. extern bool ResolvePathCaseInsensitive(char* pathName, S32 pathNameSize, bool requiredAbsolute);
  141. const int MaxPath = PATH_MAX;
  142. namespace
  143. {
  144. const char sTempDir[] = "/tmp/";
  145. static char sBinPathName[MaxPath] = "";
  146. static char *sBinName = sBinPathName;
  147. bool sUseRedirect = true;
  148. }
  149. StringTableEntry osGetTemporaryDirectory()
  150. {
  151. return StringTable->insert(sTempDir);
  152. }
  153. // Various handy utility functions:
  154. //------------------------------------------------------------------------------
  155. // find all \ in a path and convert them in place to /
  156. static void ForwardSlash(char *str)
  157. {
  158. while(*str)
  159. {
  160. if(*str == '\\')
  161. *str = '/';
  162. str++;
  163. }
  164. }
  165. //------------------------------------------------------------------------------
  166. // copy a file from src to dest
  167. static bool CopyFile(const char* src, const char* dest)
  168. {
  169. S32 srcFd = x86UNIXOpen(src, O_RDONLY);
  170. S32 destFd = x86UNIXOpen(dest, O_WRONLY | O_CREAT | O_TRUNC);
  171. bool error = false;
  172. if (srcFd != -1 && destFd != -1)
  173. {
  174. const int BufSize = 8192;
  175. char buf[BufSize];
  176. S32 bytesRead = 0;
  177. while ((bytesRead = x86UNIXRead(srcFd, buf, BufSize)) > 0)
  178. {
  179. // write data
  180. if (x86UNIXWrite(destFd, buf, bytesRead) == -1)
  181. {
  182. error = true;
  183. break;
  184. }
  185. }
  186. if (bytesRead == -1)
  187. error = true;
  188. }
  189. if (srcFd != -1)
  190. x86UNIXClose(srcFd);
  191. if (destFd != -1)
  192. x86UNIXClose(destFd);
  193. if (error)
  194. {
  195. Con::errorf("Error copying file: %s, %s", src, dest);
  196. remove(dest);
  197. }
  198. return error;
  199. }
  200. bool dPathCopy(const char *fromName, const char *toName, bool nooverwrite)
  201. {
  202. return CopyFile(fromName,toName);
  203. }
  204. //-----------------------------------------------------------------------------
  205. static char sgPrefDir[MaxPath];
  206. static bool sgPrefDirInitialized = false;
  207. // get the "pref dir", which is where game output files are stored. the pref
  208. // dir is ~/PREF_DIR_ROOT/PREF_DIR_GAME_NAME
  209. static const char* GetPrefDir()
  210. {
  211. if (sgPrefDirInitialized)
  212. return sgPrefDir;
  213. if (sUseRedirect)
  214. {
  215. const char *home = getenv("HOME");
  216. AssertFatal(home, "HOME environment variable must be set");
  217. dSprintf(sgPrefDir, MaxPath, "%s/%s/%s",
  218. home, PREF_DIR_ROOT, PREF_DIR_GAME_NAME);
  219. }
  220. else
  221. {
  222. getcwd(sgPrefDir, MaxPath);
  223. }
  224. sgPrefDirInitialized = true;
  225. return sgPrefDir;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Returns true if the pathname exists, false otherwise. If isFile is true,
  229. // the pathname is assumed to be a file path, and only the directory part
  230. // will be examined (everything before last /)
  231. bool DirExists(char* pathname, bool isFile)
  232. {
  233. static char testpath[20000];
  234. dStrncpy(testpath, pathname, sizeof(testpath));
  235. if (isFile)
  236. {
  237. // find the last / and make it into null
  238. char* lastSlash = dStrrchr(testpath, '/');
  239. if (lastSlash != NULL)
  240. *lastSlash = 0;
  241. }
  242. return Platform::isDirectory(testpath);
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Munge the specified path.
  246. static void MungePath(char* dest, S32 destSize,
  247. const char* src, const char* absolutePrefix)
  248. {
  249. char tempBuf[MaxPath];
  250. dStrncpy(dest, src, MaxPath);
  251. // translate all \ to /
  252. ForwardSlash(dest);
  253. // if it is relative, make it absolute with the absolutePrefix
  254. if (dest[0] != '/')
  255. {
  256. AssertFatal(absolutePrefix, "Absolute Prefix must not be NULL");
  257. dSprintf(tempBuf, MaxPath, "%s/%s",
  258. absolutePrefix, dest);
  259. // copy the result back into dest
  260. dStrncpy(dest, tempBuf, destSize);
  261. }
  262. // if the path exists, we're done
  263. struct stat filestat;
  264. if (stat(dest, &filestat) != -1)
  265. return;
  266. // otherwise munge the case of the path
  267. ResolvePathCaseInsensitive(dest, destSize, true);
  268. }
  269. //-----------------------------------------------------------------------------
  270. enum
  271. {
  272. TOUCH,
  273. DELETE
  274. };
  275. //-----------------------------------------------------------------------------
  276. // perform a modification on the specified file. allowed modifications are
  277. // specified in the enum above.
  278. bool ModifyFile(const char * name, S32 modType)
  279. {
  280. if(!name || (dStrlen(name) >= MaxPath) || dStrstr(name, "../") != NULL)
  281. return(false);
  282. // if its absolute skip it
  283. if (name[0]=='/' || name[0]=='\\')
  284. return(false);
  285. // only modify files in home directory
  286. char prefPathName[MaxPath];
  287. MungePath(prefPathName, MaxPath, name, GetPrefDir());
  288. if (modType == TOUCH)
  289. return(utime(prefPathName, 0) != -1);
  290. else if (modType == DELETE)
  291. return (remove(prefPathName) == 0);
  292. else
  293. AssertFatal(false, "Unknown File Mod type");
  294. return false;
  295. }
  296. //-----------------------------------------------------------------------------
  297. static bool RecurseDumpPath(const char *path, const char* relativePath, const char *pattern, Vector<Platform::FileInfo> &fileVector, S32 currentDepth = 0, S32 recurseDepth = -1)
  298. {
  299. char search[1024];
  300. dSprintf(search, sizeof(search), "%s", path, pattern);
  301. DIR *directory = opendir(search);
  302. if (directory == NULL)
  303. return false;
  304. struct dirent *fEntry;
  305. fEntry = readdir(directory); // read the first "file" in the directory
  306. if (fEntry == NULL)
  307. {
  308. closedir(directory);
  309. return false;
  310. }
  311. do
  312. {
  313. char filename[BUFSIZ+1];
  314. struct stat fStat;
  315. dSprintf(filename, sizeof(filename), "%s/%s", search, fEntry->d_name); // "construct" the file name
  316. stat(filename, &fStat); // get the file stats
  317. if ( (fStat.st_mode & S_IFMT) == S_IFDIR )
  318. {
  319. // Directory
  320. // skip . and .. directories
  321. if (dStrcmp(fEntry->d_name, ".") == 0 || dStrcmp(fEntry->d_name, "..") == 0)
  322. continue;
  323. // skip excluded directories
  324. if( Platform::isExcludedDirectory(fEntry->d_name))
  325. continue;
  326. char child[MaxPath];
  327. dSprintf(child, sizeof(child), "%s/%s", path, fEntry->d_name);
  328. char* childRelative = NULL;
  329. char childRelativeBuf[MaxPath];
  330. if (relativePath)
  331. {
  332. dSprintf(childRelativeBuf, sizeof(childRelativeBuf), "%s/%s",
  333. relativePath, fEntry->d_name);
  334. childRelative = childRelativeBuf;
  335. }
  336. if (currentDepth < recurseDepth || recurseDepth == -1 )
  337. RecurseDumpPath(child, childRelative, pattern, fileVector, currentDepth+1, recurseDepth);
  338. }
  339. else
  340. {
  341. // File
  342. // add it to the list
  343. fileVector.increment();
  344. Platform::FileInfo& rInfo = fileVector.last();
  345. if (relativePath)
  346. rInfo.pFullPath = StringTable->insert(relativePath);
  347. else
  348. rInfo.pFullPath = StringTable->insert(path);
  349. rInfo.pFileName = StringTable->insert(fEntry->d_name);
  350. rInfo.fileSize = fStat.st_size;
  351. //dPrintf("Adding file: %s/%s\n", rInfo.pFullPath, rInfo.pFileName);
  352. }
  353. } while( (fEntry = readdir(directory)) != NULL );
  354. closedir(directory);
  355. return true;
  356. }
  357. //-----------------------------------------------------------------------------
  358. bool dFileDelete(const char * name)
  359. {
  360. return ModifyFile(name, DELETE);
  361. }
  362. //-----------------------------------------------------------------------------
  363. bool dFileTouch(const char * name)
  364. {
  365. return ModifyFile(name, TOUCH);
  366. }
  367. bool dFileRename(const char *oldName, const char *newName)
  368. {
  369. AssertFatal( oldName != NULL && newName != NULL, "dFileRename - NULL file name" );
  370. // only modify files in home directory
  371. TempAlloc<char> oldPrefPathName(MaxPath);
  372. TempAlloc<char> newPrefPathName(MaxPath);
  373. MungePath(oldPrefPathName, MaxPath, oldName, GetPrefDir());
  374. MungePath(newPrefPathName, MaxPath, newName, GetPrefDir());
  375. return rename(oldPrefPathName, newPrefPathName) == 0;
  376. }
  377. //-----------------------------------------------------------------------------
  378. // Constructors & Destructor
  379. //-----------------------------------------------------------------------------
  380. //-----------------------------------------------------------------------------
  381. // After construction, the currentStatus will be Closed and the capabilities
  382. // will be 0.
  383. //-----------------------------------------------------------------------------
  384. File::File()
  385. : currentStatus(Closed), capability(0)
  386. {
  387. // AssertFatal(sizeof(int) == sizeof(void *), "File::File: cannot cast void* to int");
  388. handle = (void *)NULL;
  389. }
  390. //-----------------------------------------------------------------------------
  391. // insert a copy constructor here... (currently disabled)
  392. //-----------------------------------------------------------------------------
  393. //-----------------------------------------------------------------------------
  394. // Destructor
  395. //-----------------------------------------------------------------------------
  396. File::~File()
  397. {
  398. close();
  399. handle = (void *)NULL;
  400. }
  401. //-----------------------------------------------------------------------------
  402. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  403. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  404. // true.
  405. //
  406. // Sets capability appropriate to the openMode.
  407. // Returns the currentStatus of the file.
  408. //-----------------------------------------------------------------------------
  409. File::FileStatus File::open(const char *filename, const AccessMode openMode)
  410. {
  411. AssertFatal(NULL != filename, "File::open: NULL filename");
  412. AssertWarn(NULL == handle, "File::open: handle already valid");
  413. // Close the file if it was already open...
  414. if (Closed != currentStatus)
  415. close();
  416. char prefPathName[MaxPath];
  417. char gamePathName[MaxPath];
  418. char cwd[MaxPath];
  419. getcwd(cwd, MaxPath);
  420. MungePath(prefPathName, MaxPath, filename, GetPrefDir());
  421. MungePath(gamePathName, MaxPath, filename, cwd);
  422. int oflag;
  423. struct stat filestat;
  424. handle = (void *)dRealMalloc(sizeof(int));
  425. switch (openMode)
  426. {
  427. case Read:
  428. oflag = O_RDONLY;
  429. break;
  430. case Write:
  431. oflag = O_WRONLY | O_CREAT | O_TRUNC;
  432. break;
  433. case ReadWrite:
  434. oflag = O_RDWR | O_CREAT;
  435. // if the file does not exist copy it before reading/writing
  436. if (stat(prefPathName, &filestat) == -1)
  437. bool ret = CopyFile(gamePathName, prefPathName);
  438. break;
  439. case WriteAppend:
  440. oflag = O_WRONLY | O_CREAT | O_APPEND;
  441. // if the file does not exist copy it before appending
  442. if (stat(prefPathName, &filestat) == -1)
  443. bool ret = CopyFile(gamePathName, prefPathName);
  444. break;
  445. default:
  446. AssertFatal(false, "File::open: bad access mode"); // impossible
  447. }
  448. // if we are writing, make sure output path exists
  449. if (openMode == Write || openMode == ReadWrite || openMode == WriteAppend)
  450. Platform::createPath(prefPathName);
  451. int fd = -1;
  452. fd = x86UNIXOpen(prefPathName, oflag);
  453. if (fd == -1 && openMode == Read)
  454. // for read only files we can use the gamePathName
  455. fd = x86UNIXOpen(gamePathName, oflag);
  456. dMemcpy(handle, &fd, sizeof(int));
  457. #ifdef DEBUG
  458. // fprintf(stdout,"fd = %d, handle = %d\n", fd, *((int *)handle));
  459. #endif
  460. if (*((int *)handle) == -1)
  461. {
  462. // handle not created successfully
  463. Con::errorf("Can't open file: %s", filename);
  464. return setStatus();
  465. }
  466. else
  467. {
  468. // successfully created file, so set the file capabilities...
  469. switch (openMode)
  470. {
  471. case Read:
  472. capability = U32(FileRead);
  473. break;
  474. case Write:
  475. case WriteAppend:
  476. capability = U32(FileWrite);
  477. break;
  478. case ReadWrite:
  479. capability = U32(FileRead) |
  480. U32(FileWrite);
  481. break;
  482. default:
  483. AssertFatal(false, "File::open: bad access mode");
  484. }
  485. return currentStatus = Ok; // success!
  486. }
  487. }
  488. //-----------------------------------------------------------------------------
  489. // Get the current position of the file pointer.
  490. //-----------------------------------------------------------------------------
  491. U32 File::getPosition() const
  492. {
  493. AssertFatal(Closed != currentStatus, "File::getPosition: file closed");
  494. AssertFatal(NULL != handle, "File::getPosition: invalid file handle");
  495. #ifdef DEBUG
  496. // fprintf(stdout, "handle = %d\n",*((int *)handle));fflush(stdout);
  497. #endif
  498. return (U32) lseek(*((int *)handle), 0, SEEK_CUR);
  499. }
  500. //-----------------------------------------------------------------------------
  501. // Set the position of the file pointer.
  502. // Absolute and relative positioning is supported via the absolutePos
  503. // parameter.
  504. //
  505. // If positioning absolutely, position MUST be positive - an IOError results if
  506. // position is negative.
  507. // Position can be negative if positioning relatively, however positioning
  508. // before the start of the file is an IOError.
  509. //
  510. // Returns the currentStatus of the file.
  511. //-----------------------------------------------------------------------------
  512. File::FileStatus File::setPosition(S32 position, bool absolutePos)
  513. {
  514. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  515. AssertFatal(NULL != handle, "File::setPosition: invalid file handle");
  516. if (Ok != currentStatus && EOS != currentStatus)
  517. return currentStatus;
  518. U32 finalPos = 0;
  519. if (absolutePos)
  520. {
  521. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  522. // position beyond EOS is OK
  523. finalPos = lseek(*((int *)handle), position, SEEK_SET);
  524. }
  525. else
  526. {
  527. AssertFatal((getPosition() >= (U32)abs(position) && 0 > position) || 0 <= position, "File::setPosition: negative relative position");
  528. // position beyond EOS is OK
  529. finalPos = lseek(*((int *)handle), position, SEEK_CUR);
  530. }
  531. if (0xffffffff == finalPos)
  532. return setStatus(); // unsuccessful
  533. else if (finalPos >= getSize())
  534. return currentStatus = EOS; // success, at end of file
  535. else
  536. return currentStatus = Ok; // success!
  537. }
  538. //-----------------------------------------------------------------------------
  539. // Get the size of the file in bytes.
  540. // It is an error to query the file size for a Closed file, or for one with an
  541. // error status.
  542. //-----------------------------------------------------------------------------
  543. U32 File::getSize() const
  544. {
  545. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  546. AssertFatal(NULL != handle, "File::getSize: invalid file handle");
  547. if (Ok == currentStatus || EOS == currentStatus)
  548. {
  549. long currentOffset = getPosition(); // keep track of our current position
  550. long fileSize;
  551. lseek(*((int *)handle), 0, SEEK_END); // seek to the end of the file
  552. fileSize = getPosition(); // get the file size
  553. lseek(*((int *)handle), currentOffset, SEEK_SET); // seek back to our old offset
  554. return fileSize; // success!
  555. }
  556. else
  557. return 0; // unsuccessful
  558. }
  559. //-----------------------------------------------------------------------------
  560. // Flush the file.
  561. // It is an error to flush a read-only file.
  562. // Returns the currentStatus of the file.
  563. //-----------------------------------------------------------------------------
  564. File::FileStatus File::flush()
  565. {
  566. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  567. AssertFatal(NULL != handle, "File::flush: invalid file handle");
  568. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  569. if (fsync(*((int *)handle)) == 0)
  570. return currentStatus = Ok; // success!
  571. else
  572. return setStatus(); // unsuccessful
  573. }
  574. //-----------------------------------------------------------------------------
  575. // Close the File.
  576. //
  577. // Returns the currentStatus
  578. //-----------------------------------------------------------------------------
  579. File::FileStatus File::close()
  580. {
  581. // if the handle is non-NULL, close it if necessary and free it
  582. if (NULL != handle)
  583. {
  584. // make a local copy of the handle value and
  585. // free the handle
  586. int handleVal = *((int *)handle);
  587. dRealFree(handle);
  588. handle = (void *)NULL;
  589. // close the handle if it is valid
  590. if (handleVal != -1 && x86UNIXClose(handleVal) != 0)
  591. return setStatus(); // unsuccessful
  592. }
  593. // Set the status to closed
  594. return currentStatus = Closed;
  595. }
  596. //-----------------------------------------------------------------------------
  597. // Self-explanatory.
  598. //-----------------------------------------------------------------------------
  599. File::FileStatus File::getStatus() const
  600. {
  601. return currentStatus;
  602. }
  603. //-----------------------------------------------------------------------------
  604. // Sets and returns the currentStatus when an error has been encountered.
  605. //-----------------------------------------------------------------------------
  606. File::FileStatus File::setStatus()
  607. {
  608. Con::printf("File IO error: %s", strerror(errno));
  609. return currentStatus = IOError;
  610. }
  611. //-----------------------------------------------------------------------------
  612. // Sets and returns the currentStatus to status.
  613. //-----------------------------------------------------------------------------
  614. File::FileStatus File::setStatus(File::FileStatus status)
  615. {
  616. return currentStatus = status;
  617. }
  618. //-----------------------------------------------------------------------------
  619. // Read from a file.
  620. // The number of bytes to read is passed in size, the data is returned in src.
  621. // The number of bytes read is available in bytesRead if a non-Null pointer is
  622. // provided.
  623. //-----------------------------------------------------------------------------
  624. File::FileStatus File::read(U32 size, char *dst, U32 *bytesRead)
  625. {
  626. #ifdef DEBUG
  627. // fprintf(stdout,"reading %d bytes\n",size);fflush(stdout);
  628. #endif
  629. AssertFatal(Closed != currentStatus, "File::read: file closed");
  630. AssertFatal(NULL != handle, "File::read: invalid file handle");
  631. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  632. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  633. AssertWarn(0 != size, "File::read: size of zero");
  634. /* show stats for this file */
  635. #ifdef DEBUG
  636. //struct stat st;
  637. //fstat(*((int *)handle), &st);
  638. //fprintf(stdout,"file size = %d\n", st.st_size);
  639. #endif
  640. /****************************/
  641. if (Ok != currentStatus || 0 == size)
  642. return currentStatus;
  643. else
  644. {
  645. long lastBytes;
  646. long *bytes = (NULL == bytesRead) ? &lastBytes : (long *)bytesRead;
  647. if ( (*((U32 *)bytes) = x86UNIXRead(*((int *)handle), dst, size)) == -1)
  648. {
  649. #ifdef DEBUG
  650. // fprintf(stdout,"unsuccessful: %d\n", *((U32 *)bytes));fflush(stdout);
  651. #endif
  652. return setStatus(); // unsuccessful
  653. } else {
  654. // dst[*((U32 *)bytes)] = '\0';
  655. if (*((U32 *)bytes) != size || *((U32 *)bytes) == 0) {
  656. #ifdef DEBUG
  657. // fprintf(stdout,"end of stream: %d\n", *((U32 *)bytes));fflush(stdout);
  658. #endif
  659. return currentStatus = EOS; // end of stream
  660. }
  661. }
  662. }
  663. // dst[*bytesRead] = '\0';
  664. #ifdef DEBUG
  665. //fprintf(stdout, "We read:\n");
  666. //fprintf(stdout, "====================================================\n");
  667. //fprintf(stdout, "%s\n",dst);
  668. //fprintf(stdout, "====================================================\n");
  669. //fprintf(stdout,"read ok: %d\n", *bytesRead);fflush(stdout);
  670. #endif
  671. return currentStatus = Ok; // successfully read size bytes
  672. }
  673. //-----------------------------------------------------------------------------
  674. // Write to a file.
  675. // The number of bytes to write is passed in size, the data is passed in src.
  676. // The number of bytes written is available in bytesWritten if a non-Null
  677. // pointer is provided.
  678. //-----------------------------------------------------------------------------
  679. File::FileStatus File::write(U32 size, const char *src, U32 *bytesWritten)
  680. {
  681. // JMQ: despite the U32 parameters, the maximum filesize supported by this
  682. // function is probably the max value of S32, due to the unix syscall
  683. // api.
  684. AssertFatal(Closed != currentStatus, "File::write: file closed");
  685. AssertFatal(NULL != handle, "File::write: invalid file handle");
  686. AssertFatal(NULL != src, "File::write: NULL source pointer");
  687. AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability");
  688. AssertWarn(0 != size, "File::write: size of zero");
  689. if ((Ok != currentStatus && EOS != currentStatus) || 0 == size)
  690. return currentStatus;
  691. else
  692. {
  693. S32 numWritten = x86UNIXWrite(*((int *)handle), src, size);
  694. if (numWritten < 0)
  695. return setStatus();
  696. if (bytesWritten)
  697. *bytesWritten = static_cast<U32>(numWritten);
  698. return currentStatus = Ok;
  699. }
  700. }
  701. //-----------------------------------------------------------------------------
  702. // Self-explanatory. JMQ: No explanation needed. Move along. These aren't
  703. // the droids you're looking for.
  704. //-----------------------------------------------------------------------------
  705. bool File::hasCapability(Capability cap) const
  706. {
  707. return (0 != (U32(cap) & capability));
  708. }
  709. //-----------------------------------------------------------------------------
  710. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  711. {
  712. if(a > b)
  713. return 1;
  714. if(a < b)
  715. return -1;
  716. return 0;
  717. }
  718. //-----------------------------------------------------------------------------
  719. static bool GetFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime)
  720. {
  721. struct stat fStat;
  722. if (stat(filePath, &fStat) == -1)
  723. return false;
  724. if(createTime)
  725. {
  726. // no where does SysV/BSD UNIX keep a record of a file's
  727. // creation time. instead of creation time I'll just use
  728. // changed time for now.
  729. *createTime = fStat.st_ctime;
  730. }
  731. if(modifyTime)
  732. {
  733. *modifyTime = fStat.st_mtime;
  734. }
  735. return true;
  736. }
  737. //-----------------------------------------------------------------------------
  738. bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime)
  739. {
  740. char pathName[MaxPath];
  741. // if it starts with cwd, we need to strip that off so that we can look for
  742. // the file in the pref dir
  743. char cwd[MaxPath];
  744. getcwd(cwd, MaxPath);
  745. if (dStrstr(filePath, cwd) == filePath)
  746. filePath = filePath + dStrlen(cwd) + 1;
  747. // if its relative, first look in the pref dir
  748. if (filePath[0] != '/' && filePath[0] != '\\')
  749. {
  750. MungePath(pathName, MaxPath, filePath, GetPrefDir());
  751. if (GetFileTimes(pathName, createTime, modifyTime))
  752. return true;
  753. }
  754. // here if the path is absolute or not in the pref dir
  755. MungePath(pathName, MaxPath, filePath, cwd);
  756. return GetFileTimes(pathName, createTime, modifyTime);
  757. }
  758. //-----------------------------------------------------------------------------
  759. bool Platform::createPath(const char *file)
  760. {
  761. char pathbuf[MaxPath];
  762. const char *dir;
  763. pathbuf[0] = 0;
  764. U32 pathLen = 0;
  765. // all paths should be created in home directory
  766. char prefPathName[MaxPath];
  767. MungePath(prefPathName, MaxPath, file, GetPrefDir());
  768. file = prefPathName;
  769. // does the directory exist already?
  770. if (DirExists(prefPathName, true)) // true means that the path is a filepath
  771. return true;
  772. while((dir = dStrchr(file, '/')) != NULL)
  773. {
  774. dStrncpy(pathbuf + pathLen, file, dir - file);
  775. pathbuf[pathLen + dir-file] = 0;
  776. bool ret = mkdir(pathbuf, 0700);
  777. pathLen += dir - file;
  778. pathbuf[pathLen++] = '/';
  779. file = dir + 1;
  780. }
  781. return true;
  782. }
  783. // JMQ: Platform:cdFileExists in unimplemented
  784. //------------------------------------------------------------------------------
  785. // bool Platform::cdFileExists(const char *filePath, const char *volumeName,
  786. // S32 serialNum)
  787. // {
  788. // }
  789. //-----------------------------------------------------------------------------
  790. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo> &fileVector, int depth)
  791. {
  792. const char* pattern = "*";
  793. // if it is not absolute, dump the pref dir first
  794. if (path[0] != '/' && path[0] != '\\')
  795. {
  796. char prefPathName[MaxPath];
  797. MungePath(prefPathName, MaxPath, path, GetPrefDir());
  798. RecurseDumpPath(prefPathName, path, pattern, fileVector, 0, depth);
  799. }
  800. // munge the requested path and dump it
  801. char mungedPath[MaxPath];
  802. char cwd[MaxPath];
  803. getcwd(cwd, MaxPath);
  804. MungePath(mungedPath, MaxPath, path, cwd);
  805. return RecurseDumpPath(mungedPath, path, pattern, fileVector, 0, depth);
  806. }
  807. //-----------------------------------------------------------------------------
  808. StringTableEntry Platform::getCurrentDirectory()
  809. {
  810. char cwd_buf[2048];
  811. getcwd(cwd_buf, 2047);
  812. return StringTable->insert(cwd_buf);
  813. }
  814. //-----------------------------------------------------------------------------
  815. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  816. {
  817. if (Platform::getWebDeployment())
  818. return true;
  819. TempAlloc< UTF8 > buf( dStrlen( newDir ) + 2 );
  820. dStrcpy( buf, newDir, buf.size );
  821. ForwardSlash( buf );
  822. return chdir( buf ) == 0;
  823. }
  824. //-----------------------------------------------------------------------------
  825. const char *Platform::getUserDataDirectory()
  826. {
  827. return StringTable->insert( GetPrefDir() );
  828. }
  829. //-----------------------------------------------------------------------------
  830. StringTableEntry Platform::getUserHomeDirectory()
  831. {
  832. char *home = getenv( "HOME" );
  833. return StringTable->insert( home );
  834. }
  835. StringTableEntry Platform::getExecutablePath()
  836. {
  837. if( !sBinPathName[0] )
  838. {
  839. const char *cpath;
  840. if( (cpath = torque_getexecutablepath()) )
  841. {
  842. dStrncpy(sBinPathName, cpath, sizeof(sBinPathName));
  843. chdir(sBinPathName);
  844. }
  845. else
  846. {
  847. getcwd(sBinPathName, sizeof(sBinPathName)-1);
  848. }
  849. }
  850. return StringTable->insert(sBinPathName);
  851. }
  852. //-----------------------------------------------------------------------------
  853. bool Platform::isFile(const char *pFilePath)
  854. {
  855. if (!pFilePath || !*pFilePath)
  856. return false;
  857. // Get file info
  858. struct stat fStat;
  859. if (stat(pFilePath, &fStat) < 0)
  860. {
  861. // Since file does not exist on disk see if it exists in a zip file loaded
  862. return Torque::FS::IsFile(pFilePath);
  863. }
  864. // if the file is a "regular file" then true
  865. if ( (fStat.st_mode & S_IFMT) == S_IFREG)
  866. return true;
  867. // must be some other file (directory, device, etc.)
  868. return false;
  869. }
  870. //-----------------------------------------------------------------------------
  871. S32 Platform::getFileSize(const char *pFilePath)
  872. {
  873. if (!pFilePath || !*pFilePath)
  874. return -1;
  875. // Get the file info
  876. struct stat fStat;
  877. if (stat(pFilePath, &fStat) < 0)
  878. return -1;
  879. // if the file is a "regular file" then return the size
  880. if ( (fStat.st_mode & S_IFMT) == S_IFREG)
  881. return fStat.st_size;
  882. // Must be something else or we can't read the file.
  883. return -1;
  884. }
  885. //-----------------------------------------------------------------------------
  886. bool Platform::isDirectory(const char *pDirPath)
  887. {
  888. if (!pDirPath || !*pDirPath)
  889. return false;
  890. // Get file info
  891. struct stat fStat;
  892. if (stat(pDirPath, &fStat) < 0)
  893. return false;
  894. // if the file is a Directory then true
  895. if ( (fStat.st_mode & S_IFMT) == S_IFDIR)
  896. return true;
  897. return false;
  898. }
  899. //-----------------------------------------------------------------------------
  900. bool Platform::isSubDirectory(const char *pParent, const char *pDir)
  901. {
  902. if (!pParent || !*pDir)
  903. return false;
  904. // this is somewhat of a brute force method but we need to be 100% sure
  905. // that the user cannot enter things like ../dir or /dir etc,...
  906. DIR *directory;
  907. directory = opendir(pParent);
  908. if (directory == NULL)
  909. return false;
  910. struct dirent *fEntry;
  911. fEntry = readdir(directory);
  912. if ( fEntry == NULL )
  913. {
  914. closedir(directory);
  915. return false;
  916. }
  917. do
  918. {
  919. char dirBuf[MaxPath];
  920. struct stat fStat;
  921. dSprintf(dirBuf, sizeof(dirBuf), "%s/%s", pParent, fEntry->d_name);
  922. if (stat(dirBuf, &fStat) < 0)
  923. continue;
  924. // if it is a directory...
  925. if ( (fStat.st_mode & S_IFMT) == S_IFDIR)
  926. {
  927. // and the names match
  928. if (dStrcmp(pDir, fEntry->d_name ) == 0)
  929. {
  930. // then we have a real sub directory
  931. closedir(directory);
  932. return true;
  933. }
  934. }
  935. } while( (fEntry = readdir(directory)) != NULL );
  936. closedir(directory);
  937. return false;
  938. }
  939. //-----------------------------------------------------------------------------
  940. // This is untested -- BJG
  941. bool Platform::fileTimeToString(FileTime * time, char * string, U32 strLen)
  942. {
  943. if(!time || !string)
  944. return(false);
  945. dSprintf(string, strLen, "%ld", *time);
  946. return(true);
  947. }
  948. bool Platform::stringToFileTime(const char * string, FileTime * time)
  949. {
  950. if(!time || !string)
  951. return(false);
  952. *time = dAtoi(string);
  953. return(true);
  954. }
  955. bool Platform::hasSubDirectory(const char *pPath)
  956. {
  957. if (!pPath)
  958. return false;
  959. struct dirent *d;
  960. DIR *dip;
  961. dip = opendir(pPath);
  962. if (dip == NULL)
  963. return false;
  964. while ((d = readdir(dip)))
  965. {
  966. bool isDir = false;
  967. if (d->d_type == DT_UNKNOWN)
  968. {
  969. char child [1024];
  970. if ((pPath[dStrlen(pPath) - 1] == '/'))
  971. dSprintf(child, 1024, "%s%s", pPath, d->d_name);
  972. else
  973. dSprintf(child, 1024, "%s/%s", pPath, d->d_name);
  974. isDir = Platform::isDirectory (child);
  975. }
  976. else if (d->d_type & DT_DIR)
  977. isDir = true;
  978. if( isDir )
  979. {
  980. // Skip the . and .. directories
  981. if (dStrcmp(d->d_name, ".") == 0 ||dStrcmp(d->d_name, "..") == 0)
  982. continue;
  983. if (Platform::isExcludedDirectory(d->d_name))
  984. continue;
  985. Platform::clearExcludedDirectories();
  986. closedir(dip);
  987. return true;
  988. }
  989. }
  990. closedir(dip);
  991. Platform::clearExcludedDirectories();
  992. return false;
  993. }
  994. bool Platform::fileDelete(const char * name)
  995. {
  996. return ModifyFile(name, DELETE);
  997. }
  998. static bool recurseDumpDirectories(const char *basePath, const char *subPath, Vector<StringTableEntry> &directoryVector, S32 currentDepth, S32 recurseDepth, bool noBasePath)
  999. {
  1000. char Path[1024];
  1001. DIR *dip;
  1002. struct dirent *d;
  1003. dsize_t trLen = basePath ? dStrlen(basePath) : 0;
  1004. dsize_t subtrLen = subPath ? dStrlen(subPath) : 0;
  1005. char trail = trLen > 0 ? basePath[trLen - 1] : '\0';
  1006. char subTrail = subtrLen > 0 ? subPath[subtrLen - 1] : '\0';
  1007. char subLead = subtrLen > 0 ? subPath[0] : '\0';
  1008. if (trail == '/')
  1009. {
  1010. if (subPath && (dStrncmp(subPath, "", 1) != 0))
  1011. {
  1012. if (subTrail == '/')
  1013. dSprintf(Path, 1024, "%s%s", basePath, subPath);
  1014. else
  1015. dSprintf(Path, 1024, "%s%s/", basePath, subPath);
  1016. }
  1017. else
  1018. dSprintf(Path, 1024, "%s", basePath);
  1019. }
  1020. else
  1021. {
  1022. if (subPath && (dStrncmp(subPath, "", 1) != 0))
  1023. {
  1024. if (subTrail == '/')
  1025. dSprintf(Path, 1024, "%s%s", basePath, subPath);
  1026. else
  1027. dSprintf(Path, 1024, "%s%s/", basePath, subPath);
  1028. }
  1029. else
  1030. dSprintf(Path, 1024, "%s/", basePath);
  1031. }
  1032. dip = opendir(Path);
  1033. if (dip == NULL)
  1034. return false;
  1035. //////////////////////////////////////////////////////////////////////////
  1036. // add path to our return list ( provided it is valid )
  1037. //////////////////////////////////////////////////////////////////////////
  1038. if (!Platform::isExcludedDirectory(subPath))
  1039. {
  1040. if (noBasePath)
  1041. {
  1042. // We have a path and it's not an empty string or an excluded directory
  1043. if ( (subPath && (dStrncmp (subPath, "", 1) != 0)) )
  1044. directoryVector.push_back(StringTable->insert(subPath));
  1045. }
  1046. else
  1047. {
  1048. if ( (subPath && (dStrncmp(subPath, "", 1) != 0)) )
  1049. {
  1050. char szPath[1024];
  1051. dMemset(szPath, 0, 1024);
  1052. if (trail == '/')
  1053. {
  1054. if ((basePath[dStrlen(basePath) - 1]) != '/')
  1055. dSprintf(szPath, 1024, "%s%s", basePath, &subPath[1]);
  1056. else
  1057. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  1058. }
  1059. else
  1060. {
  1061. if ((basePath[dStrlen(basePath) - 1]) != '/')
  1062. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  1063. else
  1064. dSprintf(szPath, 1024, "%s/%s", basePath, subPath);
  1065. }
  1066. directoryVector.push_back(StringTable->insert(szPath));
  1067. }
  1068. else
  1069. directoryVector.push_back(StringTable->insert(basePath));
  1070. }
  1071. }
  1072. //////////////////////////////////////////////////////////////////////////
  1073. // Iterate through and grab valid directories
  1074. //////////////////////////////////////////////////////////////////////////
  1075. while ((d = readdir(dip)))
  1076. {
  1077. bool isDir;
  1078. isDir = false;
  1079. if (d->d_type == DT_UNKNOWN)
  1080. {
  1081. char child [1024];
  1082. if (Path[dStrlen(Path) - 1] == '/')
  1083. dSprintf(child, 1024, "%s%s", Path, d->d_name);
  1084. else
  1085. dSprintf(child, 1024, "%s/%s", Path, d->d_name);
  1086. isDir = Platform::isDirectory (child);
  1087. }
  1088. else if (d->d_type & DT_DIR)
  1089. isDir = true;
  1090. if ( isDir )
  1091. {
  1092. if (dStrcmp(d->d_name, ".") == 0 ||
  1093. dStrcmp(d->d_name, "..") == 0)
  1094. continue;
  1095. if (Platform::isExcludedDirectory(d->d_name))
  1096. continue;
  1097. if ( (subPath && (dStrncmp(subPath, "", 1) != 0)) )
  1098. {
  1099. char child[1024];
  1100. if ((subPath[dStrlen(subPath) - 1] == '/'))
  1101. dSprintf(child, 1024, "%s%s", subPath, d->d_name);
  1102. else
  1103. dSprintf(child, 1024, "%s/%s", subPath, d->d_name);
  1104. if (currentDepth < recurseDepth || recurseDepth == -1 )
  1105. recurseDumpDirectories(basePath, child, directoryVector,
  1106. currentDepth + 1, recurseDepth,
  1107. noBasePath);
  1108. }
  1109. else
  1110. {
  1111. char child[1024];
  1112. if ( (basePath[dStrlen(basePath) - 1]) == '/')
  1113. dStrcpy (child, d->d_name, 1024);
  1114. else
  1115. dSprintf(child, 1024, "/%s", d->d_name);
  1116. if (currentDepth < recurseDepth || recurseDepth == -1)
  1117. recurseDumpDirectories(basePath, child, directoryVector,
  1118. currentDepth + 1, recurseDepth,
  1119. noBasePath);
  1120. }
  1121. }
  1122. }
  1123. closedir(dip);
  1124. return true;
  1125. }
  1126. bool Platform::dumpDirectories(const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath)
  1127. {
  1128. #ifdef TORQUE_POSIX_PATH_CASE_INSENSITIVE
  1129. dsize_t pathLength = dStrlen(path);
  1130. char* caseSensitivePath = new char[pathLength + 1];
  1131. // Load path into temporary buffer
  1132. dMemcpy(caseSensitivePath, path, pathLength);
  1133. caseSensitivePath[pathLength] = 0x00;
  1134. ResolvePathCaseInsensitive(caseSensitivePath, pathLength, false);
  1135. #else
  1136. const char* caseSensitivePath = path;
  1137. #endif
  1138. bool retVal = recurseDumpDirectories(caseSensitivePath, "", directoryVector, -1, depth, noBasePath);
  1139. clearExcludedDirectories();
  1140. #ifdef TORQUE_POSIX_PATH_CASE_INSENSITIVE
  1141. delete[] caseSensitivePath;
  1142. #endif
  1143. return retVal;
  1144. }
  1145. StringTableEntry Platform::getExecutableName()
  1146. {
  1147. return StringTable->insert(sBinName);
  1148. }
  1149. extern "C"
  1150. {
  1151. void setExePathName(const char* exePathName)
  1152. {
  1153. if (exePathName == NULL)
  1154. sBinPathName[0] = '\0';
  1155. else
  1156. dStrncpy(sBinPathName, exePathName, sizeof(sBinPathName));
  1157. // set the base exe name field
  1158. char *binName = dStrrchr(sBinPathName, '/');
  1159. if( !binName )
  1160. binName = sBinPathName;
  1161. else
  1162. *binName++ = '\0';
  1163. sBinName = binName;
  1164. }
  1165. }
  1166. #endif