Guaranteed file deletion upon program termination (C/C++)

前端 未结 8 1265
长发绾君心
长发绾君心 2020-12-14 02:16

Win32\'s CreateFile has FILE_FLAG_DELETE_ON_CLOSE, but I\'m on Linux.

I want to open a temporary file which will always be deleted upon program terminat

8条回答
  •  情歌与酒
    2020-12-14 03:18

    • Have a book-keeping directory for temporary files under your dot-directory.
    • When creating a temp-file, first create book-keeping file into the book-keeping directory that contains path or UUID to your to-be temp file.
    • Create that temp file.
    • When temp-file is deleted, then delete the book-keeping file.
    • When the program starts, scan the book-keeping directory for any files containing paths to temporary files and try to delete them if found, them delete book-keeping files.
    • (Log noisily if any step fails.)

    I don't see ways to do it any way simpler. This is the boilerplate any production quality program must go through; +500 lines easily.

提交回复
热议问题