posixVolume.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 <unistd.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include "core/crc.h"
  26. #include "core/frameAllocator.h"
  27. #include "core/util/str.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "platform/platformVolume.h"
  30. #include "platformPOSIX/posixVolume.h"
  31. #ifndef PATH_MAX
  32. #include <sys/syslimits.h>
  33. #endif
  34. #ifndef NGROUPS_UMAX
  35. #define NGROUPS_UMAX 32
  36. #endif
  37. //#define DEBUG_SPEW
  38. namespace Torque
  39. {
  40. namespace Posix
  41. {
  42. //-----------------------------------------------------------------------------
  43. static String buildFileName(const String& prefix,const Path& path)
  44. {
  45. // Need to join the path (minus the root) with our
  46. // internal path name.
  47. String file = prefix;
  48. file = Path::Join(file,'/',path.getPath());
  49. file = Path::Join(file,'/',path.getFileName());
  50. file = Path::Join(file,'.',path.getExtension());
  51. return file;
  52. }
  53. /*
  54. static bool isFile(const String& file)
  55. {
  56. struct stat info;
  57. if (stat(file.c_str(),&info) == 0)
  58. return S_ISREG(info.st_mode);
  59. return false;
  60. }
  61. static bool isDirectory(const String& file)
  62. {
  63. struct stat info;
  64. if (stat(file.c_str(),&info) == 0)
  65. return S_ISDIR(info.st_mode);
  66. return false;
  67. }
  68. */
  69. //-----------------------------------------------------------------------------
  70. static uid_t _Uid; // Current user id
  71. static int _GroupCount; // Number of groups in the table
  72. static gid_t _Groups[NGROUPS_UMAX+1]; // Table of all the user groups
  73. static void copyStatAttributes(const struct stat& info, FileNode::Attributes* attr)
  74. {
  75. // We need to user and group id's in order to determin file
  76. // read-only access permission. This information is only retrieved
  77. // once per execution.
  78. if (!_Uid)
  79. {
  80. _Uid = getuid();
  81. _GroupCount = getgroups(NGROUPS_UMAX,_Groups);
  82. _Groups[_GroupCount++] = getegid();
  83. }
  84. // Fill in the return struct. The read-only flag is determined
  85. // by comparing file user and group ownership.
  86. attr->flags = 0;
  87. if (S_ISDIR(info.st_mode))
  88. attr->flags |= FileNode::Directory;
  89. if (S_ISREG(info.st_mode))
  90. attr->flags |= FileNode::File;
  91. if (info.st_uid == _Uid)
  92. {
  93. if (!(info.st_mode & S_IWUSR))
  94. attr->flags |= FileNode::ReadOnly;
  95. }
  96. else
  97. {
  98. S32 i = 0;
  99. for (; i < _GroupCount; i++)
  100. {
  101. if (_Groups[i] == info.st_gid)
  102. break;
  103. }
  104. if (i != _GroupCount)
  105. {
  106. if (!(info.st_mode & S_IWGRP))
  107. attr->flags |= FileNode::ReadOnly;
  108. }
  109. else
  110. {
  111. if (!(info.st_mode & S_IWOTH))
  112. attr->flags |= FileNode::ReadOnly;
  113. }
  114. }
  115. attr->size = info.st_size;
  116. attr->mtime = UnixTimeToTime(info.st_mtime);
  117. attr->atime = UnixTimeToTime(info.st_atime);
  118. }
  119. //-----------------------------------------------------------------------------
  120. PosixFileSystem::PosixFileSystem(String volume)
  121. {
  122. _volume = volume;
  123. }
  124. PosixFileSystem::~PosixFileSystem()
  125. {
  126. }
  127. static void MungeCase(char* pathName, S32 pathNameSize)
  128. {
  129. const int MaxPath = PATH_MAX;
  130. char tempBuf[MaxPath];
  131. dStrncpy(tempBuf, pathName, pathNameSize);
  132. AssertFatal(pathName[0] == '/', "PATH must be absolute");
  133. struct stat filestat;
  134. const int MaxPathEl = 200;
  135. char *currChar = pathName;
  136. char testPath[MaxPath];
  137. char pathEl[MaxPathEl];
  138. bool done = false;
  139. dStrncpy(tempBuf, "/", MaxPath);
  140. currChar++;
  141. while (!done)
  142. {
  143. char* termChar = dStrchr(currChar, '/');
  144. if (termChar == NULL)
  145. termChar = dStrchr(currChar, '\0');
  146. AssertFatal(termChar, "Can't find / or NULL terminator");
  147. S32 pathElLen = (termChar - currChar);
  148. dStrncpy(pathEl, currChar, pathElLen);
  149. pathEl[pathElLen] = '\0';
  150. dStrncpy(testPath, tempBuf, MaxPath);
  151. dStrcat(testPath, pathEl, MaxPath);
  152. if (stat(testPath, &filestat) != -1)
  153. {
  154. dStrncpy(tempBuf, testPath, MaxPath);
  155. }
  156. else
  157. {
  158. DIR *dir = opendir(tempBuf);
  159. struct dirent* ent;
  160. bool foundMatch = false;
  161. while (dir != NULL && (ent = readdir(dir)) != NULL)
  162. {
  163. if (dStricmp(pathEl, ent->d_name) == 0)
  164. {
  165. foundMatch = true;
  166. dStrcat(tempBuf, ent->d_name, MaxPath);
  167. break;
  168. }
  169. }
  170. if (!foundMatch)
  171. dStrncpy(tempBuf, testPath, MaxPath);
  172. if (dir)
  173. closedir(dir);
  174. }
  175. if (*termChar == '/')
  176. {
  177. dStrcat(tempBuf, "/", MaxPath);
  178. termChar++;
  179. currChar = termChar;
  180. }
  181. else
  182. done = true;
  183. }
  184. dStrncpy(pathName, tempBuf, pathNameSize);
  185. }
  186. // static void MungeCase(char* pathName, S32 pathNameSize)
  187. FileNodeRef PosixFileSystem::resolve(const Path& path)
  188. {
  189. String file = buildFileName(_volume,path);
  190. struct stat info;
  191. UTF8 testPath[1024];
  192. dMemcpy(testPath, file.c_str(), file.length());
  193. testPath[file.length()] = 0x00;
  194. MungeCase(testPath, file.length());
  195. String realFile(testPath);
  196. if (stat(realFile.c_str(),&info) == 0)
  197. {
  198. // Construct the appropriate object
  199. if (S_ISREG(info.st_mode))
  200. return new PosixFile(path,realFile);
  201. if (S_ISDIR(info.st_mode))
  202. return new PosixDirectory(path,realFile);
  203. }
  204. return 0;
  205. }
  206. FileNodeRef PosixFileSystem::create(const Path& path, FileNode::Mode mode)
  207. {
  208. // The file will be created on disk when it's opened.
  209. if (mode & FileNode::File)
  210. return new PosixFile(path,buildFileName(_volume,path));
  211. // Default permissions are read/write/search/executate by everyone,
  212. // though this will be modified by the current umask
  213. if (mode & FileNode::Directory)
  214. {
  215. String file = buildFileName(_volume,path);
  216. if (mkdir(file.c_str(),S_IRWXU | S_IRWXG | S_IRWXO))
  217. return new PosixDirectory(path,file);
  218. }
  219. return 0;
  220. }
  221. bool PosixFileSystem::remove(const Path& path)
  222. {
  223. // Should probably check for outstanding files or directory objects.
  224. String file = buildFileName(_volume,path);
  225. struct stat info;
  226. int error = stat(file.c_str(),&info);
  227. if (error < 0)
  228. return false;
  229. if (S_ISDIR(info.st_mode))
  230. return !rmdir(file);
  231. return !unlink(file);
  232. }
  233. bool PosixFileSystem::rename(const Path& from,const Path& to)
  234. {
  235. String fa = buildFileName(_volume,from);
  236. String fb = buildFileName(_volume,to);
  237. if (!::rename(fa.c_str(),fb.c_str()))
  238. return true;
  239. return false;
  240. }
  241. Path PosixFileSystem::mapTo(const Path& path)
  242. {
  243. return buildFileName(_volume,path);
  244. }
  245. Path PosixFileSystem::mapFrom(const Path& path)
  246. {
  247. const String::SizeType volumePathLen = _volume.length();
  248. String pathStr = path.getFullPath();
  249. if ( _volume.compare( pathStr, volumePathLen, String::NoCase ))
  250. return Path();
  251. return pathStr.substr( volumePathLen, pathStr.length() - volumePathLen );
  252. }
  253. //-----------------------------------------------------------------------------
  254. PosixFile::PosixFile(const Path& path,String name)
  255. {
  256. _path = path;
  257. _name = name;
  258. _status = Closed;
  259. _handle = 0;
  260. }
  261. PosixFile::~PosixFile()
  262. {
  263. if (_handle)
  264. close();
  265. }
  266. Path PosixFile::getName() const
  267. {
  268. return _path;
  269. }
  270. FileNode::NodeStatus PosixFile::getStatus() const
  271. {
  272. return _status;
  273. }
  274. bool PosixFile::getAttributes(Attributes* attr)
  275. {
  276. struct stat info;
  277. int error = _handle? fstat(fileno(_handle),&info): stat(_name.c_str(),&info);
  278. if (error < 0)
  279. {
  280. _updateStatus();
  281. return false;
  282. }
  283. copyStatAttributes(info,attr);
  284. attr->name = _path;
  285. return true;
  286. }
  287. U32 PosixFile::calculateChecksum()
  288. {
  289. if (!open( Read ))
  290. return 0;
  291. U64 fileSize = getSize();
  292. U32 bufSize = 1024 * 1024 * 4;
  293. FrameTemp< U8 > buf( bufSize );
  294. U32 crc = CRC::INITIAL_CRC_VALUE;
  295. while( fileSize > 0 )
  296. {
  297. U32 bytesRead = getMin( fileSize, bufSize );
  298. if( read( buf, bytesRead ) != bytesRead )
  299. {
  300. close();
  301. return 0;
  302. }
  303. fileSize -= bytesRead;
  304. crc = CRC::calculateCRC( buf, bytesRead, crc );
  305. }
  306. close();
  307. return crc;
  308. }
  309. bool PosixFile::open(AccessMode mode)
  310. {
  311. close();
  312. if (_name.isEmpty())
  313. {
  314. return _status;
  315. }
  316. #ifdef DEBUG_SPEW
  317. Platform::outputDebugString( "[PosixFile] opening '%s'", _name.c_str() );
  318. #endif
  319. const char* fmode = "r";
  320. switch (mode)
  321. {
  322. case Read: fmode = "r"; break;
  323. case Write: fmode = "w"; break;
  324. case ReadWrite:
  325. {
  326. fmode = "r+";
  327. // Ensure the file exists.
  328. FILE* temp = fopen( _name.c_str(), "a+" );
  329. fclose( temp );
  330. break;
  331. }
  332. case WriteAppend: fmode = "a"; break;
  333. default: break;
  334. }
  335. if (!(_handle = fopen(_name.c_str(), fmode)))
  336. {
  337. _updateStatus();
  338. return false;
  339. }
  340. _status = Open;
  341. return true;
  342. }
  343. bool PosixFile::close()
  344. {
  345. if (_handle)
  346. {
  347. #ifdef DEBUG_SPEW
  348. Platform::outputDebugString( "[PosixFile] closing '%s'", _name.c_str() );
  349. #endif
  350. fflush(_handle);
  351. fclose(_handle);
  352. _handle = 0;
  353. }
  354. _status = Closed;
  355. return true;
  356. }
  357. U32 PosixFile::getPosition()
  358. {
  359. if (_status == Open || _status == EndOfFile)
  360. return ftell(_handle);
  361. return 0;
  362. }
  363. U32 PosixFile::setPosition(U32 delta, SeekMode mode)
  364. {
  365. if (_status != Open && _status != EndOfFile)
  366. return 0;
  367. S32 fmode = 0;
  368. switch (mode)
  369. {
  370. case Begin: fmode = SEEK_SET; break;
  371. case Current: fmode = SEEK_CUR; break;
  372. case End: fmode = SEEK_END; break;
  373. default: break;
  374. }
  375. if (fseek(_handle, delta, fmode))
  376. {
  377. _status = UnknownError;
  378. return 0;
  379. }
  380. _status = Open;
  381. return ftell(_handle);
  382. }
  383. U32 PosixFile::read(void* dst, U32 size)
  384. {
  385. if (_status != Open && _status != EndOfFile)
  386. return 0;
  387. U32 bytesRead = fread(dst, 1, size, _handle);
  388. if (bytesRead != size)
  389. {
  390. if (feof(_handle))
  391. _status = EndOfFile;
  392. else
  393. _updateStatus();
  394. }
  395. return bytesRead;
  396. }
  397. U32 PosixFile::write(const void* src, U32 size)
  398. {
  399. if ((_status != Open && _status != EndOfFile) || !size)
  400. return 0;
  401. U32 bytesWritten = fwrite(src, 1, size, _handle);
  402. if (bytesWritten != size)
  403. _updateStatus();
  404. return bytesWritten;
  405. }
  406. void PosixFile::_updateStatus()
  407. {
  408. switch (errno)
  409. {
  410. case EACCES: _status = AccessDenied; break;
  411. case ENOSPC: _status = FileSystemFull; break;
  412. case ENOTDIR: _status = NoSuchFile; break;
  413. case ENOENT: _status = NoSuchFile; break;
  414. case EISDIR: _status = AccessDenied; break;
  415. case EROFS: _status = AccessDenied; break;
  416. default: _status = UnknownError; break;
  417. }
  418. }
  419. //-----------------------------------------------------------------------------
  420. PosixDirectory::PosixDirectory(const Path& path,String name)
  421. {
  422. _path = path;
  423. _name = name;
  424. _status = Closed;
  425. _handle = 0;
  426. }
  427. PosixDirectory::~PosixDirectory()
  428. {
  429. if (_handle)
  430. close();
  431. }
  432. Path PosixDirectory::getName() const
  433. {
  434. return _path;
  435. }
  436. bool PosixDirectory::open()
  437. {
  438. if ((_handle = opendir(_name)) == 0)
  439. {
  440. _updateStatus();
  441. return false;
  442. }
  443. _status = Open;
  444. return true;
  445. }
  446. bool PosixDirectory::close()
  447. {
  448. if (_handle)
  449. {
  450. closedir(_handle);
  451. _handle = NULL;
  452. return true;
  453. }
  454. return false;
  455. }
  456. bool PosixDirectory::read(Attributes* entry)
  457. {
  458. if (_status != Open)
  459. return false;
  460. struct dirent* de = readdir(_handle);
  461. if (!de)
  462. {
  463. _status = EndOfFile;
  464. return false;
  465. }
  466. // Skip "." and ".." entries
  467. if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
  468. (de->d_name[1] == '.' && de->d_name[2] == '\0')))
  469. return read(entry);
  470. // The dirent structure doesn't actually return much beside
  471. // the name, so we must call stat for more info.
  472. struct stat info;
  473. String file = _name + "/" + de->d_name;
  474. int error = stat(file.c_str(),&info);
  475. if (error < 0)
  476. {
  477. _updateStatus();
  478. return false;
  479. }
  480. copyStatAttributes(info,entry);
  481. entry->name = de->d_name;
  482. return true;
  483. }
  484. U32 PosixDirectory::calculateChecksum()
  485. {
  486. // Return checksum of current entry
  487. return 0;
  488. }
  489. bool PosixDirectory::getAttributes(Attributes* attr)
  490. {
  491. struct stat info;
  492. if (stat(_name.c_str(),&info))
  493. {
  494. _updateStatus();
  495. return false;
  496. }
  497. copyStatAttributes(info,attr);
  498. attr->name = _path;
  499. return true;
  500. }
  501. FileNode::NodeStatus PosixDirectory::getStatus() const
  502. {
  503. return _status;
  504. }
  505. void PosixDirectory::_updateStatus()
  506. {
  507. switch (errno)
  508. {
  509. case EACCES: _status = AccessDenied; break;
  510. case ENOTDIR: _status = NoSuchFile; break;
  511. case ENOENT: _status = NoSuchFile; break;
  512. default: _status = UnknownError; break;
  513. }
  514. }
  515. } // Namespace POSIX
  516. } // Namespace Torque
  517. //-----------------------------------------------------------------------------
  518. #ifndef TORQUE_OS_MAC // Mac has its own native FS build on top of the POSIX one.
  519. Torque::FS::FileSystemRef Platform::FS::createNativeFS( const String &volume )
  520. {
  521. return new Posix::PosixFileSystem( volume );
  522. }
  523. #endif
  524. String Platform::FS::getAssetDir()
  525. {
  526. return Platform::getExecutablePath();
  527. }
  528. /// Function invoked by the kernel layer to install OS specific
  529. /// file systems.
  530. bool Platform::FS::InstallFileSystems()
  531. {
  532. Platform::FS::Mount( "/", Platform::FS::createNativeFS( String() ) );
  533. // Setup the current working dir.
  534. char buffer[PATH_MAX];
  535. if (::getcwd(buffer,sizeof(buffer)))
  536. {
  537. // add trailing '/' if it isn't there
  538. if (buffer[dStrlen(buffer) - 1] != '/')
  539. dStrcat(buffer, "/", PATH_MAX);
  540. Platform::FS::SetCwd(buffer);
  541. }
  542. // Mount the home directory
  543. if (char* home = getenv("HOME"))
  544. Platform::FS::Mount( "home", Platform::FS::createNativeFS(home) );
  545. return true;
  546. }