Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
Creating a temporary file: do I really need to use mkstmp?
I want a temporary file which can be opened and closed repeatedly. For example, I want to be able to empty it by closing it and reopening it in write mode. If I do this with a file created by mkstemp, it's no different in principle to using mktemp or tempnam.
I can see the need for extra care if a program runs as root, but this one wouldn't, and the temporary files would be created in the user's home directory which isn't writable by anyone else. I could set the mode to 600; wouldn't this be good enough?
Though,I'm not sure whether following going to help you or not.
One of my requirement for my tool is to create unique names that can be used later.
For example,I'll get the unique file using mkstemp and get its name after that close the fd and delete it. Now we have filename alone not file itself. Use this file name for creating a new file at a different location.
char fname = "extcarveXXXXXX";
/* The trick to get a unique name using mkstemp and unlink the temp.file and then use it. */
temfd = mkstemp (fname);
strcpy (tmp_dname, restore_device_dir);
strcat (tmp_dname, "/");
strcat (tmp_dname, fname);
strcat (tmp_dname, needle->dotpart);
temfd = creat (tmp_dname, 0700);
if (temfd < 0)
("Please check permission for destination directory %s.Something wrong",
Yes, that's quite similar to what I'm doing except that I don't need to move the file around so I can just open and close it rather than deleting and recreating it.
The point made in some of the literature is that if you don't have the temporary file open continuously, someone else can delete it and replace it by a link to an important system file (for example the /etc/passwd file); then when you write to what you think is your file, you actually clobber your system. But that seems to me not to be a problem unless you're running with root permissions and creating your files in a world-writeable directory such as /tmp.
Why do programmers have to be so paranoid?
07-18-2011 #4Why do programmers have to be so paranoid?
07-18-2011 #5Should you be sitting wondering,
Which Batman is the best,
There's only one true answer my friend,
It's Adam Bloody West!
The Fifth Continent