]> git.lizzy.rs Git - minetest.git/blobdiff - src/filesys.cpp
Remove ClientMap::m_camera_mutex
[minetest.git] / src / filesys.cpp
index 4a4a2e4186b55f847edcbd3dda12dd89227a4852..501f9ad6ca53f721f4d117b2a199bb6b2e251af9 100644 (file)
@@ -142,7 +142,7 @@ bool RecursiveDelete(const std::string &path)
                infostream<<"RecursiveDelete: Deleting content of directory "
                                <<path<<std::endl;
                std::vector<DirListNode> content = GetDirListing(path);
-               for(int i=0; i<content.size(); i++){
+               for(size_t i=0; i<content.size(); i++){
                        const DirListNode &n = content[i];
                        std::string fullpath = path + DIR_DELIM + n.name;
                        bool did = RecursiveDelete(fullpath);
@@ -183,7 +183,7 @@ bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
 
 std::string TempPath()
 {
-       DWORD bufsize = GetTempPath(0, "");
+       DWORD bufsize = GetTempPath(0, NULL);
        if(bufsize == 0){
                errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
                return "";
@@ -662,6 +662,19 @@ std::string RemoveRelativePathComponents(std::string path)
        return path.substr(0, pos);
 }
 
+std::string AbsolutePath(const std::string &path)
+{
+#ifdef _WIN32
+       char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
+#else
+       char *abs_path = realpath(path.c_str(), NULL);
+#endif
+       if (!abs_path) return "";
+       std::string abs_path_str(abs_path);
+       free(abs_path);
+       return abs_path_str;
+}
+
 const char *GetFilenameFromPath(const char *path)
 {
        const char *filename = strrchr(path, DIR_DELIM_CHAR);
@@ -680,13 +693,22 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
        os.flush();
        os.close();
        if (os.fail()) {
+               // Remove the temporary file because writing it failed and it's useless.
                remove(tmp_file.c_str());
                return false;
        }
 
-       // Copy file
+       // Move the finished temporary file over the real file
+#ifdef _WIN32
+       // On POSIX compliant systems rename() is specified to be able to swap the
+       // file in place of the destination file, making this a truly error-proof
+       // transaction.
+       // However, on Windows, the target file has to be removed first.
        remove(path.c_str());
+#endif
        if(rename(tmp_file.c_str(), path.c_str())) {
+               // Remove the temporary file because moving it over the target file
+               // failed.
                remove(tmp_file.c_str());
                return false;
        } else {
@@ -694,5 +716,10 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
        }
 }
 
+bool Rename(const std::string &from, const std::string &to)
+{
+       return rename(from.c_str(), to.c_str()) == 0;
+}
+
 } // namespace fs