CCFileUtils-linux.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /****************************************************************************
  2. Copyright (c) 2011 Laschweinski
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_LINUX
  23. #include "platform/linux/CCFileUtils-linux.h"
  24. #include "platform/linux/CCApplication-linux.h"
  25. #include "platform/CCCommon.h"
  26. #include "base/ccMacros.h"
  27. #include "base/ccUTF8.h"
  28. #include <unistd.h>
  29. #include <sys/stat.h>
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #ifndef CC_RESOURCE_FOLDER_LINUX
  33. #define CC_RESOURCE_FOLDER_LINUX ("/Resources/")
  34. #endif
  35. using namespace std;
  36. NS_CC_BEGIN
  37. FileUtils* FileUtils::getInstance()
  38. {
  39. if (s_sharedFileUtils == nullptr)
  40. {
  41. s_sharedFileUtils = new FileUtilsLinux();
  42. if(!s_sharedFileUtils->init())
  43. {
  44. delete s_sharedFileUtils;
  45. s_sharedFileUtils = nullptr;
  46. CCLOG("ERROR: Could not init CCFileUtilsLinux");
  47. }
  48. }
  49. return s_sharedFileUtils;
  50. }
  51. FileUtilsLinux::FileUtilsLinux()
  52. {}
  53. bool FileUtilsLinux::init()
  54. {
  55. // get application path
  56. char fullpath[256] = {0};
  57. ssize_t length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)-1);
  58. if (length <= 0) {
  59. return false;
  60. }
  61. fullpath[length] = '\0';
  62. std::string appPath = fullpath;
  63. _defaultResRootPath = appPath.substr(0, appPath.find_last_of("/"));
  64. _defaultResRootPath += CC_RESOURCE_FOLDER_LINUX;
  65. // Set writable path to $XDG_CONFIG_HOME or ~/.config/<app name>/ if $XDG_CONFIG_HOME not exists.
  66. const char* xdg_config_path = getenv("XDG_CONFIG_HOME");
  67. std::string xdgConfigPath;
  68. if (xdg_config_path == NULL) {
  69. xdgConfigPath = getenv("HOME");
  70. xdgConfigPath += "/.config";
  71. } else {
  72. xdgConfigPath = xdg_config_path;
  73. }
  74. _writablePath = xdgConfigPath;
  75. _writablePath += appPath.substr(appPath.find_last_of("/"));
  76. _writablePath += "/";
  77. return FileUtils::init();
  78. }
  79. string FileUtilsLinux::getWritablePath() const
  80. {
  81. struct stat st;
  82. stat(_writablePath.c_str(), &st);
  83. if (!S_ISDIR(st.st_mode)) {
  84. mkdir(_writablePath.c_str(), 0744);
  85. }
  86. return _writablePath;
  87. }
  88. bool FileUtilsLinux::isFileExistInternal(const std::string& strFilePath) const
  89. {
  90. if (strFilePath.empty())
  91. {
  92. return false;
  93. }
  94. std::string strPath = strFilePath;
  95. if (!isAbsolutePath(strPath))
  96. { // Not absolute path, add the default root path at the beginning.
  97. strPath.insert(0, _defaultResRootPath);
  98. }
  99. struct stat sts;
  100. return (stat(strPath.c_str(), &sts) != -1) ? true : false;
  101. }
  102. NS_CC_END
  103. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_LINUX