FarManager icon indicating copy to clipboard operation
FarManager copied to clipboard

"Can't replace the file" error on first edit of file with elevation

Open Px-x64 opened this issue 1 year ago • 1 comments

Far Manager version

3.0.6278

OS version

10.0.19045.4046

Other software

None

Steps to reproduce

  1. Start Far under regular user who can elevate to administrator.
  2. Open C:\Windows\System32\drivers\etc\hosts file for editing.
  3. Add "127.0.0.1 test.com" line to the end of the file.
  4. Save file.
  5. Receive Access Denied dialog, press OK
  6. Receive system UAC dialog, allow Far to make changes

Expected behavior

File is saved without any issues

Actual behavior

On first edit after boot/Far start/sometimes on consequent edits you got the following error:

╔═══════════════════════════════════════════ Editor ═══════════════════════════════════════════╗
║                                     Cannot save the file                                     ║
║                            C:\Windows\System32\drivers\etc\hosts                             ║
╟──────────────────────────────────────────────────────────────────────────────────────────────╢
║                                    Can't replace the file                                    ║
║ 0x00000020 - The process cannot access the file because it is being used by another process. ║
╟──────────────────────────────────────────────────────────────────────────────────────────────╢
║                                     { Retry } [ Cancel ]                                     ║
╚══════════════════════════════════════════════════════════════════════════════════════════════╝

F3

errno:
0x00000000 - No error
LastError:
0x00000020 - The process cannot access the file because it is being used by another process.
NTSTATUS:
0xC0000043 - A file cannot be opened because the share access flags are incompatible.

Upon pressing Retry the file is successfully saved, but you have a temporary file (like FAR1C12.tmp) present in the folder.

I've tried to capture a trace in Process Monitor, but it is a bit noisy, because upon Far actions other apps like Edge/Slack are starting to sniff hosts file. I see two "PRIVILEGE NOT HELD" results for Far CreateFile calls, and then two "SHARING VIOLATION", and finally "ACCESS DENIED". It looks like some kind of race condition here, and it is not clear what triggers it from Far side, because before Far tries to write the file, other apps doesn't look into it. Also, it would be nice to at least remove temporary file in case if retry on save was successful.

Px-x64 avatar Feb 19 '24 15:02 Px-x64

3.0.6300 the same

chevcheli0s avatar Apr 12 '24 07:04 chevcheli0s