summaryrefslogtreecommitdiff
path: root/src/utils/files.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-11-04 21:50:15 +0300
committerAndrei Karas <akaras@inbox.ru>2013-11-04 22:20:41 +0300
commit57cb5102b2fd47fed46bd85ab07e0135e19dd484 (patch)
treef3f6a0d888611120fcf28c3d57413d71a0b9357c /src/utils/files.cpp
parent3bd429f0ce19526b6b34a2c27d2d3085f012e192 (diff)
downloadplus-57cb5102b2fd47fed46bd85ab07e0135e19dd484.tar.gz
plus-57cb5102b2fd47fed46bd85ab07e0135e19dd484.tar.bz2
plus-57cb5102b2fd47fed46bd85ab07e0135e19dd484.tar.xz
plus-57cb5102b2fd47fed46bd85ab07e0135e19dd484.zip
improve renameFile function.
add test for this function.
Diffstat (limited to 'src/utils/files.cpp')
-rw-r--r--src/utils/files.cpp52
1 files changed, 29 insertions, 23 deletions
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 <http://www.gnu.org/licenses/>.
*/
-#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__