Skip to content

Commit aaf0edf

Browse files
committed
pwrite-with-append.c: ensure that O_APPEND is actually working
the old code would pass for an implementation which ignores O_APPEND.
1 parent 1237aa3 commit aaf0edf

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
#include <assert.h>
2-
#include <stdio.h>
2+
#include <fcntl.h>
33
#include <stdlib.h>
44
#include <unistd.h>
55

66
int main() {
77
char buf[4];
88
int fd;
9-
FILE *file;
109
size_t size;
1110

12-
file = fopen("fs-tests.dir/pwrite.cleanup", "a+");
13-
assert(file != NULL);
11+
fd = open("fs-tests.dir/pwrite.cleanup", O_CREAT | O_TRUNC | O_WRONLY | O_APPEND);
12+
assert(fd != -1);
1413

15-
fd = fileno(file);
14+
size = write(fd, buf, 2);
15+
assert(size == 2);
1616

17-
size = fwrite(buf, 1, 4, file);
18-
assert(size == sizeof(buf));
19-
fflush(file);
17+
// test if O_APPEND is working
18+
assert(lseek(fd, 0, SEEK_SET) == 0);
19+
size = write(fd, buf, 2);
20+
assert(size == 2);
21+
assert(lseek(fd, 0, SEEK_CUR) == 4);
2022

21-
size = pwrite(fd, buf, 4, 0);
22-
assert(size == sizeof(buf));
23-
fflush(file);
23+
size = pwrite(fd, buf, 3, 0);
24+
assert(size == 3);
2425

2526
// fd_pwrite should write from offset 0 regardless of append.
27+
// (thus shouln't extend the file.)
28+
// it shouldn't move the file offset either.
29+
assert(lseek(fd, 0, SEEK_CUR) == 4);
2630
assert(lseek(fd, 0, SEEK_END) == 4);
2731

28-
fclose(file);
32+
close(fd);
2933

3034
return EXIT_SUCCESS;
3135
}

0 commit comments

Comments
 (0)