Use concurrent pipe() in copyFile().

This commit is contained in:
Sönke Ludwig 2020-10-21 16:58:03 +02:00
parent 730b42bf31
commit eb183d5ab2

View file

@ -242,7 +242,7 @@ void copyFile(NativePath from, NativePath to, bool overwrite = false)
auto dst = openFile(to, FileMode.createTrunc); auto dst = openFile(to, FileMode.createTrunc);
scope(exit) dst.close(); scope(exit) dst.close();
dst.truncate(src.size); dst.truncate(src.size);
dst.write(src); src.pipe(dst, PipeMode.concurrent);
} }
// TODO: also retain creation time on windows // TODO: also retain creation time on windows
@ -651,7 +651,7 @@ struct FileStream {
void write(InputStream)(InputStream stream, ulong nbytes = ulong.max) void write(InputStream)(InputStream stream, ulong nbytes = ulong.max)
if (isInputStream!InputStream) if (isInputStream!InputStream)
{ {
writeDefault(this, stream, nbytes); pipe(stream, this, nbytes, PipeMode.concurrent);
} }
void flush() void flush()
@ -670,38 +670,6 @@ struct FileStream {
mixin validateRandomAccessStream!FileStream; mixin validateRandomAccessStream!FileStream;
private void writeDefault(OutputStream, InputStream)(ref OutputStream dst, InputStream stream, ulong nbytes = ulong.max)
if (isOutputStream!OutputStream && isInputStream!InputStream)
{
import vibe.internal.allocator : theAllocator, make, dispose;
import std.algorithm.comparison : min;
static struct Buffer { ubyte[64*1024] bytes = void; }
auto bufferobj = () @trusted { return theAllocator.make!Buffer(); } ();
scope (exit) () @trusted { theAllocator.dispose(bufferobj); } ();
auto buffer = bufferobj.bytes[];
//logTrace("default write %d bytes, empty=%s", nbytes, stream.empty);
if (nbytes == ulong.max) {
while (!stream.empty) {
size_t chunk = min(stream.leastSize, buffer.length);
assert(chunk > 0, "leastSize returned zero for non-empty stream.");
//logTrace("read pipe chunk %d", chunk);
stream.read(buffer[0 .. chunk]);
dst.write(buffer[0 .. chunk]);
}
} else {
while (nbytes > 0) {
size_t chunk = min(nbytes, buffer.length);
//logTrace("read pipe chunk %d", chunk);
stream.read(buffer[0 .. chunk]);
dst.write(buffer[0 .. chunk]);
nbytes -= chunk;
}
}
}
/** /**
Interface for directory watcher implementations. Interface for directory watcher implementations.