Skip to content

Commit c98d2d7

Browse files
author
skywind3000
committed
use lfs module or cffi in luajit if possible
1 parent 6c567b6 commit c98d2d7

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ z.lua can be **4-5** times faster than zoxide after enabling czmod.
468468

469469
## History
470470

471+
- 1.8.7 (2020-06-29): use lfs or luajit's cffi if possible.
471472
- 1.8.4 (2020-02-10): fish shell: set `$_ZL_ECHO` to global scope.
472473
- 1.8.3 (2020-02-09): new: `z -b -i` and `z -b -I` to jump backwards in interactive mode.
473474
- 1.7.4 (2019-12-29): new: `$_ZL_HYPHEN` to treat hyphen as a normal character, see [here](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-input-a-hyphen---in-the-keyword-).

z.lua

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-- z.lua - a cd command that learns, by skywind 2018, 2019, 2020
55
-- Licensed under MIT license.
66
--
7-
-- Version 1.8.6, Last Modified: 2020/03/17 16:32
7+
-- Version 1.8.7, Last Modified: 2020/06/29 18:04
88
--
99
-- * 10x faster than fasd and autojump, 3x faster than z.sh
1010
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -319,14 +319,16 @@ if os.native.status then
319319
uint32_t GetTickCount(void);
320320
uint32_t GetFileAttributesA(const char *name);
321321
uint32_t GetCurrentDirectoryA(uint32_t size, char *ptr);
322+
uint32_t GetShortPathNameA(const char *longname, char *shortname, uint32_t size);
323+
uint32_t GetLongPathNameA(const char *shortname, char *longname, uint32_t size);
322324
]]
323325
local kernel32 = ffi.load('kernel32.dll')
324-
local buffer = ffi.new('char[?]', 300)
326+
local buffer = ffi.new('char[?]', 4100)
325327
local INVALID_FILE_ATTRIBUTES = 0xffffffff
326328
local FILE_ATTRIBUTE_DIRECTORY = 0x10
327329
os.native.kernel32 = kernel32
328330
function os.native.GetFullPathName(name)
329-
local hr = kernel32.GetFullPathNameA(name, 290, buffer, nil)
331+
local hr = kernel32.GetFullPathNameA(name, 4096, buffer, nil)
330332
return (hr > 0) and ffi.string(buffer, hr) or nil
331333
end
332334
function os.native.ReplaceFile(replaced, replacement)
@@ -339,6 +341,21 @@ if os.native.status then
339341
function os.native.GetFileAttributes(name)
340342
return kernel32.GetFileAttributesA(name)
341343
end
344+
function os.native.GetLongPathName(name)
345+
local hr = kernel32.GetLongPathNameA(name, buffer, 4096)
346+
return (hr ~= 0) and ffi.string(buffer, hr) or nil
347+
end
348+
function os.native.GetShortPathName(name)
349+
local hr = kernel32.GetShortPathNameA(name, buffer, 4096)
350+
return (hr ~= 0) and ffi.string(buffer, hr) or nil
351+
end
352+
function os.native.GetRealPathName(name)
353+
local short = os.native.GetShortPathName(name)
354+
if short then
355+
return os.native.GetLongPathName(short)
356+
end
357+
return nil
358+
end
342359
function os.native.exists(name)
343360
local attr = os.native.GetFileAttributes(name)
344361
return attr ~= INVALID_FILE_ATTRIBUTES
@@ -352,7 +369,7 @@ if os.native.status then
352369
return (attr % (2 * isdir)) >= isdir
353370
end
354371
function os.native.getcwd()
355-
local hr = kernel32.GetCurrentDirectoryA(299, buffer)
372+
local hr = kernel32.GetCurrentDirectoryA(4096, buffer)
356373
if hr <= 0 then return nil end
357374
return ffi.string(buffer, hr)
358375
end
@@ -554,6 +571,9 @@ function os.path.exists(name)
554571
if name == '/' then
555572
return true
556573
end
574+
if os.native and os.native.exists then
575+
return os.native.exists(name)
576+
end
557577
local ok, err, code = os.rename(name, name)
558578
if not ok then
559579
if code == 13 then
@@ -1357,6 +1377,14 @@ function z_add(path)
13571377
end
13581378
end
13591379
if not skip then
1380+
if windows then
1381+
if os.native and os.native.GetRealPathName then
1382+
local ts = os.native.GetRealPathName(path)
1383+
if ts then
1384+
path = ts
1385+
end
1386+
end
1387+
end
13601388
M = data_insert(M, path)
13611389
count = count + 1
13621390
end
@@ -2663,23 +2691,20 @@ end
26632691
-- LFS optimize
26642692
-----------------------------------------------------------------------
26652693
os.lfs = {}
2666-
os.lfs.enable = os.getenv('_ZL_USE_LFS')
2667-
if os.lfs.enable ~= nil then
2668-
local m = string.lower(os.lfs.enable)
2669-
if (m == '1' or m == 'yes' or m == 'true' or m == 't') then
2670-
os.lfs.status, os.lfs.pkg = pcall(require, 'lfs')
2671-
if os.lfs.status then
2672-
local lfs = os.lfs.pkg
2673-
os.path.exists = function (name)
2674-
return lfs.attributes(name) and true or false
2675-
end
2676-
os.path.isdir = function (name)
2677-
local mode = lfs.attributes(name)
2678-
if not mode then
2679-
return false
2680-
end
2681-
return (mode.mode == 'directory') and true or false
2694+
os.lfs.disable = os.getenv('_ZL_DISABLE_LFS')
2695+
if os.lfs.disable == nil then
2696+
os.lfs.status, os.lfs.pkg = pcall(require, 'lfs')
2697+
if os.lfs.status then
2698+
local lfs = os.lfs.pkg
2699+
os.path.exists = function (name)
2700+
return lfs.attributes(name) and true or false
2701+
end
2702+
os.path.isdir = function (name)
2703+
local mode = lfs.attributes(name)
2704+
if not mode then
2705+
return false
26822706
end
2707+
return (mode.mode == 'directory') and true or false
26832708
end
26842709
end
26852710
end

0 commit comments

Comments
 (0)