diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index 66481e8..7b4afc1 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -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); diff --git a/source/eventcore/drivers/threadedfile.d b/source/eventcore/drivers/threadedfile.d index 13dbe1a..4a0a646 100644 --- a/source/eventcore/drivers/threadedfile.d +++ b/source/eventcore/drivers/threadedfile.d @@ -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); diff --git a/source/eventcore/drivers/winapi/files.d b/source/eventcore/drivers/winapi/files.d index 32bae89..0001073 100644 --- a/source/eventcore/drivers/winapi/files.d +++ b/source/eventcore/drivers/winapi/files.d @@ -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);