aboutsummaryrefslogtreecommitdiff
path: root/src/filesys.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/filesys.cpp')
-rw-r--r--src/filesys.cpp85
1 files changed, 66 insertions, 19 deletions
diff --git a/src/filesys.cpp b/src/filesys.cpp
index 28a33f4d0..99b030624 100644
--- a/src/filesys.cpp
+++ b/src/filesys.cpp
@@ -27,9 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "config.h"
#include "porting.h"
-#ifdef __ANDROID__
-#include "settings.h" // For g_settings
-#endif
namespace fs
{
@@ -359,8 +356,9 @@ std::string TempPath()
compatible with lua's os.tmpname which under the default
configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
*/
+
#ifdef __ANDROID__
- return g_settings->get("TMPFolder");
+ return porting::path_cache;
#else
return DIR_DELIM "tmp";
#endif
@@ -401,21 +399,6 @@ void GetRecursiveSubPaths(const std::string &path,
}
}
-bool DeletePaths(const std::vector<std::string> &paths)
-{
- bool success = true;
- // Go backwards to succesfully delete the output of GetRecursiveSubPaths
- for(int i=paths.size()-1; i>=0; i--){
- const std::string &path = paths[i];
- bool did = DeleteSingleFileOrEmptyDirectory(path);
- if(!did){
- errorstream<<"Failed to delete "<<path<<std::endl;
- success = false;
- }
- }
- return success;
-}
-
bool RecursiveDeleteContent(const std::string &path)
{
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
@@ -744,6 +727,70 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
return true;
}
+bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination)
+{
+ if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) {
+ return false;
+ }
+
+ sanity_check(fs->getFileArchiveCount() > 0);
+
+ /**********************************************************************/
+ /* WARNING this is not threadsafe!! */
+ /**********************************************************************/
+ io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1);
+
+ const io::IFileList* files_in_zip = opened_zip->getFileList();
+
+ unsigned int number_of_files = files_in_zip->getFileCount();
+
+ for (unsigned int i=0; i < number_of_files; i++) {
+ std::string fullpath = destination;
+ fullpath += DIR_DELIM;
+ fullpath += files_in_zip->getFullFileName(i).c_str();
+ std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
+
+ if (!files_in_zip->isDirectory(i)) {
+ if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
+ fs->removeFileArchive(fs->getFileArchiveCount()-1);
+ return false;
+ }
+
+ io::IReadFile* toread = opened_zip->createAndOpenFile(i);
+
+ FILE *targetfile = fopen(fullpath.c_str(),"wb");
+
+ if (targetfile == NULL) {
+ fs->removeFileArchive(fs->getFileArchiveCount()-1);
+ return false;
+ }
+
+ char read_buffer[1024];
+ long total_read = 0;
+
+ while (total_read < toread->getSize()) {
+
+ unsigned int bytes_read =
+ toread->read(read_buffer,sizeof(read_buffer));
+ if ((bytes_read == 0 ) ||
+ (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
+ {
+ fclose(targetfile);
+ fs->removeFileArchive(fs->getFileArchiveCount() - 1);
+ return false;
+ }
+ total_read += bytes_read;
+ }
+
+ fclose(targetfile);
+ }
+
+ }
+
+ fs->removeFileArchive(fs->getFileArchiveCount() - 1);
+ return true;
+}
+
bool ReadFile(const std::string &path, std::string &out)
{
std::ifstream is(path, std::ios::binary | std::ios::ate);