vim-win32-installer icon indicating copy to clipboard operation
vim-win32-installer copied to clipboard

Can't exit vim editing file with no write access for user

Open Praful opened this issue 3 years ago • 5 comments

I'm not sure whether to log this with Vim or here.

I am logged in as UserA and have read-only access to a folder that is owned by Administrator. If I open a file in gvim, I can edit it. However, when I try to save it, the following message appears:

File permissions of [filename] are read-only. It may still be possible to write it. Do you wish to try?

The options are Yes or No. Whichever I choose, I get asked again. The only way to exit this infinite loop is to kill the gvim process from Task Manager or equivalent.

Praful avatar May 03 '22 17:05 Praful

so this happens also with gvim --clean? I have never noticed that

chrisbra avatar May 03 '22 17:05 chrisbra

Hm, if I try this here, I get:

E45: 'readonly' option is set (add ! to override)

if I then try using :w! I get:

E212: can't open file for writing

But I never get that prompt. Hm, perhaps the 'backupcopy' could make difference?

Are those file permissions for that file special?

Note: already when opening, I see an indicator [readonly] in the status bar.

chrisbra avatar May 03 '22 17:05 chrisbra

There are NTFS permissions set on the folder, which the files in it inherit. This is different from setting the read-only attribute on the file. The administrator has "full control" NTFS permission and UserA has "read-only" NTFS permission. To set the NTFS permission, I right-click folder in File Explorer > Properties > Security tab > set permissions.

The read-only attribute has not been set.

I do have the backup files option enabled in vim. I'll try in gvim clean mode tomorrow when I'm by PC.

Praful avatar May 03 '22 21:05 Praful

I ran gvim --clean and the problem went away. I noticed that no dialog box appeared. I tried with a minimal vimrc, adding set confirm and the dialog box appears but the problem doesn't ie the dialog appears once then not again after I click 'No'.

Then I tried gvim --noplugin ie with my vimrc and the problem didn't appear. So it looks like a plugin is causing the issue.

For backup, swap and undo directories, I set directory, backupdir, and undodir to a directory that UserA has write access to.

I will investigate further and see if I can narrow down the issue.

Praful avatar May 04 '22 18:05 Praful

I've narrowed it down to the following minimalist vimrc:

set confirm
if has("autocmd")
  augroup PK2
    autocmd!
    autocmd BufLeave,FocusLost * silent! wall
  augroup END
endif 

The culprit seems to be set confirm. If that's commented out, there is no infinite prompting to save a file.

Steps to reproduce:

  1. Save above in c:\temp\vimrc
  2. cd c:\temp
  3. create a file test.txt
  4. make file read-only: attrib +r test.txt
  5. run: gvim --clean .\test.txt -u c:\temp\vimrc
  6. edit test.txt
  7. save it: :w

The attempt to save test.txt will result in a dialog box repeatedly appearing.

In the above example, I'm using the read-only attribute but the same happens with NTFS permissions.

Obviously this is not a big issue since I can comment out the focuslost command or the set confirm command to work around the issue.

Edit: my guess is that the appearance of a Windows dialog box, even if it's created by vim causes a FocusLost event, which results in the confirm kicking in, which shows the dialog box, which, and so on. Is the answer not to treat vim-generated dialog boxes as a loss of focus?

Praful avatar May 04 '22 20:05 Praful