summaryrefslogtreecommitdiff
path: root/tests/testopen.c
blob: d46a31f20d7ec121c52b3087db2c971dd6cb15c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
    Copyright (C) 2012 - 2015 Andreas Baumann <mail@andreasbaumann.cc>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main( void )
{
	char buf[4096];
	int fd;
	int res;
	ssize_t wres;
	struct stat s;

	/* Make sure no file is around from last invocation */
	(void)unlink( "./mnt/testopen" );
	
	/* This should succeed */
	fd = open( "./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR );
	if( fd < 0 ) {
		perror( "Unable to create testopen" );
		return 1;
	}

	/* write some data into it so we know if truncate really works */
	memset( buf, 0, 4096 );

	wres = write( fd, buf, 4096 );
	if( wres != 4096 ) {
		perror( "Error writing" );
		(void)close( fd );
		return 1;
	}
	
	(void)close( fd );

	/* check if the file has size 4096 */
	res = stat( "./mnt/testopen", &s );
	if( res < 0 ) {
		perror( "Error stating testopen" );
		return 1;
	}
	if( s.st_size == 4096 ) {
		// OK
	} else {
		fprintf( stderr, "Size of file should be 4096, but is %jd\n", s.st_size );
		return 1;
	}
	
	/* Second one should fail with EEXIST */
	fd = open( "./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR );
	if( fd < 0 && errno == EEXIST ) {
		// OK
	} else {
		fprintf( stderr, "Second create succedded and should not" );
		return 1;
	}
	
	/* Third one should success and append to the file */
	fd = open( "./mnt/testopen", O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR );
	
	/* check if the file has size 4096 */
	res = stat( "./mnt/testopen", &s );
	if( res < 0 ) {
		perror( "Error stating testopen" );
		return 1;
	}
	if( s.st_size == 4096 ) {
		// OK
	} else {
		fprintf( stderr, "Size of file should be 4096, but is %jd\n", s.st_size );
		return 1;
	}

	/* write some more data, this data should be appended */
	wres = write( fd, buf, 4096 );
	if( wres != 4096 ) {
		perror( "Error writing" );
		(void)close( fd );
		return 1;
	}

	(void)close( fd );	
	
	/* check if the file has size 2 * 4096 now */
	res = stat( "./mnt/testopen", &s );
	if( res < 0 ) {
		perror( "Error stating testopen" );
		return 1;
	}
	if( s.st_size == 8192 ) {
		// OK
	} else {
		fprintf( stderr, "Size of file should be 8192, but is %jd\n", s.st_size );
		return 1;
	}
	
	/* Fourth one should success and truncate the file */
	fd = open( "./mnt/testopen", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR );
	(void)close( fd );	
	
	/* check if the file has size zero */
	res = stat( "./mnt/testopen", &s );
	if( res < 0 ) {
		perror( "Error stating testopen" );
		return 1;
	}
	if( s.st_size == 0 ) {
		// OK
	} else {
		fprintf( stderr, "Size of file should be 0, but is %jd\n", s.st_size );
		return 1;
	}
	
	res = unlink( "./mnt/testopen" );
	if( res < 0 ) {
		perror( "Error removing testopen" );
		return 1;
	}
	
	return 0;
}