blob: 652a41c33f9d6426ff893a60de41dacf82c4b908 [file] [log] [blame]
Denys Vlasenko14f6f0a2009-06-18 08:20:02 +02001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko14f6f0a2009-06-18 08:20:02 +02008 */
Pere Orga34425382011-03-31 14:43:25 +02009
10//usage:#define fsync_trivial_usage
11//usage: "[-d] FILE..."
12//usage:#define fsync_full_usage "\n\n"
13//usage: "Write files' buffered blocks to disk\n"
Pere Orga34425382011-03-31 14:43:25 +020014//usage: "\n -d Avoid syncing metadata"
15
Denys Vlasenko14f6f0a2009-06-18 08:20:02 +020016#include "libbb.h"
Denys Vlasenko10544a82010-02-09 00:26:10 +010017#ifndef O_NOATIME
18# define O_NOATIME 0
19#endif
Denys Vlasenko14f6f0a2009-06-18 08:20:02 +020020
21/* This is a NOFORK applet. Be very careful! */
22
23int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24int fsync_main(int argc UNUSED_PARAM, char **argv)
25{
26 int status;
27 int opts;
28
29 opts = getopt32(argv, "d"); /* fdatasync */
30 argv += optind;
31 if (!*argv) {
32 bb_show_usage();
33 }
34
35 status = EXIT_SUCCESS;
36 do {
Denys Vlasenkobec58882010-10-20 13:21:22 +020037 int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY);
Denys Vlasenko14f6f0a2009-06-18 08:20:02 +020038
39 if (fd == -1) {
40 status = EXIT_FAILURE;
41 continue;
42 }
43 if ((opts ? fdatasync(fd) : fsync(fd))) {
44 //status = EXIT_FAILURE; - do we want this?
45 bb_simple_perror_msg(*argv);
46 }
47 close(fd);
48 } while (*++argv);
49
50 return status;
51}