Skip to content

Commit fc4cbb9

Browse files
PatriceJiangdrelaptop
authored andcommitted
[linux] Fix FileUtils::getContents with folder (cocos2d#19157)
* fix FileUtils::getContents on linux/mac * use stat.st_mode * simplify
1 parent f3a85ec commit fc4cbb9

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

cocos/platform/CCFileUtils.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -669,20 +669,21 @@ FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableB
669669
if (fullPath.empty())
670670
return Status::NotExists;
671671

672-
FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb");
673-
if (!fp)
674-
return Status::OpenFailed;
672+
std::string suitableFullPath = fs->getSuitableFOpen(fullPath);
675673

676-
#if defined(_MSC_VER)
677-
auto descriptor = _fileno(fp);
678-
#else
679-
auto descriptor = fileno(fp);
680-
#endif
681674
struct stat statBuf;
682-
if (fstat(descriptor, &statBuf) == -1) {
683-
fclose(fp);
675+
if (stat(suitableFullPath.c_str(), &statBuf) == -1) {
684676
return Status::ReadFailed;
685677
}
678+
679+
if (!(statBuf.st_mode & S_IFREG)) {
680+
return Status::NotRegularFileType;
681+
}
682+
683+
FILE *fp = fopen(suitableFullPath.c_str(), "rb");
684+
if (!fp)
685+
return Status::OpenFailed;
686+
686687
size_t size = statBuf.st_size;
687688

688689
buffer->resize(size);

cocos/platform/CCFileUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class CC_DLL FileUtils
197197
ReadFailed = 3, // Read failed
198198
NotInitialized = 4, // FileUtils is not initializes
199199
TooLarge = 5, // The file is too large (great than 2^32-1)
200-
ObtainSizeFailed = 6 // Failed to obtain the file size.
200+
ObtainSizeFailed = 6, // Failed to obtain the file size.
201+
NotRegularFileType = 7 // File type is not S_IFREG
201202
};
202203

203204
/**

0 commit comments

Comments
 (0)