Skip to content

Commit

Permalink
Merge pull request #3258 from CyberShadow/pull-20150505-180120-std-st…
Browse files Browse the repository at this point in the history
…dio-sync

fix Issue 14548 - std.stdio.File should have sync() method (fsync/FlushF...
  • Loading branch information
DmitryOlshansky committed Jun 7, 2015
2 parents e3899bb + a5e1ae4 commit 4567cf6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions std/stdio.d
Expand Up @@ -694,6 +694,8 @@ _clearerr) for the file handle.
}

/**
Flushes the C $(D FILE) buffers.
Calls $(WEB cplusplus.com/reference/clibrary/cstdio/_fflush.html, _fflush)
for the file handle.
Expand Down Expand Up @@ -721,6 +723,33 @@ Throws: $(D Exception) if the file is not opened or if the call to $(D fflush) f
assertThrown(f.flush());
}

/**
Forces any data buffered by the OS to be written to disk.
Call $(LREF flush) before calling this function to flush the C $(D FILE) buffers first.
This function calls
$(WEB msdn.microsoft.com/en-us/library/windows/desktop/aa364439%28v=vs.85%29.aspx,
$(D FlushFileBuffers)) on Windows and
$(WEB pubs.opengroup.org/onlinepubs/7908799/xsh/fsync.html,
$(D fsync)) on POSIX for the file handle.
Throws: $(D Exception) if the file is not opened or if the OS call fails.
*/
void sync() @trusted
{
import std.exception : enforce, errnoEnforce;

enforce(isOpen, "Attempting to sync() an unopened file");

version (Windows)
wenforce(FlushFileBuffers(windowsHandle), "FlushFileBuffers failed");
else
{
import core.sys.posix.unistd : fsync;
errnoEnforce(fsync(fileno) == 0, "fsync failed");
}
}

/**
Calls $(WEB cplusplus.com/reference/clibrary/cstdio/fread.html, fread) for the
file handle. The number of items to read and the size of
Expand Down

0 comments on commit 4567cf6

Please sign in to comment.