platformFileIO.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "platformFileIO_ScriptBinding.h"
  29. //-----------------------------------------------------------------------------
  30. StringTableEntry Platform::getTemporaryDirectory()
  31. {
  32. StringTableEntry path = osGetTemporaryDirectory();
  33. if(! Platform::isDirectory(path))
  34. path = Platform::getCurrentDirectory();
  35. return path;
  36. }
  37. StringTableEntry Platform::getTemporaryFileName()
  38. {
  39. char buf[512];
  40. StringTableEntry path = Platform::getTemporaryDirectory();
  41. dSprintf(buf, sizeof(buf), "%s/tgb.%08x.%02x.tmp", path, Platform::getRealMilliseconds(), U32(Platform::getRandom() * 255));
  42. // [tom, 9/7/2006] This shouldn't be needed, but just in case
  43. if(Platform::isFile(buf))
  44. return Platform::getTemporaryFileName();
  45. return StringTable->insert(buf);
  46. }
  47. //-----------------------------------------------------------------------------
  48. static char filePathBuffer[1024];
  49. static bool deleteDirectoryRecusrive( const char* pPath )
  50. {
  51. // Sanity!
  52. AssertFatal( pPath != NULL, "Cannot delete directory that is NULL." );
  53. // Find directories.
  54. Vector<StringTableEntry> directories;
  55. if ( !Platform::dumpDirectories( pPath, directories, 0 ) )
  56. {
  57. // Warn.
  58. Con::warnf( "Could not retrieve sub-directories of '%s'.", pPath );
  59. return false;
  60. }
  61. // Iterate directories.
  62. for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )
  63. {
  64. // Fetch base path.
  65. StringTableEntry basePath = *basePathItr;
  66. // Skip if the base path.
  67. if ( basePathItr == directories.begin() && dStrcmp( pPath, basePath ) == 0 )
  68. continue;
  69. // Delete any directories recursively.
  70. if ( !deleteDirectoryRecusrive( basePath ) )
  71. return false;
  72. }
  73. // Find files.
  74. Vector<Platform::FileInfo> files;
  75. if ( !Platform::dumpPath( pPath, files, 0 ) )
  76. {
  77. // Warn.
  78. Con::warnf( "Could not retrieve files for directory '%s'.", pPath );
  79. return false;
  80. }
  81. // Iterate files.
  82. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  83. {
  84. // Format file.
  85. dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", fileItr->pFullPath, fileItr->pFileName );
  86. // Delete file.
  87. if ( !Platform::fileDelete( filePathBuffer ) )
  88. {
  89. // Warn.
  90. Con::warnf( "Could not delete file '%s'.", filePathBuffer );
  91. return false;
  92. }
  93. }
  94. // Delete the directory.
  95. if ( !Platform::fileDelete( pPath ) )
  96. {
  97. // Warn.
  98. Con::warnf( "Could not delete directory '%s'.", pPath );
  99. return false;
  100. }
  101. return true;
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool Platform::deleteDirectory( const char* pPath )
  105. {
  106. // Sanity!
  107. AssertFatal( pPath != NULL, "Cannot delete directory that is NULL." );
  108. // Is the path a file?
  109. if ( Platform::isFile( pPath ) )
  110. {
  111. // Yes, so warn.
  112. Con::warnf( "Cannot delete directory '%s' as it specifies a file.", pPath );
  113. return false;
  114. }
  115. // Expand module location.
  116. char pathBuffer[1024];
  117. Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath, NULL, true );
  118. // Delete directory recursively.
  119. return deleteDirectoryRecusrive( pathBuffer );
  120. }
  121. //-----------------------------------------------------------------------------
  122. static StringTableEntry sgMainCSDir = NULL;
  123. StringTableEntry Platform::getMainDotCsDir()
  124. {
  125. if(sgMainCSDir == NULL)
  126. sgMainCSDir = Platform::getExecutablePath();
  127. return sgMainCSDir;
  128. }
  129. void Platform::setMainDotCsDir(const char *dir)
  130. {
  131. sgMainCSDir = StringTable->insert(dir);
  132. }
  133. //-----------------------------------------------------------------------------
  134. typedef Vector<char*> CharVector;
  135. static CharVector gPlatformDirectoryExcludeList;
  136. void Platform::addExcludedDirectory(const char *pDir)
  137. {
  138. gPlatformDirectoryExcludeList.push_back(dStrdup(pDir));
  139. }
  140. void Platform::clearExcludedDirectories()
  141. {
  142. while(gPlatformDirectoryExcludeList.size())
  143. {
  144. dFree(gPlatformDirectoryExcludeList.last());
  145. gPlatformDirectoryExcludeList.pop_back();
  146. }
  147. }
  148. bool Platform::isExcludedDirectory(const char *pDir)
  149. {
  150. for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++)
  151. if(!dStricmp(pDir, *i))
  152. return true;
  153. return false;
  154. }
  155. //-----------------------------------------------------------------------------
  156. inline void catPath(char *dst, const char *src, U32 len)
  157. {
  158. if(*dst != '/')
  159. {
  160. ++dst; --len;
  161. *dst = '/';
  162. }
  163. ++dst; --len;
  164. dStrncpy(dst, src, len);
  165. dst[len - 1] = 0;
  166. }
  167. // converts the posix root path "/" to "c:/" for win32
  168. // FIXME: this is not ideal. the c: drive is not guaranteed to exist.
  169. #if defined(TORQUE_OS_WIN)
  170. static inline void _resolveLeadingSlash(char* buf, U32 size)
  171. {
  172. if(buf[0] != '/')
  173. return;
  174. AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long");
  175. dMemmove(buf + 2, buf, dStrlen(buf));
  176. buf[0] = 'c';
  177. buf[1] = ':';
  178. }
  179. #endif
  180. char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */)
  181. {
  182. char bspath[1024];
  183. dStrncpy(bspath, path, sizeof(bspath));
  184. bspath[sizeof(bspath)-1] = 0;
  185. for(U32 i = 0;i < dStrlen(bspath);++i)
  186. {
  187. if(bspath[i] == '\\')
  188. bspath[i] = '/';
  189. }
  190. if(Platform::isFullPath(bspath))
  191. {
  192. // Already a full path
  193. #if defined(TORQUE_OS_WIN)
  194. _resolveLeadingSlash(bspath, sizeof(bspath));
  195. #endif
  196. dStrncpy(buffer, bspath, size);
  197. buffer[size-1] = 0;
  198. return buffer;
  199. }
  200. if(cwd == NULL)
  201. cwd = Platform::getMainDotCsDir();
  202. dStrncpy(buffer, cwd, size);
  203. buffer[size-1] = 0;
  204. char *ptr = bspath;
  205. char *slash = NULL;
  206. char *endptr = buffer + dStrlen(buffer) - 1;
  207. do
  208. {
  209. slash = dStrchr(ptr, '/');
  210. if(slash)
  211. {
  212. *slash = 0;
  213. // Directory
  214. if(dStrcmp(ptr, "..") == 0)
  215. {
  216. // Parent
  217. endptr = dStrrchr(buffer, '/');
  218. if (endptr)
  219. *endptr-- = 0;
  220. }
  221. else if(dStrcmp(ptr, ".") == 0)
  222. {
  223. // Current dir
  224. }
  225. else if(endptr)
  226. {
  227. catPath(endptr, ptr, (U32)(size - (endptr - buffer)));
  228. endptr += dStrlen(endptr) - 1;
  229. }
  230. ptr = slash + 1;
  231. }
  232. else if(endptr)
  233. {
  234. // File
  235. catPath(endptr, ptr, (U32)(size - (endptr - buffer)));
  236. endptr += dStrlen(endptr) - 1;
  237. }
  238. } while(slash);
  239. return buffer;
  240. }
  241. bool Platform::isFullPath(const char *path)
  242. {
  243. // Quick way out
  244. if(path[0] == '/' || path[1] == ':')
  245. return true;
  246. return false;
  247. }
  248. //-----------------------------------------------------------------------------
  249. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  250. {
  251. char buffer[1024];
  252. if(path[0] != '/' && path[1] != ':')
  253. {
  254. // It's already relative, bail
  255. return StringTable->insert(path);
  256. }
  257. // [tom, 12/13/2006] We need a trailing / for this to work, so add one if needed
  258. if(*(to + dStrlen(to) - 1) != '/')
  259. {
  260. dSprintf(buffer, sizeof(buffer), "%s/", to);
  261. to = StringTable->insert(buffer);
  262. }
  263. const char *pathPtr, *toPtr, *branch = path;
  264. char *bufPtr = buffer;
  265. // Find common part of path
  266. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  267. {
  268. if(*pathPtr == '/')
  269. branch = pathPtr;
  270. }
  271. if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  272. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  273. {
  274. *bufPtr++ = '.';
  275. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  276. *bufPtr++ = '/';
  277. *bufPtr = 0;
  278. return StringTable->insert(buffer);
  279. }
  280. if(*toPtr == '/' && *pathPtr == 0)
  281. branch = pathPtr;
  282. // Figure out parent dirs
  283. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  284. {
  285. if(*toPtr == '/' && *(toPtr + 1) != 0)
  286. {
  287. *bufPtr++ = '.';
  288. *bufPtr++ = '.';
  289. *bufPtr++ = '/';
  290. }
  291. }
  292. *bufPtr = 0;
  293. // Copy the rest
  294. if(*branch)
  295. dStrcpy(bufPtr, branch + 1);
  296. else
  297. *--bufPtr = 0;
  298. return StringTable->insert(buffer);
  299. }
  300. //-----------------------------------------------------------------------------
  301. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  302. {
  303. U32 len = dStrlen(base);
  304. if(dStrnicmp(path, base, len) == 0)
  305. {
  306. if(*(path + len) == '/') ++len;
  307. return StringTable->insert(path + len, true);
  308. }
  309. return NULL;
  310. }
  311. StringTableEntry Platform::stripBasePath(const char *path)
  312. {
  313. StringTableEntry str = NULL;
  314. str = tryStripBasePath( path, Platform::getMainDotCsDir() );
  315. if ( str )
  316. return str;
  317. str = tryStripBasePath( path, Platform::getCurrentDirectory() );
  318. if ( str )
  319. return str;
  320. str = tryStripBasePath( path, Platform::getPrefsPath() );
  321. if ( str )
  322. return str;
  323. return path;
  324. }
  325. //-----------------------------------------------------------------------------
  326. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  327. {
  328. char buf[1024];
  329. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  330. if ( file )
  331. {
  332. if ( dStrstr( file, ".." ) || dStrstr( file, "./" ) || dStrstr( file, "~/" ) )
  333. {
  334. Con::errorf( "getPrefsPath - Filename (%s) cannot be relative.", file );
  335. return NULL;
  336. }
  337. dSprintf( buf, sizeof( buf ), "%s/%s", Platform::getUserDataDirectory(), file );
  338. }
  339. else
  340. {
  341. dSprintf( buf, sizeof( buf ), "%s", Platform::getUserDataDirectory() );
  342. }
  343. return StringTable->insert(buf, true);
  344. #endif
  345. const char *company = Con::getVariable("$Game::CompanyName");
  346. if(company == NULL || *company == 0)
  347. company = "GarageGames";
  348. const char *appName = Con::getVariable("$Game::ProductName");
  349. if(appName == NULL || *appName == 0)
  350. appName = TORQUE_GAME_NAME;
  351. if(file)
  352. {
  353. if(dStrstr(file, "..") || dStrstr(file, "./") || dStrstr(file, "~/"))
  354. {
  355. Con::errorf("getPrefsPath - Filename (%s) cannot be relative.", file);
  356. return NULL;
  357. }
  358. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  359. }
  360. else
  361. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  362. return StringTable->insert(buf, true);
  363. }
  364. //-----------------------------------------------------------------------------