AndroidFileio.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "platform/platform.h"
  23. #include "platformAndroid/platformAndroid.h"
  24. #include "platform/platformFileIO.h"
  25. #include "collection/vector.h"
  26. #include "string/stringTable.h"
  27. #include "string/stringUnit.h"
  28. #include "console/console.h"
  29. #include "debug/profiler.h"
  30. #include "io/resource/resourceManager.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <utime.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include <sys/time.h>
  40. #define MAX_MAC_PATH_LONG 2048
  41. //-----------------------------------------------------------------------------
  42. bool Platform::fileDelete(const char * name)
  43. {
  44. Con::warnf("Platform::FileDelete() - Not supported on android.");
  45. return false;
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool dFileTouch(const char *path)
  49. {
  50. Con::warnf("Platform::dFileTouch() - Not supported on android.");
  51. return true;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Constructors & Destructor
  55. //-----------------------------------------------------------------------------
  56. //-----------------------------------------------------------------------------
  57. // After construction, the currentStatus will be Closed and the capabilities
  58. // will be 0.
  59. //-----------------------------------------------------------------------------
  60. File::File()
  61. : currentStatus(Closed), capability(0)
  62. {
  63. buffer = NULL;
  64. size = 0;
  65. filePointer = 0;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // insert a copy constructor here... (currently disabled)
  69. //-----------------------------------------------------------------------------
  70. //-----------------------------------------------------------------------------
  71. // Destructor
  72. //-----------------------------------------------------------------------------
  73. File::~File()
  74. {
  75. if (buffer != NULL)
  76. close();
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  80. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  81. // true.
  82. //
  83. // Sets capability appropriate to the openMode.
  84. // Returns the currentStatus of the file.
  85. //-----------------------------------------------------------------------------
  86. File::Status File::open(const char *filename, const AccessMode openMode)
  87. {
  88. if (dStrlen(filename) > MAX_MAC_PATH_LONG)
  89. Con::warnf("File::open: Filename length is pretty long...");
  90. // Close the file if it was already open...
  91. if (currentStatus != Closed || buffer != NULL)
  92. close();
  93. // create the appropriate type of file...
  94. switch (openMode)
  95. {
  96. case Read:
  97. filePointer = 0;
  98. buffer = (U8*)_AndroidLoadFile(filename, &size);
  99. if (buffer == NULL) {
  100. currentStatus = UnknownError;
  101. capability = FileRead;
  102. return currentStatus;
  103. }
  104. break;
  105. case Write:
  106. //AssertFatal(false, "File::open: Write not supported on Android");
  107. //Platform::getUserDataDirectory()
  108. return currentStatus;
  109. case ReadWrite:
  110. //AssertFatal(false, "File::open: ReadWrite not supported on Android");
  111. return currentStatus;
  112. case WriteAppend:
  113. //AssertFatal(false, "File::open: WriteAppend not supported on Android");
  114. return currentStatus;
  115. default:
  116. AssertFatal(false, "File::open: bad access mode");
  117. }
  118. capability = FileRead;
  119. // must set the file status before setting the position.
  120. currentStatus = Ok;
  121. // success!
  122. return currentStatus;
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Get the current position of the file pointer.
  126. //-----------------------------------------------------------------------------
  127. U32 File::getPosition() const
  128. {
  129. AssertFatal(currentStatus != Closed , "File::getPosition: file closed");
  130. AssertFatal(buffer != NULL, "File::getPosition: invalid file buffer");
  131. return filePointer;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Set the position of the file pointer.
  135. // Absolute and relative positioning is supported via the absolutePos
  136. // parameter.
  137. //
  138. // If positioning absolutely, position MUST be positive - an IOError results if
  139. // position is negative.
  140. // Position can be negative if positioning relatively, however positioning
  141. // before the start of the file is an IOError.
  142. //
  143. // Returns the currentStatus of the file.
  144. //-----------------------------------------------------------------------------
  145. File::Status File::setPosition(S32 position, bool absolutePos)
  146. {
  147. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  148. AssertFatal(buffer != NULL, "File::setPosition: invalid file buffer");
  149. if (currentStatus != Ok && currentStatus != EOS )
  150. return currentStatus;
  151. U32 finalPos;
  152. if(absolutePos)
  153. {
  154. // absolute position
  155. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  156. // position beyond EOS is OK
  157. filePointer = position;
  158. finalPos = filePointer;
  159. }
  160. else
  161. {
  162. // relative position
  163. AssertFatal((getPosition() + position) >= 0, "File::setPosition: negative relative position");
  164. // position beyond EOS is OK
  165. filePointer += position;
  166. finalPos = filePointer;
  167. }
  168. // ftell returns -1 on error. set error status
  169. if (0xffffffff == finalPos)
  170. return setStatus();
  171. // success, at end of file
  172. else if (finalPos >= getSize())
  173. return currentStatus = EOS;
  174. // success!
  175. else
  176. return currentStatus = Ok;
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Get the size of the file in bytes.
  180. // It is an error to query the file size for a Closed file, or for one with an
  181. // error status.
  182. //-----------------------------------------------------------------------------
  183. U32 File::getSize() const
  184. {
  185. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  186. AssertFatal(buffer != NULL, "File::getSize: invalid file buffer");
  187. if (Ok == currentStatus || EOS == currentStatus)
  188. {
  189. return size;
  190. }
  191. return 0;
  192. }
  193. //-----------------------------------------------------------------------------
  194. // Flush the file.
  195. // It is an error to flush a read-only file.
  196. // Returns the currentStatus of the file.
  197. //-----------------------------------------------------------------------------
  198. File::Status File::flush()
  199. {
  200. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  201. AssertFatal(buffer != NULL, "File::flush: invalid file buffer");
  202. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  203. return setStatus();
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Close the File.
  207. //
  208. // Returns the currentStatus
  209. //-----------------------------------------------------------------------------
  210. File::Status File::close()
  211. {
  212. // check if it's already closed...
  213. if (Closed == currentStatus)
  214. return currentStatus;
  215. // it's not, so close it...
  216. if (buffer != NULL)
  217. {
  218. delete[] buffer;
  219. buffer = NULL;
  220. size = 0;
  221. filePointer = 0;
  222. }
  223. return currentStatus = Closed;
  224. }
  225. //-----------------------------------------------------------------------------
  226. // Self-explanatory.
  227. //-----------------------------------------------------------------------------
  228. File::Status File::getStatus() const
  229. {
  230. return currentStatus;
  231. }
  232. //-----------------------------------------------------------------------------
  233. // Sets and returns the currentStatus when an error has been encountered.
  234. //-----------------------------------------------------------------------------
  235. File::Status File::setStatus()
  236. {
  237. currentStatus = UnknownError;
  238. return currentStatus;
  239. }
  240. //-----------------------------------------------------------------------------
  241. // Sets and returns the currentStatus to status.
  242. //-----------------------------------------------------------------------------
  243. File::Status File::setStatus(File::Status status)
  244. {
  245. return currentStatus = status;
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Read from a file.
  249. // The number of bytes to read is passed in size, the data is returned in src.
  250. // The number of bytes read is available in bytesRead if a non-Null pointer is
  251. // provided.
  252. //-----------------------------------------------------------------------------
  253. File::Status File::read(U32 _size, char *dst, U32 *bytesRead)
  254. {
  255. AssertFatal(Closed != currentStatus, "File::read: file closed");
  256. AssertFatal(buffer != NULL, "File::read: invalid file buffer");
  257. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  258. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  259. AssertWarn(0 != size, "File::read: size of zero");
  260. if (Ok != currentStatus || 0 == size)
  261. return currentStatus;
  262. // read from stream
  263. U32 nBytes = 0;
  264. if ((size-filePointer) > (_size))
  265. {
  266. memcpy(dst, buffer+filePointer, _size);
  267. nBytes = _size;
  268. }
  269. else if (size-filePointer <= 0)
  270. {
  271. nBytes = 0;
  272. }
  273. else
  274. {
  275. memcpy(dst, buffer+filePointer, size-filePointer);
  276. nBytes = size-filePointer;
  277. }
  278. //Advanced the pointer
  279. filePointer += nBytes;
  280. // did we hit the end of the stream?
  281. if( nBytes != _size)
  282. currentStatus = EOS;
  283. // if bytesRead is a valid pointer, send number of bytes read there.
  284. if(bytesRead)
  285. *bytesRead = nBytes;
  286. // successfully read size bytes
  287. return currentStatus;
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Write to a file.
  291. // The number of bytes to write is passed in size, the data is passed in src.
  292. // The number of bytes written is available in bytesWritten if a non-Null
  293. // pointer is provided.
  294. //-----------------------------------------------------------------------------
  295. File::Status File::write(U32 size, const char *src, U32 *bytesWritten)
  296. {
  297. AssertFatal(0, "File::write: Not supported on Android.");
  298. return setStatus();
  299. }
  300. //-----------------------------------------------------------------------------
  301. // Self-explanatory.
  302. //-----------------------------------------------------------------------------
  303. bool File::hasCapability(Capability cap) const
  304. {
  305. return (0 != (U32(cap) & capability));
  306. }
  307. //-----------------------------------------------------------------------------
  308. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  309. {
  310. if(a > b)
  311. return 1;
  312. if(a < b)
  313. return -1;
  314. return 0;
  315. }
  316. //-----------------------------------------------------------------------------
  317. // either time param COULD be null.
  318. //-----------------------------------------------------------------------------
  319. bool Platform::getFileTimes(const char *path, FileTime *createTime, FileTime *modifyTime)
  320. {
  321. // MacOSX is NOT guaranteed to be running off a HFS volume,
  322. // and UNIX does not keep a record of a file's creation time anywhere.
  323. // So instead of creation time we return changed time,
  324. // just like the Linux platform impl does.
  325. if (!path || !*path)
  326. return false;
  327. Con::warnf("Platform::getFileTimes - Not supported on android.");
  328. return true;
  329. }
  330. //-----------------------------------------------------------------------------
  331. bool Platform::createPath(const char *file)
  332. {
  333. Con::warnf("Platform::createPath() - Not supported on android.");
  334. return false;
  335. }
  336. #pragma mark ---- Directories ----
  337. //-----------------------------------------------------------------------------
  338. StringTableEntry Platform::getCurrentDirectory()
  339. {
  340. // get the current directory, the one that would be opened if we did a fopen(".")
  341. char* cwd = getcwd(NULL, 0);
  342. StringTableEntry ret = StringTable->insert(cwd);
  343. free(cwd);
  344. return ret;
  345. }
  346. //-----------------------------------------------------------------------------
  347. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  348. {
  349. return (chdir(newDir) == 0);
  350. }
  351. //-----------------------------------------------------------------------------
  352. void Platform::openFolder(const char* path )
  353. {
  354. // TODO: users can still run applications by calling openfolder on an app bundle.
  355. // this may be a bad thing.
  356. if(!Platform::isDirectory(path))
  357. {
  358. Con::errorf(avar("Error: not a directory: %s",path));
  359. return;
  360. }
  361. const char* arg = avar("open '%s'", path);
  362. U32 ret = system(arg);
  363. if(ret != 0)
  364. Con::printf(strerror(errno));
  365. }
  366. static bool isMainDotCsPresent(char *dir)
  367. {
  368. char maincsbuf[MAX_MAC_PATH_LONG];
  369. const char *maincsname = "/main.cs";
  370. const U32 len = dStrlen(dir) + dStrlen(maincsname)+1;
  371. AssertISV(len < MAX_MAC_PATH_LONG, "Sorry, path is too long, I can't run from this folder.");
  372. dSprintf(maincsbuf,MAX_MAC_PATH_LONG,"%s%s", dir, maincsname);
  373. return Platform::isFile(maincsbuf);
  374. }
  375. //-----------------------------------------------------------------------------
  376. /// Finds and sets the current working directory.
  377. /// Torque tries to automatically detect whether you have placed the game files
  378. /// inside or outside the application's bundle. It checks for the presence of
  379. /// the file 'main.cs'. If it finds it, Torque will assume that the other game
  380. /// files are there too. If Torque does not see 'main.cs' inside its bundle, it
  381. /// will assume the files are outside the bundle.
  382. /// Since you probably don't want to copy the game files into the app every time
  383. /// you build, you will want to leave them outside the bundle for development.
  384. ///
  385. /// Android reads all assets out of compressed bundle so we dont realy have an executable path
  386. StringTableEntry Platform::getExecutablePath()
  387. {
  388. if(platState.mainDotCsDir)
  389. return platState.mainDotCsDir;
  390. char* ret = NULL;
  391. if(StringTable)
  392. platState.mainDotCsDir = StringTable->insert(".");
  393. else
  394. ret = dStrdup(".");
  395. return ret ? ret : platState.mainDotCsDir;
  396. }
  397. //-----------------------------------------------------------------------------
  398. StringTableEntry Platform::getExecutableName()
  399. {
  400. //TODO: does this need anything further?
  401. return StringTable->insert("Torque2D");
  402. }
  403. //-----------------------------------------------------------------------------
  404. bool Platform::isFile(const char *path)
  405. {
  406. if (!path || !*path)
  407. return false;
  408. return android_IsFile(path);
  409. }
  410. //-----------------------------------------------------------------------------
  411. bool Platform::isDirectory(const char *path)
  412. {
  413. if (!path || !*path)
  414. return false;
  415. return android_IsDir(path);
  416. }
  417. S32 Platform::getFileSize(const char* pFilePath)
  418. {
  419. if (!pFilePath || !*pFilePath)
  420. return 0;
  421. return android_GetFileSize(pFilePath);
  422. }
  423. //-----------------------------------------------------------------------------
  424. bool Platform::isSubDirectory(const char *pathParent, const char *pathSub)
  425. {
  426. char fullpath[MAX_MAC_PATH_LONG];
  427. dStrcpyl(fullpath, MAX_MAC_PATH_LONG, pathParent, "/", pathSub, NULL);
  428. return isDirectory((const char *)fullpath);
  429. }
  430. void getDirectoryName(const char* path, char* name)
  431. {
  432. int cnt = StringUnit::getUnitCount(path, "/");
  433. strcpy(name,StringUnit::getUnit(path, cnt-1, "/"));
  434. }
  435. //-----------------------------------------------------------------------------
  436. // utility for platform::hasSubDirectory() and platform::dumpDirectories()
  437. // ensures that the entry is a directory, and isnt on the ignore lists.
  438. inline bool isGoodDirectory(const char* path)
  439. {
  440. char name[80];
  441. getDirectoryName(path, name);
  442. return (Platform::isDirectory(path) // is a dir
  443. && dStrcmp(name,".") != 0 // not here
  444. && dStrcmp(name,"..") != 0 // not parent
  445. && !Platform::isExcludedDirectory(name)); // not excluded
  446. }
  447. //-----------------------------------------------------------------------------
  448. bool Platform::hasSubDirectory(const char *path)
  449. {
  450. android_InitDirList(path);
  451. char dir[80];
  452. char pdir[255];
  453. strcpy(dir,"");
  454. android_GetNextDir(path, dir);
  455. while(strcmp(dir,"") != 0)
  456. {
  457. sprintf(pdir, "%s/%s", path, dir);
  458. if (isGoodDirectory(pdir))
  459. return true;
  460. android_GetNextDir(path, dir);
  461. }
  462. return false;
  463. }
  464. //-----------------------------------------------------------------------------
  465. bool recurseDumpDirectories(const char *basePath, const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath)
  466. {
  467. char aPath[80];
  468. if (basePath[0] == '/')
  469. {
  470. strcpy(aPath, basePath+1);
  471. }
  472. else
  473. {
  474. strcpy(aPath, basePath);
  475. }
  476. const U32 len = dStrlen(aPath) + dStrlen(path) + 2;
  477. char pathbuf[len];
  478. // construct the file path
  479. if (strcmp(path,"") != 0)
  480. dSprintf(pathbuf, len, "%s/%s", aPath, path);
  481. else
  482. strcpy(pathbuf, aPath);
  483. // be sure it opens.
  484. android_InitDirList(pathbuf);
  485. // look inside the current directory
  486. char dir[80];
  487. char pdir[255];
  488. strcpy(dir,"");
  489. android_GetNextDir(pathbuf, dir);
  490. while(strcmp(dir,"") != 0)
  491. {
  492. sprintf(pdir, "%s/%s", pathbuf, dir);
  493. if (!isGoodDirectory(pdir))
  494. return false;
  495. // construct the new path string, we'll need this below.
  496. const U32 newpathlen = dStrlen(path) + dStrlen(dir) + 2;
  497. char newpath[newpathlen];
  498. if(dStrlen(path) > 0)
  499. {
  500. dSprintf(newpath, newpathlen, "%s/%s", path, dir);
  501. }
  502. else
  503. {
  504. dSprintf(newpath, newpathlen, "%s", dir);
  505. }
  506. // we have a directory, add it to the list.
  507. if( noBasePath )
  508. {
  509. directoryVector.push_back(StringTable->insert(newpath));
  510. }
  511. else
  512. {
  513. const U32 fullpathlen = dStrlen(aPath) + dStrlen(newpath) + 2;
  514. char fullpath[fullpathlen];
  515. dSprintf(fullpath, fullpathlen, "%s/%s",aPath,newpath);
  516. directoryVector.push_back(StringTable->insert(fullpath));
  517. }
  518. // and recurse into it, unless we've run out of depth
  519. if( depth != 0) // passing a val of -1 as the recurse depth means go forever
  520. recurseDumpDirectories(aPath, newpath, directoryVector, depth-1, noBasePath);
  521. android_GetNextDir(pathbuf, dir);
  522. }
  523. return true;
  524. }
  525. //-----------------------------------------------------------------------------
  526. bool Platform::dumpDirectories(const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath)
  527. {
  528. PROFILE_START(dumpDirectories);
  529. ResourceManager->initExcludedDirectories();
  530. const S32 len = dStrlen(path)+1;
  531. char newpath[len];
  532. if (path[0] == '/')
  533. {
  534. dSprintf(newpath, len-1, "%s", path+1);
  535. }
  536. else
  537. {
  538. dSprintf(newpath, len, "%s", path);
  539. }
  540. if(newpath[len - 1] == '/')
  541. newpath[len - 1] = '\0'; // cut off the trailing slash, if there is one
  542. // Insert base path to follow what Windows does.
  543. if ( !noBasePath )
  544. directoryVector.push_back(StringTable->insert(newpath));
  545. bool ret = recurseDumpDirectories(newpath, "", directoryVector, depth, noBasePath);
  546. PROFILE_END();
  547. return ret;
  548. }
  549. //-----------------------------------------------------------------------------
  550. static bool recurseDumpPath(const char* curPath, Vector<Platform::FileInfo>& fileVector, U32 depth)
  551. {
  552. android_InitDirList(curPath);
  553. // look inside the current directory
  554. char dir[80];
  555. char file[80];
  556. strcpy(dir,"");
  557. strcpy(file,"");
  558. android_GetNextDir(curPath, dir);
  559. android_GetNextFile(curPath, file);
  560. while(strcmp(file,"") != 0)
  561. {
  562. // construct the full file path. we need this to get the file size and to recurse
  563. const U32 len = dStrlen(curPath) + dStrlen(file) + 2;
  564. char pathbuf[len];
  565. dSprintf( pathbuf, len, "%s/%s", curPath, file);
  566. //add the file entry to the list
  567. // unlike recurseDumpDirectories(), we need to return more complex info here.
  568. //<Mat> commented this out in case we ever want a dir file printout again
  569. //printf( "File Name: %s ", entry->d_name );
  570. const U32 fileSize = Platform::getFileSize(pathbuf);
  571. fileVector.increment();
  572. Platform::FileInfo& rInfo = fileVector.last();
  573. rInfo.pFullPath = StringTable->insert(curPath);
  574. rInfo.pFileName = StringTable->insert(file);
  575. rInfo.fileSize = fileSize;
  576. android_GetNextFile(curPath, file);
  577. }
  578. while(strcmp(dir,"") != 0)
  579. {
  580. // construct the full file path. we need this to get the file size and to recurse
  581. const U32 len = dStrlen(curPath) + dStrlen(dir) + 2;
  582. char pathbuf[len];
  583. if (strcmp(curPath,"") == 0)
  584. dSprintf( pathbuf, len, "%s", dir);
  585. else
  586. dSprintf( pathbuf, len, "%s/%s", curPath, dir);
  587. if( depth == 0)
  588. {
  589. android_GetNextDir(curPath, dir);
  590. continue;
  591. }
  592. // filter out dirs we dont want.
  593. if( !isGoodDirectory(pathbuf) )
  594. {
  595. android_GetNextDir(curPath, dir);
  596. continue;
  597. }
  598. // recurse into the dir
  599. recurseDumpPath( pathbuf, fileVector, depth-1);
  600. android_GetNextDir(curPath, dir);
  601. }
  602. return true;
  603. }
  604. //-----------------------------------------------------------------------------
  605. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo>& fileVector, S32 depth)
  606. {
  607. PROFILE_START(dumpPath);
  608. char apath[80];
  609. if (path[0] == '/')
  610. {
  611. strcpy(apath, path+1);
  612. }
  613. else
  614. {
  615. strcpy(apath, path);
  616. }
  617. const S32 len = dStrlen(apath) + 1;
  618. char newpath[len];
  619. dSprintf(newpath, len, "%s", apath);
  620. if(newpath[len - 2] == '/')
  621. newpath[len - 2] = '\0'; // cut off the trailing slash, if there is one
  622. bool ret = recurseDumpPath( newpath, fileVector, depth);
  623. PROFILE_END();
  624. return ret;
  625. }
  626. //-----------------------------------------------------------------------------
  627. #if defined(TORQUE_DEBUG)
  628. ConsoleFunction(testHasSubdir,void,2,2,"tests platform::hasSubDirectory") {
  629. Con::printf("testing %s",argv[1]);
  630. Platform::addExcludedDirectory(".svn");
  631. if(Platform::hasSubDirectory(argv[1]))
  632. Con::printf(" has subdir");
  633. else
  634. Con::printf(" does not have subdir");
  635. }
  636. ConsoleFunction(testDumpDirectories,void,4,4,"testDumpDirectories('path', int depth, bool noBasePath)") {
  637. Vector<StringTableEntry> paths;
  638. S32 depth = dAtoi(argv[2]);
  639. Platform::addExcludedDirectory(".svn");
  640. Platform::dumpDirectories(argv[1],paths,dAtoi(argv[2]),dAtob(argv[3]));
  641. Con::printf("Dumping directories starting from %s with depth %i", argv[1],depth);
  642. for(Vector<StringTableEntry>::iterator itr = paths.begin(); itr != paths.end(); itr++) {
  643. Con::printf(*itr);
  644. }
  645. }
  646. ConsoleFunction(testDumpPaths, void, 3, 3, "testDumpPaths('path', int depth)")
  647. {
  648. Vector<Platform::FileInfo> files;
  649. S32 depth = dAtoi(argv[2]);
  650. Platform::addExcludedDirectory(".svn");
  651. Platform::dumpPath(argv[1], files, depth);
  652. for(Vector<Platform::FileInfo>::iterator itr = files.begin(); itr != files.end(); itr++) {
  653. Con::printf("%s/%s",itr->pFullPath, itr->pFileName);
  654. }
  655. }
  656. //-----------------------------------------------------------------------------
  657. ConsoleFunction(testFileTouch, bool , 2,2, "testFileTouch('path')")
  658. {
  659. return dFileTouch(argv[1]);
  660. }
  661. ConsoleFunction(testGetFileTimes, bool, 2,2, "testGetFileTimes('path')")
  662. {
  663. FileTime create, modify;
  664. bool ok;
  665. ok = Platform::getFileTimes(argv[1],&create, &modify);
  666. Con::printf("%s Platform::getFileTimes %i, %i", ok ? "+OK" : "-FAIL", create, modify);
  667. return ok;
  668. }
  669. #endif