Rev | Author | # | Line |
---|---|---|---|
1 | perry | 1 | fdatasync - synchronize a file's in-core data with that on disk |
2 | !!SYNOPSIS | ||
3 | |||
2 | WikiAdmin | 4 | __#include <unistd.h>__ |
5 | __#ifdef _POSIX_SYNCHRONIZED_IO__ | ||
6 | __int fdatasync(int__ ''fd''__);__ | ||
7 | __#endif__ | ||
1 | perry | 8 | |
9 | !!DESCRIPTION | ||
2 | WikiAdmin | 10 | fdatasync(2) flushes all data buffers of a file to disk (before the system call returns). It resembles fsync(2) but is not required to update the metadata such as access time. |
1 | perry | 11 | |
2 | WikiAdmin | 12 | Applications that access databases or log files often write a tiny data fragment (e.g., one line in a log file) and then call fsync(2) immediately in order to ensure that the written data is physically stored on the harddisk. Unfortunately, fsync(2) will always initiate two write operations: one for the newly written data and another one in order to update the modification time stored in the inode. If the modification time is not a part of the transaction concept fdatasync(2) can be used to avoid unnecessary inode disk write operations. |
1 | perry | 13 | |
14 | !!RETURN VALUE | ||
2 | WikiAdmin | 15 | On success, zero is returned. On error, -1 is returned, and ''errno'' is set appropriately. |
1 | perry | 16 | |
17 | !!ERRORS | ||
2 | WikiAdmin | 18 | ;[EBADF]: ''fd'' is not a valid file descriptor open for writing. |
19 | ;[EROFS], [EINVAL]: ''fd'' is bound to a special file which does not support synchronization. | ||
20 | ;[EIO]: An error occurred during synchronization. | ||
1 | perry | 21 | |
2 | WikiAdmin | 22 | !!BUGS |
23 | Currently (Linux 2.2) fdatasync(2) is equivalent to fsync(2) | ||
1 | perry | 24 | |
2 | WikiAdmin | 25 | !!CONFORMING TO |
26 | POSIX1b (formerly POSIX.4) | ||
1 | perry | 27 | |
2 | WikiAdmin | 28 | !!EXAMPLES |
29 | /* | ||
30 | * This program demonstrates fdatasync | ||
31 | * | ||
32 | */ | ||
1 | perry | 33 | |
2 | WikiAdmin | 34 | #include <sys/stat.h> /* for S_* constants */ |
35 | #include <sys/types.h> /* for mode_t for creat() */ | ||
36 | #include <string.h> /* for strerror() prototype */ | ||
37 | #include <fcntl.h> /* for creat() prototype */ | ||
38 | #include <stdio.h> /* for fprintf(),stderr protype */ | ||
39 | #include <errno.h> /* for errno prototype */ | ||
40 | #include <unistd.h> /* for write(2), fdatasync(2), close(2) prototypes */ | ||
1 | perry | 41 | |
2 | WikiAdmin | 42 | #define FILENAME "/tmp/fdatasync.example" |
43 | #define TEXT "blargh!\n" | ||
1 | perry | 44 | |
2 | WikiAdmin | 45 | int main(int argc,char **argv) |
46 | { | ||
47 | int fd; | ||
48 | int err; | ||
1 | perry | 49 | |
2 | WikiAdmin | 50 | fd = creat(FILENAME,S_IRUSR|S_IWUSR); |
51 | if (fd==-1) { | ||
52 | fprintf( | ||
53 | stderr, | ||
54 | "creat(\"%s\",S_IRUSR|S_IWUSR): %s (%i)\n", | ||
55 | FILENAME, | ||
56 | strerror(errno), | ||
57 | errno); | ||
58 | return 1; | ||
59 | } | ||
1 | perry | 60 | |
2 | WikiAdmin | 61 | err=write(fd,TEXT,strlen(TEXT)); |
1 | perry | 62 | |
2 | WikiAdmin | 63 | if (err==-1) { |
64 | fprintf( | ||
65 | stderr, | ||
66 | "write(fd,\"%s\",strlen(\"%s\")): %s (%i)\n", | ||
67 | TEXT, | ||
68 | TEXT, | ||
69 | strerror(errno), | ||
70 | errno); | ||
71 | return 1; | ||
72 | } | ||
1 | perry | 73 | |
2 | WikiAdmin | 74 | if (err!=strlen(TEXT)) { |
75 | fprintf(stderr,"Failed to write all the data, and I'm lazy and not " | ||
76 | "going to retry\n"); | ||
77 | return 1; | ||
78 | } | ||
1 | perry | 79 | |
2 | WikiAdmin | 80 | /* Flush the data only */ |
81 | if (fdatasync(fd)==-1) { | ||
82 | fprintf( | ||
83 | stderr, | ||
84 | "fdatasync(fd): %s (%i)\n", | ||
85 | strerror(errno), | ||
86 | errno); | ||
87 | return 1; | ||
88 | } | ||
1 | perry | 89 | |
2 | WikiAdmin | 90 | if (close(fd)==-1) { |
91 | fprintf( | ||
92 | stderr, | ||
93 | "close(fd): %s (%i)\n", | ||
94 | strerror(errno), | ||
95 | errno); | ||
96 | return 1; | ||
97 | } | ||
1 | perry | 98 | |
2 | WikiAdmin | 99 | printf( |
100 | "%s successfully written\n", | ||
101 | FILENAME); | ||
102 | return 0; | ||
103 | } | ||
1 | perry | 104 | |
105 | |||
106 | !!SEE ALSO | ||
2 | WikiAdmin | 107 | fsync(2), B.O. Gallmeister, POSIX.4, O'Reilly, pp. 220-223 and 343. |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 6 times)