I ran vim
using strace
. I created a file in /tmp named "abc" with content "foo", set it read-only (chmod 400 abc
), opened it in vim
(strace -o vim.log vim abc
) and saved it using ":wq!".
Here is the strace
log:
...
getcwd("/tmp", 4096) = 5
write(1, "\33[?25l\"abc\"", 11) = 11
stat("abc", {st_mode=S_IFREG|0400, st_size=4, ...}) = 0
getxattr("abc", "system.posix_acl_access", 0x7fff5725fd60, 132) = -1 ENODATA (No data available)
stat("abc", {st_mode=S_IFREG|0400, st_size=4, ...}) = 0
getuid() = 1000
chmod("abc", 0100600) = 0
open("abc", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 9
write(9, "foo\n", 4) = 4
fsync(9) = 0
close(9) = 0
chmod("abc", 0100400) = 0
setxattr("abc", "system.posix_acl_access", "\x02\x00\x00\x00\x01\x00\x04\x00\xff\xff\xff\xff\x04\x00\x00\x00\xff\xff\xff\xff \x00\x00\x00\xff\xff\xff\xff", 28, 0) = 0
...
We can see that vim
calls stat
to get the current permission, then the equivalent of chmod(u+w)
, then the write, then it restores the original permissions (in this case, chmod(400)
).
:w!
not successfully save readonly files in Fedora's vi?