From 0b1e3240be1534186b01261a6eaa8e389bb6a274 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 23 Apr 2009 12:09:07 +0200 Subject: [PATCH] add position arguments to File#read and File#write --- src/file.cc | 20 +++++++++++--------- src/file.js | 14 +++++++++----- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/file.cc b/src/file.cc index 32298b8681c..8fb76a31809 100644 --- a/src/file.cc +++ b/src/file.cc @@ -351,8 +351,8 @@ File::AfterOpen (eio_req *req) Handle File::Write (const Arguments& args) { - if (args.Length() < 1) return Undefined(); - if (!args[0]->IsString()) + if (args.Length() < 2) return Undefined(); + if (!args[1]->IsNumber()) return Undefined(); HandleScope scope; @@ -362,14 +362,14 @@ File::Write (const Arguments& args) size_t length = 0; if (args[0]->IsString()) { - // utf8 encoded data + // utf8 encoding Local string = args[0]->ToString(); length = string->Utf8Length(); buf = static_cast(malloc(length)); string->WriteUtf8(buf, length); } else if (args[0]->IsArray()) { - // binary data + // raw encoding Local array = Local::Cast(args[0]); length = array->Length(); buf = static_cast(malloc(length)); @@ -383,6 +383,8 @@ File::Write (const Arguments& args) return Undefined(); } + size_t pos = args[1]->Uint32Value(); + if (file->handle_->Has(FD_SYMBOL) == false) { printf("trying to write to a bad fd!\n"); return Undefined(); @@ -390,9 +392,8 @@ File::Write (const Arguments& args) int fd = file->GetFD(); - // NOTE: -1 offset in eio_write() invokes write() instead of pwrite() node_eio_warmup(); - eio_req *req = eio_write(fd, buf, length, -1, EIO_PRI_DEFAULT, File::AfterWrite, file); + eio_req *req = eio_write(fd, buf, length, pos, EIO_PRI_DEFAULT, File::AfterWrite, file); return Undefined(); } @@ -422,17 +423,18 @@ File::Read (const Arguments& args) { if (args.Length() < 1) return Undefined(); if (!args[0]->IsNumber()) return Undefined(); + if (!args[1]->IsNumber()) return Undefined(); HandleScope scope; File *file = File::Unwrap(args.Holder()); size_t length = args[0]->IntegerValue(); + size_t pos = args[1]->Uint32Value(); int fd = file->GetFD(); - // NOTE: -1 offset in eio_read() invokes read() instead of pread() - // NULL pointer tells eio to allocate it itself + // NOTE: NULL pointer tells eio to allocate it itself node_eio_warmup(); - eio_req *req = eio_read(fd, NULL, length, -1, EIO_PRI_DEFAULT, File::AfterRead, file); + eio_req *req = eio_read(fd, NULL, length, pos, EIO_PRI_DEFAULT, File::AfterRead, file); assert(req); return Undefined(); diff --git a/src/file.js b/src/file.js index b93651d5a11..9e8da416ba3 100644 --- a/src/file.js +++ b/src/file.js @@ -15,11 +15,14 @@ File.exists = function (path, callback) { File.cat = function (path, callback) { var content = ""; var file = new File; + var pos = 0; + var chunkSize = 10*1024; function readChunk () { - file.read(1024, function (status, chunk) { + file.read(chunkSize, pos, function (status, chunk) { if (chunk) { content += chunk.encodeUtf8(); + pos += chunk.length; readChunk(); } else { callback(0, content); @@ -48,12 +51,12 @@ File.prototype.close = function (callback) { this._addAction("close", [], callback); }; -File.prototype.write = function (buf, callback) { - this._addAction("write", [buf], callback); +File.prototype.write = function (buf, pos, callback) { + this._addAction("write", [buf, pos], callback); }; -File.prototype.read = function (length, callback) { - this._addAction("read", [length], callback); +File.prototype.read = function (length, pos, callback) { + this._addAction("read", [length, pos], callback); }; // Some explanation of the File binding. @@ -92,6 +95,7 @@ File._addAction = File.prototype._addAction = function (method, args, callback) File._act = File.prototype._act = function () { var action = this._actionQueue[0]; if (action) + // TODO FIXME what if the action throws an error? this["_ffi_" + action.method].apply(this, action.args); };