Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using temporary files safely

There is a static library I use in my program which can only take filenames as its input, not actual file contents. There is nothing I can do about the library's source code. So I want to: create a brand-new file, store data to being processed into it, flush it onto the disk(?), pass its name to the library, then delete it.

But I also want this process to be rather secure:
1) the file must be created anew, without any bogus data (maybe it's not critical, but whatever);
2) anyone but my process must not be able read or write from/to this file (I want the library to process my actual data, not bogus data some wiseguy managed to plug in);
3) after I'm done with this file, it must be deleted (okay, if someone TerminateProcess() me, I guess there is nothing much can be done, but still).

The library seems to use non-Unicode fopen() to open the given file though, so I am not quite sure how to handle all this, since the program is intended to run on Windows. Any suggestions?

like image 306
Joker_vD Avatar asked Jan 09 '13 08:01

Joker_vD


People also ask

Are temporary files harmful?

In general, it's safe to delete anything in the Temp folder. Sometimes, you may get a "can't delete because the file is in use" message, but you can just skip those files. For safety, do your Temp directory deleting just after you reboot the computer.

Is it safe to clean up temporary files?

Is it safe to delete temp files? Yes, it's safe to delete temporary files from Windows. Most of the time, they'll be deleted automatically — if they're not, you can go in and delete them yourself without any worries.

Should I delete all temp files?

There's no hard-and-fast rule about when you should delete temporary files. If you want your computer in top operating condition, then it's recommended that you delete temporary files once they're no longer being used by an app. You can delete your system's temporary files as often as you feel comfortable doing so.

Do I need to keep temporary files on my computer?

There's nothing harmful about temporary files, but once the cached data they contain has served its purpose, they're no longer needed. Ideally, unneeded temp files should be deleted by the process or program that created them.


1 Answers

You have a lot of suggestions already, but another option that I don't think has been mentioned is using named pipes. It will depend on the library in question as to whether it works or not, but it might be worth a try. You can create a named pipe in your application using the CreateNamedPipe function, and pass the name of the pipe to the library to operate on (the filename you would pass would be \\.\pipe\PipeName). Whether the library accepts a filename like that or not is something you would have to try, but if it works the advantage is your file never has to actually be written to disk.

like image 134
Jonathan Potter Avatar answered Sep 21 '22 09:09

Jonathan Potter