Skip to content

Commit

Permalink
Allow table writes for uv.fs_write (closes #108)
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jan 28, 2015
1 parent 846c7fa commit cda72d1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,27 @@ static int luv_fs_write(lua_State* L) {
int64_t offset;
int ref;
uv_fs_t* req;
buf.base = (char*)luaL_checklstring(L, 2, &buf.len);
size_t count;
uv_buf_t *bufs = NULL;

if (lua_istable(L, 2)) {
bufs = luv_prep_bufs(L, 2, &count);
}
else if (lua_isstring(L, 2)) {
buf.base = (char*) luaL_checklstring(L, 2, &buf.len);
count = 1;
}
else {
return luaL_argerror(L, 2, "data must be string or table of strings");
}

offset = luaL_checkinteger(L, 3);
ref = luv_check_continuation(L, 4);
req = lua_newuserdata(L, sizeof(*req));
req->data = luv_setup_req(L, ref);
req->ptr = buf.base;
FS_CALL(write, req, file, &buf, 1, offset);
((luv_req_t*)req->data)->data = bufs;
FS_CALL(write, req, file, bufs ? bufs : &buf, count, offset);
}

static int luv_fs_mkdir(lua_State* L) {
Expand Down
2 changes: 1 addition & 1 deletion src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static int luv_spawn(lua_State* L) {

luv_clean_options(&options);
if (ret < 0) {
uv_close(handle, NULL);
uv_close((uv_handle_t*)handle, NULL);
return luv_error(L, ret);
}
lua_pushinteger(L, handle->pid);
Expand Down
9 changes: 9 additions & 0 deletions tests/test-fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ return require('lib/tap')(function (test)
end))
end)

test("fs.write", function (print, p, expect, uv)
local path = "_test_"
local fd = assert(uv.fs_open(path, "w", 438))
uv.fs_write(fd, "Hello World\n", -1)
uv.fs_write(fd, {"with\n", "more\n", "lines\n"}, -1)
uv.fs_close(fd)
uv.fs_unlink(path)
end)

test("fs.stat sync", function (print, p, expect, uv)
local stat = assert(uv.fs_stat("README.md"))
assert(stat.size)
Expand Down
11 changes: 11 additions & 0 deletions tests/test-leaks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ return require('lib/tap')(function (test)
assert(after < before * 1.5)
end

test("fs-write", function (print, p, expect, uv)
bench(uv, p, 0x7000, function ()
local path = "_test_"
local fd = assert(uv.fs_open(path, "w", 438))
uv.fs_write(fd, "Hello World\n", -1)
uv.fs_write(fd, {"with\n", "more\n", "lines\n"}, -1)
uv.fs_close(fd)
uv.fs_unlink(path)
end)
end)

test("lots-o-timers", function (print, p, expect, uv)
bench(uv, p, 0x10000, function ()
local timer = uv.new_timer()
Expand Down

0 comments on commit cda72d1

Please sign in to comment.