~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/testcases/truncate.c

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <unistd.h>
 
2
#include <sys/types.h>
 
3
#include <sys/stat.h>
 
4
#include <fcntl.h>
 
5
#include <stdio.h>
 
6
#include <errno.h>
 
7
#include <stdlib.h>
 
8
 
 
9
int main(int argc, char **argv)
 
10
{
 
11
        int fd;
 
12
        struct stat *fstat;
 
13
        char *filename;
 
14
        ssize_t size;
 
15
 
 
16
        if (argc < 2) {
 
17
                printf("Usage: test_truncate <path>\n");
 
18
                return 1;
 
19
        }
 
20
        filename = argv[1];
 
21
        fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
 
22
        if (fd == -1) {
 
23
                printf("error opening file\n");
 
24
                return 1;
 
25
        }
 
26
        size = write(fd , "abc" , 3);
 
27
        if (size < 0) {
 
28
                printf("data not written to file");
 
29
                return 1;
 
30
        }
 
31
        if ((close(fd) != 0)) {
 
32
                printf("error closing file");
 
33
                return 1;
 
34
        }
 
35
        fd = open(filename, O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
 
36
        if (fd == -1) {
 
37
                printf("2 error opening file\n");
 
38
                return 1;
 
39
        }
 
40
        if ((close(fd) != 0)) {
 
41
                printf("2 error closing file");
 
42
                return 1;
 
43
        }
 
44
        fstat = malloc(sizeof(struct stat));
 
45
        if (!fstat) {
 
46
                printf("Unable to allocate memory\n");
 
47
                return 1;
 
48
        }
 
49
        stat(filename, fstat);
 
50
        if (fstat->st_size != 0) {
 
51
                printf("test file should be 0 bytes\n");
 
52
                printf("test file is [%d] bytes\n", fstat->st_size);
 
53
                free(fstat);
 
54
                if (unlink(filename)) {
 
55
                        printf("error deleting file\n");
 
56
                        return 1;
 
57
                }
 
58
                return 1;
 
59
        }
 
60
        free(fstat);
 
61
        if (unlink(filename)) {
 
62
                printf("error deleting file\n");
 
63
                return 1;
 
64
        }
 
65
        return 0;
 
66
}