Skip to content

Instantly share code, notes, and snippets.

@jpf91
Created August 31, 2011 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpf91/1184671 to your computer and use it in GitHub Desktop.
Save jpf91/1184671 to your computer and use it in GitHub Desktop.
import std.datetime, std.concurrency, std.stdio, core.sys.posix.unistd;
import std.exception;
enum BUFFER_SIZE = 1 * 1024 * 1024UL;
void main(string[] args)
{
assert(args.length == 4);
ubyte[BUFFER_SIZE] buf;
auto sw = StopWatch();
auto input = File(args[2], "r");
if(args[1] == "--async")
{
auto copy = spawn(&copyThread, thisTid, args[3]);
receiveOnly!bool();
sw.start();
while(true)
{
auto read = input.rawRead(new ubyte[BUFFER_SIZE]);
if(read.length == 0)
{
copy.send(assumeUnique(read));
break;
}
copy.send(assumeUnique(read));
}
receiveOnly!bool();
sw.stop();
}
else if(args[1] == "--sync")
{
auto output = File(args[3], "w");
sw.start();
while(true)
{
auto read = input.rawRead(buf[]);
if(read.length == 0)
break;
output.rawWrite(read);
}
fsync(output.fileno);
output.close();
sw.stop();
}
writefln("Copied %s bytes in %s msec (%s kB/s)", input.size,
sw.peek().msecs, input.size / (1024 * sw.peek().seconds));
}
void copyThread(Tid sender, string outName)
{
auto output = File(outName, "w");
bool cont = true;
sender.send(true);
while(cont)
{
receive(
(immutable(ubyte[]) data) {
if(data.length == 0)
{
cont = false;
return;
}
output.rawWrite(data);
});
}
fsync(output.fileno);
output.close();
sender.send(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment