• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "posixtest.h"
26 
27 #define TNAME "fsync/4-1.c"
28 
main(void)29 int main(void)
30 {
31 	char tmpfname[256];
32 	char *data;
33 	int total_size = 1024;
34 	int fd;
35 
36 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_fsync_4_1_%d", getpid());
37 	unlink(tmpfname);
38 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
39 	if (fd == -1) {
40 		printf(TNAME " Error at open(): %s\n", strerror(errno));
41 		exit(PTS_UNRESOLVED);
42 	}
43 
44 	/* Make sure the file is removed when it is closed */
45 	unlink(tmpfname);
46 	data = malloc(total_size);
47 	memset(data, 'a', total_size);
48 	if (write(fd, data, total_size) != total_size) {
49 		printf(TNAME "Error at write(): %s\n", strerror(errno));
50 		free(data);
51 		exit(PTS_UNRESOLVED);
52 	}
53 	free(data);
54 
55 	if (fsync(fd) == -1) {
56 		printf(TNAME "Error at fsync(): %s\n", strerror(errno));
57 		exit(PTS_FAIL);
58 	}
59 
60 	close(fd);
61 	printf("Test PASSED\n");
62 	return PTS_PASS;
63 }
64