我有一个基本脚本,我想执行以下操作:
- 如果 new/file.txt 存在
- mv new/file.txt current/file.txt
现在 - 如果 2 个服务器同时运行这个相同的脚本(访问相同的 NFS 文件共享):
- 服务器 1 - 步骤 1. 检查文件是否存在。是的
- 服务器 2 - 步骤 1. 检查文件是否存在。是的
- 服务器 1 - 步骤 2. 开始执行“mv”命令
- 服务器 2 - 第 2 步。????
从我在网上找到的内容来看,服务器 2 似乎只会抛出一个错误 - 但没有文件损坏或任何值得关注的事情: http://nfs.sourceforge.net/
The file handle refers to a deleted file. After a file is deleted on the server, clients don't find out until they try to access the file with a file handle they had cached from a previous LOOKUP. Using rsync or mv to replace a file while it is in use on another client is a common scenario that results in an ESTALE error.
我已经尝试对此进行模拟以确认,但未能成功 - 所以我想在实现此策略之前先在此处进行检查:
问题: 我的理解是否准确 - 还是我需要使用不同的策略来确保 file.txt 不被损坏?
其他详细信息: 亚马逊 Linux 操作系统。安装的驱动器是 nsf4。文件最大可达 100MB
最佳答案
check-if-file-exists 是典型的 TOCTTOU问题。
最简单的解决方法是不检查,尝试mv
。
如果没有要移动的文件,mv
将失败(并且不执行任何操作)。
关于linux - NFS - 来自两个客户端文件损坏的 mv 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41578600/