*/
#include "filesys.h"
-#include "strfnd.h"
+#include "util/string.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <fstream>
#include "log.h"
+#include "config.h"
namespace fs
{
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <malloc.h>
-#include <tchar.h>
-#include <wchar.h>
+#include <tchar.h>
+#include <wchar.h>
#define BUFSIZE MAX_PATH
// Find the first file in the directory.
hFind = FindFirstFile(DirSpec, &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
+ if (hFind == INVALID_HANDLE_VALUE)
{
retval = (-1);
goto Cleanup;
- }
- else
+ }
+ else
{
// NOTE:
// Be very sure to not include '..' in the results, it will
listing.push_back(node);
// List all the other files in the directory.
- while (FindNextFile(hFind, &FindFileData) != 0)
+ while (FindNextFile(hFind, &FindFileData) != 0)
{
DirListNode node;
node.name = FindFileData.cFileName;
dwError = GetLastError();
FindClose(hFind);
- if (dwError != ERROR_NO_MORE_FILES)
+ if (dwError != ERROR_NO_MORE_FILES)
{
errorstream<<"GetDirListing: FindNextFile error. Error is "
<<dwError<<std::endl;
compatible with lua's os.tmpname which under the default
configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
*/
- return std::string(DIR_DELIM) + "tmp";
+#ifdef __ANDROID__
+ return DIR_DELIM "sdcard" DIR_DELIM PROJECT_NAME DIR_DELIM "tmp";
+#else
+ return DIR_DELIM "tmp";
+#endif
}
#endif
return path.substr(0, pos);
}
+bool safeWriteToFile(const std::string &path, const std::string &content)
+{
+ std::string tmp_file = path + ".~mt";
+
+ // Write to a tmp file
+ std::ofstream os(tmp_file.c_str(), std::ios::binary);
+ if (!os.good())
+ return false;
+ os << content;
+ os.flush();
+ os.close();
+ if (os.fail()) {
+ remove(tmp_file.c_str());
+ return false;
+ }
+
+ // Copy file
+ remove(path.c_str());
+ if(rename(tmp_file.c_str(), path.c_str())) {
+ remove(tmp_file.c_str());
+ return false;
+ } else {
+ return true;
+ }
+}
+
} // namespace fs