Skip to content

Commit 49450bb

Browse files
committed
Create a temp file utility to prevent filename conflicts
Added a `TmpFile` function to dynamically generate temporary filenames, ensuring no conflicts during file operations. Updated logic in the script to use this utility.
1 parent 6ed2a47 commit 49450bb

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

demo/extra/dosfren.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#!/usr/bin/env lua
1+
#!/usr/bin/env lua
22

3-
help = [[
3+
H = [[
44
DOS and UNIX disagree on what a new line should be
55
DOS says it's a carriage return followed by a line feed (CRLF)
66
while UNIX says it's just a line feed on its own (LF).
@@ -17,7 +17,7 @@ both system families can pleasantly read and run the file.
1717
]]
1818

1919
if #arg < 1 then
20-
print(arg[-1] .. " " .. arg[0] .. " [FILE]..." .. '\n\n' .. help)
20+
print(arg[-1] .. " " .. arg[0] .. " [FILE]..." .. '\n\n' .. H)
2121
os.exit(1)
2222
end
2323

@@ -43,13 +43,23 @@ local function Shebang(inFile)
4343
return nil
4444
end
4545

46+
local function TmpFile(m)
47+
for i = 1, 10000 do
48+
local t = tostring(os.time() + i):sub(-8)
49+
local f, e = io.open(t, "r")
50+
if not f then f, e = io.open(t, m) return f, e, t end
51+
f:close()
52+
i = i + 1
53+
end
54+
end
55+
4656
for j = 1, #arg do
4757
local i, e = io.open(arg[j])
4858
if not i then print(arg[j] .. ": " .. e) else
49-
local o
50-
o, e = io.open(arg[j] .. ".tmp", "wb")
59+
local o, t
60+
o, e, t = TmpFile("wb")
5161
if not o then
52-
print(arg[j] .. ".tmp: " .. e)
62+
print(t .. ": " .. e)
5363
else
5464
local s = Shebang(i)
5565
if s then
@@ -70,7 +80,7 @@ for j = 1, #arg do
7080
This allows system file permissions to remain unchanged.
7181
--]]
7282

73-
i = io.open(arg[j] .. ".tmp", "rb")
83+
i = io.open(t, "rb")
7484
o = io.open(arg[j], "wb")
7585
if i and o then
7686
while true do
@@ -83,7 +93,7 @@ for j = 1, #arg do
8393
i:close() o:close()
8494
end
8595
end
86-
os.remove(arg[j] .. ".tmp")
96+
os.remove(t)
8797
end
8898
end
8999
end

0 commit comments

Comments
 (0)