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