3
0

fsync.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //config:config FSYNC
  10. //config: bool "fsync"
  11. //config: default y
  12. //config: help
  13. //config: fsync is used to flush file-related cached blocks to disk.
  14. //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync))
  15. //kbuild:lib-$(CONFIG_FSYNC) += fsync.o
  16. //usage:#define fsync_trivial_usage
  17. //usage: "[-d] FILE..."
  18. //usage:#define fsync_full_usage "\n\n"
  19. //usage: "Write files' buffered blocks to disk\n"
  20. //usage: "\n -d Avoid syncing metadata"
  21. #include "libbb.h"
  22. #ifndef O_NOATIME
  23. # define O_NOATIME 0
  24. #endif
  25. /* This is a NOFORK applet. Be very careful! */
  26. int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int fsync_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. int status;
  30. int opts;
  31. opts = getopt32(argv, "d"); /* fdatasync */
  32. argv += optind;
  33. if (!*argv) {
  34. bb_show_usage();
  35. }
  36. status = EXIT_SUCCESS;
  37. do {
  38. int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY);
  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. return status;
  50. }