1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * This file is licensed under the GPL license. For the full content
4 * of this license, see the COPYING file at the top level of this
5 * source tree.
6
7 * Upon successful completion, fsync() shall return 0.
8 * Otherwise, .1 shall be returned and errno set
9 * to indicate the error. If the fsync() function fails.
10 *
11 * 1. Create a regular file;
12 * 2. Write a few bytes to the file, call fsycn(), should return 0;
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/mman.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "posixtest.h"
27 #include "tempfile.h"
28
29 #define TNAME "fsync/4-1.c"
30
main(void)31 int main(void)
32 {
33 char tmpfname[PATH_MAX];
34 char *data;
35 int total_size = 1024;
36 int fd;
37
38 PTS_GET_TMP_FILENAME(tmpfname, "pts_fsync_4_1");
39 unlink(tmpfname);
40 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
41 if (fd == -1) {
42 printf(TNAME " Error at open(): %s\n", strerror(errno));
43 exit(PTS_UNRESOLVED);
44 }
45
46 /* Make sure the file is removed when it is closed */
47 unlink(tmpfname);
48 data = malloc(total_size);
49 memset(data, 'a', total_size);
50 if (write(fd, data, total_size) != total_size) {
51 printf(TNAME "Error at write(): %s\n", strerror(errno));
52 free(data);
53 exit(PTS_UNRESOLVED);
54 }
55 free(data);
56
57 if (fsync(fd) == -1) {
58 printf(TNAME "Error at fsync(): %s\n", strerror(errno));
59 exit(PTS_FAIL);
60 }
61
62 close(fd);
63 printf("Test PASSED\n");
64 return PTS_PASS;
65 }
66