-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpfiletest.c
66 lines (53 loc) · 1.37 KB
/
tmpfiletest.c
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
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#define MAX_BYTES (100ULL<<20) /*100 MB */
int main(void) {
int fd;
FILE* f;
uint64_t count = 0;
uint64_t file_value;
fd = open(".", __O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1) {
printf("O_TMPFILE error: %s\n", strerror(errno));
return EXIT_FAILURE;
}
printf("O_TMPFILE success\n");
// write
f = fdopen(fd, "w+");
if (!f) {
printf("fdopen() failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
for (count = 0; count < MAX_BYTES / sizeof(count); count++) {
if (fwrite(&count, sizeof(count), 1, f) != 1) {
printf("fwrite() failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
}
fflush(f);
rewind(f);
for (count = 0; count < MAX_BYTES / sizeof(count); count++) {
if (fread(&file_value, sizeof(file_value), 1, f) != 1) {
printf("fwrite() failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (count != file_value) {
printf("value mismatch: expected %llu, got %llu\n", count, file_value);
return EXIT_FAILURE;
}
}
if (fread(&file_value, sizeof(file_value), 1, f) == 1) {
printf("unexcpected read success: read %llu, expected eof", file_value);
return EXIT_FAILURE;
}
fclose(f);
printf("success\n");
return EXIT_SUCCESS;
}