2
0

winFileio.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "platformWin32/platformWin32.h"
  24. #include "platform/platformFileIO.h"
  25. #include "collection/vector.h"
  26. #include "string/stringTable.h"
  27. #include "console/console.h"
  28. #include "io/resource/resourceManager.h"
  29. #include "string/unicode.h"
  30. // Microsoft VC++ has this POSIX header in the wrong directory
  31. #if defined(TORQUE_COMPILER_VISUALC)
  32. #include <sys/utime.h>
  33. #elif defined (TORQUE_COMPILER_GCC)
  34. #include <time.h>
  35. #include <sys/utime.h>
  36. #else
  37. #include <utime.h>
  38. #endif
  39. //------------------------------------------------------------------------------
  40. enum DriveType
  41. {
  42. DRIVETYPE_FIXED = 0,
  43. DRIVETYPE_REMOVABLE = 1,
  44. DRIVETYPE_REMOTE = 2,
  45. DRIVETYPE_CDROM = 3,
  46. DRIVETYPE_RAMDISK = 4,
  47. DRIVETYPE_UNKNOWN = 5
  48. };
  49. //-------------------------------------- Helper Functions
  50. static void forwardslash(char *str)
  51. {
  52. while(*str)
  53. {
  54. if(*str == '\\')
  55. *str = '/';
  56. str++;
  57. }
  58. }
  59. static void backslash(char *str)
  60. {
  61. while(*str)
  62. {
  63. if(*str == '/')
  64. *str = '\\';
  65. str++;
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. bool Platform::fileDelete(const char * name)
  70. {
  71. if(!name || (dStrlen(name) >= MAX_PATH))
  72. return(false);
  73. //return(::DeleteFile(name));
  74. if(Platform::isFile(name))
  75. return(remove(name) == 0);
  76. else
  77. return ::RemoveDirectoryA(name) != 0;
  78. }
  79. bool Platform::fileRename(const char *oldName, const char *newName)
  80. {
  81. if(oldName == NULL || newName == NULL || dStrlen(oldName) >= MAX_PATH || dStrlen(newName) > MAX_PATH)
  82. return false;
  83. char oldf[MAX_PATH], newf[MAX_PATH];
  84. dStrcpy(oldf, oldName);
  85. dStrcpy(newf, newName);
  86. backslash(oldf);
  87. backslash(newf);
  88. return rename(oldf, newf) == 0;
  89. }
  90. bool Platform::fileTouch(const char * name)
  91. {
  92. // change the modified time to the current time (0byte WriteFile fails!)
  93. return(utime(name, 0) != -1);
  94. };
  95. static S32 QSORT_CALLBACK pathCompare(const void* a,const void* b)
  96. {
  97. const char *aa = *((const char **)a), *ptrA;
  98. const char *bb = *((const char **)b), *ptrB;
  99. while(aa && bb && *aa && *bb)
  100. {
  101. ptrA = dStrchr(aa, '/');
  102. ptrB = dStrchr(bb, '/');
  103. if(ptrA && ptrB)
  104. {
  105. ++ptrA; ++ptrB;
  106. S32 len = ptrA - aa > ptrB - bb ? ptrA - aa : ptrB - bb;
  107. S32 ret = dStrnicmp(aa, bb, len);
  108. if(ret != 0)
  109. return ret;
  110. }
  111. aa = ptrA;
  112. bb = ptrB;
  113. }
  114. if(aa && ! bb)
  115. return -1;
  116. if(bb && ! aa)
  117. return 1;
  118. if(aa && bb)
  119. return dStricmp(aa, bb);
  120. return 0;
  121. }
  122. bool Platform::pathCopy(const char *fromName, const char *toName, bool nooverwrite)
  123. {
  124. if(!fromName || (dStrlen(fromName) >= MAX_PATH) ||
  125. !toName || (dStrlen(toName) >= MAX_PATH))
  126. return(false);
  127. static char filebuf[2048];
  128. dStrcpy(filebuf, fromName);
  129. backslash(filebuf);
  130. fromName = filebuf;
  131. static char filebuf2[2048];
  132. dStrcpy(filebuf2, toName);
  133. backslash(filebuf2);
  134. toName = filebuf2;
  135. // Copy File
  136. if (Platform::isFile(fromName))
  137. {
  138. #ifdef UNICODE
  139. UTF16 f[1024], t[1024];
  140. convertUTF8toUTF16((UTF8 *)fromName, f, sizeof(f));
  141. convertUTF8toUTF16((UTF8 *)toName, t, sizeof(t));
  142. #else
  143. char *f = (char*)fromName;
  144. char *t = (char*)toName;
  145. #endif
  146. if(::CopyFile( f, t, nooverwrite))
  147. {
  148. return true;
  149. }
  150. return false;
  151. }
  152. // Copy Path
  153. else if (Platform::isDirectory(fromName))
  154. {
  155. // If the destination path exists and we don't want to overwrite, return.
  156. if ((Platform::isDirectory(toName) || Platform::isFile(toName)) && nooverwrite)
  157. return false;
  158. Vector<StringTableEntry> directoryInfo;
  159. Platform::dumpDirectories(fromName, directoryInfo, -1);
  160. ResourceManager->initExcludedDirectories();
  161. Vector<Platform::FileInfo> fileInfo;
  162. Platform::dumpPath(fromName, fileInfo);
  163. Platform::clearExcludedDirectories();
  164. // Create all the directories.
  165. for (S32 i = 0; i < directoryInfo.size(); i++)
  166. {
  167. const char* from = directoryInfo[i];
  168. char to[1024];
  169. Platform::makeFullPathName(from + dStrlen(fromName) + (dStricmp(from, fromName) ? 1 : 0), to, sizeof(to), toName);
  170. if(*(to + dStrlen(to) - 1) != '/')
  171. dStrcat(to, "/");
  172. forwardslash(to);
  173. if (!Platform::createPath(to))
  174. {
  175. // New directory should be deleted here.
  176. return false;
  177. }
  178. }
  179. for (S32 i = 0; i < fileInfo.size(); i++)
  180. {
  181. char from[1024];
  182. dSprintf(from, 1024, "%s/%s", fileInfo[i].pFullPath, fileInfo[i].pFileName);
  183. char to[1024];
  184. Platform::makeFullPathName(fileInfo[i].pFullPath + dStrlen(fromName) + (dStricmp(fileInfo[i].pFullPath, fromName) ? 1 : 0), to, sizeof(to), toName);
  185. dStrcat(to, "/");
  186. dStrcat(to, fileInfo[i].pFileName);
  187. backslash(from);
  188. backslash(to);
  189. #ifdef UNICODE
  190. UTF16 f[1024], t[1024];
  191. convertUTF8toUTF16((UTF8 *)from, f, sizeof(f));
  192. convertUTF8toUTF16((UTF8 *)to, t, sizeof(t));
  193. #else
  194. char *f = (char*)from;
  195. char *t = (char*)to;
  196. #endif
  197. if (!::CopyFile(f, t, nooverwrite))
  198. {
  199. // New directory should be deleted here.
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. return false;
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Constructors & Destructor
  209. //-----------------------------------------------------------------------------
  210. //-----------------------------------------------------------------------------
  211. // After construction, the currentStatus will be Closed and the capabilities
  212. // will be 0.
  213. //-----------------------------------------------------------------------------
  214. File::File()
  215. : currentStatus(Closed), capability(0)
  216. {
  217. AssertFatal(sizeof(HANDLE) == sizeof(void *), "File::File: cannot cast void* to HANDLE");
  218. handle = (void *)INVALID_HANDLE_VALUE;
  219. }
  220. //-----------------------------------------------------------------------------
  221. // insert a copy constructor here... (currently disabled)
  222. //-----------------------------------------------------------------------------
  223. //-----------------------------------------------------------------------------
  224. // Destructor
  225. //-----------------------------------------------------------------------------
  226. File::~File()
  227. {
  228. close();
  229. handle = (void *)INVALID_HANDLE_VALUE;
  230. }
  231. //-----------------------------------------------------------------------------
  232. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  233. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  234. // true.
  235. //
  236. // Sets capability appropriate to the openMode.
  237. // Returns the currentStatus of the file.
  238. //-----------------------------------------------------------------------------
  239. File::Status File::open(const char *filename, const AccessMode openMode)
  240. {
  241. static char filebuf[2048];
  242. dStrcpy(filebuf, filename);
  243. backslash(filebuf);
  244. #ifdef UNICODE
  245. UTF16 fname[2048];
  246. convertUTF8toUTF16((UTF8 *)filebuf, fname, sizeof(fname));
  247. #else
  248. char *fname;
  249. fname = filebuf;
  250. #endif
  251. AssertFatal(NULL != fname, "File::open: NULL fname");
  252. AssertWarn(INVALID_HANDLE_VALUE == (HANDLE)handle, "File::open: handle already valid");
  253. // Close the file if it was already open...
  254. if (Closed != currentStatus)
  255. close();
  256. // create the appropriate type of file...
  257. switch (openMode)
  258. {
  259. case Read:
  260. handle = (void *)CreateFile(fname,
  261. GENERIC_READ,
  262. FILE_SHARE_READ,
  263. NULL,
  264. OPEN_EXISTING,
  265. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  266. NULL);
  267. break;
  268. case Write:
  269. handle = (void *)CreateFile(fname,
  270. GENERIC_WRITE,
  271. 0,
  272. NULL,
  273. CREATE_ALWAYS,
  274. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  275. NULL);
  276. break;
  277. case ReadWrite:
  278. handle = (void *)CreateFile(fname,
  279. GENERIC_WRITE | GENERIC_READ,
  280. 0,
  281. NULL,
  282. OPEN_ALWAYS,
  283. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  284. NULL);
  285. break;
  286. case WriteAppend:
  287. handle = (void *)CreateFile(fname,
  288. GENERIC_WRITE,
  289. 0,
  290. NULL,
  291. OPEN_ALWAYS,
  292. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  293. NULL);
  294. break;
  295. default:
  296. AssertFatal(false, "File::open: bad access mode"); // impossible
  297. }
  298. if (INVALID_HANDLE_VALUE == (HANDLE)handle) // handle not created successfully
  299. {
  300. return setStatus();
  301. }
  302. else
  303. {
  304. // successfully created file, so set the file capabilities...
  305. switch (openMode)
  306. {
  307. case Read:
  308. capability = U32(FileRead);
  309. break;
  310. case Write:
  311. case WriteAppend:
  312. capability = U32(FileWrite);
  313. break;
  314. case ReadWrite:
  315. capability = U32(FileRead) |
  316. U32(FileWrite);
  317. break;
  318. default:
  319. AssertFatal(false, "File::open: bad access mode");
  320. }
  321. return currentStatus = Ok; // success!
  322. }
  323. }
  324. //-----------------------------------------------------------------------------
  325. // Get the current position of the file pointer.
  326. //-----------------------------------------------------------------------------
  327. U32 File::getPosition() const
  328. {
  329. AssertFatal(Closed != currentStatus, "File::getPosition: file closed");
  330. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getPosition: invalid file handle");
  331. return SetFilePointer((HANDLE)handle,
  332. 0, // how far to move
  333. NULL, // pointer to high word
  334. FILE_CURRENT); // from what point
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Set the position of the file pointer.
  338. // Absolute and relative positioning is supported via the absolutePos
  339. // parameter.
  340. //
  341. // If positioning absolutely, position MUST be positive - an IOError results if
  342. // position is negative.
  343. // Position can be negative if positioning relatively, however positioning
  344. // before the start of the file is an IOError.
  345. //
  346. // Returns the currentStatus of the file.
  347. //-----------------------------------------------------------------------------
  348. File::Status File::setPosition(S32 position, bool absolutePos)
  349. {
  350. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  351. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::setPosition: invalid file handle");
  352. if (Ok != currentStatus && EOS != currentStatus)
  353. return currentStatus;
  354. U32 finalPos;
  355. switch (absolutePos)
  356. {
  357. case true: // absolute position
  358. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  359. // position beyond EOS is OK
  360. finalPos = SetFilePointer((HANDLE)handle,
  361. position,
  362. NULL,
  363. FILE_BEGIN);
  364. break;
  365. case false: // relative position
  366. AssertFatal((getPosition() >= (U32)abs(position) && 0 > position) || 0 <= position, "File::setPosition: negative relative position");
  367. // position beyond EOS is OK
  368. finalPos = SetFilePointer((HANDLE)handle,
  369. position,
  370. NULL,
  371. FILE_CURRENT);
  372. }
  373. if (0xffffffff == finalPos)
  374. return setStatus(); // unsuccessful
  375. else if (finalPos >= getSize())
  376. return currentStatus = EOS; // success, at end of file
  377. else
  378. return currentStatus = Ok; // success!
  379. }
  380. //-----------------------------------------------------------------------------
  381. // Get the size of the file in bytes.
  382. // It is an error to query the file size for a Closed file, or for one with an
  383. // error status.
  384. //-----------------------------------------------------------------------------
  385. U32 File::getSize() const
  386. {
  387. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  388. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getSize: invalid file handle");
  389. if (Ok == currentStatus || EOS == currentStatus)
  390. {
  391. DWORD high;
  392. return GetFileSize((HANDLE)handle, &high); // success!
  393. }
  394. else
  395. return 0; // unsuccessful
  396. }
  397. //-----------------------------------------------------------------------------
  398. // Flush the file.
  399. // It is an error to flush a read-only file.
  400. // Returns the currentStatus of the file.
  401. //-----------------------------------------------------------------------------
  402. File::Status File::flush()
  403. {
  404. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  405. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::flush: invalid file handle");
  406. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  407. if (0 != FlushFileBuffers((HANDLE)handle))
  408. return setStatus(); // unsuccessful
  409. else
  410. return currentStatus = Ok; // success!
  411. }
  412. //-----------------------------------------------------------------------------
  413. // Close the File.
  414. //
  415. // Returns the currentStatus
  416. //-----------------------------------------------------------------------------
  417. File::Status File::close()
  418. {
  419. // check if it's already closed...
  420. if (Closed == currentStatus)
  421. return currentStatus;
  422. // it's not, so close it...
  423. if (INVALID_HANDLE_VALUE != (HANDLE)handle)
  424. {
  425. if (0 == CloseHandle((HANDLE)handle))
  426. return setStatus(); // unsuccessful
  427. }
  428. handle = (void *)INVALID_HANDLE_VALUE;
  429. return currentStatus = Closed;
  430. }
  431. //-----------------------------------------------------------------------------
  432. // Self-explanatory.
  433. //-----------------------------------------------------------------------------
  434. File::Status File::getStatus() const
  435. {
  436. return currentStatus;
  437. }
  438. //-----------------------------------------------------------------------------
  439. // Sets and returns the currentStatus when an error has been encountered.
  440. //-----------------------------------------------------------------------------
  441. File::Status File::setStatus()
  442. {
  443. switch (GetLastError())
  444. {
  445. case ERROR_INVALID_HANDLE:
  446. case ERROR_INVALID_ACCESS:
  447. case ERROR_TOO_MANY_OPEN_FILES:
  448. case ERROR_FILE_NOT_FOUND:
  449. case ERROR_SHARING_VIOLATION:
  450. case ERROR_HANDLE_DISK_FULL:
  451. return currentStatus = IOError;
  452. default:
  453. return currentStatus = UnknownError;
  454. }
  455. }
  456. //-----------------------------------------------------------------------------
  457. // Sets and returns the currentStatus to status.
  458. //-----------------------------------------------------------------------------
  459. File::Status File::setStatus(File::Status status)
  460. {
  461. return currentStatus = status;
  462. }
  463. //-----------------------------------------------------------------------------
  464. // Read from a file.
  465. // The number of bytes to read is passed in size, the data is returned in src.
  466. // The number of bytes read is available in bytesRead if a non-Null pointer is
  467. // provided.
  468. //-----------------------------------------------------------------------------
  469. File::Status File::read(U32 size, char *dst, U32 *bytesRead)
  470. {
  471. AssertFatal(Closed != currentStatus, "File::read: file closed");
  472. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::read: invalid file handle");
  473. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  474. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  475. AssertWarn(0 != size, "File::read: size of zero");
  476. if (Ok != currentStatus || 0 == size)
  477. return currentStatus;
  478. else
  479. {
  480. DWORD lastBytes;
  481. DWORD *bytes = (NULL == bytesRead) ? &lastBytes : (DWORD *)bytesRead;
  482. if (0 != ReadFile((HANDLE)handle, dst, size, bytes, NULL))
  483. {
  484. if(*((U32 *)bytes) != size)
  485. return currentStatus = EOS; // end of stream
  486. }
  487. else
  488. return setStatus(); // unsuccessful
  489. }
  490. return currentStatus = Ok; // successfully read size bytes
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Write to a file.
  494. // The number of bytes to write is passed in size, the data is passed in src.
  495. // The number of bytes written is available in bytesWritten if a non-Null
  496. // pointer is provided.
  497. //-----------------------------------------------------------------------------
  498. File::Status File::write(U32 size, const char *src, U32 *bytesWritten)
  499. {
  500. AssertFatal(Closed != currentStatus, "File::write: file closed");
  501. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::write: invalid file handle");
  502. AssertFatal(NULL != src, "File::write: NULL source pointer");
  503. AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability");
  504. AssertWarn(0 != size, "File::write: size of zero");
  505. if ((Ok != currentStatus && EOS != currentStatus) || 0 == size)
  506. return currentStatus;
  507. else
  508. {
  509. DWORD lastBytes;
  510. DWORD *bytes = (NULL == bytesWritten) ? &lastBytes : (DWORD *)bytesWritten;
  511. if (0 != WriteFile((HANDLE)handle, src, size, bytes, NULL))
  512. return currentStatus = Ok; // success!
  513. else
  514. return setStatus(); // unsuccessful
  515. }
  516. }
  517. //-----------------------------------------------------------------------------
  518. // Self-explanatory.
  519. //-----------------------------------------------------------------------------
  520. bool File::hasCapability(Capability cap) const
  521. {
  522. return (0 != (U32(cap) & capability));
  523. }
  524. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  525. {
  526. if(a.v2 > b.v2)
  527. return 1;
  528. if(a.v2 < b.v2)
  529. return -1;
  530. if(a.v1 > b.v1)
  531. return 1;
  532. if(a.v1 < b.v1)
  533. return -1;
  534. return 0;
  535. }
  536. static bool recurseDumpPath(const char *path, const char *pattern, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth )
  537. {
  538. char buf[1024], fullPath[1024];
  539. WIN32_FIND_DATA findData;
  540. Platform::makeFullPathName(path, fullPath, sizeof(fullPath));
  541. dSprintf(buf, sizeof(buf), "%s/%s", fullPath, pattern);
  542. #ifdef UNICODE
  543. UTF16 search[1024];
  544. convertUTF8toUTF16((UTF8 *)buf, search, sizeof(search));
  545. #else
  546. char *search = buf;
  547. #endif
  548. HANDLE handle = FindFirstFile(search, &findData);
  549. if (handle == INVALID_HANDLE_VALUE)
  550. return false;
  551. do
  552. {
  553. #ifdef UNICODE
  554. char fnbuf[1024];
  555. convertUTF16toUTF8(findData.cFileName, (UTF8 *)fnbuf, sizeof(fnbuf));
  556. #else
  557. char *fnbuf = findData.cFileName;
  558. #endif
  559. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  560. {
  561. // make sure it is a directory
  562. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  563. continue;
  564. // skip . and .. directories
  565. if (dStrcmp(fnbuf, ".") == 0 || dStrcmp(fnbuf, "..") == 0)
  566. continue;
  567. // Skip excluded directores
  568. if(Platform::isExcludedDirectory(fnbuf))
  569. continue;
  570. char child[1024];
  571. dSprintf(child, sizeof(child), "%s/%s", path, fnbuf);
  572. if( recurseDepth > 0 )
  573. recurseDumpPath(child, pattern, fileVector, recurseDepth - 1);
  574. else if (recurseDepth == -1)
  575. recurseDumpPath(child, pattern, fileVector, -1);
  576. }
  577. else
  578. {
  579. // make sure it is the kind of file we're looking for
  580. if (findData.dwFileAttributes &
  581. (FILE_ATTRIBUTE_DIRECTORY|
  582. FILE_ATTRIBUTE_OFFLINE|
  583. FILE_ATTRIBUTE_SYSTEM|
  584. FILE_ATTRIBUTE_TEMPORARY) )
  585. continue;
  586. // add it to the list
  587. fileVector.increment();
  588. Platform::FileInfo& rInfo = fileVector.last();
  589. rInfo.pFullPath = StringTable->insert(path);
  590. rInfo.pFileName = StringTable->insert(fnbuf);
  591. rInfo.fileSize = findData.nFileSizeLow;
  592. }
  593. }while(FindNextFile(handle, &findData));
  594. FindClose(handle);
  595. return true;
  596. }
  597. //--------------------------------------
  598. bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime)
  599. {
  600. WIN32_FIND_DATA findData;
  601. #ifdef UNICODE
  602. UTF16 fp[512];
  603. convertUTF8toUTF16((UTF8 *)filePath, fp, sizeof(fp));
  604. #else
  605. const char *fp = filePath;
  606. #endif
  607. HANDLE h = FindFirstFile(fp, &findData);
  608. if(h == INVALID_HANDLE_VALUE)
  609. return false;
  610. if(createTime)
  611. {
  612. createTime->v1 = findData.ftCreationTime.dwLowDateTime;
  613. createTime->v2 = findData.ftCreationTime.dwHighDateTime;
  614. }
  615. if(modifyTime)
  616. {
  617. modifyTime->v1 = findData.ftLastWriteTime.dwLowDateTime;
  618. modifyTime->v2 = findData.ftLastWriteTime.dwHighDateTime;
  619. }
  620. FindClose(h);
  621. return true;
  622. }
  623. //--------------------------------------
  624. bool Platform::createPath(const char *file)
  625. {
  626. char pathbuf[1024];
  627. const char *dir;
  628. pathbuf[0] = 0;
  629. U32 pathLen = 0;
  630. while((dir = dStrchr(file, '/')) != NULL)
  631. {
  632. dStrncpy(pathbuf + pathLen, file, dir - file);
  633. pathbuf[pathLen + dir-file] = 0;
  634. #ifdef UNICODE
  635. UTF16 b[1024];
  636. convertUTF8toUTF16((UTF8 *)pathbuf, b, sizeof(b));
  637. bool ret = CreateDirectory(b, NULL);
  638. #else
  639. bool ret = CreateDirectory(pathbuf, NULL);
  640. #endif
  641. pathLen += dir - file;
  642. pathbuf[pathLen++] = '/';
  643. file = dir + 1;
  644. }
  645. return true;
  646. }
  647. //--------------------------------------
  648. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth)
  649. {
  650. return recurseDumpPath(path, "*", fileVector, recurseDepth );
  651. }
  652. //--------------------------------------
  653. StringTableEntry Platform::getCurrentDirectory()
  654. {
  655. char cwd_buf[2048];
  656. GetCurrentDirectoryA(2047, cwd_buf);
  657. forwardslash(cwd_buf);
  658. return StringTable->insert(cwd_buf);
  659. }
  660. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  661. {
  662. //StringTableEntry lastDir = Platform::getCurrentDirectory();
  663. char cwd_buf[2048];
  664. dStrncpy(cwd_buf, newDir, sizeof(cwd_buf)-1);
  665. cwd_buf[sizeof(cwd_buf)-1] = 0;
  666. backslash(cwd_buf);
  667. return SetCurrentDirectoryA(cwd_buf);
  668. }
  669. StringTableEntry Platform::getExecutableName()
  670. {
  671. static StringTableEntry cen = NULL;
  672. if (!cen)
  673. {
  674. char cen_buf[2048];
  675. GetModuleFileNameA( NULL, cen_buf, 2047);
  676. forwardslash(cen_buf);
  677. char *delimiter = dStrrchr( cen_buf, '/' );
  678. if( delimiter != NULL )
  679. {
  680. *delimiter = 0x00;
  681. delimiter++;
  682. cen = StringTable->insert(delimiter);
  683. }
  684. else
  685. cen = StringTable->insert(cen_buf);
  686. }
  687. return cen;
  688. }
  689. StringTableEntry Platform::getExecutablePath()
  690. {
  691. static StringTableEntry cen = NULL;
  692. if (!cen)
  693. {
  694. char cen_buf[2048];
  695. GetModuleFileNameA( NULL, cen_buf, 2047);
  696. forwardslash(cen_buf);
  697. char *delimiter = dStrrchr( cen_buf, '/' );
  698. if( delimiter != NULL )
  699. *delimiter = 0x00;
  700. cen = StringTable->insert(cen_buf);
  701. }
  702. return cen;
  703. }
  704. //--------------------------------------
  705. bool Platform::isFile(const char *pFilePath)
  706. {
  707. if (!pFilePath || !*pFilePath)
  708. return false;
  709. // Get file info
  710. WIN32_FIND_DATA findData;
  711. #ifdef UNICODE
  712. UTF16 b[512];
  713. convertUTF8toUTF16((UTF8 *)pFilePath, b, sizeof(b));
  714. HANDLE handle = FindFirstFile(b, &findData);
  715. #else
  716. HANDLE handle = FindFirstFile(pFilePath, &findData);
  717. #endif
  718. // [neo, 4/6/2007]
  719. // This used to be after the FindClose() call
  720. if(handle == INVALID_HANDLE_VALUE)
  721. return false;
  722. FindClose(handle);
  723. // if the file is a Directory, Offline, System or Temporary then FALSE
  724. if (findData.dwFileAttributes &
  725. (FILE_ATTRIBUTE_DIRECTORY|
  726. FILE_ATTRIBUTE_OFFLINE|
  727. FILE_ATTRIBUTE_SYSTEM|
  728. FILE_ATTRIBUTE_TEMPORARY) )
  729. return false;
  730. // must be a real file then
  731. return true;
  732. }
  733. //--------------------------------------
  734. S32 Platform::getFileSize(const char *pFilePath)
  735. {
  736. if (!pFilePath || !*pFilePath)
  737. return -1;
  738. // Get file info
  739. WIN32_FIND_DATAA findData;
  740. HANDLE handle = FindFirstFileA(pFilePath, &findData);
  741. FindClose(handle);
  742. if(handle == INVALID_HANDLE_VALUE)
  743. return -1;
  744. // if the file is a Directory, Offline, System or Temporary then FALSE
  745. if (findData.dwFileAttributes &
  746. (FILE_ATTRIBUTE_DIRECTORY|
  747. FILE_ATTRIBUTE_OFFLINE|
  748. FILE_ATTRIBUTE_SYSTEM|
  749. FILE_ATTRIBUTE_TEMPORARY) )
  750. return -1;
  751. // must be a real file then
  752. return findData.nFileSizeLow;;
  753. }
  754. //--------------------------------------
  755. bool Platform::isDirectory(const char *pDirPath)
  756. {
  757. if (!pDirPath || !*pDirPath)
  758. return false;
  759. // Get file info
  760. WIN32_FIND_DATA findData;
  761. #ifdef UNICODE
  762. UTF16 b[512];
  763. convertUTF8toUTF16((UTF8 *)pDirPath, b, sizeof(b));
  764. HANDLE handle = FindFirstFile(b, &findData);
  765. #else
  766. HANDLE handle = FindFirstFile(pDirPath, &findData);
  767. #endif
  768. // [neo, 5/15/2007]
  769. // This check was AFTER FindClose for some reason - this is most probably the
  770. // original intent.
  771. if(handle == INVALID_HANDLE_VALUE)
  772. return false;
  773. FindClose(handle);
  774. // if the file is a Directory, Offline, System or Temporary then FALSE
  775. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  776. {
  777. // make sure it's a valid game directory
  778. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  779. return false;
  780. // must be a directory
  781. return true;
  782. }
  783. return false;
  784. }
  785. //--------------------------------------
  786. bool Platform::isSubDirectory(const char *pParent, const char *pDir)
  787. {
  788. if (!pParent || !*pDir)
  789. return false;
  790. // this is somewhat of a brute force method but we need to be 100% sure
  791. // that the user cannot enter things like ../dir or /dir etc,...
  792. WIN32_FIND_DATAA findData;
  793. HANDLE handle = FindFirstFileA(avar("%s/*", pParent), &findData);
  794. if (handle == INVALID_HANDLE_VALUE)
  795. return false;
  796. do
  797. {
  798. // if it is a directory...
  799. if (findData.dwFileAttributes &
  800. (FILE_ATTRIBUTE_DIRECTORY|
  801. FILE_ATTRIBUTE_OFFLINE|
  802. FILE_ATTRIBUTE_SYSTEM|
  803. FILE_ATTRIBUTE_TEMPORARY) )
  804. {
  805. // and the names match
  806. if (dStrcmp(pDir, findData.cFileName ) == 0)
  807. {
  808. // then we have a real sub directory
  809. FindClose(handle);
  810. return true;
  811. }
  812. }
  813. }while(FindNextFileA(handle, &findData));
  814. FindClose(handle);
  815. return false;
  816. }
  817. bool Platform::hasSubDirectory(const char *pPath)
  818. {
  819. if( !pPath )
  820. return false;
  821. ResourceManager->initExcludedDirectories();
  822. char search[1024];
  823. WIN32_FIND_DATAA findData;
  824. // Compose our search string - Format : ([path]/[subpath]/*)
  825. char trail = pPath[ dStrlen(pPath) - 1 ];
  826. if( trail == '/' )
  827. dStrcpy( search, pPath );
  828. else
  829. dSprintf(search, 1024, "%s/*", pPath );
  830. // See if we get any hits
  831. HANDLE handle = FindFirstFileA(search, &findData);
  832. if (handle == INVALID_HANDLE_VALUE)
  833. return false;
  834. do
  835. {
  836. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  837. {
  838. // skip . and .. directories
  839. if (dStrcmp(findData.cFileName, ".") == 0 || dStrcmp(findData.cFileName, "..") == 0)
  840. continue;
  841. if( Platform::isExcludedDirectory( findData.cFileName ) )
  842. continue;
  843. Platform::clearExcludedDirectories();
  844. return true;
  845. }
  846. }
  847. while(FindNextFileA(handle, &findData));
  848. FindClose(handle);
  849. Platform::clearExcludedDirectories();
  850. return false;
  851. }
  852. static bool recurseDumpDirectories(const char *basePath, const char *subPath, Vector<StringTableEntry> &directoryVector, S32 currentDepth, S32 recurseDepth, bool noBasePath)
  853. {
  854. char search[1024];
  855. WIN32_FIND_DATAA findData;
  856. //-----------------------------------------------------------------------------
  857. // Compose our search string - Format : ([path]/[subpath]/*)
  858. //-----------------------------------------------------------------------------
  859. char trail = basePath[ dStrlen(basePath) - 1 ];
  860. char subTrail;
  861. char subLead;
  862. if( subPath )
  863. {
  864. subTrail = subPath[ dStrlen(subPath) - 1 ];
  865. subLead = subPath[0];
  866. }
  867. if( trail == '/' )
  868. {
  869. // we have a sub path and it's not an empty string
  870. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  871. {
  872. if( subTrail == '/' )
  873. dSprintf(search, 1024, "%s%s*", basePath,subPath );
  874. else
  875. dSprintf(search, 1024, "%s%s/*", basePath,subPath );
  876. }
  877. else
  878. dSprintf( search, 1024, "%s*", basePath );
  879. }
  880. else
  881. {
  882. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  883. if( subTrail == '/' )
  884. dSprintf(search, 1024, "%s%s*", basePath,subPath );
  885. else
  886. dSprintf(search, 1024, "%s%s/*", basePath,subPath );
  887. else
  888. dSprintf(search, 1024, "%s/*", basePath );
  889. }
  890. //-----------------------------------------------------------------------------
  891. // See if we get any hits
  892. //-----------------------------------------------------------------------------
  893. HANDLE handle = FindFirstFileA(search, &findData);
  894. if (handle == INVALID_HANDLE_VALUE)
  895. return false;
  896. //-----------------------------------------------------------------------------
  897. // add path to our return list ( provided it is valid )
  898. //-----------------------------------------------------------------------------
  899. if( !Platform::isExcludedDirectory( subPath ) )
  900. {
  901. if( noBasePath )
  902. {
  903. // We have a path and it's not an empty string or an excluded directory
  904. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  905. directoryVector.push_back( StringTable->insert( subPath ) );
  906. }
  907. else
  908. {
  909. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  910. {
  911. char szPath [ 1024 ];
  912. dMemset( szPath, 0, 1024 );
  913. if ( trail == '/' )
  914. {
  915. if ( subLead == '/' )
  916. dSprintf( szPath, 1024, "%s%s", basePath, &subPath[1] );
  917. else
  918. dSprintf( szPath, 1024, "%s%s", basePath, subPath );
  919. }
  920. else
  921. {
  922. if( subLead == '/' )
  923. dSprintf( szPath, 1024, "%s%s", basePath, subPath );
  924. else
  925. dSprintf( szPath, 1024, "%s/%s", basePath, subPath );
  926. }
  927. directoryVector.push_back( StringTable->insert( szPath ) );
  928. }
  929. else
  930. directoryVector.push_back( StringTable->insert( basePath ) );
  931. }
  932. }
  933. //-----------------------------------------------------------------------------
  934. // Iterate through and grab valid directories
  935. //-----------------------------------------------------------------------------
  936. do
  937. {
  938. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  939. {
  940. // skip . and .. directories
  941. if (dStrcmp(findData.cFileName, ".") == 0 || dStrcmp(findData.cFileName, "..") == 0)
  942. continue;
  943. // skip excluded directories
  944. if( Platform::isExcludedDirectory( findData.cFileName ) )
  945. continue;
  946. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ))
  947. {
  948. char child[1024];
  949. if( subTrail == '/' )
  950. dSprintf(child, sizeof(child), "%s%s", subPath, findData.cFileName);
  951. else
  952. dSprintf(child, sizeof(child), "%s/%s", subPath, findData.cFileName);
  953. if( currentDepth < recurseDepth || recurseDepth == -1 )
  954. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  955. }
  956. else
  957. {
  958. char child[1024];
  959. if( trail == '/' )
  960. dStrcpy( child, findData.cFileName );
  961. else
  962. dSprintf(child, sizeof(child), "/%s", findData.cFileName);
  963. if( currentDepth < recurseDepth || recurseDepth == -1 )
  964. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  965. }
  966. }
  967. }
  968. while(FindNextFileA(handle, &findData));
  969. FindClose(handle);
  970. return true;
  971. }
  972. bool Platform::dumpDirectories( const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath )
  973. {
  974. ResourceManager->initExcludedDirectories();
  975. bool retVal = recurseDumpDirectories( path, "", directoryVector, -1, depth, noBasePath );
  976. clearExcludedDirectories();
  977. return retVal;
  978. }
  979. //-----------------------------------------------------------------------------
  980. StringTableEntry Platform::osGetTemporaryDirectory()
  981. {
  982. //#pragma message("Implement UNICODE support for osGetTemporaryDirectory [01/23/2007 tom]" )
  983. char buf[512];
  984. DWORD len = GetTempPathA(sizeof(buf), buf);
  985. // Remove the trailing slash
  986. buf[len-1] = 0;
  987. forwardslash(buf);
  988. return StringTable->insert(buf);
  989. }