Skip to content

Instantly share code, notes, and snippets.

@jbylund
Created January 12, 2020 18:25
Show Gist options
  • Save jbylund/6ca123b988996bcae3c1517d8b98c7ff to your computer and use it in GitHub Desktop.
Save jbylund/6ca123b988996bcae3c1517d8b98c7ff to your computer and use it in GitHub Desktop.
class Copier {
public void copy (string src_filename, string dest_filename) {
File src_fh = File.new_for_path(src_filename);
File dest_fh = File.new_for_path(dest_filename);
try {
dest_fh.create(FileCreateFlags.NONE, null);
} catch (Error err) {
if (!(err is IOError.EXISTS)) {
warning("claim_file %s: %s", dest_fh.get_path(), err.message);
throw err;
}
}
src_fh.copy(
dest_fh,
FileCopyFlags.ALL_METADATA |
FileCopyFlags.OVERWRITE |
0
);
}
public void fallback_copy(string src_filename, string dest_filename) {
File src_fh = File.new_for_path(src_filename);
File dest_fh = File.new_for_path(dest_filename);
fallback_copy_inner(src_fh, dest_fh, null);
}
public void fallback_copy_inner(File? src, File? dst, FileProgressCallback? callback) throws Error {
if (src == null || dst == null) {
return;
}
var f = FileStream.open(src.get_path(), "rb");
var size = 0;
if (f != null) {
if (callback != null) {
f.seek(0, FileSeek.END);
size = f.tell();
}
f.seek(0, FileSeek.SET);
var g = FileStream.open(dst.get_path(), "wb");
if (g != null) {
uint8 buffer[2*4096];
while (!f.eof()) {
var len = f.read(buffer);
if (len > 0) {
var out_len = g.write(buffer[0:len]);
if (out_len < 0) { // check the num bytes written
critical("Failed to write to file %s: %m", dst.get_path());
throw new IOError.FAILED("Failed to write to %s", dst.get_path());
}
if (callback != null)
callback (g.tell(), size);
} else if (len < 0) {
critical("Failed to read from file %s: %m", src.get_path());
throw new IOError.FAILED("Failed to read from %s", src.get_path());
}
}
} else {
critical ("Failed to open %s: %m", dst.get_path());
throw new IOError.FAILED("Failed to open %s", dst.get_path());
}
} else {
critical ("Failed to open %s: %m", src.get_path());
throw new IOError.FAILED("Failed to open %s", src.get_path());
}
}
}
string format_str_array(string[] str_array) {
if(0 == str_array.length)
return "[]";
var builder = new StringBuilder ();
builder.append("[");
foreach (string istr in str_array) {
builder.append("\"");
builder.append(istr);
builder.append("\", ");
}
builder.erase(builder.len - 2, -1);
builder.append("]");
return builder.str;
}
void main (string[] args) {
stderr.printf("%s\n", format_str_array(args));
// treat args[1] as the source and
// args[2] as the dest
var copier = new Copier();
Timer timer = new Timer();
copier.copy(args[1], args[2]);
double builtin_time = timer.elapsed();
stderr.printf("builtin elapsed = %f\n", builtin_time);
timer.start();
copier.fallback_copy(args[1], args[2]);
double fallback_time = timer.elapsed();
stderr.printf("fallback elapsed = %f\n", fallback_time);
stderr.printf("relative speedup %f\n", builtin_time / fallback_time);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment