CCFileUtils-linux.cpp 3.8 KB

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