x86UNIXFileio.cpp 42 KB

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