platformFileIO.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/platformFileIO.h"
  24. #include "collection/vector.h"
  25. #include "console/console.h"
  26. #include "string/stringTable.h"
  27. #include "io/resource/resourceManager.h"
  28. //-----------------------------------------------------------------------------
  29. StringTableEntry Platform::getTemporaryDirectory()
  30. {
  31. StringTableEntry path = osGetTemporaryDirectory();
  32. if(! Platform::isDirectory(path))
  33. path = Platform::getCurrentDirectory();
  34. return path;
  35. }
  36. ConsoleFunction(getTemporaryDirectory, const char *, 1, 1, "() Gets the path to the system's temporary directory\n"
  37. "@return Returns the path to the temporary directory as a string")
  38. {
  39. return Platform::getTemporaryDirectory();
  40. }
  41. StringTableEntry Platform::getTemporaryFileName()
  42. {
  43. char buf[512];
  44. StringTableEntry path = Platform::getTemporaryDirectory();
  45. dSprintf(buf, sizeof(buf), "%s/tgb.%08x.%02x.tmp", path, Platform::getRealMilliseconds(), U32(Platform::getRandom() * 255));
  46. // [tom, 9/7/2006] This shouldn't be needed, but just in case
  47. if(Platform::isFile(buf))
  48. return Platform::getTemporaryFileName();
  49. return StringTable->insert(buf);
  50. }
  51. ConsoleFunction(getTemporaryFileName, const char *, 1, 1, "() Generates a temporary filename for use.\n"
  52. "@return Returns the formatted temporary filename for use")
  53. {
  54. return Platform::getTemporaryFileName();
  55. }
  56. //-----------------------------------------------------------------------------
  57. static char filePathBuffer[1024];
  58. static bool deleteDirectoryRecusrive( const char* pPath )
  59. {
  60. // Sanity!
  61. AssertFatal( pPath != NULL, "Cannot delete directory that is NULL." );
  62. // Find directories.
  63. Vector<StringTableEntry> directories;
  64. if ( !Platform::dumpDirectories( pPath, directories, 0 ) )
  65. {
  66. // Warn.
  67. Con::warnf( "Could not retrieve sub-directories of '%s'.", pPath );
  68. return false;
  69. }
  70. // Iterate directories.
  71. for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )
  72. {
  73. // Fetch base path.
  74. StringTableEntry basePath = *basePathItr;
  75. // Skip if the base path.
  76. if ( basePathItr == directories.begin() && dStrcmp( pPath, basePath ) == 0 )
  77. continue;
  78. // Delete any directories recursively.
  79. if ( !deleteDirectoryRecusrive( basePath ) )
  80. return false;
  81. }
  82. // Find files.
  83. Vector<Platform::FileInfo> files;
  84. if ( !Platform::dumpPath( pPath, files, 0 ) )
  85. {
  86. // Warn.
  87. Con::warnf( "Could not retrieve files for directory '%s'.", pPath );
  88. return false;
  89. }
  90. // Iterate files.
  91. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  92. {
  93. // Format file.
  94. dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", fileItr->pFullPath, fileItr->pFileName );
  95. // Delete file.
  96. if ( !Platform::fileDelete( filePathBuffer ) )
  97. {
  98. // Warn.
  99. Con::warnf( "Could not delete file '%s'.", filePathBuffer );
  100. return false;
  101. }
  102. }
  103. // Delete the directory.
  104. if ( !Platform::fileDelete( pPath ) )
  105. {
  106. // Warn.
  107. Con::warnf( "Could not delete directory '%s'.", pPath );
  108. return false;
  109. }
  110. return true;
  111. }
  112. //-----------------------------------------------------------------------------
  113. bool Platform::deleteDirectory( const char* pPath )
  114. {
  115. // Sanity!
  116. AssertFatal( pPath != NULL, "Cannot delete directory that is NULL." );
  117. // Is the path a file?
  118. if ( Platform::isFile( pPath ) )
  119. {
  120. // Yes, so warn.
  121. Con::warnf( "Cannot delete directory '%s' as it specifies a file.", pPath );
  122. return false;
  123. }
  124. // Expand module location.
  125. char pathBuffer[1024];
  126. Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath, NULL, true );
  127. // Delete directory recursively.
  128. return deleteDirectoryRecusrive( pathBuffer );
  129. }
  130. //-----------------------------------------------------------------------------
  131. static StringTableEntry sgMainCSDir = NULL;
  132. StringTableEntry Platform::getMainDotCsDir()
  133. {
  134. if(sgMainCSDir == NULL)
  135. sgMainCSDir = Platform::getExecutablePath();
  136. return sgMainCSDir;
  137. }
  138. void Platform::setMainDotCsDir(const char *dir)
  139. {
  140. sgMainCSDir = StringTable->insert(dir);
  141. }
  142. //-----------------------------------------------------------------------------
  143. typedef Vector<char*> CharVector;
  144. static CharVector gPlatformDirectoryExcludeList;
  145. void Platform::addExcludedDirectory(const char *pDir)
  146. {
  147. gPlatformDirectoryExcludeList.push_back(dStrdup(pDir));
  148. }
  149. void Platform::clearExcludedDirectories()
  150. {
  151. while(gPlatformDirectoryExcludeList.size())
  152. {
  153. dFree(gPlatformDirectoryExcludeList.last());
  154. gPlatformDirectoryExcludeList.pop_back();
  155. }
  156. }
  157. bool Platform::isExcludedDirectory(const char *pDir)
  158. {
  159. for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++)
  160. if(!dStricmp(pDir, *i))
  161. return true;
  162. return false;
  163. }
  164. //-----------------------------------------------------------------------------
  165. inline void catPath(char *dst, const char *src, U32 len)
  166. {
  167. if(*dst != '/')
  168. {
  169. ++dst; --len;
  170. *dst = '/';
  171. }
  172. ++dst; --len;
  173. dStrncpy(dst, src, len);
  174. dst[len - 1] = 0;
  175. }
  176. // converts the posix root path "/" to "c:/" for win32
  177. // FIXME: this is not ideal. the c: drive is not guaranteed to exist.
  178. #if defined(TORQUE_OS_WIN32)
  179. static inline void _resolveLeadingSlash(char* buf, U32 size)
  180. {
  181. if(buf[0] != '/')
  182. return;
  183. AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long");
  184. dMemmove(buf + 2, buf, dStrlen(buf));
  185. buf[0] = 'c';
  186. buf[1] = ':';
  187. }
  188. #endif
  189. char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */)
  190. {
  191. char bspath[1024];
  192. dStrncpy(bspath, path, sizeof(bspath));
  193. bspath[sizeof(bspath)-1] = 0;
  194. for(U32 i = 0;i < dStrlen(bspath);++i)
  195. {
  196. if(bspath[i] == '\\')
  197. bspath[i] = '/';
  198. }
  199. if(Platform::isFullPath(bspath))
  200. {
  201. // Already a full path
  202. #if defined(TORQUE_OS_WIN32)
  203. _resolveLeadingSlash(bspath, sizeof(bspath));
  204. #endif
  205. dStrncpy(buffer, bspath, size);
  206. buffer[size-1] = 0;
  207. return buffer;
  208. }
  209. if(cwd == NULL)
  210. cwd = Platform::getCurrentDirectory();
  211. dStrncpy(buffer, cwd, size);
  212. buffer[size-1] = 0;
  213. char *ptr = bspath;
  214. char *slash = NULL;
  215. char *endptr = buffer + dStrlen(buffer) - 1;
  216. do
  217. {
  218. slash = dStrchr(ptr, '/');
  219. if(slash)
  220. {
  221. *slash = 0;
  222. // Directory
  223. if(dStrcmp(ptr, "..") == 0)
  224. {
  225. // Parent
  226. endptr = dStrrchr(buffer, '/');
  227. }
  228. else if(dStrcmp(ptr, ".") == 0)
  229. {
  230. // Current dir
  231. }
  232. else if(endptr)
  233. {
  234. catPath(endptr, ptr, size - (endptr - buffer));
  235. endptr += dStrlen(endptr) - 1;
  236. }
  237. ptr = slash + 1;
  238. }
  239. else if(endptr)
  240. {
  241. // File
  242. catPath(endptr, ptr, size - (endptr - buffer));
  243. endptr += dStrlen(endptr) - 1;
  244. }
  245. } while(slash);
  246. return buffer;
  247. }
  248. bool Platform::isFullPath(const char *path)
  249. {
  250. // Quick way out
  251. if(path[0] == '/' || path[1] == ':')
  252. return true;
  253. return false;
  254. }
  255. //-----------------------------------------------------------------------------
  256. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  257. {
  258. char buffer[1024];
  259. if(path[0] != '/' && path[1] != ':')
  260. {
  261. // It's already relative, bail
  262. return StringTable->insert(path);
  263. }
  264. // [tom, 12/13/2006] We need a trailing / for this to work, so add one if needed
  265. if(*(to + dStrlen(to) - 1) != '/')
  266. {
  267. dSprintf(buffer, sizeof(buffer), "%s/", to);
  268. to = StringTable->insert(buffer);
  269. }
  270. const char *pathPtr, *toPtr, *branch = path;
  271. char *bufPtr = buffer;
  272. // Find common part of path
  273. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  274. {
  275. if(*pathPtr == '/')
  276. branch = pathPtr;
  277. }
  278. if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  279. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  280. {
  281. *bufPtr++ = '.';
  282. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  283. *bufPtr++ = '/';
  284. *bufPtr = 0;
  285. return StringTable->insert(buffer);
  286. }
  287. if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0))
  288. branch = pathPtr;
  289. // Figure out parent dirs
  290. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  291. {
  292. if(*toPtr == '/' && *(toPtr + 1) != 0)
  293. {
  294. *bufPtr++ = '.';
  295. *bufPtr++ = '.';
  296. *bufPtr++ = '/';
  297. }
  298. }
  299. *bufPtr = 0;
  300. // Copy the rest
  301. if(*branch)
  302. dStrcpy(bufPtr, branch + 1);
  303. else
  304. *--bufPtr = 0;
  305. return StringTable->insert(buffer);
  306. }
  307. //-----------------------------------------------------------------------------
  308. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  309. {
  310. U32 len = dStrlen(base);
  311. if(dStrnicmp(path, base, len) == 0)
  312. {
  313. if(*(path + len) == '/') ++len;
  314. return StringTable->insert(path + len, true);
  315. }
  316. return NULL;
  317. }
  318. StringTableEntry Platform::stripBasePath(const char *path)
  319. {
  320. StringTableEntry str = NULL;
  321. str = tryStripBasePath( path, Platform::getMainDotCsDir() );
  322. if ( str )
  323. return str;
  324. str = tryStripBasePath( path, Platform::getCurrentDirectory() );
  325. if ( str )
  326. return str;
  327. str = tryStripBasePath( path, Platform::getPrefsPath() );
  328. if ( str )
  329. return str;
  330. return path;
  331. }
  332. //-----------------------------------------------------------------------------
  333. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  334. {
  335. char buf[1024];
  336. #ifdef TORQUE_OS_IOS
  337. if ( file )
  338. {
  339. if ( dStrstr( file, ".." ) || dStrstr( file, "./" ) || dStrstr( file, "~/" ) )
  340. {
  341. Con::errorf( "getPrefsPath - Filename (%s) cannot be relative.", file );
  342. return NULL;
  343. }
  344. dSprintf( buf, sizeof( buf ), "%s/%s", Platform::getUserDataDirectory(), file );
  345. }
  346. else
  347. {
  348. dSprintf( buf, sizeof( buf ), "%s", Platform::getUserDataDirectory() );
  349. }
  350. return StringTable->insert(buf, true);
  351. #endif
  352. const char *company = Con::getVariable("$Game::CompanyName");
  353. if(company == NULL || *company == 0)
  354. company = "GarageGames";
  355. const char *appName = Con::getVariable("$Game::ProductName");
  356. if(appName == NULL || *appName == 0)
  357. appName = TORQUE_GAME_NAME;
  358. if(file)
  359. {
  360. if(dStrstr(file, "..") || dStrstr(file, "./") || dStrstr(file, "~/"))
  361. {
  362. Con::errorf("getPrefsPath - Filename (%s) cannot be relative.", file);
  363. return NULL;
  364. }
  365. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  366. }
  367. else
  368. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  369. return StringTable->insert(buf, true);
  370. }
  371. //-----------------------------------------------------------------------------
  372. ConsoleFunction(getUserDataDirectory, const char*, 1, 1, "()\n"
  373. "@return Returns a string to the directory storing the user's data")
  374. {
  375. return Platform::getUserDataDirectory();
  376. }
  377. ConsoleFunction(getUserHomeDirectory, const char*, 1, 1, "() \n"
  378. "@return Returns the path to the user's home directory.")
  379. {
  380. return Platform::getUserHomeDirectory();
  381. }