winFileio.cpp 45 KB

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