HDD-delay-safe fwrite in C++ (need help for RNPC)
#5

You can force-write the file cache (stuff you write to the file with "fwrite" is first written into a cache) into the file on the physical disk with "fsync" on Linux/OSX. A call to that function is blocking.
It's not that easy on Windows though. It seems that fsync does not exist on Windows platforms. You'd have to use the Windows API to create a file with a special flag (FILE_FLAG_WRITE_THROUGH).
Here's a quick example on how to do that:
Код:
HANDLE file = CreateFile("something.rec", GENERIC_WRITE, 0, NULL, CREATE_NEW, 
	FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
if (file == INVALID_HANDLE_VALUE)
{
	//error
	return;
}

char data[] = "data to write to the file";
DWORD
	bytes_to_write = (DWORD)strlen(data),
	bytes_written = 0;
bool res = WriteFile(file, data, bytes_to_write, &bytes_written, NULL);

if (res == false)
{
	//error
}
else if (bytes_to_write != bytes_written)
{
	//error
}
else
{
	//success!
}

CloseHandle(file);
That solution (WriteFile in particular) will also block until everything is actually written to the file on the disk. This will of course also halt the SA-MP server. You'd have to thread all file operations to avoid that, but I guess that'd be a little bit overkill.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)