Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config key to set the file creation permissions #537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ CONFIG *oil-confi
return nil
end,
},
-- Set the default mode to create files
new_files_mode = 644,
-- Set the default mode to create directories
new_dir_mode = 755,
-- Extra arguments to pass to SCP when moving/copying files over SSH
extra_scp_args = {},
-- EXPERIMENTAL support for performing file operations with git
Expand Down
4 changes: 2 additions & 2 deletions lua/oil/adapters/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ M.perform_action = function(action, cb)
end

if action.entry_type == "directory" then
uv.fs_mkdir(path, 493, function(err)
uv.fs_mkdir(path, config.new_dir_mode, function(err)
-- Ignore if the directory already exists
if not err or err:match("^EEXIST:") then
cb()
Expand All @@ -550,7 +550,7 @@ M.perform_action = function(action, cb)
---@diagnostic disable-next-line: param-type-mismatch
uv.fs_symlink(target, path, flags, cb)
else
fs.touch(path, cb)
fs.touch(path, config.new_file_mode, cb)
end
elseif action.type == "delete" then
local _, path = util.parse_url(action.url)
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/adapters/trash/mac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ M.perform_action = function(action, cb)
---@diagnostic disable-next-line: param-type-mismatch
uv.fs_symlink(target, path, flags, cb)
else
fs.touch(path, cb)
fs.touch(path, config.new_file_mode, cb)
end
elseif action.type == "delete" then
local _, path = util.parse_url(action.url)
Expand Down
11 changes: 11 additions & 0 deletions lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ local default_config = {
return nil
end,
},
-- Set the default mode to create files
new_file_mode = 644,
-- Set the default mode to create directories
new_dir_mode = 755,
-- Extra arguments to pass to SCP when moving/copying files over SSH
extra_scp_args = {},
-- EXPERIMENTAL support for performing file operations with git
Expand Down Expand Up @@ -232,6 +236,8 @@ default_config.view_options.highlight_filename = nil
---@field keymaps table<string, any>
---@field use_default_keymaps boolean
---@field view_options oil.ViewOptions
---@field new_file_mode integer
---@field new_dir_mode integer
---@field extra_scp_args string[]
---@field git oil.GitOptions
---@field float oil.FloatWindowConfig
Expand Down Expand Up @@ -260,6 +266,8 @@ local M = {}
---@field keymaps? table<string, any>
---@field use_default_keymaps? boolean Set to false to disable all of the above keymaps
---@field view_options? oil.SetupViewOptions Configure which files are shown and how they are shown.
---@field new_file_mode? integer Set the default mode to create files
---@field new_dir_mode? integer Set the default mode to create directories
---@field extra_scp_args? string[] Extra arguments to pass to SCP when moving/copying files over SSH
---@field git? oil.SetupGitOptions EXPERIMENTAL support for performing file operations with git
---@field float? oil.SetupFloatWindowConfig Configuration for the floating window in oil.open_float
Expand Down Expand Up @@ -408,6 +416,9 @@ M.setup = function(opts)
if opts.preview and not opts.confirmation then
new_conf.confirmation = vim.tbl_deep_extend("keep", opts.preview, default_config.confirmation)
end

new_conf.new_file_mode = tonumber(tostring(new_conf.new_file_mode), 8)
new_conf.new_dir_mode = tonumber(tostring(new_conf.new_dir_mode), 8)
Comment on lines +420 to +421
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you don't actually need the tostring() here. tonumber will convert an integer or a string the same way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about it, i try the following code:

local x = 493
print(tonumber(x, 8))

and I get the following error:

$ lua test.lua
lua: test.lua:2: bad argument #1 to 'tonumber' (string expected, got number)
stack traceback:
        [C]: in function 'tonumber'
        test.lua:2: in main chunk
        [C]: in ?

I'm not a Lua expert, in fact this is my first productive code in Lua, so maybe I'm ignoring something

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second argument to tonumber is the base to interpret the number as. 493 is not a valid octal number. If you do tonumber(755, 8) it prints out 493 as expected

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the same:

$ lua -e "print(tonumber(755, 8))"
lua: (command line):1: bad argument #1 to 'tonumber' (string expected, got number)
stack traceback:
        [C]: in function 'tonumber'
        (command line):1: in main chunk
        [C]: in ?

Maybe this has something to do with my interpreter? to run this examples I used the standalone interpreter:

$ lua
Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, must be a difference between LuaJIT and Lua then (or 5.1 and 5.4). I retract the nit

-- Backwards compatibility. We renamed the 'preview' config to 'preview_win'
if opts.preview and opts.preview.update_on_cursor_moved ~= nil then
new_conf.preview_win.update_on_cursor_moved = opts.preview.update_on_cursor_moved
Expand Down
6 changes: 3 additions & 3 deletions lua/oil/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ M.abspath = function(path)
end

---@param path string
---@param mode integer mode to open file (octal)
---@param cb fun(err: nil|string)
M.touch = function(path, cb)
uv.fs_open(path, "a", 420, function(err, fd) -- 0644
M.touch = function(path, mode, cb)
uv.fs_open(path, "a", mode, function(err, fd)
if err then
cb(err)
else
Expand Down Expand Up @@ -146,7 +147,6 @@ end
---@param dir string
---@param mode? integer
M.mkdirp = function(dir, mode)
mode = mode or 493
local mod = ""
Comment on lines 148 to 150
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs, uv.fs_mkdir expects a non-nil integer as the second argument. Did you intend to replace this with the value in the config file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea is to use the config value, and now we have a default one, so it's always available

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you'll need to update the function signature to make mode non-optional, and update the call here to pass the mode

fs.mkdirp(tmpdir)

local path = dir
while vim.fn.isdirectory(path) == 0 do
Expand Down
Loading