Skip to content

Commit 38c1741

Browse files
author
skywind3000
committed
prevent writing file racing
1 parent fdff5c5 commit 38c1741

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ As you see, z.lua is the fastest one and requires less resource.
458458

459459
## History
460460

461+
- 1.5.10 (2019-03-01): Prevent writing file racing.
461462
- 1.5.9 (2019-02-25): `z -b` should not match current directory (close #56).
462463
- 1.5.8 (2019-02-21): new `$_ZL_FZF_HEIGHT` to control `--height` parameter in fzf.
463464
- 1.5.7 (2019-02-21): rename `$_ZL_FZF_SORT` to `$_ZL_INT_SORT` it will affect both `-i` and `-I`.

z.lua

Lines changed: 11 additions & 6 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
55
-- Licensed under MIT license.
66
--
7-
-- Version 1.5.9, Last Modified: 2019/02/25 23:17
7+
-- Version 1.5.10, Last Modified: 2019/03/01 13:08
88
--
99
-- * 10x faster than fasd and autojump, 3x faster than z.sh
1010
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -960,11 +960,16 @@ function data_save(filename, M)
960960
fp = io.open(filename, 'w')
961961
else
962962
math.random_init()
963-
tmpname = filename .. '.' .. tostring(os.time())
964-
tmpname = tmpname .. math.random_string(8)
965-
local rnd = os.getenv('_ZL_RANDOM')
966-
tmpname = tmpname .. '' .. (rnd and rnd or '')
967-
-- print('tmpname: '..tmpname)
963+
while true do
964+
tmpname = filename .. '.' .. tostring(os.time())
965+
tmpname = tmpname .. math.random_string(8)
966+
local rnd = os.getenv('_ZL_RANDOM')
967+
tmpname = tmpname .. '' .. (rnd and rnd or '')
968+
if not os.path.exists(tmpname) then
969+
-- print('tmpname: '..tmpname)
970+
break
971+
end
972+
end
968973
fp = io.open(tmpname, 'w')
969974
end
970975
if fp == nil then

0 commit comments

Comments
 (0)