winFileio.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "platformWin32/platformWin32.h"
  24. #include "core/fileio.h"
  25. #include "core/util/tVector.h"
  26. #include "core/stringTable.h"
  27. #include "console/console.h"
  28. #include "core/strings/unicode.h"
  29. #include "util/tempAlloc.h"
  30. #include "core/util/safeDelete.h"
  31. #include "core/volume.h"
  32. // Microsoft VC++ has this POSIX header in the wrong directory
  33. #if defined(TORQUE_COMPILER_VISUALC)
  34. #include <sys/utime.h>
  35. #elif defined (TORQUE_COMPILER_GCC)
  36. #include <time.h>
  37. #include <sys/utime.h>
  38. #else
  39. #include <utime.h>
  40. #endif
  41. StringTableEntry Platform::createPlatformFriendlyFilename( const char *filename )
  42. {
  43. return StringTable->insert( filename );
  44. }
  45. //-----------------------------------------------------------------------------
  46. bool dFileDelete(const char * name)
  47. {
  48. AssertFatal( name != NULL, "dFileDelete - NULL file name" );
  49. TempAlloc< TCHAR > buf( dStrlen( name ) + 1 );
  50. #ifdef UNICODE
  51. convertUTF8toUTF16N( name, buf, buf.size );
  52. #else
  53. dStrcpy( buf, name, buf.size );
  54. #endif
  55. backslash( buf );
  56. if( Platform::isFile( name ) )
  57. return DeleteFile( buf );
  58. else
  59. return RemoveDirectory( buf );
  60. }
  61. bool Platform::fileDelete(const char * name)
  62. {
  63. if (!name || (dStrlen(name) >= MAX_PATH))
  64. return(false);
  65. //return(::DeleteFile(name));
  66. if (Platform::isFile(name))
  67. return(remove(name) == 0);
  68. else
  69. return ::RemoveDirectoryA(name) != 0;
  70. }
  71. bool dFileRename(const char *oldName, const char *newName)
  72. {
  73. AssertFatal( oldName != NULL && newName != NULL, "dFileRename - NULL file name" );
  74. TempAlloc< TCHAR > oldf( dStrlen( oldName ) + 1 );
  75. TempAlloc< TCHAR > newf( dStrlen( newName ) + 1 );
  76. #ifdef UNICODE
  77. convertUTF8toUTF16N( oldName, oldf, oldf.size );
  78. convertUTF8toUTF16N( newName, newf, newf.size );
  79. #else
  80. dStrcpy(oldf, oldName, oldf.size);
  81. dStrcpy(newf, newName, newf.size);
  82. #endif
  83. backslash(oldf);
  84. backslash(newf);
  85. return MoveFile( oldf, newf );
  86. }
  87. bool dFileTouch(const char * name)
  88. {
  89. AssertFatal( name != NULL, "dFileTouch - NULL file name" );
  90. TempAlloc< TCHAR > buf( dStrlen( name ) + 1 );
  91. #ifdef UNICODE
  92. convertUTF8toUTF16N( name, buf, buf.size );
  93. #else
  94. dStrcpy( buf, name, buf.size );
  95. #endif
  96. backslash( buf );
  97. FILETIME ftime;
  98. GetSystemTimeAsFileTime( &ftime );
  99. HANDLE handle = CreateFile( buf, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  100. NULL, OPEN_EXISTING, 0, NULL );
  101. if( handle == INVALID_HANDLE_VALUE )
  102. return false;
  103. bool result = SetFileTime( handle, NULL, NULL, &ftime );
  104. CloseHandle( handle );
  105. return result;
  106. };
  107. bool dPathCopy(const char *fromName, const char *toName, bool nooverwrite)
  108. {
  109. AssertFatal( fromName != NULL && toName != NULL, "dPathCopy - NULL file name" );
  110. TempAlloc< TCHAR > from( dStrlen( fromName ) + 1 );
  111. TempAlloc< TCHAR > to( dStrlen( toName ) + 1 );
  112. #ifdef UNICODE
  113. convertUTF8toUTF16N( fromName, from, from.size );
  114. convertUTF8toUTF16N( toName, to, to.size );
  115. #else
  116. dStrcpy( from, fromName, from.size );
  117. dStrcpy( to, toName, to.size );
  118. #endif
  119. backslash( from );
  120. backslash( to );
  121. // Copy File
  122. if (Platform::isFile(fromName))
  123. return CopyFile( from, to, nooverwrite );
  124. // Copy Path
  125. else if (Platform::isDirectory(fromName))
  126. {
  127. // If the destination path exists and we don't want to overwrite, return.
  128. if ((Platform::isDirectory(toName) || Platform::isFile(toName)) && nooverwrite)
  129. return false;
  130. Vector<StringTableEntry> directoryInfo;
  131. Platform::dumpDirectories(fromName, directoryInfo, -1);
  132. Vector<Platform::FileInfo> fileInfo;
  133. Platform::dumpPath(fromName, fileInfo);
  134. Platform::clearExcludedDirectories();
  135. TempAlloc< char > tempBuf( to.size * 3 + MAX_PATH * 3 );
  136. // Create all the directories.
  137. for (S32 i = 0; i < directoryInfo.size(); i++)
  138. {
  139. const char* fromDir = directoryInfo[i];
  140. char* toDir = tempBuf;
  141. Platform::makeFullPathName(fromDir + dStrlen(fromName) + (dStricmp(fromDir, fromName) ? 1 : 0), tempBuf, tempBuf.size, toName);
  142. if(*(toDir + dStrlen(toDir) - 1) != '/')
  143. dStrcat(toDir, "/", tempBuf.size);
  144. forwardslash(toDir);
  145. if (!Platform::createPath(toDir))
  146. {
  147. //TODO: New directory should be deleted here.
  148. return false;
  149. }
  150. }
  151. TempAlloc< char > tempBuf1( from.size * 3 + MAX_PATH * 3 );
  152. #ifdef UNICODE
  153. TempAlloc< WCHAR > wtempBuf( tempBuf.size / 3 );
  154. TempAlloc< WCHAR > wtempBuf1( tempBuf1.size / 3 );
  155. #endif
  156. for (S32 i = 0; i < fileInfo.size(); i++)
  157. {
  158. char* fromFile = tempBuf1;
  159. dSprintf( tempBuf1, tempBuf1.size, "%s/%s", fileInfo[i].pFullPath, fileInfo[i].pFileName);
  160. char* toFile = tempBuf;
  161. Platform::makeFullPathName(fileInfo[i].pFullPath + dStrlen(fromName) + (dStricmp(fileInfo[i].pFullPath, fromName) ? 1 : 0), tempBuf, tempBuf.size, toName);
  162. dStrcat(toFile, "/", tempBuf.size);
  163. dStrcat(toFile, fileInfo[i].pFileName, tempBuf.size);
  164. backslash(fromFile);
  165. backslash(toFile);
  166. #ifdef UNICODE
  167. convertUTF8toUTF16N( tempBuf, wtempBuf, wtempBuf.size );
  168. convertUTF8toUTF16N( tempBuf1, wtempBuf1, wtempBuf1.size );
  169. WCHAR* f = wtempBuf1;
  170. WCHAR* t = wtempBuf;
  171. #else
  172. char *f = (char*)fromFile;
  173. char *t = (char*)toFile;
  174. #endif
  175. if (!::CopyFile(f, t, nooverwrite))
  176. {
  177. // New directory should be deleted here.
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. return false;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Constructors & Destructor
  187. //-----------------------------------------------------------------------------
  188. //-----------------------------------------------------------------------------
  189. // After construction, the currentStatus will be Closed and the capabilities
  190. // will be 0.
  191. //-----------------------------------------------------------------------------
  192. File::File()
  193. : currentStatus(Closed), capability(0)
  194. {
  195. AssertFatal(sizeof(HANDLE) == sizeof(void *), "File::File: cannot cast void* to HANDLE");
  196. handle = (void *)INVALID_HANDLE_VALUE;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // insert a copy constructor here... (currently disabled)
  200. //-----------------------------------------------------------------------------
  201. //-----------------------------------------------------------------------------
  202. // Destructor
  203. //-----------------------------------------------------------------------------
  204. File::~File()
  205. {
  206. close();
  207. handle = (void *)INVALID_HANDLE_VALUE;
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  211. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  212. // true.
  213. //
  214. // Sets capability appropriate to the openMode.
  215. // Returns the currentStatus of the file.
  216. //-----------------------------------------------------------------------------
  217. File::FileStatus File::open(const char *filename, const AccessMode openMode)
  218. {
  219. AssertFatal(NULL != filename, "File::open: NULL fname");
  220. AssertWarn(INVALID_HANDLE_VALUE == (HANDLE)handle, "File::open: handle already valid");
  221. TempAlloc< TCHAR > fname( dStrlen( filename ) + 1 );
  222. #ifdef UNICODE
  223. convertUTF8toUTF16N( filename, fname, fname.size );
  224. #else
  225. dStrcpy(fname, filename, fname.size);
  226. #endif
  227. backslash( fname );
  228. // Close the file if it was already open...
  229. if (Closed != currentStatus)
  230. close();
  231. // create the appropriate type of file...
  232. switch (openMode)
  233. {
  234. case Read:
  235. handle = (void *)CreateFile(fname,
  236. GENERIC_READ,
  237. FILE_SHARE_READ,
  238. NULL,
  239. OPEN_EXISTING,
  240. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  241. NULL);
  242. break;
  243. case Write:
  244. handle = (void *)CreateFile(fname,
  245. GENERIC_WRITE,
  246. 0,
  247. NULL,
  248. CREATE_ALWAYS,
  249. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  250. NULL);
  251. break;
  252. case ReadWrite:
  253. handle = (void *)CreateFile(fname,
  254. GENERIC_WRITE | GENERIC_READ,
  255. 0,
  256. NULL,
  257. OPEN_ALWAYS,
  258. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  259. NULL);
  260. break;
  261. case WriteAppend:
  262. handle = (void *)CreateFile(fname,
  263. GENERIC_WRITE,
  264. 0,
  265. NULL,
  266. OPEN_ALWAYS,
  267. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  268. NULL);
  269. break;
  270. default:
  271. AssertFatal(false, "File::open: bad access mode"); // impossible
  272. }
  273. if (INVALID_HANDLE_VALUE == (HANDLE)handle) // handle not created successfully
  274. {
  275. return setStatus();
  276. }
  277. else
  278. {
  279. // successfully created file, so set the file capabilities...
  280. switch (openMode)
  281. {
  282. case Read:
  283. capability = U32(FileRead);
  284. break;
  285. case Write:
  286. case WriteAppend:
  287. capability = U32(FileWrite);
  288. break;
  289. case ReadWrite:
  290. capability = U32(FileRead) |
  291. U32(FileWrite);
  292. break;
  293. default:
  294. AssertFatal(false, "File::open: bad access mode");
  295. }
  296. return currentStatus = Ok; // success!
  297. }
  298. }
  299. //-----------------------------------------------------------------------------
  300. // Get the current position of the file pointer.
  301. //-----------------------------------------------------------------------------
  302. U32 File::getPosition() const
  303. {
  304. AssertFatal(Closed != currentStatus, "File::getPosition: file closed");
  305. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getPosition: invalid file handle");
  306. return SetFilePointer((HANDLE)handle,
  307. 0, // how far to move
  308. NULL, // pointer to high word
  309. FILE_CURRENT); // from what point
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Set the position of the file pointer.
  313. // Absolute and relative positioning is supported via the absolutePos
  314. // parameter.
  315. //
  316. // If positioning absolutely, position MUST be positive - an IOError results if
  317. // position is negative.
  318. // Position can be negative if positioning relatively, however positioning
  319. // before the start of the file is an IOError.
  320. //
  321. // Returns the currentStatus of the file.
  322. //-----------------------------------------------------------------------------
  323. File::FileStatus File::setPosition(S32 position, bool absolutePos)
  324. {
  325. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  326. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::setPosition: invalid file handle");
  327. if (Ok != currentStatus && EOS != currentStatus)
  328. return currentStatus;
  329. U32 finalPos;
  330. if (absolutePos)
  331. {
  332. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  333. // position beyond EOS is OK
  334. finalPos = SetFilePointer((HANDLE)handle,
  335. position,
  336. NULL,
  337. FILE_BEGIN);
  338. }
  339. else
  340. {
  341. AssertFatal((getPosition() >= (U32)abs(position) && 0 > position) || 0 <= position, "File::setPosition: negative relative position");
  342. // position beyond EOS is OK
  343. finalPos = SetFilePointer((HANDLE)handle,
  344. position,
  345. NULL,
  346. FILE_CURRENT);
  347. }
  348. if (0xffffffff == finalPos)
  349. return setStatus(); // unsuccessful
  350. else if (finalPos >= getSize())
  351. return currentStatus = EOS; // success, at end of file
  352. else
  353. return currentStatus = Ok; // success!
  354. }
  355. //-----------------------------------------------------------------------------
  356. // Get the size of the file in bytes.
  357. // It is an error to query the file size for a Closed file, or for one with an
  358. // error status.
  359. //-----------------------------------------------------------------------------
  360. U32 File::getSize() const
  361. {
  362. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  363. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getSize: invalid file handle");
  364. if (Ok == currentStatus || EOS == currentStatus)
  365. {
  366. DWORD high;
  367. return GetFileSize((HANDLE)handle, &high); // success!
  368. }
  369. else
  370. return 0; // unsuccessful
  371. }
  372. //-----------------------------------------------------------------------------
  373. // Flush the file.
  374. // It is an error to flush a read-only file.
  375. // Returns the currentStatus of the file.
  376. //-----------------------------------------------------------------------------
  377. File::FileStatus File::flush()
  378. {
  379. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  380. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::flush: invalid file handle");
  381. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  382. if (0 != FlushFileBuffers((HANDLE)handle))
  383. return setStatus(); // unsuccessful
  384. else
  385. return currentStatus = Ok; // success!
  386. }
  387. //-----------------------------------------------------------------------------
  388. // Close the File.
  389. //
  390. // Returns the currentStatus
  391. //-----------------------------------------------------------------------------
  392. File::FileStatus File::close()
  393. {
  394. // check if it's already closed...
  395. if (Closed == currentStatus)
  396. return currentStatus;
  397. // it's not, so close it...
  398. if (INVALID_HANDLE_VALUE != (HANDLE)handle)
  399. {
  400. if (0 == CloseHandle((HANDLE)handle))
  401. return setStatus(); // unsuccessful
  402. }
  403. handle = (void *)INVALID_HANDLE_VALUE;
  404. return currentStatus = Closed;
  405. }
  406. //-----------------------------------------------------------------------------
  407. // Self-explanatory.
  408. //-----------------------------------------------------------------------------
  409. File::FileStatus File::getStatus() const
  410. {
  411. return currentStatus;
  412. }
  413. //-----------------------------------------------------------------------------
  414. // Sets and returns the currentStatus when an error has been encountered.
  415. //-----------------------------------------------------------------------------
  416. File::FileStatus File::setStatus()
  417. {
  418. switch (GetLastError())
  419. {
  420. case ERROR_INVALID_HANDLE:
  421. case ERROR_INVALID_ACCESS:
  422. case ERROR_TOO_MANY_OPEN_FILES:
  423. case ERROR_FILE_NOT_FOUND:
  424. case ERROR_SHARING_VIOLATION:
  425. case ERROR_HANDLE_DISK_FULL:
  426. return currentStatus = IOError;
  427. default:
  428. return currentStatus = UnknownError;
  429. }
  430. }
  431. //-----------------------------------------------------------------------------
  432. // Sets and returns the currentStatus to status.
  433. //-----------------------------------------------------------------------------
  434. File::FileStatus File::setStatus(File::FileStatus status)
  435. {
  436. return currentStatus = status;
  437. }
  438. //-----------------------------------------------------------------------------
  439. // Read from a file.
  440. // The number of bytes to read is passed in size, the data is returned in src.
  441. // The number of bytes read is available in bytesRead if a non-Null pointer is
  442. // provided.
  443. //-----------------------------------------------------------------------------
  444. File::FileStatus File::read(U32 size, char *dst, U32 *bytesRead)
  445. {
  446. AssertFatal(Closed != currentStatus, "File::read: file closed");
  447. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::read: invalid file handle");
  448. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  449. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  450. AssertWarn(0 != size, "File::read: size of zero");
  451. if (Ok != currentStatus || 0 == size)
  452. return currentStatus;
  453. else
  454. {
  455. DWORD lastBytes;
  456. DWORD *bytes = (NULL == bytesRead) ? &lastBytes : (DWORD *)bytesRead;
  457. if (0 != ReadFile((HANDLE)handle, dst, size, bytes, NULL))
  458. {
  459. if(*((U32 *)bytes) != size)
  460. return currentStatus = EOS; // end of stream
  461. }
  462. else
  463. return setStatus(); // unsuccessful
  464. }
  465. return currentStatus = Ok; // successfully read size bytes
  466. }
  467. //-----------------------------------------------------------------------------
  468. // Write to a file.
  469. // The number of bytes to write is passed in size, the data is passed in src.
  470. // The number of bytes written is available in bytesWritten if a non-Null
  471. // pointer is provided.
  472. //-----------------------------------------------------------------------------
  473. File::FileStatus File::write(U32 size, const char *src, U32 *bytesWritten)
  474. {
  475. AssertFatal(Closed != currentStatus, "File::write: file closed");
  476. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::write: invalid file handle");
  477. AssertFatal(NULL != src, "File::write: NULL source pointer");
  478. AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability");
  479. AssertWarn(0 != size, "File::write: size of zero");
  480. if ((Ok != currentStatus && EOS != currentStatus) || 0 == size)
  481. return currentStatus;
  482. else
  483. {
  484. DWORD lastBytes;
  485. DWORD *bytes = (NULL == bytesWritten) ? &lastBytes : (DWORD *)bytesWritten;
  486. if (0 != WriteFile((HANDLE)handle, src, size, bytes, NULL))
  487. return currentStatus = Ok; // success!
  488. else
  489. return setStatus(); // unsuccessful
  490. }
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Self-explanatory.
  494. //-----------------------------------------------------------------------------
  495. bool File::hasCapability(Capability cap) const
  496. {
  497. return (0 != (U32(cap) & capability));
  498. }
  499. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  500. {
  501. if(a.v2 > b.v2)
  502. return 1;
  503. if(a.v2 < b.v2)
  504. return -1;
  505. if(a.v1 > b.v1)
  506. return 1;
  507. if(a.v1 < b.v1)
  508. return -1;
  509. return 0;
  510. }
  511. static bool recurseDumpPath(const char *path, const char *pattern, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth )
  512. {
  513. WIN32_FIND_DATA findData;
  514. TempAlloc< char > fullPath( dStrlen( path ) * 3 + MAX_PATH * 3 + 1 );
  515. Platform::makeFullPathName( path, fullPath, fullPath.size );
  516. U32 lenFullPath = dStrlen( fullPath );
  517. TempAlloc< char > buf( lenFullPath + MAX_PATH * 3 + 2 );
  518. dSprintf( buf, buf.size, "%s/%s", fullPath.ptr, pattern );
  519. #ifdef UNICODE
  520. TempAlloc< WCHAR > searchBuf( buf.size );
  521. convertUTF8toUTF16N( buf, searchBuf, searchBuf.size );
  522. WCHAR* search = searchBuf;
  523. #else
  524. char *search = buf;
  525. #endif
  526. backslash( search );
  527. HANDLE handle = FindFirstFile(search, &findData);
  528. if (handle == INVALID_HANDLE_VALUE)
  529. return false;
  530. do
  531. {
  532. #ifdef UNICODE
  533. convertUTF16toUTF8N( findData.cFileName, buf, buf.size );
  534. char* fnbuf = buf;
  535. #else
  536. char *fnbuf = findData.cFileName;
  537. #endif
  538. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  539. {
  540. // make sure it is a directory
  541. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  542. continue;
  543. // skip . and .. directories
  544. if (String::compare( findData.cFileName, TEXT( "." ) ) == 0 || String::compare( findData.cFileName, TEXT( ".." ) ) == 0)
  545. continue;
  546. // Skip excluded directores
  547. if(Platform::isExcludedDirectory(fnbuf))
  548. continue;
  549. dSprintf( fullPath, fullPath.size, "%s/%s", path, fnbuf);
  550. char* child = fullPath;
  551. if( recurseDepth > 0 )
  552. recurseDumpPath(child, pattern, fileVector, recurseDepth - 1);
  553. else if (recurseDepth == -1)
  554. recurseDumpPath(child, pattern, fileVector, -1);
  555. }
  556. else
  557. {
  558. // make sure it is the kind of file we're looking for
  559. if (findData.dwFileAttributes &
  560. (FILE_ATTRIBUTE_DIRECTORY|
  561. FILE_ATTRIBUTE_OFFLINE|
  562. FILE_ATTRIBUTE_SYSTEM|
  563. FILE_ATTRIBUTE_TEMPORARY) )
  564. continue;
  565. // add it to the list
  566. fileVector.increment();
  567. Platform::FileInfo& rInfo = fileVector.last();
  568. forwardslash( fnbuf );
  569. rInfo.pFullPath = StringTable->insert(path);
  570. rInfo.pFileName = StringTable->insert(fnbuf);
  571. rInfo.fileSize = findData.nFileSizeLow;
  572. }
  573. }while(FindNextFile(handle, &findData));
  574. FindClose(handle);
  575. return true;
  576. }
  577. //--------------------------------------
  578. bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime)
  579. {
  580. WIN32_FIND_DATA findData;
  581. TempAlloc< TCHAR > fp( dStrlen( filePath ) + 1 );
  582. #ifdef UNICODE
  583. convertUTF8toUTF16N( filePath, fp, fp.size );
  584. #else
  585. dStrcpy( fp, filePath, fp.size );
  586. #endif
  587. backslash( fp );
  588. HANDLE h = FindFirstFile(fp, &findData);
  589. if(h == INVALID_HANDLE_VALUE)
  590. return false;
  591. if(createTime)
  592. {
  593. createTime->v1 = findData.ftCreationTime.dwLowDateTime;
  594. createTime->v2 = findData.ftCreationTime.dwHighDateTime;
  595. }
  596. if(modifyTime)
  597. {
  598. modifyTime->v1 = findData.ftLastWriteTime.dwLowDateTime;
  599. modifyTime->v2 = findData.ftLastWriteTime.dwHighDateTime;
  600. }
  601. FindClose(h);
  602. return true;
  603. }
  604. //--------------------------------------
  605. bool Platform::createPath(const char *file)
  606. {
  607. TempAlloc< TCHAR > pathbuf( dStrlen( file ) + 1 );
  608. #ifdef UNICODE
  609. TempAlloc< WCHAR > fileBuf( pathbuf.size );
  610. convertUTF8toUTF16N( file, fileBuf, fileBuf.size );
  611. const WCHAR* fileName = fileBuf;
  612. const WCHAR* dir;
  613. #else
  614. const char* fileName = file;
  615. const char* dir;
  616. #endif
  617. pathbuf[ 0 ] = 0;
  618. U32 pathLen = 0;
  619. while((dir = dStrchr(fileName, '/')) != NULL)
  620. {
  621. TCHAR* pathptr = pathbuf;
  622. dMemcpy( pathptr + pathLen, fileName, ( dir - fileName ) * sizeof( TCHAR ) );
  623. pathbuf[pathLen + dir-fileName] = 0;
  624. // ignore return value because we are fine with already existing directory
  625. CreateDirectory(pathbuf, NULL);
  626. pathLen += dir - fileName;
  627. pathbuf[pathLen++] = '\\';
  628. fileName = dir + 1;
  629. }
  630. return true;
  631. }
  632. // [rene, 04/05/2008] Not used currently so did not bother updating.
  633. #if 0
  634. // [tom, 7/12/2005] Rather then converting this to unicode, just using the ANSI
  635. // versions of the Win32 API as its quicker for testing.
  636. bool Platform::cdFileExists(const char *filePath, const char *volumeName, S32 serialNum)
  637. {
  638. if (!filePath || !filePath[0])
  639. return true;
  640. //first find the CD device...
  641. char fileBuf[1024];
  642. char drivesBuf[256];
  643. S32 length = GetLogicalDriveStringsA(256, drivesBuf);
  644. char *drivePtr = drivesBuf;
  645. while (S32(drivePtr - drivesBuf) < length)
  646. {
  647. char driveVolume[256], driveFileSystem[256];
  648. U32 driveSerial, driveFNLength, driveFlags;
  649. if ((dStricmp(drivePtr, "A:\\") != 0 && dStricmp(drivePtr, "B:\\") != 0) &&
  650. GetVolumeInformationA((const char*)drivePtr, &driveVolume[0], (unsigned long)255,
  651. (unsigned long*)&driveSerial, (unsigned long*)&driveFNLength,
  652. (unsigned long*)&driveFlags, &driveFileSystem[0], (unsigned long)255))
  653. {
  654. #if defined (TORQUE_DEBUG) || !defined (TORQUE_SHIPPING)
  655. Con::printf("Found Drive: %s, vol: %s, serial: %d", drivePtr, driveVolume, driveSerial);
  656. #endif
  657. //see if the volume and serial number match
  658. if (!dStricmp(volumeName, driveVolume) && (!serialNum || (serialNum == driveSerial)))
  659. {
  660. //see if the file exists on this volume
  661. if(dStrlen(drivePtr) == 3 && drivePtr[2] == '\\' && filePath[0] == '\\')
  662. dSprintf(fileBuf, sizeof(fileBuf), "%s%s", drivePtr, filePath + 1);
  663. else
  664. dSprintf(fileBuf, sizeof(fileBuf), "%s%s", drivePtr, filePath);
  665. #if defined (TORQUE_DEBUG) || !defined (TORQUE_SHIPPING)
  666. Con::printf("Looking for file: %s on %s", fileBuf, driveVolume);
  667. #endif
  668. WIN32_FIND_DATAA findData;
  669. HANDLE h = FindFirstFileA(fileBuf, &findData);
  670. if(h != INVALID_HANDLE_VALUE)
  671. {
  672. FindClose(h);
  673. return true;
  674. }
  675. FindClose(h);
  676. }
  677. }
  678. //check the next drive
  679. drivePtr += dStrlen(drivePtr) + 1;
  680. }
  681. return false;
  682. }
  683. #endif
  684. //--------------------------------------
  685. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth)
  686. {
  687. return recurseDumpPath(path, "*", fileVector, recurseDepth );
  688. }
  689. //--------------------------------------
  690. //StringTableEntry Platform::getWorkingDirectory()
  691. //{
  692. // return getCurrentDirectory();
  693. //}
  694. StringTableEntry Platform::getCurrentDirectory()
  695. {
  696. TempAlloc< TCHAR > buf( 2048 );
  697. GetCurrentDirectory( buf.size, buf );
  698. forwardslash( buf );
  699. #ifdef UNICODE
  700. char* utf8 = createUTF8string( buf );
  701. StringTableEntry result = StringTable->insert( utf8 );
  702. SAFE_DELETE_ARRAY( utf8 );
  703. return result;
  704. #else
  705. return StringTable->insert( buf );
  706. #endif
  707. }
  708. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  709. {
  710. if (Platform::getWebDeployment())
  711. return true;
  712. TempAlloc< TCHAR > buf( dStrlen( newDir ) + 2 );
  713. #ifdef UNICODE
  714. convertUTF8toUTF16N( newDir, buf, buf.size - 1 );
  715. #else
  716. dStrcpy( buf, newDir, buf.size );
  717. #endif
  718. backslash( buf );
  719. return SetCurrentDirectory( buf );
  720. }
  721. #ifdef UNICODE
  722. static void getExecutableInfo( StringTableEntry* path, StringTableEntry* exe )
  723. {
  724. static StringTableEntry pathEntry = NULL;
  725. static StringTableEntry exeEntry = NULL;
  726. if( !pathEntry )
  727. {
  728. if (!Platform::getWebDeployment())
  729. {
  730. WCHAR cen_buf[ 2048 ];
  731. GetModuleFileNameW( NULL, cen_buf, sizeof( cen_buf ) / sizeof( cen_buf[ 0 ] ) );
  732. forwardslash( cen_buf );
  733. WCHAR* delimiter = dStrrchr( cen_buf, '/' );
  734. if( delimiter )
  735. *delimiter = '\0';
  736. char* pathBuf = createUTF8string( cen_buf );
  737. char* exeBuf = createUTF8string( delimiter + 1 );
  738. pathEntry = StringTable->insert( pathBuf );
  739. exeEntry = StringTable->insert( exeBuf );
  740. SAFE_DELETE_ARRAY( pathBuf );
  741. SAFE_DELETE_ARRAY( exeBuf );
  742. }
  743. else
  744. {
  745. char cdir[4096];
  746. GetCurrentDirectoryA(4096, cdir);
  747. pathEntry = StringTable->insert(cdir);
  748. exeEntry = StringTable->insert("WebGameCtrl.exe");
  749. }
  750. }
  751. if( path )
  752. *path = pathEntry;
  753. if( exe )
  754. *exe = exeEntry;
  755. }
  756. #endif
  757. StringTableEntry Platform::getExecutableName()
  758. {
  759. #ifdef UNICODE
  760. StringTableEntry exe;
  761. getExecutableInfo( NULL, &exe );
  762. return exe;
  763. #else
  764. static StringTableEntry cen = NULL;
  765. if (!cen)
  766. {
  767. char cen_buf[2048];
  768. GetModuleFileNameA( NULL, cen_buf, 2047);
  769. forwardslash(cen_buf);
  770. char *delimiter = dStrrchr( cen_buf, '/' );
  771. if( delimiter != NULL )
  772. {
  773. *delimiter = 0x00;
  774. delimiter++;
  775. cen = StringTable->insert(delimiter);
  776. }
  777. else
  778. cen = StringTable->insert(cen_buf);
  779. }
  780. return cen;
  781. #endif
  782. }
  783. StringTableEntry Platform::getExecutablePath()
  784. {
  785. #ifdef UNICODE
  786. StringTableEntry path;
  787. getExecutableInfo( &path, NULL );
  788. return path;
  789. #else
  790. static StringTableEntry cen = NULL;
  791. if (!cen)
  792. {
  793. char cen_buf[2048];
  794. GetModuleFileNameA( NULL, cen_buf, 2047);
  795. forwardslash(cen_buf);
  796. char *delimiter = dStrrchr( cen_buf, '/' );
  797. if( delimiter != NULL )
  798. *delimiter = 0x00;
  799. cen = StringTable->insert(cen_buf);
  800. }
  801. return cen;
  802. #endif
  803. }
  804. //--------------------------------------
  805. bool Platform::isFile(const char *pFilePath)
  806. {
  807. if (!pFilePath || !*pFilePath)
  808. return false;
  809. TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 );
  810. #ifdef UNICODE
  811. convertUTF8toUTF16N( pFilePath, buf, buf.size );
  812. #else
  813. dStrcpy( buf, pFilePath, buf.size );
  814. #endif
  815. backslash( buf );
  816. // Get file info
  817. WIN32_FIND_DATA findData;
  818. HANDLE handle = FindFirstFile(buf, &findData);
  819. FindClose(handle);
  820. if(handle == INVALID_HANDLE_VALUE)
  821. {
  822. // Since file does not exist on disk see if it exists in a zip file loaded
  823. return Torque::FS::IsFile(pFilePath);
  824. }
  825. // if the file is a Directory, Offline, System or Temporary then FALSE
  826. if (findData.dwFileAttributes &
  827. (FILE_ATTRIBUTE_DIRECTORY|
  828. FILE_ATTRIBUTE_OFFLINE|
  829. FILE_ATTRIBUTE_SYSTEM|
  830. FILE_ATTRIBUTE_TEMPORARY) )
  831. return false;
  832. // must be a real file then
  833. return true;
  834. }
  835. //--------------------------------------
  836. S32 Platform::getFileSize(const char *pFilePath)
  837. {
  838. if (!pFilePath || !*pFilePath)
  839. return -1;
  840. TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 );
  841. #ifdef UNICODE
  842. convertUTF8toUTF16N( pFilePath, buf, buf.size );
  843. #else
  844. dStrcpy( buf, pFilePath, buf.size );
  845. #endif
  846. backslash( buf );
  847. // Get file info
  848. WIN32_FIND_DATA findData;
  849. HANDLE handle = FindFirstFile(buf, &findData);
  850. if(handle == INVALID_HANDLE_VALUE)
  851. return -1;
  852. FindClose(handle);
  853. // if the file is a Directory, Offline, System or Temporary then FALSE
  854. if (findData.dwFileAttributes &
  855. (FILE_ATTRIBUTE_DIRECTORY|
  856. FILE_ATTRIBUTE_OFFLINE|
  857. FILE_ATTRIBUTE_SYSTEM|
  858. FILE_ATTRIBUTE_TEMPORARY) )
  859. return -1;
  860. // must be a real file then
  861. return ((findData.nFileSizeHigh * (MAXDWORD+1)) + findData.nFileSizeLow);
  862. }
  863. //--------------------------------------
  864. bool Platform::isDirectory(const char *pDirPath)
  865. {
  866. if (!pDirPath || !*pDirPath)
  867. return false;
  868. TempAlloc< TCHAR > buf( dStrlen( pDirPath ) + 1 );
  869. #ifdef UNICODE
  870. convertUTF8toUTF16N( pDirPath, buf, buf.size );
  871. #else
  872. dStrcpy( buf, pDirPath, buf.size );
  873. #endif
  874. backslash( buf );
  875. // Get file info
  876. WIN32_FIND_DATA findData;
  877. HANDLE handle = FindFirstFile(buf, &findData);
  878. // [neo, 5/15/2007]
  879. // This check was AFTER FindClose for some reason - this is most probably the
  880. // original intent.
  881. if(handle == INVALID_HANDLE_VALUE)
  882. return false;
  883. FindClose(handle);
  884. // if the file is a Directory, Offline, System or Temporary then FALSE
  885. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  886. {
  887. // make sure it's a valid game directory
  888. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  889. return false;
  890. // must be a directory
  891. return true;
  892. }
  893. return false;
  894. }
  895. //--------------------------------------
  896. bool Platform::isSubDirectory(const char *pParent, const char *pDir)
  897. {
  898. if (!pParent || !*pDir)
  899. return false;
  900. const char* fileName = avar("%s/*", pParent);
  901. TempAlloc< TCHAR > file( dStrlen( fileName ) + 1 );
  902. TempAlloc< TCHAR > dir( dStrlen( pDir ) + 1 );
  903. #ifdef UNICODE
  904. convertUTF8toUTF16N( fileName, file, file.size );
  905. convertUTF8toUTF16N( pDir, dir, dir.size );
  906. #else
  907. dStrcpy( file, fileName, file.size );
  908. dStrcpy( dir, pDir, dir.size );
  909. #endif
  910. backslash( file );
  911. backslash( dir );
  912. // this is somewhat of a brute force method but we need to be 100% sure
  913. // that the user cannot enter things like ../dir or /dir etc,...
  914. WIN32_FIND_DATA findData;
  915. HANDLE handle = FindFirstFile(file, &findData);
  916. if (handle == INVALID_HANDLE_VALUE)
  917. return false;
  918. do
  919. {
  920. // if it is a directory...
  921. if (findData.dwFileAttributes &
  922. (FILE_ATTRIBUTE_DIRECTORY|
  923. FILE_ATTRIBUTE_OFFLINE|
  924. FILE_ATTRIBUTE_SYSTEM|
  925. FILE_ATTRIBUTE_TEMPORARY) )
  926. {
  927. //FIXME: this has to be dStrcasecmp but there's no implementation for Unicode
  928. // and the names match
  929. if (String::compare(dir, findData.cFileName ) == 0)
  930. {
  931. // then we have a real sub directory
  932. FindClose(handle);
  933. return true;
  934. }
  935. }
  936. }while(FindNextFile(handle, &findData));
  937. FindClose(handle);
  938. return false;
  939. }
  940. //------------------------------------------------------------------------------
  941. bool Platform::fileTimeToString(FileTime * time, char * string, U32 strLen)
  942. {
  943. if(!time || !string)
  944. return(false);
  945. dSprintf(string, strLen, "%d:%d", time->v2, time->v1);
  946. return(true);
  947. }
  948. bool Platform::stringToFileTime(const char * string, FileTime * time)
  949. {
  950. if(!time || !string)
  951. return(false);
  952. char buf[80];
  953. dSprintf(buf, sizeof(buf), (char *)string);
  954. char * sep = (char *)dStrstr((const char *)buf, (const char *)":");
  955. if(!sep)
  956. return(false);
  957. *sep = 0;
  958. sep++;
  959. time->v2 = dAtoi(buf);
  960. time->v1 = dAtoi(sep);
  961. return(true);
  962. }
  963. // Volume Functions
  964. void Platform::getVolumeNamesList( Vector<const char*>& out_rNameVector, bool bOnlyFixedDrives )
  965. {
  966. DWORD dwDrives = GetLogicalDrives();
  967. DWORD dwMask = 1;
  968. char driveLetter[12];
  969. out_rNameVector.clear();
  970. for(S32 i = 0; i < 32; i++ )
  971. {
  972. dMemset(driveLetter,0,12);
  973. if( dwDrives & dwMask )
  974. {
  975. dSprintf(driveLetter, 12, "%c:", (i + 'A'));
  976. if( bOnlyFixedDrives && GetDriveTypeA(driveLetter) == DRIVE_FIXED )
  977. out_rNameVector.push_back( StringTable->insert( driveLetter ) );
  978. else if ( !bOnlyFixedDrives )
  979. out_rNameVector.push_back( StringTable->insert( driveLetter ) );
  980. }
  981. dwMask <<= 1;
  982. }
  983. }
  984. void Platform::getVolumeInformationList( Vector<VolumeInformation>& out_rVolumeInfoVector, bool bOnlyFixedDrives )
  985. {
  986. Vector<const char*> drives;
  987. getVolumeNamesList( drives, bOnlyFixedDrives );
  988. if( ! drives.empty() )
  989. {
  990. Vector<StringTableEntry>::iterator i;
  991. for( i = drives.begin(); i != drives.end(); i++ )
  992. {
  993. VolumeInformation info;
  994. TCHAR lpszVolumeName[ 256 ];
  995. TCHAR lpszFileSystem[ 256 ];
  996. DWORD dwSerial = 0;
  997. DWORD dwMaxComponentLength = 0;
  998. DWORD dwFileSystemFlags = 0;
  999. dMemset( lpszVolumeName, 0, sizeof( lpszVolumeName ) );
  1000. dMemset( lpszFileSystem, 0, sizeof( lpszFileSystem ) );
  1001. dMemset( &info, 0, sizeof( VolumeInformation ) );
  1002. // More volume information
  1003. UINT uDriveType = GetDriveTypeA( (*i) );
  1004. if( uDriveType == DRIVE_UNKNOWN )
  1005. info.Type = DRIVETYPE_UNKNOWN;
  1006. else if( uDriveType == DRIVE_REMOVABLE )
  1007. info.Type = DRIVETYPE_REMOVABLE;
  1008. else if( uDriveType == DRIVE_FIXED )
  1009. info.Type = DRIVETYPE_FIXED;
  1010. else if( uDriveType == DRIVE_CDROM )
  1011. info.Type = DRIVETYPE_CDROM;
  1012. else if( uDriveType == DRIVE_RAMDISK )
  1013. info.Type = DRIVETYPE_RAMDISK;
  1014. else if( uDriveType == DRIVE_REMOTE )
  1015. info.Type = DRIVETYPE_REMOTE;
  1016. info.RootPath = StringTable->insert( (*i) );
  1017. // We don't retrieve drive volume info for removable drives, because it's loud :(
  1018. if( info.Type != DRIVETYPE_REMOVABLE )
  1019. {
  1020. #ifdef UNICODE
  1021. WCHAR ibuf[ 3 ];
  1022. ibuf[ 0 ] = ( *i )[ 0 ];
  1023. ibuf[ 1 ] = ':';
  1024. ibuf[ 2 ] = '\0';
  1025. #else
  1026. char* ibuf = *i;
  1027. #endif
  1028. // Standard volume information
  1029. GetVolumeInformation( ibuf, lpszVolumeName, sizeof( lpszVolumeName ) / sizeof( lpszVolumeName[ 0 ] ),
  1030. &dwSerial, &dwMaxComponentLength, &dwFileSystemFlags, lpszFileSystem,
  1031. sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) );
  1032. #ifdef UNICODE
  1033. char buf[ sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) * 3 + 1 ];
  1034. convertUTF16toUTF8( lpszFileSystem, buf );
  1035. info.FileSystem = StringTable->insert( buf );
  1036. convertUTF16toUTF8( lpszVolumeName, buf );
  1037. info.Name = StringTable->insert( buf );
  1038. #else
  1039. info.FileSystem = StringTable->insert( lpszFileSystem );
  1040. info.Name = StringTable->insert( lpszVolumeName );
  1041. #endif
  1042. info.SerialNumber = dwSerial;
  1043. // Won't compile on something prior to XP.
  1044. info.ReadOnly = dwFileSystemFlags & FILE_READ_ONLY_VOLUME;
  1045. }
  1046. out_rVolumeInfoVector.push_back( info );
  1047. // I opted not to get free disk space because of the overhead of the calculations required for it
  1048. }
  1049. }
  1050. }
  1051. bool Platform::hasSubDirectory(const char *pPath)
  1052. {
  1053. if( !pPath )
  1054. return false;
  1055. char searchBuf[1024];
  1056. // Compose our search string - Format : ([path]/[subpath]/*)
  1057. char trail = pPath[ dStrlen(pPath) - 1 ];
  1058. if( trail == '/' )
  1059. dStrcpy( searchBuf, pPath, 1024 );
  1060. else
  1061. dSprintf(searchBuf, 1024, "%s/*", pPath );
  1062. #ifdef UNICODE
  1063. WCHAR buf[ 1024 ];
  1064. convertUTF8toUTF16( searchBuf, buf );
  1065. WCHAR* search = buf;
  1066. #else
  1067. char* search = searchBuf;
  1068. #endif
  1069. backslash( search );
  1070. // See if we get any hits
  1071. WIN32_FIND_DATA findData;
  1072. HANDLE handle = FindFirstFile(search, &findData);
  1073. if (handle == INVALID_HANDLE_VALUE)
  1074. return false;
  1075. bool result = false;
  1076. do
  1077. {
  1078. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1079. {
  1080. // skip . and .. directories
  1081. if (String::compare(findData.cFileName, TEXT( "." ) ) == 0 || String::compare(findData.cFileName, TEXT( ".." ) ) == 0)
  1082. continue;
  1083. #ifdef UNICODE
  1084. char fileName[ 1024 ];
  1085. convertUTF16toUTF8( findData.cFileName, fileName );
  1086. #else
  1087. char* fileName = findData.cFileName;
  1088. #endif
  1089. if( Platform::isExcludedDirectory( fileName ) )
  1090. continue;
  1091. result = true;
  1092. break;
  1093. }
  1094. }
  1095. while(FindNextFile(handle, &findData));
  1096. FindClose(handle);
  1097. Platform::clearExcludedDirectories();
  1098. return result;
  1099. }
  1100. static bool recurseDumpDirectories(const char *basePath, const char *subPath, Vector<StringTableEntry> &directoryVector, S32 currentDepth, S32 recurseDepth, bool noBasePath)
  1101. {
  1102. TempAlloc< char > search( 1024 );
  1103. //-----------------------------------------------------------------------------
  1104. // Compose our search string - Format : ([path]/[subpath]/*)
  1105. //-----------------------------------------------------------------------------
  1106. dsize_t trLen = basePath ? dStrlen(basePath) : 0;
  1107. dsize_t subtrLen = subPath ? dStrlen(subPath) : 0;
  1108. char trail = trLen > 0 ? basePath[ trLen - 1 ] : '\0';
  1109. char subTrail = subtrLen > 0 ? subPath[ subtrLen - 1 ] : '\0';
  1110. char subLead = subtrLen > 0 ? subPath[0] : '\0';
  1111. if( trail == '/' )
  1112. {
  1113. // we have a sub path and it's not an empty string
  1114. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1115. {
  1116. if( subTrail == '/' )
  1117. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1118. else
  1119. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1120. }
  1121. else
  1122. dSprintf( search, search.size, "%s*", basePath );
  1123. }
  1124. else
  1125. {
  1126. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1127. if( subTrail == '/' )
  1128. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1129. else
  1130. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1131. else
  1132. dSprintf(search, search.size, "%s/*", basePath );
  1133. }
  1134. #ifdef UNICODE
  1135. TempAlloc< WCHAR > searchStr( dStrlen( search ) + 1 );
  1136. convertUTF8toUTF16N( search, searchStr, searchStr.size );
  1137. #else
  1138. char* searchStr = search;
  1139. #endif
  1140. backslash( searchStr );
  1141. //-----------------------------------------------------------------------------
  1142. // See if we get any hits
  1143. //-----------------------------------------------------------------------------
  1144. WIN32_FIND_DATA findData;
  1145. HANDLE handle = FindFirstFile(searchStr, &findData);
  1146. if (handle == INVALID_HANDLE_VALUE)
  1147. return false;
  1148. //-----------------------------------------------------------------------------
  1149. // add path to our return list ( provided it is valid )
  1150. //-----------------------------------------------------------------------------
  1151. if( !Platform::isExcludedDirectory( subPath ) )
  1152. {
  1153. if( noBasePath )
  1154. {
  1155. // We have a path and it's not an empty string or an excluded directory
  1156. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1157. directoryVector.push_back( StringTable->insert( subPath ) );
  1158. }
  1159. else
  1160. {
  1161. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1162. {
  1163. char szPath[1024];
  1164. dMemset(szPath, 0, 1024);
  1165. if (trail == '/')
  1166. {
  1167. if (subLead == '/')
  1168. dSprintf(szPath, 1024, "%s%s", basePath, &subPath[1]);
  1169. else
  1170. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  1171. }
  1172. else
  1173. {
  1174. if (subLead == '/')
  1175. dSprintf(szPath, 1024, "%s%s", basePath, subPath);
  1176. else
  1177. dSprintf(szPath, 1024, "%s/%s", basePath, subPath);
  1178. }
  1179. directoryVector.push_back(StringTable->insert(szPath));
  1180. }
  1181. else
  1182. directoryVector.push_back( StringTable->insert( basePath ) );
  1183. }
  1184. }
  1185. //-----------------------------------------------------------------------------
  1186. // Iterate through and grab valid directories
  1187. //-----------------------------------------------------------------------------
  1188. #ifdef UNICODE
  1189. TempAlloc< char > fileName( 1024 );
  1190. #endif
  1191. do
  1192. {
  1193. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1194. {
  1195. // skip . and .. directories
  1196. if (String::compare(findData.cFileName, TEXT( "." )) == 0 || String::compare(findData.cFileName, TEXT( ".." )) == 0)
  1197. continue;
  1198. #ifdef UNICODE
  1199. convertUTF16toUTF8N( findData.cFileName, fileName, fileName.size );
  1200. #else
  1201. char* fileName = findData.cFileName;
  1202. #endif
  1203. // skip excluded directories
  1204. if( Platform::isExcludedDirectory( fileName ) )
  1205. continue;
  1206. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ))
  1207. {
  1208. if( subTrail == '/' )
  1209. dSprintf(search, search.size, "%s%s", subPath, fileName.ptr);
  1210. else
  1211. dSprintf(search, search.size, "%s/%s", subPath, fileName.ptr);
  1212. char* child = search;
  1213. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1214. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1215. }
  1216. else
  1217. {
  1218. char* child;
  1219. if( trail == '/' )
  1220. child = fileName;
  1221. else
  1222. {
  1223. dSprintf(search, search.size, "/%s", fileName.ptr);
  1224. child = search;
  1225. }
  1226. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1227. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1228. }
  1229. }
  1230. }
  1231. while(FindNextFile(handle, &findData));
  1232. FindClose(handle);
  1233. return true;
  1234. }
  1235. bool Platform::dumpDirectories( const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath )
  1236. {
  1237. bool retVal = recurseDumpDirectories( path, "", directoryVector, -1, depth, noBasePath );
  1238. clearExcludedDirectories();
  1239. return retVal;
  1240. }
  1241. //-----------------------------------------------------------------------------
  1242. StringTableEntry osGetTemporaryDirectory()
  1243. {
  1244. TCHAR buf[ 1024 ];
  1245. const U32 bufSize = sizeof( buf ) / sizeof( buf[ 0 ] );
  1246. DWORD len = GetTempPath( sizeof( buf ) / sizeof( buf[ 0 ] ), buf );
  1247. TempAlloc< TCHAR > temp;
  1248. TCHAR* buffer = buf;
  1249. if( len > bufSize - 1 )
  1250. {
  1251. temp = TempAlloc< TCHAR >( len + 1 );
  1252. buffer = temp;
  1253. GetTempPath( len + 1, buffer );
  1254. }
  1255. // Remove the trailing slash
  1256. buffer[len-1] = 0;
  1257. #ifdef UNICODE
  1258. TempAlloc< char > dirBuffer( len * 3 + 1 );
  1259. char* dir = dirBuffer;
  1260. convertUTF16toUTF8N( buffer, dir, dirBuffer.size );
  1261. #else
  1262. char* dir = buf;
  1263. #endif
  1264. forwardslash(dir);
  1265. return StringTable->insert(dir);
  1266. }