Safe stream update of file

前端 未结 8 1391
别那么骄傲
别那么骄傲 2020-11-30 14:37

We perform updates of large text files by writing new records to a temp file, then replacing the old file with the temp file. A heavily abbreviated version:

         


        
8条回答
  •  甜味超标
    2020-11-30 15:26

    The normal way of avoiding the "delete then move fails problem" is:

    • Write to file.new
    • Move file.current to file.old
    • Move file.new to file.current
    • Delete file.new

    Then when you come to read, use file.new if file.current is missing, deleting file.old if you see it.

    Checking for whether or not the file is available: try opening it for write, but appending to the end. Of course, you'll need to close the handle before you then move it, and in-between someone else could open it - but it would at least be a reasonable optimisation.

    Not sure about copying summaries etc, I'm afraid.

提交回复
热议问题