md5/sha1sum: better fix for small resource leak
[platform/upstream/busybox.git] / coreutils / fsync.c
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  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
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"
14 //usage:     "\nOptions:"
15 //usage:     "\n        -d      Avoid syncing metadata"
16
17 #include "libbb.h"
18 #ifndef O_NOATIME
19 # define O_NOATIME 0
20 #endif
21
22 /* This is a NOFORK applet. Be very careful! */
23
24 int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25 int fsync_main(int argc UNUSED_PARAM, char **argv)
26 {
27         int status;
28         int opts;
29
30         opts = getopt32(argv, "d"); /* fdatasync */
31         argv += optind;
32         if (!*argv) {
33                 bb_show_usage();
34         }
35
36         status = EXIT_SUCCESS;
37         do {
38                 int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY);
39
40                 if (fd == -1) {
41                         status = EXIT_FAILURE;
42                         continue;
43                 }
44                 if ((opts ? fdatasync(fd) : fsync(fd))) {
45                         //status = EXIT_FAILURE; - do we want this?
46                         bb_simple_perror_msg(*argv);
47                 }
48                 close(fd);
49         } while (*++argv);
50
51         return status;
52 }