2024-01-12 05:03:25 -07:00
|
|
|
local uv = vim.uv
|
2015-11-17 07:49:12 -07:00
|
|
|
local bit = require('bit')
|
|
|
|
|
2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.unit.testutil')
|
2024-04-08 02:03:20 -07:00
|
|
|
local itp = t.gen_itp(it)
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local cimport = t.cimport
|
|
|
|
local cppimport = t.cppimport
|
|
|
|
local internalize = t.internalize
|
|
|
|
local ok = t.ok
|
|
|
|
local eq = t.eq
|
|
|
|
local neq = t.neq
|
|
|
|
local ffi = t.ffi
|
|
|
|
local cstr = t.cstr
|
|
|
|
local to_cstr = t.to_cstr
|
|
|
|
local OK = t.OK
|
|
|
|
local FAIL = t.FAIL
|
|
|
|
local NULL = t.NULL
|
|
|
|
local mkdir = t.mkdir
|
2024-01-12 04:28:20 -07:00
|
|
|
local endswith = vim.endswith
|
2017-03-11 06:02:47 -07:00
|
|
|
|
2016-05-22 11:32:57 -07:00
|
|
|
local NODE_NORMAL = 0
|
|
|
|
local NODE_WRITABLE = 1
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2017-02-02 18:18:22 -07:00
|
|
|
local fs = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
|
2014-08-30 07:26:56 -07:00
|
|
|
cppimport('sys/stat.h')
|
2016-01-14 15:19:36 -07:00
|
|
|
cppimport('fcntl.h')
|
2019-07-28 02:10:22 -07:00
|
|
|
cimport('uv.h')
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2016-06-21 15:01:21 -07:00
|
|
|
local s = ''
|
|
|
|
for i = 0, 255 do
|
|
|
|
s = s .. (i == 0 and '\0' or ('%c'):format(i))
|
|
|
|
end
|
|
|
|
local fcontents = s:rep(16)
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local directory = nil
|
2014-09-16 17:10:36 -07:00
|
|
|
local absolute_executable = nil
|
2014-09-06 15:36:00 -07:00
|
|
|
local executable_name = nil
|
|
|
|
|
2015-08-11 14:04:00 -07:00
|
|
|
local function set_bit(number, to_set)
|
|
|
|
return bit.bor(number, to_set)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function unset_bit(number, to_unset)
|
|
|
|
return bit.band(number, (bit.bnot(to_unset)))
|
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function assert_file_exists(filepath)
|
2024-01-12 05:03:25 -07:00
|
|
|
neq(nil, uv.fs_stat(filepath))
|
2014-08-30 07:26:56 -07:00
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function assert_file_does_not_exist(filepath)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(nil, uv.fs_stat(filepath))
|
2014-08-30 07:26:56 -07:00
|
|
|
end
|
|
|
|
|
2015-08-11 14:04:00 -07:00
|
|
|
local function os_setperm(filename, perm)
|
|
|
|
return fs.os_setperm((to_cstr(filename)), perm)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function os_getperm(filename)
|
|
|
|
local perm = fs.os_getperm((to_cstr(filename)))
|
|
|
|
return tonumber(perm)
|
|
|
|
end
|
|
|
|
|
2017-04-24 13:45:03 -07:00
|
|
|
describe('fs.c', function()
|
|
|
|
local function os_isdir(name)
|
|
|
|
return fs.os_isdir(to_cstr(name))
|
|
|
|
end
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
before_each(function()
|
2023-12-04 15:32:39 -07:00
|
|
|
mkdir('unit-test-directory')
|
2015-08-11 14:04:00 -07:00
|
|
|
|
2022-12-22 07:17:01 -07:00
|
|
|
io.open('unit-test-directory/test.file', 'w'):close()
|
2015-08-11 14:04:00 -07:00
|
|
|
|
2022-12-22 07:17:01 -07:00
|
|
|
io.open('unit-test-directory/test_2.file', 'w'):close()
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_symlink('test.file', 'unit-test-directory/test_link.file')
|
2016-07-05 22:40:25 -07:00
|
|
|
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_symlink('non_existing_file.file', 'unit-test-directory/test_broken_link.file')
|
2017-02-02 18:18:22 -07:00
|
|
|
-- The tests are invoked with an absolute path to `busted` executable.
|
2014-09-16 17:10:36 -07:00
|
|
|
absolute_executable = arg[0]
|
2017-02-02 18:18:22 -07:00
|
|
|
-- Split the absolute_executable path into a directory and filename.
|
2014-08-30 07:26:56 -07:00
|
|
|
directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
|
|
|
|
end)
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
after_each(function()
|
2014-08-30 07:26:56 -07:00
|
|
|
os.remove('unit-test-directory/test.file')
|
|
|
|
os.remove('unit-test-directory/test_2.file')
|
|
|
|
os.remove('unit-test-directory/test_link.file')
|
2014-08-08 07:53:03 -07:00
|
|
|
os.remove('unit-test-directory/test_hlink.file')
|
2016-07-05 22:40:25 -07:00
|
|
|
os.remove('unit-test-directory/test_broken_link.file')
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory')
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_dirname', function()
|
2017-04-24 13:45:03 -07:00
|
|
|
itp('returns OK and writes current directory to the buffer', function()
|
2024-01-12 05:03:25 -07:00
|
|
|
local length = string.len(uv.cwd()) + 1
|
2017-04-24 13:45:03 -07:00
|
|
|
local buf = cstr(length, '')
|
|
|
|
eq(OK, fs.os_dirname(buf, length))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(uv.cwd(), ffi.string(buf))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns FAIL if the buffer is too small', function()
|
2024-01-12 05:03:25 -07:00
|
|
|
local length = string.len(uv.cwd()) + 1
|
2017-04-24 13:45:03 -07:00
|
|
|
local buf = cstr(length - 1, '')
|
|
|
|
eq(FAIL, fs.os_dirname(buf, length - 1))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2017-04-24 13:45:03 -07:00
|
|
|
describe('os_chdir', function()
|
|
|
|
itp('fails with path="~"', function()
|
2019-09-16 15:39:33 -07:00
|
|
|
eq(false, os_isdir('~'), 'sanity check: no literal "~" directory')
|
2017-04-24 13:45:03 -07:00
|
|
|
local length = 4096
|
|
|
|
local expected_cwd = cstr(length, '')
|
|
|
|
local cwd = cstr(length, '')
|
|
|
|
eq(OK, fs.os_dirname(expected_cwd, length))
|
|
|
|
|
|
|
|
-- os_chdir returns 0 for success, not OK (1).
|
2023-12-04 15:32:39 -07:00
|
|
|
neq(0, fs.os_chdir('~')) -- fail
|
|
|
|
neq(0, fs.os_chdir('~/')) -- fail
|
2017-04-24 13:45:03 -07:00
|
|
|
|
|
|
|
eq(OK, fs.os_dirname(cwd, length))
|
|
|
|
-- CWD did not change.
|
|
|
|
eq(ffi.string(expected_cwd), ffi.string(cwd))
|
|
|
|
end)
|
|
|
|
end)
|
2014-08-30 07:26:56 -07:00
|
|
|
|
|
|
|
describe('os_isdir', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if an empty string is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(false, (os_isdir('')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if a nonexisting directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(false, (os_isdir('non-existing-directory')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if a nonexisting absolute directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(false, (os_isdir('/non-existing-directory')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if an existing file is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(false, (os_isdir('unit-test-directory/test.file')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if the current directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(true, (os_isdir('.')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if the parent directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(true, (os_isdir('..')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if an arbitrary directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(true, (os_isdir('unit-test-directory')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if an absolute directory is given', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(true, (os_isdir(directory)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_can_exe', function()
|
2014-09-16 17:10:36 -07:00
|
|
|
local function os_can_exe(name)
|
|
|
|
local buf = ffi.new('char *[1]')
|
|
|
|
buf[0] = NULL
|
2016-06-05 03:40:37 -07:00
|
|
|
local ce_ret = fs.os_can_exe(to_cstr(name), buf, true)
|
2014-09-16 17:10:36 -07:00
|
|
|
|
|
|
|
-- When os_can_exe returns true, it must set the path.
|
|
|
|
-- When it returns false, the path must be NULL.
|
2016-06-05 03:40:37 -07:00
|
|
|
if ce_ret then
|
2014-09-16 17:10:36 -07:00
|
|
|
neq(NULL, buf[0])
|
|
|
|
return internalize(buf[0])
|
|
|
|
else
|
|
|
|
eq(NULL, buf[0])
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function cant_exe(name)
|
|
|
|
eq(nil, os_can_exe(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function exe(name)
|
|
|
|
return os_can_exe(name)
|
2014-08-30 07:26:56 -07:00
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false when given a directory', function()
|
2014-09-16 17:10:36 -07:00
|
|
|
cant_exe('./unit-test-directory')
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false when given a regular file without executable bit set', function()
|
2014-09-16 17:10:36 -07:00
|
|
|
cant_exe('unit-test-directory/test.file')
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false when the given file does not exists', function()
|
2014-09-16 17:10:36 -07:00
|
|
|
cant_exe('does-not-exist.file')
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the absolute path when given an executable inside $PATH', function()
|
2017-02-02 18:18:22 -07:00
|
|
|
local fullpath = exe('ls')
|
2023-12-16 14:14:28 -07:00
|
|
|
eq(true, fs.path_is_absolute(to_cstr(fullpath)))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the absolute path when given an executable relative to the current dir', function()
|
2024-01-12 05:03:25 -07:00
|
|
|
local old_dir = uv.cwd()
|
2015-02-09 04:49:11 -07:00
|
|
|
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.chdir(directory)
|
2015-02-09 04:49:11 -07:00
|
|
|
|
2016-11-16 16:33:45 -07:00
|
|
|
-- Rely on currentdir to resolve symlinks, if any. Testing against
|
|
|
|
-- the absolute path taken from arg[0] may result in failure where
|
2015-03-31 13:21:21 -07:00
|
|
|
-- the path has a symlink in it.
|
2024-01-12 05:03:25 -07:00
|
|
|
local canonical = uv.cwd() .. '/' .. executable_name
|
2015-03-31 13:21:21 -07:00
|
|
|
local expected = exe(canonical)
|
|
|
|
local relative_executable = './' .. executable_name
|
2014-09-16 17:10:36 -07:00
|
|
|
local res = exe(relative_executable)
|
2015-02-09 04:49:11 -07:00
|
|
|
|
2015-03-31 13:21:21 -07:00
|
|
|
-- Don't test yet; we need to chdir back first.
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.chdir(old_dir)
|
2015-02-09 04:49:11 -07:00
|
|
|
eq(expected, res)
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('file permissions', function()
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_fchown(filename, user_id, group_id)
|
2014-08-30 07:26:56 -07:00
|
|
|
local fd = ffi.C.open(filename, 0)
|
|
|
|
local res = fs.os_fchown(fd, user_id, group_id)
|
|
|
|
ffi.C.close(fd)
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2015-08-11 14:04:00 -07:00
|
|
|
local function os_file_is_readable(filename)
|
|
|
|
return fs.os_file_is_readable((to_cstr(filename)))
|
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_file_is_writable(filename)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_file_is_writable((to_cstr(filename)))
|
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function bit_set(number, check_bit)
|
2014-08-30 07:26:56 -07:00
|
|
|
return 0 ~= (bit.band(number, check_bit))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe('os_getperm', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns UV_ENOENT when the given file does not exist', function()
|
2015-10-27 06:15:41 -07:00
|
|
|
eq(ffi.C.UV_ENOENT, (os_getperm('non-existing-file')))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns a perm > 0 when given an existing file', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
assert.is_true((os_getperm('unit-test-directory')) > 0)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns S_IRUSR when the file is readable', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local perm = os_getperm('unit-test-directory')
|
|
|
|
assert.is_true((bit_set(perm, ffi.C.kS_IRUSR)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_setperm', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can set and unset the executable bit of a file', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local perm = os_getperm('unit-test-directory/test.file')
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IXUSR)
|
|
|
|
eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
|
|
|
|
perm = os_getperm('unit-test-directory/test.file')
|
|
|
|
assert.is_false((bit_set(perm, ffi.C.kS_IXUSR)))
|
|
|
|
perm = set_bit(perm, ffi.C.kS_IXUSR)
|
|
|
|
eq(OK, os_setperm('unit-test-directory/test.file', perm))
|
|
|
|
perm = os_getperm('unit-test-directory/test.file')
|
|
|
|
assert.is_true((bit_set(perm, ffi.C.kS_IXUSR)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails if given file does not exist', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local perm = ffi.C.kS_IXUSR
|
|
|
|
eq(FAIL, (os_setperm('non-existing-file', perm)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_fchown', function()
|
|
|
|
local filename = 'unit-test-directory/test.file'
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('does not change owner and group if respective IDs are equal to -1', function()
|
2024-01-12 05:03:25 -07:00
|
|
|
local uid = uv.fs_stat(filename).uid
|
|
|
|
local gid = uv.fs_stat(filename).gid
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(0, os_fchown(filename, -1, -1))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(uid, uv.fs_stat(filename).uid)
|
|
|
|
return eq(gid, uv.fs_stat(filename).gid)
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2015-05-24 23:45:17 -07:00
|
|
|
-- Some systems may not have `id` utility.
|
2023-12-04 15:32:39 -07:00
|
|
|
if os.execute('id -G > /dev/null 2>&1') ~= 0 then
|
2015-08-07 15:25:03 -07:00
|
|
|
pending('skipped (missing `id` utility)', function() end)
|
2015-05-24 23:45:17 -07:00
|
|
|
else
|
2023-12-04 15:32:39 -07:00
|
|
|
itp(
|
|
|
|
'owner of a file may change the group of the file to any group of which that owner is a member',
|
|
|
|
function()
|
2024-01-12 05:03:25 -07:00
|
|
|
local file_gid = uv.fs_stat(filename).gid
|
2023-12-04 15:32:39 -07:00
|
|
|
|
|
|
|
-- Gets ID of any group of which current user is a member except the
|
|
|
|
-- group that owns the file.
|
|
|
|
local id_fd = io.popen('id -G')
|
|
|
|
local new_gid = id_fd:read('*n')
|
|
|
|
if new_gid == file_gid then
|
|
|
|
new_gid = id_fd:read('*n')
|
|
|
|
end
|
|
|
|
id_fd:close()
|
|
|
|
|
|
|
|
-- User can be a member of only one group.
|
|
|
|
-- In that case we can not perform this test.
|
|
|
|
if new_gid then
|
|
|
|
eq(0, (os_fchown(filename, -1, new_gid)))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(new_gid, uv.fs_stat(filename).gid)
|
2023-12-04 15:32:39 -07:00
|
|
|
end
|
2014-08-30 07:26:56 -07:00
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
)
|
2015-05-24 23:45:17 -07:00
|
|
|
end
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
if ffi.os == 'Windows' or ffi.C.geteuid() == 0 then
|
2015-08-11 18:00:56 -07:00
|
|
|
pending('skipped (uv_fs_chown is no-op on Windows)', function() end)
|
2015-05-24 23:45:17 -07:00
|
|
|
else
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns nonzero if process has not enough permissions', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
-- chown to root
|
|
|
|
neq(0, os_fchown(filename, 0, 0))
|
2015-05-24 23:45:17 -07:00
|
|
|
end)
|
|
|
|
end
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2015-08-11 14:04:00 -07:00
|
|
|
describe('os_file_is_readable', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if the file is not readable', function()
|
2015-08-11 14:04:00 -07:00
|
|
|
local perm = os_getperm('unit-test-directory/test.file')
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IRUSR)
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IRGRP)
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IROTH)
|
|
|
|
eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
|
|
|
|
eq(false, os_file_is_readable('unit-test-directory/test.file'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if the file does not exist', function()
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(false, os_file_is_readable('unit-test-directory/what_are_you_smoking.gif'))
|
2015-08-11 14:04:00 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if the file is readable', function()
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(true, os_file_is_readable('unit-test-directory/test.file'))
|
2015-08-11 14:04:00 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-30 07:26:56 -07:00
|
|
|
describe('os_file_is_writable', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns 0 if the file is readonly', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local perm = os_getperm('unit-test-directory/test.file')
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IWUSR)
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IWGRP)
|
|
|
|
perm = unset_bit(perm, ffi.C.kS_IWOTH)
|
|
|
|
eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
|
|
|
|
eq(0, os_file_is_writable('unit-test-directory/test.file'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns 1 if the file is writable', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(1, os_file_is_writable('unit-test-directory/test.file'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns 2 when given a folder with rights to write into', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(2, os_file_is_writable('unit-test-directory'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('file operations', function()
|
2016-07-05 22:40:25 -07:00
|
|
|
local function os_path_exists(filename)
|
|
|
|
return fs.os_path_exists((to_cstr(filename)))
|
2014-08-30 07:26:56 -07:00
|
|
|
end
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_rename(path, new_path)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_rename((to_cstr(path)), (to_cstr(new_path)))
|
|
|
|
end
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_remove(path)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_remove((to_cstr(path)))
|
|
|
|
end
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_open(path, flags, mode)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_open((to_cstr(path)), flags, mode)
|
|
|
|
end
|
2016-06-05 03:40:37 -07:00
|
|
|
local function os_close(fd)
|
|
|
|
return fs.os_close(fd)
|
|
|
|
end
|
2016-06-23 12:25:35 -07:00
|
|
|
-- For some reason if length of NUL-bytes-string is the same as `char[?]`
|
|
|
|
-- size luajit crashes. Though it does not do so in this test suite, better
|
|
|
|
-- be cautios and allocate more elements then needed. I only did this to
|
|
|
|
-- strings.
|
2016-06-05 03:40:37 -07:00
|
|
|
local function os_read(fd, size)
|
|
|
|
local buf = nil
|
|
|
|
if size == nil then
|
|
|
|
size = 0
|
|
|
|
else
|
2016-06-23 12:25:35 -07:00
|
|
|
buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
|
2016-06-05 03:40:37 -07:00
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
local eof = ffi.new('bool[?]', 1, { true })
|
2017-03-19 07:29:48 -07:00
|
|
|
local ret2 = fs.os_read(fd, eof, buf, size, false)
|
2016-06-05 03:40:37 -07:00
|
|
|
local ret1 = eof[0]
|
|
|
|
local ret3 = ''
|
|
|
|
if buf ~= nil then
|
|
|
|
ret3 = ffi.string(buf, size)
|
|
|
|
end
|
|
|
|
return ret1, ret2, ret3
|
|
|
|
end
|
|
|
|
local function os_readv(fd, sizes)
|
|
|
|
local bufs = {}
|
|
|
|
for i, size in ipairs(sizes) do
|
|
|
|
bufs[i] = {
|
2023-12-04 15:32:39 -07:00
|
|
|
iov_base = ffi.new('char[?]', size + 1, ('\0'):rep(size)),
|
|
|
|
iov_len = size,
|
2016-06-05 03:40:37 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
local iov = ffi.new('struct iovec[?]', #sizes, bufs)
|
2023-12-04 15:32:39 -07:00
|
|
|
local eof = ffi.new('bool[?]', 1, { true })
|
2017-03-19 07:29:48 -07:00
|
|
|
local ret2 = fs.os_readv(fd, eof, iov, #sizes, false)
|
2016-06-05 03:40:37 -07:00
|
|
|
local ret1 = eof[0]
|
|
|
|
local ret3 = {}
|
2023-12-04 15:32:39 -07:00
|
|
|
for i = 1, #sizes do
|
2016-06-05 03:40:37 -07:00
|
|
|
-- Warning: iov may not be used.
|
|
|
|
ret3[i] = ffi.string(bufs[i].iov_base, bufs[i].iov_len)
|
|
|
|
end
|
|
|
|
return ret1, ret2, ret3
|
|
|
|
end
|
2016-06-21 12:51:40 -07:00
|
|
|
local function os_write(fd, data)
|
2017-03-19 07:29:48 -07:00
|
|
|
return fs.os_write(fd, data, data and #data or 0, false)
|
2016-06-21 12:51:40 -07:00
|
|
|
end
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2016-07-05 22:40:25 -07:00
|
|
|
describe('os_path_exists', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false when given a non-existing file', function()
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(false, (os_path_exists('non-existing-file')))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true when given an existing file', function()
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(true, (os_path_exists('unit-test-directory/test.file')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false when given a broken symlink', function()
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(false, (os_path_exists('unit-test-directory/test_broken_link.file')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true when given a directory', function()
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(true, (os_path_exists('unit-test-directory')))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_rename', function()
|
|
|
|
local test = 'unit-test-directory/test.file'
|
|
|
|
local not_exist = 'unit-test-directory/not_exist.file'
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can rename file if destination file does not exist', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(OK, (os_rename(test, not_exist)))
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(false, (os_path_exists(test)))
|
|
|
|
eq(true, (os_path_exists(not_exist)))
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(OK, (os_rename(not_exist, test))) -- restore test file
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fail if source file does not exist', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(FAIL, (os_rename(not_exist, test)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can overwrite destination file if it exists', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local other = 'unit-test-directory/other.file'
|
|
|
|
local file = io.open(other, 'w')
|
|
|
|
file:write('other')
|
|
|
|
file:flush()
|
|
|
|
file:close()
|
|
|
|
|
|
|
|
eq(OK, (os_rename(other, test)))
|
2016-07-05 22:40:25 -07:00
|
|
|
eq(false, (os_path_exists(other)))
|
|
|
|
eq(true, (os_path_exists(test)))
|
2014-08-30 07:26:56 -07:00
|
|
|
file = io.open(test, 'r')
|
|
|
|
eq('other', (file:read('*all')))
|
|
|
|
file:close()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_remove', function()
|
|
|
|
before_each(function()
|
2022-12-22 07:17:01 -07:00
|
|
|
io.open('unit-test-directory/test_remove.file', 'w'):close()
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove('unit-test-directory/test_remove.file')
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns non-zero when given a non-existing file', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
neq(0, (os_remove('non-existing-file')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('removes the given file and returns 0', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local f = 'unit-test-directory/test_remove.file'
|
|
|
|
assert_file_exists(f)
|
|
|
|
eq(0, (os_remove(f)))
|
|
|
|
assert_file_does_not_exist(f)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2017-03-19 06:09:48 -07:00
|
|
|
describe('os_dup', function()
|
|
|
|
itp('returns new file descriptor', function()
|
|
|
|
local dup0 = fs.os_dup(0)
|
|
|
|
local dup1 = fs.os_dup(1)
|
|
|
|
local dup2 = fs.os_dup(2)
|
2023-12-04 15:32:39 -07:00
|
|
|
local tbl = {
|
|
|
|
[0] = true,
|
|
|
|
[1] = true,
|
|
|
|
[2] = true,
|
|
|
|
[tonumber(dup0)] = true,
|
|
|
|
[tonumber(dup1)] = true,
|
|
|
|
[tonumber(dup2)] = true,
|
|
|
|
}
|
2017-03-19 06:09:48 -07:00
|
|
|
local i = 0
|
|
|
|
for _, _ in pairs(tbl) do
|
|
|
|
i = i + 1
|
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(i, 6) -- All fds must be unique
|
2017-03-19 06:09:48 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-30 07:26:56 -07:00
|
|
|
describe('os_open', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local new_file = 'test_new_file'
|
|
|
|
local existing_file = 'unit-test-directory/test_existing.file'
|
|
|
|
|
2014-08-30 07:26:56 -07:00
|
|
|
before_each(function()
|
2016-06-05 03:40:37 -07:00
|
|
|
(io.open(existing_file, 'w')):close()
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
2016-06-05 03:40:37 -07:00
|
|
|
os.remove(existing_file)
|
|
|
|
os.remove(new_file)
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns UV_ENOENT for O_RDWR on a non-existing file', function()
|
2015-10-27 06:15:41 -07:00
|
|
|
eq(ffi.C.UV_ENOENT, (os_open('non-existing-file', ffi.C.kO_RDWR, 0)))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
itp(
|
|
|
|
'returns non-negative for O_CREAT on a non-existing file which then can be closed',
|
|
|
|
function()
|
|
|
|
assert_file_does_not_exist(new_file)
|
|
|
|
local fd = os_open(new_file, ffi.C.kO_CREAT, 0)
|
|
|
|
assert.is_true(0 <= fd)
|
|
|
|
eq(0, os_close(fd))
|
|
|
|
end
|
|
|
|
)
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns non-negative for O_CREAT on a existing file which then can be closed', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
assert_file_exists(existing_file)
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(existing_file, ffi.C.kO_CREAT, 0)
|
|
|
|
assert.is_true(0 <= fd)
|
|
|
|
eq(0, os_close(fd))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns UV_EEXIST for O_CREAT|O_EXCL on a existing file', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
assert_file_exists(existing_file)
|
2019-07-28 02:10:22 -07:00
|
|
|
eq(ffi.C.UV_EEXIST, (os_open(existing_file, (bit.bor(ffi.C.kO_CREAT, ffi.C.kO_EXCL)), 0)))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('sets `rwx` permissions for O_CREAT 700 which then can be closed', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
assert_file_does_not_exist(new_file)
|
|
|
|
--create the file
|
2023-12-04 15:32:39 -07:00
|
|
|
local fd = os_open(new_file, ffi.C.kO_CREAT, tonumber('700', 8))
|
2014-08-30 07:26:56 -07:00
|
|
|
--verify permissions
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(33216, uv.fs_stat(new_file).mode)
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('sets `rw` permissions for O_CREAT 600 which then can be closed', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
assert_file_does_not_exist(new_file)
|
|
|
|
--create the file
|
2023-12-04 15:32:39 -07:00
|
|
|
local fd = os_open(new_file, ffi.C.kO_CREAT, tonumber('600', 8))
|
2014-08-30 07:26:56 -07:00
|
|
|
--verify permissions
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(33152, uv.fs_stat(new_file).mode)
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
itp(
|
|
|
|
'returns a non-negative file descriptor for an existing file which then can be closed',
|
|
|
|
function()
|
|
|
|
local fd = os_open(existing_file, ffi.C.kO_RDWR, 0)
|
|
|
|
assert.is_true(0 <= fd)
|
|
|
|
eq(0, os_close(fd))
|
|
|
|
end
|
|
|
|
)
|
2016-06-05 03:40:37 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_close', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns EBADF for negative file descriptors', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(ffi.C.UV_EBADF, os_close(-1))
|
|
|
|
eq(ffi.C.UV_EBADF, os_close(-1000))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_read', function()
|
|
|
|
local file = 'test-unit-os-fs_spec-os_read.dat'
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
local f = io.open(file, 'w')
|
|
|
|
f:write(fcontents)
|
|
|
|
f:close()
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(file)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read zero bytes from a file', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ false, 0, '' }, { os_read(fd, nil) })
|
|
|
|
eq({ false, 0, '' }, { os_read(fd, 0) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read from a file multiple times', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ false, 2, '\000\001' }, { os_read(fd, 2) })
|
|
|
|
eq({ false, 2, '\002\003' }, { os_read(fd, 2) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read the whole file at once and then report eof', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ false, #fcontents, fcontents }, { os_read(fd, #fcontents) })
|
|
|
|
eq({ true, 0, ('\0'):rep(#fcontents) }, { os_read(fd, #fcontents) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read the whole file in two calls, one partially', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(
|
|
|
|
{ false, #fcontents * 3 / 4, fcontents:sub(1, #fcontents * 3 / 4) },
|
|
|
|
{ os_read(fd, #fcontents * 3 / 4) }
|
|
|
|
)
|
|
|
|
eq({
|
|
|
|
true,
|
|
|
|
(#fcontents * 1 / 4),
|
|
|
|
fcontents:sub(#fcontents * 3 / 4 + 1) .. ('\0'):rep(#fcontents * 2 / 4),
|
|
|
|
}, { os_read(fd, #fcontents * 3 / 4) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_readv', function()
|
|
|
|
-- Function may be absent
|
2023-12-04 15:32:39 -07:00
|
|
|
if not pcall(function()
|
|
|
|
return fs.os_readv
|
|
|
|
end) then
|
2016-06-05 03:40:37 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local file = 'test-unit-os-fs_spec-os_readv.dat'
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
local f = io.open(file, 'w')
|
|
|
|
f:write(fcontents)
|
|
|
|
f:close()
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
2016-06-05 03:40:37 -07:00
|
|
|
after_each(function()
|
|
|
|
os.remove(file)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read zero bytes from a file', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ false, 0, {} }, { os_readv(fd, {}) })
|
|
|
|
eq({ false, 0, { '', '', '' } }, { os_readv(fd, { 0, 0, 0 }) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read from a file multiple times to a differently-sized buffers', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ false, 2, { '\000\001' } }, { os_readv(fd, { 2 }) })
|
|
|
|
eq({ false, 5, { '\002\003', '\004\005\006' } }, { os_readv(fd, { 2, 3 }) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read the whole file at once and then report eof', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({
|
|
|
|
false,
|
|
|
|
#fcontents,
|
|
|
|
{
|
|
|
|
fcontents:sub(1, #fcontents * 1 / 4),
|
|
|
|
fcontents:sub(#fcontents * 1 / 4 + 1, #fcontents * 3 / 4),
|
|
|
|
fcontents:sub(#fcontents * 3 / 4 + 1, #fcontents * 15 / 16),
|
|
|
|
fcontents:sub(#fcontents * 15 / 16 + 1, #fcontents),
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
os_readv(
|
|
|
|
fd,
|
|
|
|
{ #fcontents * 1 / 4, #fcontents * 2 / 4, #fcontents * 3 / 16, #fcontents * 1 / 16 }
|
|
|
|
),
|
|
|
|
})
|
|
|
|
eq({ true, 0, { '\0' } }, { os_readv(fd, { 1 }) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read the whole file in two calls, one partially', function()
|
2016-06-05 03:40:37 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_RDONLY, 0)
|
|
|
|
ok(fd >= 0)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq(
|
|
|
|
{ false, #fcontents * 3 / 4, { fcontents:sub(1, #fcontents * 3 / 4) } },
|
|
|
|
{ os_readv(fd, { #fcontents * 3 / 4 }) }
|
|
|
|
)
|
|
|
|
eq({
|
|
|
|
true,
|
|
|
|
(#fcontents * 1 / 4),
|
|
|
|
{ fcontents:sub(#fcontents * 3 / 4 + 1) .. ('\0'):rep(#fcontents * 2 / 4) },
|
|
|
|
}, { os_readv(fd, { #fcontents * 3 / 4 }) })
|
2016-06-05 03:40:37 -07:00
|
|
|
eq(0, os_close(fd))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
2016-05-22 11:32:57 -07:00
|
|
|
|
2016-06-21 12:51:40 -07:00
|
|
|
describe('os_write', function()
|
|
|
|
-- Function may be absent
|
|
|
|
local file = 'test-unit-os-fs_spec-os_write.dat'
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
local f = io.open(file, 'w')
|
|
|
|
f:write(fcontents)
|
|
|
|
f:close()
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(file)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can write zero bytes to a file', function()
|
2016-06-21 12:51:40 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_WRONLY, 0)
|
|
|
|
ok(fd >= 0)
|
|
|
|
eq(0, os_write(fd, ''))
|
|
|
|
eq(0, os_write(fd, nil))
|
|
|
|
eq(fcontents, io.open(file, 'r'):read('*a'))
|
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can write some data to a file', function()
|
2016-06-21 12:51:40 -07:00
|
|
|
local fd = os_open(file, ffi.C.kO_WRONLY, 0)
|
|
|
|
ok(fd >= 0)
|
|
|
|
eq(3, os_write(fd, 'abc'))
|
|
|
|
eq(4, os_write(fd, ' def'))
|
|
|
|
eq('abc def' .. fcontents:sub(8), io.open(file, 'r'):read('*a'))
|
|
|
|
eq(0, os_close(fd))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-05-22 11:32:57 -07:00
|
|
|
describe('os_nodetype', function()
|
|
|
|
before_each(function()
|
|
|
|
os.remove('non-existing-file')
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NODE_NORMAL for non-existing file', function()
|
2016-05-22 11:32:57 -07:00
|
|
|
eq(NODE_NORMAL, fs.os_nodetype(to_cstr('non-existing-file')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns NODE_WRITABLE for /dev/stderr', function()
|
2016-05-22 11:32:57 -07:00
|
|
|
eq(NODE_WRITABLE, fs.os_nodetype(to_cstr('/dev/stderr')))
|
|
|
|
end)
|
|
|
|
end)
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('folder operations', function()
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_mkdir(path, mode)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_mkdir(to_cstr(path), mode)
|
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function os_rmdir(path)
|
2014-08-30 07:26:56 -07:00
|
|
|
return fs.os_rmdir(to_cstr(path))
|
|
|
|
end
|
|
|
|
|
2015-07-12 04:09:26 -07:00
|
|
|
local function os_mkdir_recurse(path, mode)
|
2023-12-04 15:32:39 -07:00
|
|
|
local failed_str = ffi.new('char *[1]', { nil })
|
|
|
|
local created_str = ffi.new('char *[1]', { nil })
|
2023-04-15 21:28:04 -07:00
|
|
|
local ret = fs.os_mkdir_recurse(path, mode, failed_str, created_str)
|
|
|
|
local failed_dir = failed_str[0]
|
|
|
|
if failed_dir ~= nil then
|
|
|
|
failed_dir = ffi.string(failed_dir)
|
2015-07-12 04:09:26 -07:00
|
|
|
end
|
2023-04-15 21:28:04 -07:00
|
|
|
local created_dir = created_str[0]
|
|
|
|
if created_dir ~= nil then
|
|
|
|
created_dir = ffi.string(created_dir)
|
|
|
|
end
|
|
|
|
return ret, failed_dir, created_dir
|
2015-07-12 04:09:26 -07:00
|
|
|
end
|
|
|
|
|
2014-08-30 07:26:56 -07:00
|
|
|
describe('os_mkdir', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns non-zero when given an already existing directory', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
|
|
|
neq(0, (os_mkdir('unit-test-directory', mode)))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('creates a directory and returns 0', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
|
|
|
eq(false, (os_isdir('unit-test-directory/new-dir')))
|
|
|
|
eq(0, (os_mkdir('unit-test-directory/new-dir', mode)))
|
|
|
|
eq(true, (os_isdir('unit-test-directory/new-dir')))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir')
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2015-07-12 04:09:26 -07:00
|
|
|
describe('os_mkdir_recurse', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns zero when given an already existing directory', function()
|
2015-07-12 04:09:26 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-04-15 21:28:04 -07:00
|
|
|
local ret, failed_dir, created_dir = os_mkdir_recurse('unit-test-directory', mode)
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq(nil, failed_dir)
|
|
|
|
eq(nil, created_dir)
|
2015-07-12 04:09:26 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to create a directory where there is a file', function()
|
2015-07-12 04:09:26 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir = os_mkdir_recurse('unit-test-directory/test.file', mode)
|
2015-07-12 04:09:26 -07:00
|
|
|
neq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq('unit-test-directory/test.file', failed_dir)
|
|
|
|
eq(nil, created_dir)
|
2015-07-12 04:09:26 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to create a directory where there is a file in path', function()
|
2015-07-12 04:09:26 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir =
|
|
|
|
os_mkdir_recurse('unit-test-directory/test.file/test', mode)
|
2015-07-12 04:09:26 -07:00
|
|
|
neq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq('unit-test-directory/test.file', failed_dir)
|
|
|
|
eq(nil, created_dir)
|
2015-07-12 04:09:26 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('succeeds to create a directory', function()
|
2015-07-12 04:09:26 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir =
|
|
|
|
os_mkdir_recurse('unit-test-directory/new-dir-recurse', mode)
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq(nil, failed_dir)
|
|
|
|
ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse')
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('succeeds to create a directory ending with ///', function()
|
2015-10-19 05:25:49 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir =
|
|
|
|
os_mkdir_recurse('unit-test-directory/new-dir-recurse///', mode)
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq(nil, failed_dir)
|
|
|
|
ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse')
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('succeeds to create a directory ending with /', function()
|
2015-10-19 05:25:49 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir =
|
|
|
|
os_mkdir_recurse('unit-test-directory/new-dir-recurse/', mode)
|
2015-10-19 05:25:49 -07:00
|
|
|
eq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq(nil, failed_dir)
|
|
|
|
ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse')
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('succeeds to create a directory tree', function()
|
2015-07-12 04:09:26 -07:00
|
|
|
local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret, failed_dir, created_dir =
|
|
|
|
os_mkdir_recurse('unit-test-directory/new-dir-recurse/1/2/3', mode)
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(0, ret)
|
2023-04-15 21:28:04 -07:00
|
|
|
eq(nil, failed_dir)
|
|
|
|
ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
|
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse/1'))
|
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2'))
|
|
|
|
eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2/3'))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse/1/2/3')
|
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse/1/2')
|
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse/1')
|
|
|
|
uv.fs_rmdir('unit-test-directory/new-dir-recurse')
|
2015-07-12 04:09:26 -07:00
|
|
|
eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-30 07:26:56 -07:00
|
|
|
describe('os_rmdir', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns non_zero when given a non-existing directory', function()
|
2014-08-30 07:26:56 -07:00
|
|
|
neq(0, (os_rmdir('non-existing-directory')))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('removes the given directory and returns 0', function()
|
2023-04-04 12:59:06 -07:00
|
|
|
mkdir('unit-test-directory/new-dir')
|
2015-11-17 07:49:12 -07:00
|
|
|
eq(0, os_rmdir('unit-test-directory/new-dir'))
|
2014-08-30 07:26:56 -07:00
|
|
|
eq(false, (os_isdir('unit-test-directory/new-dir')))
|
|
|
|
end)
|
|
|
|
end)
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('FileInfo', function()
|
2014-09-06 15:36:00 -07:00
|
|
|
local function file_info_new()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = ffi.new('FileInfo[1]')
|
|
|
|
info[0].stat.st_ino = 0
|
|
|
|
info[0].stat.st_dev = 0
|
|
|
|
return info
|
2014-08-31 04:45:54 -07:00
|
|
|
end
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
-- Returns true if the FileInfo object has non-empty fields.
|
|
|
|
local function has_fileinfo(info)
|
|
|
|
return info[0].stat.st_ino > 0 and info[0].stat.st_dev > 0
|
2014-08-31 04:45:54 -07:00
|
|
|
end
|
|
|
|
|
2014-09-06 15:36:00 -07:00
|
|
|
local function file_id_new()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = ffi.new('FileID[1]')
|
|
|
|
info[0].inode = 0
|
|
|
|
info[0].device_id = 0
|
|
|
|
return info
|
2014-08-31 04:45:54 -07:00
|
|
|
end
|
|
|
|
|
2014-08-27 10:14:44 -07:00
|
|
|
describe('os_fileinfo', function()
|
os_stat: return ENOENT on NULL filename arg
Closes #4370
Explication:
In the backtrace in #4370, we see that `buf_write()` was called with
non-NULL `fname` and `sfname` arguments, but they've since _become_
NULL.
#7 0x00000000004de09d in buf_write (buf=0x1dee040, fname=0x0, fname@entry=0x1e985b0 "/home/sean/src/github.com/snczl/virta/pkg/meld/segment.go",
sfname=0x0, sfname@entry=0x1ddfa60 "segment.go", start=1, end=72, eap=eap@entry=0x7ffc6b032e60, append=0,
forceit=0, reset_changed=1, filtering=0)
at /home/travis/build/neovim/bot-ci/build/neovim/src/nvim/fileio.c:2576
This is most likely due to the code that restores those values from
`buf`, which happens just before the fatal call to `os_fileinfo`
```c
/*
* The autocommands may have changed the name of the buffer, which may
* be kept in fname, ffname and sfname.
*/
if (buf_ffname)
ffname = buf->b_ffname;
if (buf_sfname)
sfname = buf->b_sfname;
if (buf_fname_f)
fname = buf->b_ffname;
if (buf_fname_s)
fname = buf->b_sfname;
```
It's worth noting that at this point `ffname` is still non-NULL, so
it _could_ be used. However, our current code is purely more strict
than Vim in this area, which has caused us problems before (e.g.,
`getdigits()`). The commentary for `struct file_buffer` clearly
indicate that all of `b_ffname`, `b_sfname`, and `b_fname` may be
NULL:
```c
/*
* b_ffname has the full path of the file (NULL for no name).
* b_sfname is the name as the user typed it (or NULL).
* b_fname is the same as b_sfname, unless ":cd" has been done,
* then it is the same as b_ffname (NULL for no name).
*/
char_u *b_ffname; /* full path file name */
char_u *b_sfname; /* short file name */
char_u *b_fname; /* current file name */
```
Vim directly calls `stat(2)` which, although it is annotated to tell
the compiler that the path argument is non-NULL, does handle a NULL
pointer by returning a `-1` value and setting `errno` to `EFAULT`.
This satisfies Vim's check, since it treats any `-1` return from
`stat(2)` to mean the file doesn't exist (at least in this code
path).
Note that Vim's mch_stat() implementations on win32 and solaris
clearly cannot accept NULL `name`. But the codepaths that call
mch_stat will NULL `name` tend to be unix-only (eg: u_read_undo)!
2017-08-08 13:18:50 -07:00
|
|
|
itp('returns false if path=NULL', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_false((fs.os_fileinfo(nil, info)))
|
os_stat: return ENOENT on NULL filename arg
Closes #4370
Explication:
In the backtrace in #4370, we see that `buf_write()` was called with
non-NULL `fname` and `sfname` arguments, but they've since _become_
NULL.
#7 0x00000000004de09d in buf_write (buf=0x1dee040, fname=0x0, fname@entry=0x1e985b0 "/home/sean/src/github.com/snczl/virta/pkg/meld/segment.go",
sfname=0x0, sfname@entry=0x1ddfa60 "segment.go", start=1, end=72, eap=eap@entry=0x7ffc6b032e60, append=0,
forceit=0, reset_changed=1, filtering=0)
at /home/travis/build/neovim/bot-ci/build/neovim/src/nvim/fileio.c:2576
This is most likely due to the code that restores those values from
`buf`, which happens just before the fatal call to `os_fileinfo`
```c
/*
* The autocommands may have changed the name of the buffer, which may
* be kept in fname, ffname and sfname.
*/
if (buf_ffname)
ffname = buf->b_ffname;
if (buf_sfname)
sfname = buf->b_sfname;
if (buf_fname_f)
fname = buf->b_ffname;
if (buf_fname_s)
fname = buf->b_sfname;
```
It's worth noting that at this point `ffname` is still non-NULL, so
it _could_ be used. However, our current code is purely more strict
than Vim in this area, which has caused us problems before (e.g.,
`getdigits()`). The commentary for `struct file_buffer` clearly
indicate that all of `b_ffname`, `b_sfname`, and `b_fname` may be
NULL:
```c
/*
* b_ffname has the full path of the file (NULL for no name).
* b_sfname is the name as the user typed it (or NULL).
* b_fname is the same as b_sfname, unless ":cd" has been done,
* then it is the same as b_ffname (NULL for no name).
*/
char_u *b_ffname; /* full path file name */
char_u *b_sfname; /* short file name */
char_u *b_fname; /* current file name */
```
Vim directly calls `stat(2)` which, although it is annotated to tell
the compiler that the path argument is non-NULL, does handle a NULL
pointer by returning a `-1` value and setting `errno` to `EFAULT`.
This satisfies Vim's check, since it treats any `-1` return from
`stat(2)` to mean the file doesn't exist (at least in this code
path).
Note that Vim's mch_stat() implementations on win32 and solaris
clearly cannot accept NULL `name`. But the codepaths that call
mch_stat will NULL `name` tend to be unix-only (eg: u_read_undo)!
2017-08-08 13:18:50 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if given a non-existing file', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_false((fs.os_fileinfo('/non-existent', info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns true if given an existing file and fills FileInfo', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, info)))
|
|
|
|
assert.is_true((has_fileinfo(info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns the FileInfo of the linked file, not the link', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test_link.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, info)))
|
|
|
|
assert.is_true((has_fileinfo(info)))
|
|
|
|
local mode = tonumber(info[0].stat.st_mode)
|
2014-08-31 04:45:54 -07:00
|
|
|
return eq(ffi.C.kS_IFREG, (bit.band(mode, ffi.C.kS_IFMT)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 10:14:44 -07:00
|
|
|
describe('os_fileinfo_link', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns false for non-existing file', function()
|
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_false((fs.os_fileinfo_link('/non-existent', info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns true and fills FileInfo for existing file', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo_link(path, info)))
|
|
|
|
assert.is_true((has_fileinfo(info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns FileInfo of the link, not its target', function()
|
|
|
|
local info = file_info_new()
|
|
|
|
local link = 'unit-test-directory/test_link.file'
|
|
|
|
assert.is_true((fs.os_fileinfo_link(link, info)))
|
|
|
|
assert.is_true((has_fileinfo(info)))
|
|
|
|
local mode = tonumber(info[0].stat.st_mode)
|
2014-08-31 04:45:54 -07:00
|
|
|
eq(ffi.C.kS_IFLNK, (bit.band(mode, ffi.C.kS_IFMT)))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 10:14:44 -07:00
|
|
|
describe('os_fileinfo_fd', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if given an invalid file descriptor', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_false((fs.os_fileinfo_fd(-1, info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns true if given a file descriptor and fills FileInfo', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
|
|
|
local fd = ffi.C.open(path, 0)
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo_fd(fd, info)))
|
|
|
|
assert.is_true((has_fileinfo(info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
ffi.C.close(fd)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 10:53:31 -07:00
|
|
|
describe('os_fileinfo_id_equal', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if file infos represent different files', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_info_1 = file_info_new()
|
|
|
|
local file_info_2 = file_info_new()
|
|
|
|
local path_1 = 'unit-test-directory/test.file'
|
|
|
|
local path_2 = 'unit-test-directory/test_2.file'
|
2014-08-27 10:14:44 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path_1, file_info_1)))
|
|
|
|
assert.is_true((fs.os_fileinfo(path_2, file_info_2)))
|
2014-08-27 10:53:31 -07:00
|
|
|
assert.is_false((fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if file infos represent the same file', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_info_1 = file_info_new()
|
|
|
|
local file_info_2 = file_info_new()
|
|
|
|
local path = 'unit-test-directory/test.file'
|
2014-08-27 10:14:44 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, file_info_1)))
|
|
|
|
assert.is_true((fs.os_fileinfo(path, file_info_2)))
|
2014-08-27 10:53:31 -07:00
|
|
|
assert.is_true((fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if file infos represent the same file (symlink)', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_info_1 = file_info_new()
|
|
|
|
local file_info_2 = file_info_new()
|
|
|
|
local path_1 = 'unit-test-directory/test.file'
|
|
|
|
local path_2 = 'unit-test-directory/test_link.file'
|
2014-08-27 10:14:44 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path_1, file_info_1)))
|
|
|
|
assert.is_true((fs.os_fileinfo(path_2, file_info_2)))
|
2014-08-27 10:53:31 -07:00
|
|
|
assert.is_true((fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 10:56:49 -07:00
|
|
|
describe('os_fileinfo_id', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('extracts ino/dev from FileInfo into file_id', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
|
|
|
local path = 'unit-test-directory/test.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, info)))
|
|
|
|
fs.os_fileinfo_id(info, file_id)
|
|
|
|
eq(info[0].stat.st_ino, file_id[0].inode)
|
|
|
|
eq(info[0].stat.st_dev, file_id[0].device_id)
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 11:04:03 -07:00
|
|
|
describe('os_fileinfo_inode', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns the inode from FileInfo', function()
|
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, info)))
|
|
|
|
local inode = fs.os_fileinfo_inode(info)
|
|
|
|
eq(info[0].stat.st_ino, inode)
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-08 07:25:33 -07:00
|
|
|
describe('os_fileinfo_size', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the correct size of a file', function()
|
2014-08-08 07:25:33 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
|
|
|
local file = io.open(path, 'w')
|
|
|
|
file:write('some bytes to get filesize != 0')
|
|
|
|
file:flush()
|
|
|
|
file:close()
|
2024-01-12 05:03:25 -07:00
|
|
|
local size = uv.fs_stat(path).size
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_true(fs.os_fileinfo(path, info))
|
|
|
|
eq(size, fs.os_fileinfo_size(info))
|
2014-08-08 07:25:33 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-08 07:53:03 -07:00
|
|
|
describe('os_fileinfo_hardlinks', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the correct number of hardlinks', function()
|
2014-08-08 07:53:03 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
|
|
|
local path_link = 'unit-test-directory/test_hlink.file'
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_true(fs.os_fileinfo(path, info))
|
|
|
|
eq(1, fs.os_fileinfo_hardlinks(info))
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_link(path, path_link)
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true(fs.os_fileinfo(path, info))
|
|
|
|
eq(2, fs.os_fileinfo_hardlinks(info))
|
2014-08-08 07:53:03 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-09 05:59:52 -07:00
|
|
|
describe('os_fileinfo_blocksize', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the correct blocksize of a file', function()
|
2014-08-09 05:59:52 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
2024-01-12 05:03:25 -07:00
|
|
|
local blksize = uv.fs_stat(path).blksize
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
|
|
|
assert.is_true(fs.os_fileinfo(path, info))
|
2014-08-09 05:59:52 -07:00
|
|
|
if blksize then
|
2019-06-01 12:59:42 -07:00
|
|
|
eq(blksize, fs.os_fileinfo_blocksize(info))
|
2014-08-09 05:59:52 -07:00
|
|
|
else
|
|
|
|
-- luafs doesn't support blksize on windows
|
|
|
|
-- libuv on windows returns a constant value as blocksize
|
|
|
|
-- checking for this constant value should be enough
|
2019-06-01 12:59:42 -07:00
|
|
|
eq(2048, fs.os_fileinfo_blocksize(info))
|
2014-08-09 05:59:52 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 11:09:39 -07:00
|
|
|
describe('os_fileid', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if given an non-existing file', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_false((fs.os_fileid('/non-existent', file_id)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if given an existing file and fills file_id', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
|
|
|
local path = 'unit-test-directory/test.file'
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_true((fs.os_fileid(path, file_id)))
|
2014-08-31 04:45:54 -07:00
|
|
|
assert.is_true(0 < file_id[0].inode)
|
|
|
|
assert.is_true(0 < file_id[0].device_id)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-08-27 11:12:11 -07:00
|
|
|
describe('os_fileid_equal', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns true if two FileIDs are equal', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
|
|
|
local path = 'unit-test-directory/test.file'
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_true((fs.os_fileid(path, file_id)))
|
2014-08-27 11:12:11 -07:00
|
|
|
assert.is_true((fs.os_fileid_equal(file_id, file_id)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns false if two FileIDs are not equal', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id_1 = file_id_new()
|
|
|
|
local file_id_2 = file_id_new()
|
|
|
|
local path_1 = 'unit-test-directory/test.file'
|
|
|
|
local path_2 = 'unit-test-directory/test_2.file'
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_true((fs.os_fileid(path_1, file_id_1)))
|
|
|
|
assert.is_true((fs.os_fileid(path_2, file_id_2)))
|
2014-08-27 11:12:11 -07:00
|
|
|
assert.is_false((fs.os_fileid_equal(file_id_1, file_id_2)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
end)
|
2014-08-30 07:26:56 -07:00
|
|
|
|
2014-08-27 11:14:39 -07:00
|
|
|
describe('os_fileid_equal_fileinfo', function()
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns true if file_id and FileInfo represent the same file', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path = 'unit-test-directory/test.file'
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_true((fs.os_fileid(path, file_id)))
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path, info)))
|
|
|
|
assert.is_true((fs.os_fileid_equal_fileinfo(file_id, info)))
|
2014-08-31 04:45:54 -07:00
|
|
|
end)
|
|
|
|
|
2019-06-01 12:59:42 -07:00
|
|
|
itp('returns false if file_id and FileInfo represent different files', function()
|
2014-08-31 04:45:54 -07:00
|
|
|
local file_id = file_id_new()
|
2019-06-01 12:59:42 -07:00
|
|
|
local info = file_info_new()
|
2014-08-31 04:45:54 -07:00
|
|
|
local path_1 = 'unit-test-directory/test.file'
|
|
|
|
local path_2 = 'unit-test-directory/test_2.file'
|
2014-08-27 11:09:39 -07:00
|
|
|
assert.is_true((fs.os_fileid(path_1, file_id)))
|
2019-06-01 12:59:42 -07:00
|
|
|
assert.is_true((fs.os_fileinfo(path_2, info)))
|
|
|
|
assert.is_false((fs.os_fileid_equal_fileinfo(file_id, info)))
|
2014-08-30 07:26:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|