当我使用 VIM 保存文件时会发生什么?
What happens when I save a file using VIM?
我使用了一些文件系统更改通知 lib 来监视目录 /asdf
,在 asdf
中,我 vim tmp
进行了一些更改,然后使用 :wq
保存文件
然后我得到了这个输出:
/asdf/4913 at watch.pl line 9.
/asdf/4913 at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.
什么是 4913 文件? tmp~ 文件是什么?
:wq
之后究竟发生了什么?
来自https://github.com/neovim/neovim/issues/3460
An interesting case was mentioned: Neo/Vim creates a temporary file to check if a directory is writable and see the resulting ACL.
So, if you’re writing software that watches for file changes, you’ll find Vim creates and deletes file 4913 on almost every edit. ref
其他详细信息
https://groups.google.com/forum/#!topic/vim_dev/sppdpElxY44
https://vi.stackexchange.com/questions/4038/why-does-set-nocompatible-result-in-vim-saving-extra-all-numeric-temporary-fi
这是导致此问题的代码
/*
* Check if we can create a file and set the owner/group to
* the ones from the original file.
* First find a file name that doesn't exist yet (use some
* arbitrary numbers).
*/
STRCPY(IObuff, fname);
for (i = 4913; ; i += 123)
{
sprintf((char *)gettail(IObuff), "%d", i);
if (mch_lstat((char *)IObuff, &st) < 0)
break;
}
fd = mch_open((char *)IObuff,
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0) /* can't write in directory */
backup_copy = TRUE;
else
{
ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
if (mch_stat((char *)IObuff, &st) < 0
|| st.st_uid != st_old.st_uid
|| st.st_gid != st_old.st_gid
|| (long)st.st_mode != perm)
backup_copy = TRUE;
/* Close the file before removing it, on MS-Windows we
* can't delete an open file. */
close(fd);
mch_remove(IObuff);
我使用了一些文件系统更改通知 lib 来监视目录 /asdf
,在 asdf
中,我 vim tmp
进行了一些更改,然后使用 :wq
保存文件
然后我得到了这个输出:
/asdf/4913 at watch.pl line 9.
/asdf/4913 at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.
什么是 4913 文件? tmp~ 文件是什么?
:wq
之后究竟发生了什么?
来自https://github.com/neovim/neovim/issues/3460
An interesting case was mentioned: Neo/Vim creates a temporary file to check if a directory is writable and see the resulting ACL.
So, if you’re writing software that watches for file changes, you’ll find Vim creates and deletes file 4913 on almost every edit. ref
其他详细信息
https://groups.google.com/forum/#!topic/vim_dev/sppdpElxY44
https://vi.stackexchange.com/questions/4038/why-does-set-nocompatible-result-in-vim-saving-extra-all-numeric-temporary-fi
这是导致此问题的代码
/*
* Check if we can create a file and set the owner/group to
* the ones from the original file.
* First find a file name that doesn't exist yet (use some
* arbitrary numbers).
*/
STRCPY(IObuff, fname);
for (i = 4913; ; i += 123)
{
sprintf((char *)gettail(IObuff), "%d", i);
if (mch_lstat((char *)IObuff, &st) < 0)
break;
}
fd = mch_open((char *)IObuff,
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0) /* can't write in directory */
backup_copy = TRUE;
else
{
ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
if (mch_stat((char *)IObuff, &st) < 0
|| st.st_uid != st_old.st_uid
|| st.st_gid != st_old.st_gid
|| (long)st.st_mode != perm)
backup_copy = TRUE;
/* Close the file before removing it, on MS-Windows we
* can't delete an open file. */
close(fd);
mch_remove(IObuff);