linux - NFS - 来自两个客户端文件损坏的 mv 命令?

标签 linux race-condition nfs corruption file-locking

我有一个基本脚本,我想执行以下操作:

  1. 如果 new/file.txt 存在
  2. 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/

相关文章:

hadoop - 将文件从 NFS 或本地 FS 复制到 HDFS

embedded-linux - linux中的NFS挂载系统调用

linux - 自动将 CSV 导入 PostgreSQL 的脚本

linux - 有没有可以在Linux上运行的好的并行混合整数编程优化器?

java - 奇怪的比赛条件?

c - pthread_create() 中是否存在潜在的竞争条件?

在 K20 上没有 -G 选项时 CUDA C 返回不确定且奇怪的结果

linux - 您好,使用 NFS 挂载和共享服务器

linux - Gst-plugin-base 从源失败构建

linux - BusyBox tar : append workaround given limited disk space?