Skip to content

Commit

Permalink
Merge pull request #293 from hnakamur/test-fs-open
Browse files Browse the repository at this point in the history
add test-fs-open, support optional number or string mode, support 'rs' and 'rs+' flag
  • Loading branch information
rphillips committed Aug 12, 2012
2 parents 331a99d + 2b2e21e commit 109d8ce
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 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
4 changes: 3 additions & 1 deletion src/luv_fs.c
Expand Up @@ -83,6 +83,8 @@ int luv_string_to_flags(lua_State* L, const char* string) {
if (strcmp(string, "w+") == 0) return O_CREAT | O_TRUNC | O_RDWR;
if (strcmp(string, "a") == 0) return O_APPEND | O_CREAT | O_WRONLY;
if (strcmp(string, "a+") == 0) return O_APPEND | O_CREAT | O_RDWR;
if (strcmp(string, "rs") == 0) return O_RDONLY | O_SYNC;
if (strcmp(string, "rs+") == 0) return O_RDWR | O_SYNC;
return luaL_error(L, "Unknown file open flag'%s'", string);
}

Expand Down Expand Up @@ -229,7 +231,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 = luaL_checkint(L, 3);
uv_fs_t* req = luv_fs_store_callback(L, 4);
FS_CALL(open, 4, path, path, flags, mode);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/test-fs-open.lua
@@ -0,0 +1,49 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
require("helper")
local FS = require('fs')
-- should throw ENOENT, not EBADF
-- see https://github.com/joyent/node/pull/1228
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')
local openFd
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', function(err, fd)
if err then return err end
openFd2 = fd
end)
--]]
process:on('exit', function()
assert(openFd)
-- assert(openFd2)
end)

0 comments on commit 109d8ce

Please sign in to comment.