winFileio.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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 );
  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);
  81. dStrcpy(newf, newName);
  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 );
  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 );
  117. dStrcpy( to, toName );
  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, "/");
  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, "/");
  163. dStrcat(toFile, fileInfo[i].pFileName);
  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);
  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 (dStrcmp( findData.cFileName, TEXT( "." ) ) == 0 || dStrcmp( 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 );
  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 );
  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 );
  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 );
  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 );
  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 );
  908. dStrcpy( dir, pDir );
  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 (dStrcmp(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 );
  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 (dStrcmp(findData.cFileName, TEXT( "." ) ) == 0 || dStrcmp(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. if( trail == '/' )
  1111. {
  1112. // we have a sub path and it's not an empty string
  1113. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1114. {
  1115. if( subTrail == '/' )
  1116. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1117. else
  1118. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1119. }
  1120. else
  1121. dSprintf( search, search.size, "%s*", basePath );
  1122. }
  1123. else
  1124. {
  1125. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1126. if( subTrail == '/' )
  1127. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1128. else
  1129. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1130. else
  1131. dSprintf(search, search.size, "%s/*", basePath );
  1132. }
  1133. #ifdef UNICODE
  1134. TempAlloc< WCHAR > searchStr( dStrlen( search ) + 1 );
  1135. convertUTF8toUTF16N( search, searchStr, searchStr.size );
  1136. #else
  1137. char* searchStr = search;
  1138. #endif
  1139. backslash( searchStr );
  1140. //-----------------------------------------------------------------------------
  1141. // See if we get any hits
  1142. //-----------------------------------------------------------------------------
  1143. WIN32_FIND_DATA findData;
  1144. HANDLE handle = FindFirstFile(searchStr, &findData);
  1145. if (handle == INVALID_HANDLE_VALUE)
  1146. return false;
  1147. //-----------------------------------------------------------------------------
  1148. // add path to our return list ( provided it is valid )
  1149. //-----------------------------------------------------------------------------
  1150. if( !Platform::isExcludedDirectory( subPath ) )
  1151. {
  1152. if( noBasePath )
  1153. {
  1154. // We have a path and it's not an empty string or an excluded directory
  1155. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1156. directoryVector.push_back( StringTable->insert( subPath ) );
  1157. }
  1158. else
  1159. {
  1160. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1161. {
  1162. char szPath [ 1024 ];
  1163. dMemset( szPath, 0, 1024 );
  1164. if( trail != '/' )
  1165. dSprintf( szPath, 1024, "%s%s", basePath, subPath );
  1166. else
  1167. dSprintf( szPath, 1024, "%s%s", basePath, &subPath[1] );
  1168. directoryVector.push_back( StringTable->insert( szPath ) );
  1169. }
  1170. else
  1171. directoryVector.push_back( StringTable->insert( basePath ) );
  1172. }
  1173. }
  1174. //-----------------------------------------------------------------------------
  1175. // Iterate through and grab valid directories
  1176. //-----------------------------------------------------------------------------
  1177. #ifdef UNICODE
  1178. TempAlloc< char > fileName( 1024 );
  1179. #endif
  1180. do
  1181. {
  1182. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1183. {
  1184. // skip . and .. directories
  1185. if (dStrcmp(findData.cFileName, TEXT( "." )) == 0 || dStrcmp(findData.cFileName, TEXT( ".." )) == 0)
  1186. continue;
  1187. #ifdef UNICODE
  1188. convertUTF16toUTF8N( findData.cFileName, fileName, fileName.size );
  1189. #else
  1190. char* fileName = findData.cFileName;
  1191. #endif
  1192. // skip excluded directories
  1193. if( Platform::isExcludedDirectory( fileName ) )
  1194. continue;
  1195. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ))
  1196. {
  1197. if( subTrail == '/' )
  1198. dSprintf(search, search.size, "%s%s", subPath, fileName.ptr);
  1199. else
  1200. dSprintf(search, search.size, "%s/%s", subPath, fileName.ptr);
  1201. char* child = search;
  1202. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1203. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1204. }
  1205. else
  1206. {
  1207. char* child;
  1208. if( trail == '/' )
  1209. child = fileName;
  1210. else
  1211. {
  1212. dSprintf(search, search.size, "/%s", fileName.ptr);
  1213. child = search;
  1214. }
  1215. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1216. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1217. }
  1218. }
  1219. }
  1220. while(FindNextFile(handle, &findData));
  1221. FindClose(handle);
  1222. return true;
  1223. }
  1224. bool Platform::dumpDirectories( const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath )
  1225. {
  1226. bool retVal = recurseDumpDirectories( path, "", directoryVector, -1, depth, noBasePath );
  1227. clearExcludedDirectories();
  1228. return retVal;
  1229. }
  1230. //-----------------------------------------------------------------------------
  1231. StringTableEntry osGetTemporaryDirectory()
  1232. {
  1233. TCHAR buf[ 1024 ];
  1234. const U32 bufSize = sizeof( buf ) / sizeof( buf[ 0 ] );
  1235. DWORD len = GetTempPath( sizeof( buf ) / sizeof( buf[ 0 ] ), buf );
  1236. TempAlloc< TCHAR > temp;
  1237. TCHAR* buffer = buf;
  1238. if( len > bufSize - 1 )
  1239. {
  1240. temp = TempAlloc< TCHAR >( len + 1 );
  1241. buffer = temp;
  1242. GetTempPath( len + 1, buffer );
  1243. }
  1244. // Remove the trailing slash
  1245. buffer[len-1] = 0;
  1246. #ifdef UNICODE
  1247. TempAlloc< char > dirBuffer( len * 3 + 1 );
  1248. char* dir = dirBuffer;
  1249. convertUTF16toUTF8N( buffer, dir, dirBuffer.size );
  1250. #else
  1251. char* dir = buf;
  1252. #endif
  1253. forwardslash(dir);
  1254. return StringTable->insert(dir);
  1255. }