Skip to content

Commit

Permalink
fix - fs copy did not maintain permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Oct 1, 2024
1 parent 57a7ad2 commit 5e9ad55
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
version: 0.34.4
version: 0.34.5
global_modules: false //disables global modules loading (only looks up for modules in cwd)
minify: true
compress: true
Expand Down
7 changes: 7 additions & 0 deletions lib/eli/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function fs.copy_file(src, dst, options)
"Invalid type of destination! (Has to be string or file*)")

options = _util.merge_tables({ binaryMode = true }, options, true)
local file_info = fs.file_info(src)
local permissions = (file_info or {}).permissions or "rw-r--r--"
---@type file*, file*
local srcf, dstf
if type(src) == "string" then
Expand All @@ -96,6 +98,9 @@ function fs.copy_file(src, dst, options)
end
if type(src) == "string" then srcf:close() end
if type(dst) == "string" then dstf:close() end
if type(dst) == "string" then
fs.chmod(dst, permissions)
end
end

---@class FsCopyoptions
Expand Down Expand Up @@ -129,7 +134,9 @@ function fs.copy(src, dst, options)
if fs.exists(dstFile) and fs.file_type(dstFile) ~= "directory" then
error"Cannot copy directory to file!"
end
local file_info = fs.file_info(srcFile) or {}
fs.mkdirp(dstFile)
fs.chmod(dstFile, file_info.permissions)
elseif not fs.exists(dstFile) or options.overwrite then
fs.mkdirp(_eliPath.dir(dstFile))
fs.copy_file(_eliPath.combine(src, srcFile) --[[@as string]], dstFile, options)
Expand Down
4 changes: 2 additions & 2 deletions lib/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ELI_LIB_VERSION = '0.34.3'
ELI_VERSION = '0.34.3'
ELI_LIB_VERSION = '0.34.5'
ELI_VERSION = '0.34.5'
do
local path = require"eli.path"
local _eos = require"eli.os"
Expand Down
Empty file added lib/tests/assets/test.bin
Empty file.
27 changes: 18 additions & 9 deletions lib/tests/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test["copy file (path)"] = function ()
local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true })
test.assert(_ok, _hash)
local _ok, _hash2 =
eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true })
eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
end
Expand Down Expand Up @@ -65,14 +65,23 @@ test["copy file (mixed)"] = function ()
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
end

test["copy file (permissions)"] = function ()
local _ok, _error = eliFs.safe_copy_file("assets/test.bin",
"tmp/test.bin")
test.assert(_ok, _error)
local info = eliFs.file_info"assets/test.bin"
local info2 = eliFs.file_info"tmp/test.bin"
test.assert(info.permissions == info2.permissions, "permissions do not match")
end

test["copy (file)"] = function ()
local _ok, _error = eliFs.safe_copy("assets/test.file",
"tmp/test.file")
test.assert(_ok, _error)
local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true })
test.assert(_ok, _hash)
local _ok, _hash2 =
eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true })
eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
end
Expand All @@ -94,7 +103,7 @@ test["copy (directory)"] = function ()
test.assert(_ok, _hash)
local destFilePath = eliPath.combine(DEST_DIR, filePath)
local _ok, _hash2 =
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
::continue::
Expand All @@ -119,7 +128,7 @@ test["copy (directory - overwrite)"] = function ()
test.assert(_ok, _hash)
local destFilePath = eliPath.combine(DEST_DIR, filePath)
local _ok, _hash2 =
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
::continue::
Expand All @@ -136,7 +145,7 @@ test["copy (directory - overwrite)"] = function ()
test.assert(_ok, _hash)
local destFilePath = eliPath.combine(DEST_DIR, filePath)
local _ok, _hash2 =
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
::continue::
Expand All @@ -154,7 +163,7 @@ test["copy (directory - overwrite)"] = function ()
test.assert(_ok, _hash)
local destFilePath = eliPath.combine(DEST_DIR, filePath)
local _ok, _hash2 =
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true })
test.assert(_ok, _hash)
test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")")
::continue::
Expand Down Expand Up @@ -401,9 +410,9 @@ end

local function _external_lock(file)
local _cmd = (os.getenv"QEMU" or "") ..
" " .. arg[-1] .. " -e \"x, err = fs.lock_file('" .. file .. "','w'); " ..
"if etype(x) == 'ELI_FILE_LOCK' then os.exit(0); end; notAvailable = tostring(err):match('Resource temporarily unavailable') or tostring(err):match('locked a portion of the file'); " ..
"exitCode = notAvailable and 11 or 12; os.exit(exitCode)\""
" " .. arg[-1] .. " -e \"x, err = fs.lock_file('" .. file .. "','w'); " ..
"if etype(x) == 'ELI_FILE_LOCK' then os.exit(0); end; notAvailable = tostring(err):match('Resource temporarily unavailable') or tostring(err):match('locked a portion of the file'); " ..
"exitCode = notAvailable and 11 or 12; os.exit(exitCode)\""
local _ok, _, _code = os.execute(_cmd)
return _ok, _code
end
Expand Down

0 comments on commit 5e9ad55

Please sign in to comment.