Skip to content

Commit

Permalink
Test removal of a file when it is open in an application
Browse files Browse the repository at this point in the history
A `cp temp-file-read{.bak,}` is required for the program to work.
  • Loading branch information
sivaraam committed Jun 18, 2018
1 parent 32f7b65 commit d5000dc
Show file tree
Hide file tree
Showing 2 changed files with 556 additions and 0 deletions.
47 changes: 47 additions & 0 deletions remove_file_while_open.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Program to test what happens when a file is deleted
* externally when it is opened in the program.
*
* In general this test tries to validate the fact that,
*
* Applications continue to have access to their open
* files even if they are removed externally by some
* means.
*
* In GNU/Linux, this seems to be true.
*/

#include <stdio.h>
#include <unistd.h>

char read_ch(FILE *fp)
{
char ch;
if (fread (&ch, 1, 1, fp))
{
return ch;
}

return 0;
}

int main(void)
{
char *read_file = "temp-file-read";
FILE *read_fp = fopen (read_file, "r");
if (read_fp != NULL)
{
printf ("First character: %c\n", read_ch(read_fp));

/* remove the file and wait for some time */
remove (read_file);
sleep(120);

fseek (read_fp, -2L, SEEK_END);
printf ("Second character: %c\n", read_ch(read_fp));
}
else
{
printf ("File '%s' missing.\n", read_file);
}
}
Loading

0 comments on commit d5000dc

Please sign in to comment.