Denys Vlasenko | 14f6f0a | 2009-06-18 08:20:02 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini fsync implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2008 Nokia Corporation. All rights reserved. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 14f6f0a | 2009-06-18 08:20:02 +0200 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config FSYNC |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 10 | //config: bool "fsync (3.7 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: fsync is used to flush file-related cached blocks to disk. |
| 14 | |
| 15 | //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) |
| 16 | |
| 17 | //kbuild:lib-$(CONFIG_FSYNC) += fsync.o |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 18 | |
| 19 | //usage:#define fsync_trivial_usage |
| 20 | //usage: "[-d] FILE..." |
| 21 | //usage:#define fsync_full_usage "\n\n" |
| 22 | //usage: "Write files' buffered blocks to disk\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 23 | //usage: "\n -d Avoid syncing metadata" |
| 24 | |
Denys Vlasenko | 14f6f0a | 2009-06-18 08:20:02 +0200 | [diff] [blame] | 25 | #include "libbb.h" |
Denys Vlasenko | 10544a8 | 2010-02-09 00:26:10 +0100 | [diff] [blame] | 26 | #ifndef O_NOATIME |
| 27 | # define O_NOATIME 0 |
| 28 | #endif |
Denys Vlasenko | 14f6f0a | 2009-06-18 08:20:02 +0200 | [diff] [blame] | 29 | |
| 30 | /* This is a NOFORK applet. Be very careful! */ |
| 31 | |
| 32 | int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 33 | int fsync_main(int argc UNUSED_PARAM, char **argv) |
| 34 | { |
| 35 | int status; |
| 36 | int opts; |
| 37 | |
| 38 | opts = getopt32(argv, "d"); /* fdatasync */ |
| 39 | argv += optind; |
| 40 | if (!*argv) { |
| 41 | bb_show_usage(); |
| 42 | } |
| 43 | |
| 44 | status = EXIT_SUCCESS; |
| 45 | do { |
Denys Vlasenko | bec5888 | 2010-10-20 13:21:22 +0200 | [diff] [blame] | 46 | int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY); |
Denys Vlasenko | 14f6f0a | 2009-06-18 08:20:02 +0200 | [diff] [blame] | 47 | |
| 48 | if (fd == -1) { |
| 49 | status = EXIT_FAILURE; |
| 50 | continue; |
| 51 | } |
| 52 | if ((opts ? fdatasync(fd) : fsync(fd))) { |
| 53 | //status = EXIT_FAILURE; - do we want this? |
| 54 | bb_simple_perror_msg(*argv); |
| 55 | } |
| 56 | close(fd); |
| 57 | } while (*++argv); |
| 58 | |
| 59 | return status; |
| 60 | } |