Add EventDriverFiles.truncate.

This commit is contained in:
Sönke Ludwig 2018-12-28 00:34:30 +01:00
parent 91db8899c5
commit 3e81cefcda
3 changed files with 46 additions and 0 deletions

View file

@ -426,6 +426,16 @@ interface EventDriverFiles {
ulong getSize(FileFD file);
/** Shrinks or extends a file to the specified size.
Params:
file = Handle of the file to resize
size = Desired file size in bytes
on_finish = Called when the operation finishes - the `size`
parameter is always set to zero
*/
void truncate(FileFD file, ulong size, FileIOCallback on_finish);
void write(FileFD file, ulong offset, const(ubyte)[] buffer, IOMode mode, FileIOCallback on_write_finish);
void read(FileFD file, ulong offset, ubyte[] buffer, IOMode mode, FileIOCallback on_read_finish);
void cancelWrite(FileFD file);

View file

@ -196,6 +196,22 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents) : EventDriverFil
}
}
override void truncate(FileFD file, ulong size, FileIOCallback on_finish)
{
version (Posix) {
// FIXME: do this in the thread pool
if (ftruncate(cast(int)file, size) != 0) {
on_finish(file, IOStatus.error, 0);
return;
}
on_finish(file, IOStatus.ok, 0);
} else {
on_finish(file, IOStatus.error, 0);
}
}
final override void write(FileFD file, ulong offset, const(ubyte)[] buffer, IOMode, FileIOCallback on_write_finish)
{
//assert(this.writable);

View file

@ -88,6 +88,26 @@ final class WinAPIEventDriverFiles : EventDriverFiles {
return size.QuadPart;
}
override void truncate(FileFD file, ulong size, FileIOCallback on_finish)
@trusted {
auto h = idToHandle(file);
// FIXME: do this in a separate thread
LARGE_INTEGER li = {QuadPart: size};
if (!SetFilePointerEx(h, li, null, FILE_BEGIN)) {
on_finish(file, IOStatus.error, 0);
return;
}
if (!SetEndOfFile(h)) {
on_finish(file, IOStatus.error, 0);
return;
}
on_finish(file, IOStatus.ok, 0);
}
override void write(FileFD file, ulong offset, const(ubyte)[] buffer, IOMode mode, FileIOCallback on_write_finish)
{
auto h = idToHandle(file);