Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support omission of mode when callback is also omitted.
  • Loading branch information
hnakamur committed Aug 12, 2012
1 parent 552e9ba commit e6afd7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
26 changes: 25 additions & 1 deletion lib/luvit/fs.lua
Expand Up @@ -21,7 +21,6 @@ local table = require('table')
local iStream = require('core').iStream
local fs = {}
local sizes = {
Open = 3,
Close = 1,
Read = 3,
Write = 3,
Expand Down Expand Up @@ -90,6 +89,31 @@ for name, arity in pairs(sizes) do
fs[name:lower() .. "Sync"] = sync
end
function modeNum(m, def)
local t = type(m)
if t == 'number' then
return m
elseif t == 'string' then
return tonumber(m, 8)
else
return def and modeNum(def) or nil
end
end
function fs.open(path, flags, mode, callback)
if callback == nil then
callback = mode
mode = 438 --[[=0666]]
else
mode = modeNum(mode)
end
native.fsOpen(path, flags, mode, callback or default)
end
function fs.openSync(path, flags, mode)
return native.fsOpen(path, flags, modeNum(mode, 438 --[[=0666]]))
end
function fs.exists(path, callback)
native.fsStat(path, function (err)
if not err then
Expand Down
2 changes: 1 addition & 1 deletion src/luv_fs.c
Expand Up @@ -229,7 +229,7 @@ uv_fs_t* luv_fs_store_callback(lua_State* L, int index) {
int luv_fs_open(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
int flags = luv_string_to_flags(L, luaL_checkstring(L, 2));
int mode = strtoul(luaL_checkstring(L, 3), NULL, 8);
int mode = strtoul(luaL_optstring(L, 3, "0666"), NULL, 8);
uv_fs_t* req = luv_fs_store_callback(L, 4);
FS_CALL(open, 4, path, path, flags, mode);
}
Expand Down
8 changes: 3 additions & 5 deletions tests/test-fs-open.lua
Expand Up @@ -22,24 +22,22 @@ local FS = require('fs')
-- should throw ENOENT, not EBADF
-- see https://github.com/joyent/node/pull/1228
-- TODO: support mode parameter may be omitted.
local ok, err = pcall(FS.openSync, '/path/to/file/that/does/not/exist', 'r', '0666')
local ok, err = pcall(FS.openSync, '/path/to/file/that/does/not/exist', 'r')
assert(not ok)
assert(err.code == 'ENOENT')
assert(err.path == '/path/to/file/that/does/not/exist')
assert(err.source == 'open')
-- TODO: support mode parameter may be omitted.
local openFd
FS.open(__filename, 'r', '0666', function(err, fd)
FS.open(__filename, 'r', function(err, fd)
if err then return err end
openFd = fd
end)
-- TODO: Support file open flag 's'
--[[
local openFd2
FS.open(__filename, 'rs', '0666', function(err, fd)
FS.open(__filename, 'rs', function(err, fd)
if err then return err end
openFd2 = fd
end)
Expand Down

0 comments on commit e6afd7c

Please sign in to comment.