diff --git a/nativefs.lua b/nativefs.lua index 2eab17f..61dab87 100644 --- a/nativefs.lua +++ b/nativefs.lua @@ -33,28 +33,10 @@ local BUFFERMODE = { if ffi.os == 'Windows' then ffi.cdef([[ - typedef void* HANDLE; - - #pragma pack(push) - #pragma pack(1) - struct WIN32_FIND_DATAW { - uint32_t dwFileWttributes; - uint64_t ftCreationTime; - uint64_t ftLastAccessTime; - uint64_t ftLastWriteTime; - uint32_t dwReserved[4]; - char cFileName[520]; - char cAlternateFileName[28]; - }; - #pragma(pop) - int MultiByteToWideChar(unsigned int cp, uint32_t flags, const char* mb, int cmb, const wchar_t* wc, int cwc); int WideCharToMultiByte(unsigned int cp, uint32_t flags, const wchar_t* wc, int cwc, const char* mb, int cmb, const char* def, int* used); int GetLogicalDrives(void); - void* FindFirstFileW(const wchar_t* lpFileName, struct WIN32_FIND_DATAW* lpFindFileData); - bool FindNextFileW(HANDLE hFindFile, struct WIN32_FIND_DATAW* fd); - bool FindClose(HANDLE hFindFile); int _wchdir(const wchar_t* path); wchar_t* _wgetcwd(wchar_t* buffer, int maxlen); FILE* _wfopen(const wchar_t* name, const wchar_t* mode); @@ -97,7 +79,7 @@ else fopen, unlink, chdir = C.fopen, C.unlink, C.chdir getcwd = function() local cwd = C.getcwd(nameBuffer, MAX_PATH) - return cwd and ffi.string(cwd) or nil + return cwd ~= nil and ffi.string(cwd) or nil end end @@ -198,6 +180,10 @@ function File:read(containerOrBytes, bytes) end local container = bytes ~= nil and containerOrBytes or 'string' + if container ~= 'string' and container ~= 'data' then + error("Invalid container type: " .. container) + end + bytes = not bytes and containerOrBytes or 'all' bytes = bytes == 'all' and self:getSize() - self:tell() or math.min(self:getSize() - self:tell(), bytes) @@ -355,18 +341,24 @@ function nativefs.load(name) return loadstring(chunk, name) end -function nativefs.getDirectoryItems(dir, callback) +local function withTempMount(dir, fn) local mountPoint = loveC.PHYSFS_getMountPoint(dir) if mountPoint ~= nil then - return love.filesystem.getDirectoryItems(ffi.string(mountPoint), callback) + return fn(ffi.string(mountPoint)) end if not nativefs.mount(dir, '__nativefs__temp__') then return false, "Could not mount " .. dir end - local items = love.filesystem.getDirectoryItems('__nativefs__temp__', callback) + local a, b, c, d = fn('__nativefs__temp__') nativefs.unmount(dir) - return items + return a, b, c, d +end + +function nativefs.getDirectoryItems(dir, callback) + return withTempMount(dir, function(mount) + return love.filesystem.getDirectoryItems(mount, callback) + end) end function nativefs.getWorkingDirectory() @@ -396,7 +388,13 @@ function nativefs.getDriveList() return drives end -function nativefs.getInfo(name) +function nativefs.getInfo(path, filtertype) + local dir = path:match("(.*[\\/]).*$") or './' + local file = love.path.leaf(path) + return withTempMount(dir, function(mount) + local filepath = string.format("%s/%s", mount, file) + return love.filesystem.getInfo(filepath, filtertype) + end) end function nativefs.createDirectory(path) diff --git a/test/main.lua b/test/main.lua index 3d34794..bb0473c 100644 --- a/test/main.lua +++ b/test/main.lua @@ -3,7 +3,6 @@ package.path = package.path .. ';../?.lua' local ffi = require('ffi') local lu = require('luaunit') -local lfs = love.filesystem local fs local equals, notEquals = lu.assertEquals, lu.assertNotEquals @@ -107,12 +106,17 @@ function test_fs_load() end function test_fs_getDirectoryItems() - local items = fs.getDirectoryItems('data') - - local map = {} + local items, map = fs.getDirectoryItems('data'), {} for i = 1, #items do map[items[i]] = true end - equals(map['ümläüt.txt'], true) - equals(map['test.lua'], true) + + local items2, map2 = love.filesystem.getDirectoryItems('data'), {} + for i = 1, #items2 do map2[items2[i]] = true end + + equals(#items, #items2) + + for i = 1, #items2 do + equals(map[items2[i]], map2[items2[i]]) + end end function test_fs_setWorkingDirectory() @@ -140,6 +144,22 @@ function test_fs_getDriveList() end function test_fs_getInfo() + local info = fs.getInfo('data') + notEquals(info, nil) + equals(info.type, 'directory') + + local info = fs.getInfo('data', 'file') + equals(info, nil) + + local info = fs.getInfo('main.lua') + notEquals(info, nil) + equals(info.type, 'file') + + local info = fs.getInfo('data/ümläüt.txt') + notEquals(info, nil) + equals(info.type, 'file') + equals(info.size, 446) + notEquals(info.modtime, nil) end function test_fs_createDirectory() @@ -173,8 +193,11 @@ function test_File_open() hasFailed('File is not open', f:close()) equals(f:getMode(), 'c') + f:close() - f:release() + local f = fs.newFile('data/𠆢ßЩ.txt') + notFailed(f:open('r')) + f:close() end function test_File_setBuffer() @@ -208,13 +231,14 @@ end function test_File_flush() end +local _globals = {} + function test_xxx_globalsCheck() for k, v in pairs(_G) do equals(v, _globals[k]) end end -_globals = {} for k, v in pairs(_G) do _globals[k] = v end fs = require('nativefs')