From 57cb5102b2fd47fed46bd85ab07e0135e19dd484 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 4 Nov 2013 21:50:15 +0300 Subject: improve renameFile function. add test for this function. --- src/utils/files.cpp | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'src/utils/files.cpp') diff --git a/src/utils/files.cpp b/src/utils/files.cpp index fa69cd6af..6f57d3062 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -18,7 +18,6 @@ * along with this program. If not, see . */ -#if defined(ANDROID) || defined(__native_client__) #include "utils/files.h" #include "logger.h" @@ -63,6 +62,7 @@ void Files::extractLocale() } #endif // ANDROID +#if defined(ANDROID) || defined(__native_client__) void Files::copyPhysFsFile(const std::string &inFile, const std::string &outFile) { @@ -100,33 +100,39 @@ void Files::extractZip(const std::string &zipName, const std::string &inDir, remove(zipName.c_str()); } -int Files::renameFile(const std::string &pFrom, const std::string &pTo) -{ - FILE *file = fopen(pFrom.c_str(), "rb"); - if (file == NULL) - return -1; - - fseek(file, 0, SEEK_END); - size_t sz = ftell(file); - fseek(file, 0, SEEK_SET); +#endif // ANDROID __native_client__ - char *buf = (char *)malloc(sz + 1); - if (fread(buf, 1, sz, file) != sz) +int Files::renameFile(const std::string &srcName, const std::string &dstName) +{ + FILE *srcFile = fopen(srcName.c_str(), "rb"); + if (srcFile == nullptr) return -1; - fclose(file); - buf[sz] = 0; - - file = fopen(pTo.c_str(), "w+b"); - if (file == NULL) + FILE *dstFile = fopen(dstName.c_str(), "w+b"); + if (dstFile == nullptr) + { + fclose(srcFile); return -1; + } - if (fwrite(buf, 1, sz, file) != sz) - return -1; - fclose(file); + const int chunkSize = 500000; + char *buf = new char[chunkSize]; + size_t sz = 0; + while ((sz = fread(buf, 1, chunkSize, srcFile))) + { + if (fwrite(buf, 1, sz, dstFile) != sz) + { + delete [] buf; + fclose(srcFile); + fclose(dstFile); + ::remove(dstName.c_str()); + return -1; + } + } - free(buf); + delete [] buf; + fclose(srcFile); + fclose(dstFile); + ::remove(srcName.c_str()); return 0; } - -#endif // ANDROID __native_client__ -- cgit v1.2.3-70-g09d2