Parcourir la source

core: rename flush() to sync() and actually sync data to disk

Daniele Bartolini il y a 1 an
Parent
commit
30f2763073

+ 3 - 2
src/core/filesystem/file.h

@@ -59,8 +59,9 @@ struct File
 	/// Writes @a size bytes from @a data to this.
 	/// Writes @a size bytes from @a data to this.
 	virtual u32 write(const void *data, u32 size) = 0;
 	virtual u32 write(const void *data, u32 size) = 0;
 
 
-	/// Forces the previouses write operations to complete.
-	virtual void flush() = 0;
+	/// Forces the previous write()s to be transferred to the underlying storage device.
+	/// Returns 0 if success, -1 otherwise.
+	virtual s32 sync() = 0;
 };
 };
 
 
 namespace file
 namespace file

+ 2 - 1
src/core/filesystem/file_buffer.inl

@@ -94,9 +94,10 @@ struct FileBuffer : public File
 		return size;
 		return size;
 	}
 	}
 
 
-	virtual void flush() override
+	virtual s32 sync() override
 	{
 	{
 		CE_NOOP();
 		CE_NOOP();
+		return 0;
 	}
 	}
 };
 };
 
 

+ 2 - 1
src/core/filesystem/file_memory.inl

@@ -95,9 +95,10 @@ struct FileMemory : public File
 		return 0;
 		return 0;
 	}
 	}
 
 
-	virtual void flush() override
+	virtual s32 sync() override
 	{
 	{
 		CE_NOOP();
 		CE_NOOP();
+		return 0;
 	}
 	}
 };
 };
 
 

+ 9 - 9
src/core/filesystem/filesystem_disk.cpp

@@ -21,6 +21,7 @@
 #else
 #else
 	#include <stdio.h>
 	#include <stdio.h>
 	#include <errno.h>
 	#include <errno.h>
+	#include <unistd.h> // fsync
 #endif
 #endif
 
 
 namespace crown
 namespace crown
@@ -211,20 +212,19 @@ struct FileDisk : public File
 #endif
 #endif
 	}
 	}
 
 
-	void flush() override
+	s32 sync() override
 	{
 	{
 		CE_ASSERT(is_open(), "File is not open");
 		CE_ASSERT(is_open(), "File is not open");
 #if CROWN_PLATFORM_WINDOWS
 #if CROWN_PLATFORM_WINDOWS
-		BOOL err = FlushFileBuffers(_file);
-		CE_ASSERT(err != 0
-			, "FlushFileBuffers: GetLastError = %d"
-			, GetLastError()
-			);
+		if (FlushFileBuffers(_file) != 0)
+			return -1;
 #else
 #else
-		int err = fflush(_file);
-		CE_ASSERT(err == 0, "fflush: errno = %d", errno);
+		if (fflush(_file) != 0)
+			return -1;
+		if (fsync(fileno(_file)) == -1)
+			return -1;
 #endif
 #endif
-		CE_UNUSED(err);
+		return 0;
 	}
 	}
 };
 };
 
 

+ 1 - 1
src/core/unit_tests.cpp

@@ -1777,7 +1777,7 @@ static void test_file_monitor()
 			ENSURE(strcmp(path, os_path.c_str()) == 0);
 			ENSURE(strcmp(path, os_path.c_str()) == 0);
 		};
 		};
 		file->write("3.14", 4);
 		file->write("3.14", 4);
-		file->flush();
+		file->sync();
 		state.wait(500);
 		state.wait(500);
 
 
 		fs.close(*file);
 		fs.close(*file);