我在Ubuntu服务器上有一个文件夹,来自不同客户端机器(也是Ubuntu)的用户可以编辑文件(通过FileZilla,但任何其他的都可以)。 但是,当一个用户正在编辑一个文件时,该文件应该被locking,以便其他人不能同时编辑它。 这应该是预期的行为( 在别处 )。 如何在Ubuntu Server 14.04中完成此操作? 看起来像一个简单,甚至是明显的任务,但我无法find任何地方。 谷歌只给我“如何解锁其他人locking的文件”。
编辑
遵循Lomwangi的build议,我正在尝试创build一个bash脚本来自动执行任务。 首先,我使用NFS在我的客户机上安装服务器文件夹的映像。 然后,我在服务器文件夹上创build了以下脚本:
flock -nx "$1" xdg-open "$1"
仍然无法testing它。
FTP(filezilla)不支持locking。 也许如果你设置NFS或SaMBa然后使用正常的文件编辑器locking将工作。
应该被locking …预期的行为…
您对Linux上的文件locking语义的理解不正确。 从给出的参考链接,你是基于Windows文件语义的假设。 他们不适用于大多数基于Unix的系统。
我无法find一个巨大的权威来源,但维基百科,但它是微不足道的testing。
https://en.wikipedia.org/wiki/File_locking#In_Unix-like_systems
类Unix操作系统(包括Linux和Apple OS X)通常不会自动locking打开的文件或正在运行的程序。
(维基百科其实是错误的运行程序,所以我编辑它!)
在Linux上,它的正常操作允许多个作者和读者同时在同一个文件上操作。
唯一不是这样的情况是文件本身是当前加载的可执行对象,并且您请求写入权限。
文件locking是作为一个非必需的选项完成的,并通过fcntl , flock或lockf ,所有使用该文件的应用程序都必须支持,并使用相同的locking方法才能工作。
另一种经常使用的方法是创build一个新文件,该文件是旧文件的副本,作为临时命名文件,然后将其重命名为旧文件的顶部。 这是因为所有的重命名都是posix兼容的文件系统上的primefaces – 人们将看到旧文件或新文件,但不是两者混合。
如果你想实现多个编辑器编辑同一个文件,你可能需要使用像git这样的方法,以便用户可以将更改推送到更改存储库。
或者,也可以只使用一个Windows系统,该function对您来说至关重要。
取决于访问文件的模式。 例如,
羊群帮助:
❯ flock -h Usage: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command... flock [-sxon][-w #] directory [-c] command... Options: -s --shared Get a shared lock -x --exclusive Get an exclusive lock -u --unlock Remove a lock -n --nonblock Fail rather than wait -w --timeout Wait for a limited amount of time -o --close Close file descriptor before running command -c --command Run a single command string through the shell -h --help Display this text -V --version Display version
例子 。
第一个子shell获得一个20秒的锁并被推到后台。 任何其他进程(第二个进程)在获取locking时立即失败。
❯ (flock -nx /tmp/a sleep 20 && echo nice..)& flock -nx /tmp/a sleep 20 || echo failed to lock ⏎ [1] 2546 failed to lock ❯ jobs [1] + running ( flock -nx /tmp/a sleep 20 && echo nice..; ) ❯ nice.. [1] + 2546 done ( flock -nx /tmp/a sleep 20 && echo nice..; )
— 编辑
# Flock using a lock file based on the md5 of the content. I'm being lazy here. I should md5 the filename... ❯ open_something() { md5=$(md5sum /tmp/a | awk '{print $1}'); flock -nx /tmp/$md5 vim "$1" || echo "Oops. Go away" } # So now let's vim a file and background it immediately. ❯ open_something /tmp/a [1] + 9185 suspended open_something /tmp/a ❯ jobs [1] + suspended (signal) open_something /tmp/a # Let's see whether flock created a lock file based on the md5 of the file and who's using it... ❯ ls /tmp vagrant VMwareDnD vmware-root a d41d8cd98f00b204e9800998ecf8427e vgauthsvclog.txt.0 ❯ fuser /tmp/d41d8cd98f00b204e9800998ecf8427e /tmp/d41d8cd98f00b204e9800998ecf8427e: 9175 9176 # Another user/process tries to open the file ❯ open_something /tmp/a Oops. Go away # Let's kill the original vim process and another process can now acquire the lock. ❯ fg [1] + 9185 continued open_something /tmp/a vagrant@ubuntu-trusty ~ ❯❯❯ open_something /tmp/a
简而言之,FTP不支持locking。
原因很简单:在通过FTP协议编辑文件时,实际上并不是编辑服务器端文件本身,而是编辑下载的副本 ,而不是使用原始文件的相同path/名称重新上传。
另一方面,作为NFS或SMB的协议是块交换协议,其允许客户端软件直接编辑。
总之:如果你需要locking,FTP不是正确的工具。
没有解决scheme,只是一个更详细的解释,我发现的链接。