Merge pull request #98 from vibe-d/file_truncation

Add EventDriverFiles.truncate.
merged-on-behalf-of: Sönke Ludwig <s-ludwig@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2018-12-28 10:27:38 +01:00 committed by GitHub
commit 36f43690c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View file

@ -426,6 +426,16 @@ interface EventDriverFiles {
ulong getSize(FileFD file); 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 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 read(FileFD file, ulong offset, ubyte[] buffer, IOMode mode, FileIOCallback on_read_finish);
void cancelWrite(FileFD file); 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) final override void write(FileFD file, ulong offset, const(ubyte)[] buffer, IOMode, FileIOCallback on_write_finish)
{ {
//assert(this.writable); //assert(this.writable);

View file

@ -88,6 +88,26 @@ final class WinAPIEventDriverFiles : EventDriverFiles {
return size.QuadPart; 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) override void write(FileFD file, ulong offset, const(ubyte)[] buffer, IOMode mode, FileIOCallback on_write_finish)
{ {
auto h = idToHandle(file); auto h = idToHandle(file);