Merge pull request #8608 from Dhilan007/v3-fileutil-ios

issue #8568: Fixed FileUtils::removeFile doesn't work on iOS
This commit is contained in:
minggo 2014-10-10 15:35:43 +08:00
commit 44f3dcdfca
1 changed files with 25 additions and 6 deletions

View File

@ -37,6 +37,10 @@ THE SOFTWARE.
#include "unzip.h"
#include <sys/stat.h>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#include <ftw.h>
#endif
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
#include <sys/types.h>
#include <errno.h>
@ -1103,6 +1107,18 @@ bool FileUtils::createDirectory(const std::string& path)
#endif
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
auto ret = remove(fpath);
if (ret) {
log("Fail to remove:%s ",fpath);
}
return ret;
}
#endif
bool FileUtils::removeDirectory(const std::string& path)
{
if (path.size() > 0 && path[path.size() - 1] != '/')
@ -1155,6 +1171,11 @@ bool FileUtils::removeDirectory(const std::string& path)
return true;
else
return false;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
if (nftw(path.c_str(),unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
return false;
else
return true;
#else
std::string command = "rm -r ";
// Path may include space.
@ -1195,13 +1216,11 @@ bool FileUtils::removeFile(const std::string &path)
else
return false;
#else
std::string command = "rm -f ";
// Path may include space.
command += "\"" + path + "\"";
if (system(command.c_str()) >= 0)
return true;
else
if (remove(path.c_str())) {
return false;
} else {
return true;
}
#endif
}