~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/testcases/open_close.c

  • Committer: Dustin Kirkland
  • Date: 2016-02-27 00:00:23 UTC
  • Revision ID: kirkland@ubuntu.com-20160227000023-h0e4oui5y1vbaurd
openingĀ 112

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <sys/types.h>
2
 
#include <sys/stat.h>
3
 
#include <fcntl.h>
4
 
#include <stdio.h>
5
 
#include <errno.h>
6
 
 
7
 
int main(int argc, char *argv[])
8
 
{
9
 
        char *filename;
10
 
        int fd;
11
 
        int i;
12
 
        int iterations;
13
 
        int do_close = 1;
14
 
        int rc;
15
 
 
16
 
        if (argc < 3) {
17
 
                printf("Usage: open_close <path> [iterations]\n");
18
 
                return 1;
19
 
        }
20
 
        iterations = atoi(argv[2]);
21
 
        if (argc == 4)
22
 
                do_close = 0;
23
 
        printf("file base [%s]; iterations = [%d]\n", argv[1], iterations);
24
 
        for (i = 0; i < iterations; i++) {
25
 
                rc = asprintf(&filename, "%s/file%d", argv[1], i);
26
 
                if (rc == -1) {
27
 
                        printf("Out of memory\n");
28
 
                        return 1;
29
 
                }
30
 
                fd = open(filename, (O_CREAT | O_EXCL));
31
 
                if (fd == -1) {
32
 
                        printf("Error creating file [%s]; errno = [%d]; "
33
 
                               "string = [%m].  Died on iteration [%d].\n",
34
 
                               filename, errno, i);
35
 
                        return 1;
36
 
                }
37
 
                if (do_close)
38
 
                        close(fd);
39
 
                rc = unlink(filename);
40
 
                if (rc == -1) {
41
 
                        printf("Error unlinking file [%s]; errno = [%d]; "
42
 
                               "string = [%m]\n", filename, errno);
43
 
                }
44
 
                free(filename); 
45
 
        }
46
 
        return 0;
47
 
}