Skip to content

Add config key to set the file creation permissions#537

Open
fortizc wants to merge 1 commit intostevearc:masterfrom
fortizc:feature/file-modes
Open

Add config key to set the file creation permissions#537
fortizc wants to merge 1 commit intostevearc:masterfrom
fortizc:feature/file-modes

Conversation

@fortizc
Copy link
Copy Markdown

@fortizc fortizc commented Dec 13, 2024

This PR defines a new config key called create_files_mode to set the default permission used when a file is created.
Currently, Oil uses 644 (420 in decimal) but, at least on Linux most of the tools like (n)vim, touch, or even a simple > filename by default uses 664 (436 decimal) so, with this option the user is able to set the default value.

@github-actions github-actions Bot requested a review from stevearc December 13, 2024 23:36
Comment thread lua/oil/config.lua Outdated
Comment on lines +31 to +32
-- Set the default mode to create files (:help oil-file_creation)
create_files_mode = 420,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We don't expect most users to need to modify this, so let's move it down to below the view_options.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If we're adding the ability to adjust the mode of the created files, we should do the same for directories. We could use two options for this new_file_mode and new_dir_mode.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Since most people are used to thinking about file permissions in octal, the config option should be octal and we should automatically convert it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok, sounds good

Comment thread lua/oil/config.lua Outdated
new_conf.confirmation = vim.tbl_deep_extend("keep", opts.preview, default_config.confirmation)
end

M.file_mode = opts.create_files_mode
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Why are we renaming this here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will fix it

Comment thread doc/oil.txt Outdated
Comment on lines +720 to +719
--------------------------------------------------------------------------------
FILE CREATION *oil-file_creation*


Oil defines `create_files_mode` to set the permission for new files. The mode
follows the Posix numeric standard which use octal notation (base-8), however
the variable must be set in decimal.

00700 user (file owner) has read, write, and execute permission
00400 user has read permission
00200 user has write permission
00100 user has execute permission
00070 group has read, write, and execute permission
00040 group has read permission
00020 group has write permission
00010 group has execute permission
00007 others have read, write, and execute permission
00004 others have read permission
00002 others have write permission
00001 others have execute permission
(from the open (2) man pages)

An easy way to provide the right value for `create_files_mode` is using
`tonumber(e [, base])` function with `base = 8`, for example:
>lua
create_files_mode = tonumber("0644", 8)

The default value is 420 in decimal or 0644 in octal


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I actually don't think we need this section. It's explaining:

  1. what the octal file permissions mean
  2. how to convert octal to decimal
  3. the default value

Point 3 is already documented in the options section. Point 2 is obviated if we accept the permission in octal. And for point 1 I would argue that if you don't know what octal file permissions are, you probably shouldn't be changing this config value and I don't think the oil docs are the correct place to be learning about them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok, then I will remove it

@github-actions github-actions Bot requested a review from stevearc December 26, 2024 00:38
@fortizc fortizc force-pushed the feature/file-modes branch 2 times, most recently from e96ca6f to d91e574 Compare December 26, 2024 00:52
Comment thread lua/oil/fs.lua Outdated
Comment thread doc/oil.txt Outdated
-- Set the default mode to create files
new_files_mode = 644,
-- Set the default mode to create folders
new_folder_mode = 755,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I am fully aware of how this sounds, and I'm really sorry to be this pedantic, but "folder" is Windows terminology. Typically in Unix contexts we would refer to these as "directories". Elsewhere in this plugin we always refer to these as directories. For consistency sake, could we rename this to new_dir_mode?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@fortizc fortizc force-pushed the feature/file-modes branch from d91e574 to 17ae7b0 Compare January 5, 2025 19:24
@github-actions github-actions Bot requested a review from stevearc January 5, 2025 19:24
Comment thread lua/oil/fs.lua Outdated
Comment thread lua/oil/config.lua
Add config key to set the file creation permissions

Rename file_mode to new_file_mode

Add new_folder_mode to store the default folder creation mode

Remove doc about new_file_mode

Fix how the folder and file mode are added to config

Rename new_folder_mode to new_dir_mode
@fortizc fortizc force-pushed the feature/file-modes branch from 17ae7b0 to e7363e4 Compare January 26, 2025 20:01
@github-actions github-actions Bot requested a review from stevearc January 26, 2025 20:01
@fortizc
Copy link
Copy Markdown
Author

fortizc commented Jan 26, 2025

Hey, all changes are done, so it's ready if you want to review it again. Thanks!

Copy link
Copy Markdown
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

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

The previous comment about mkdirp has not been addressed, and new bugs were introduced in that function

Comment thread lua/oil/fs.lua
mode = mode or 493
local mod = ""
local path = dir
local mod = mode
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What is going on here? This change looks completely wrong

barrettruth added a commit to barrettruth/canola.nvim that referenced this pull request Feb 20, 2026
Problem: files were always created with mode 0644 and directories
with 0755, hardcoded in fs.touch and uv.fs_mkdir. Users who need
different defaults (e.g. 0600 for security) had no config option.

Solution: add new_file_mode (default 420 = 0644) and new_dir_mode
(default 493 = 0755) config options, passed through to fs.touch and
uv.fs_mkdir in the files and mac trash adapters. The fs.touch
signature accepts an optional mode parameter with backwards
compatibility (detects function argument to support old callers).
Local cache directories (SSH, S3) continue using standard system
permissions rather than the user-configured mode.

Based on: stevearc#537
barrettruth added a commit to barrettruth/canola.nvim that referenced this pull request Feb 21, 2026
Problem: files were always created with mode 0644 and directories
with 0755, hardcoded in fs.touch and uv.fs_mkdir. Users who need
different defaults (e.g. 0600 for security) had no config option.

Solution: add new_file_mode (default 420 = 0644) and new_dir_mode
(default 493 = 0755) config options, passed through to fs.touch and
uv.fs_mkdir in the files and mac trash adapters. The fs.touch
signature accepts an optional mode parameter with backwards
compatibility (detects function argument to support old callers).
Local cache directories (SSH, S3) continue using standard system
permissions rather than the user-configured mode.

Based on: stevearc#537
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants