How do I atomically create a locked file in Linux?

前端 未结 3 1574
误落风尘
误落风尘 2021-02-20 02:58

Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading th

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 03:19

    The race exists anyway. If the file may or may not exist then you have to test for its existence before trying to lock it. But if the file is your mutex, then you can't possibly do that and the space between "if file exists already" (false) and "download to newly created file" is unconstrained. Another process could come by and create the file and start downloading before your download begins, and you would clobber it.

    Basically don't use fcntl locks here, use the existence of the file itself. open() with O_CREAT and O_EXCL will fail if the file already exists, telling you that someone else got there first.

提交回复
热议问题