diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index 50e604f626b1..3ab33ac7120a 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -669,20 +669,21 @@ FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableB if (fullPath.empty()) return Status::NotExists; - FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb"); - if (!fp) - return Status::OpenFailed; + std::string suitableFullPath = fs->getSuitableFOpen(fullPath); -#if defined(_MSC_VER) - auto descriptor = _fileno(fp); -#else - auto descriptor = fileno(fp); -#endif struct stat statBuf; - if (fstat(descriptor, &statBuf) == -1) { - fclose(fp); + if (stat(suitableFullPath.c_str(), &statBuf) == -1) { return Status::ReadFailed; } + + if (!(statBuf.st_mode & S_IFREG)) { + return Status::NotRegularFileType; + } + + FILE *fp = fopen(suitableFullPath.c_str(), "rb"); + if (!fp) + return Status::OpenFailed; + size_t size = statBuf.st_size; buffer->resize(size); diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index b21e58818f32..130c4b39eb32 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -203,7 +203,8 @@ class CC_DLL FileUtils ReadFailed = 3, // Read failed NotInitialized = 4, // FileUtils is not initializes TooLarge = 5, // The file is too large (great than 2^32-1) - ObtainSizeFailed = 6 // Failed to obtain the file size. + ObtainSizeFailed = 6, // Failed to obtain the file size. + NotRegularFileType = 7 // File type is not S_IFREG }; /**