winFileio.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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((LPCWSTR)f, (LPCWSTR)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((LPCWSTR)f, (LPCWSTR)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((LPCWSTR)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((LPCWSTR)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((LPCWSTR)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((LPCWSTR)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. FindClose(handle);
  845. return true;
  846. }
  847. }
  848. while(FindNextFileA(handle, &findData));
  849. FindClose(handle);
  850. Platform::clearExcludedDirectories();
  851. return false;
  852. }
  853. static bool recurseDumpDirectories(const char *basePath, const char *subPath, Vector<StringTableEntry> &directoryVector, S32 currentDepth, S32 recurseDepth, bool noBasePath)
  854. {
  855. char search[1024];
  856. //-----------------------------------------------------------------------------
  857. // Compose our search string - Format : ([path]/[subpath]/*)
  858. //-----------------------------------------------------------------------------
  859. dsize_t trLen = basePath ? dStrlen(basePath) : 0;
  860. dsize_t subtrLen = subPath ? dStrlen(subPath) : 0;
  861. char trail = trLen > 0 ? basePath[trLen - 1] : '\0';
  862. char subTrail = subtrLen > 0 ? subPath[subtrLen - 1] : '\0';
  863. char subLead = subtrLen > 0 ? subPath[0] : '\0';
  864. if (trail == '/')
  865. {
  866. // we have a sub path and it's not an empty string
  867. if (subPath && (dStrncmp(subPath, "", 1) != 0))
  868. {
  869. if (subTrail == '/')
  870. dSprintf(search, 1024, "%s%s*", basePath, subPath);
  871. else
  872. dSprintf(search, 1024, "%s%s/*", basePath, subPath);
  873. }
  874. else
  875. dSprintf(search, 1024, "%s*", basePath);
  876. }
  877. else
  878. {
  879. if (subPath && (dStrncmp(subPath, "", 1) != 0))
  880. if (subTrail == '/')
  881. dSprintf(search, 1024, "%s%s*", basePath, subPath);
  882. else
  883. dSprintf(search, 1024, "%s%s/*", basePath, subPath);
  884. else
  885. dSprintf(search, 1024, "%s/*", basePath);
  886. }
  887. //-----------------------------------------------------------------------------
  888. // See if we get any hits
  889. //-----------------------------------------------------------------------------
  890. WIN32_FIND_DATAA findData;
  891. HANDLE handle = FindFirstFileA(search, &findData);
  892. if (handle == INVALID_HANDLE_VALUE)
  893. return false;
  894. //-----------------------------------------------------------------------------
  895. // add path to our return list ( provided it is valid )
  896. //-----------------------------------------------------------------------------
  897. if (!Platform::isExcludedDirectory(subPath))
  898. {
  899. if (noBasePath)
  900. {
  901. // We have a path and it's not an empty string or an excluded directory
  902. if ((subPath && (dStrncmp(subPath, "", 1) != 0)))
  903. directoryVector.push_back(StringTable->insert(subPath));
  904. }
  905. else
  906. {
  907. if ((subPath && (dStrncmp(subPath, "", 1) != 0)))
  908. {
  909. char szPath[1024];
  910. dMemset(szPath, 0, 1024);
  911. if (trail == '/')
  912. {
  913. if (subLead == '/')
  914. dSprintf(szPath, 1024, "%s%s", basePath, &subPath[1]);
  915. else
  916. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  917. }
  918. else
  919. {
  920. if (subLead == '/')
  921. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  922. else
  923. dSprintf(szPath, 1024, "%s/%s", basePath, subPath);
  924. }
  925. directoryVector.push_back(StringTable->insert(szPath));
  926. }
  927. else
  928. directoryVector.push_back(StringTable->insert(basePath));
  929. }
  930. }
  931. //-----------------------------------------------------------------------------
  932. // Iterate through and grab valid directories
  933. //-----------------------------------------------------------------------------
  934. do
  935. {
  936. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  937. {
  938. // skip . and .. directories
  939. if (dStrcmp(findData.cFileName, ".") == 0 || dStrcmp(findData.cFileName, "..") == 0)
  940. continue;
  941. // skip excluded directories
  942. if (Platform::isExcludedDirectory(findData.cFileName))
  943. continue;
  944. if ((subPath && (dStrncmp(subPath, "", 1) != 0)))
  945. {
  946. char child[1024];
  947. if (subTrail == '/')
  948. dSprintf(child, sizeof(child), "%s%s", subPath, findData.cFileName);
  949. else
  950. dSprintf(child, sizeof(child), "%s/%s", subPath, findData.cFileName);
  951. if (currentDepth < recurseDepth || recurseDepth == -1)
  952. recurseDumpDirectories(basePath, child, directoryVector, currentDepth + 1, recurseDepth, noBasePath);
  953. }
  954. else
  955. {
  956. char child[1024];
  957. if (trail == '/')
  958. dStrcpy(child, findData.cFileName);
  959. else
  960. dSprintf(child, sizeof(child), "/%s", findData.cFileName);
  961. if (currentDepth < recurseDepth || recurseDepth == -1)
  962. recurseDumpDirectories(basePath, child, directoryVector, currentDepth + 1, recurseDepth, noBasePath);
  963. }
  964. }
  965. } while (FindNextFileA(handle, &findData));
  966. FindClose(handle);
  967. return true;
  968. }
  969. bool Platform::dumpDirectories( const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath )
  970. {
  971. ResourceManager->initExcludedDirectories();
  972. bool retVal = recurseDumpDirectories( path, "", directoryVector, -1, depth, noBasePath );
  973. clearExcludedDirectories();
  974. return retVal;
  975. }
  976. //-----------------------------------------------------------------------------
  977. StringTableEntry Platform::osGetTemporaryDirectory()
  978. {
  979. //#pragma message("Implement UNICODE support for osGetTemporaryDirectory [01/23/2007 tom]" )
  980. char buf[512];
  981. DWORD len = GetTempPathA(sizeof(buf), buf);
  982. // Remove the trailing slash
  983. buf[len-1] = 0;
  984. forwardslash(buf);
  985. return StringTable->insert(buf);
  986. }