xref: /aosp_15_r20/external/toybox/toys/other/fsync.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* fsync.c - Synchronize a file's in-core state with storage device.
2  *
3  * Copyright 2015 Ranjan Kumar <[email protected]>
4  *
5  * No Standard.
6 
7 USE_FSYNC(NEWTOY(fsync, "<1d", TOYFLAG_BIN))
8 
9 config FSYNC
10   bool "fsync"
11   default y
12   help
13     usage: fsync [-d] [FILE...]
14 
15     Flush disk cache for FILE(s), writing cached data to storage device.
16 
17     -d	Skip directory info (sync file contents only).
18 */
19 
20 #define FOR_fsync
21 #include "toys.h"
22 
do_fsync(int fd,char * name)23 static void do_fsync(int fd, char *name)
24 {
25   if (FLAG(d) ? fdatasync(fd) : fsync(fd)) perror_msg("can't sync '%s'", name);
26 }
27 
fsync_main(void)28 void fsync_main(void)
29 {
30   loopfiles_rw(toys.optargs, O_RDONLY|O_NOATIME|O_NOCTTY|O_CLOEXEC|WARN_ONLY,
31       0, do_fsync);
32 }
33