I'm trying to create a TAR archive from my program and then opening the archive for further processing. I have a 2 second delay between calling system()
and open()
. So far, it works fine, but I'm not sure why the 2 second delay is necessary or if it's the correct solution.
Without the delay, I get error code 2 (ENOENT "No such file or directory") from the open()
call. My first thought was the filesystem wasn't updating itself fast enough and open()
couldn't find the file. But what if the system is really busy? Do I need a longer delay? Should I loop until open()
succeeds instead of the delay? Is the problem something completely different?
UPDATE
The root filesystem is EXT2. /tmp
is mounted in RAM using TMPFS. I'm using tar
to create an archive, not extract contents of one. Essentially, my program is supposed to create an archive of some log files and send them over the network (that's why I open the archive after creating it).
int return_value = system("/bin/tar -czf /tmp/logs.tar.gz /var/log/mylogs.log* &> /dev/null");
// error checks on return_value as described here: http://linux.die.net/man/2/wait
if(return_value != 0) {
return return_value;
}
//usleep(2000000);
return_value = open("/tmp/logs.tar.gz", O_RDONLY | O_LARGEFILE, 0);
// success or failure depending on whether there's a delay or not