fsync.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. //usage:#define fsync_trivial_usage
  10. //usage: "[-d] FILE..."
  11. //usage:#define fsync_full_usage "\n\n"
  12. //usage: "Write files' buffered blocks to disk\n"
  13. //usage: "\n -d Avoid syncing metadata"
  14. #include "libbb.h"
  15. #ifndef O_NOATIME
  16. # define O_NOATIME 0
  17. #endif
  18. /* This is a NOFORK applet. Be very careful! */
  19. int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  20. int fsync_main(int argc UNUSED_PARAM, char **argv)
  21. {
  22. int status;
  23. int opts;
  24. opts = getopt32(argv, "d"); /* fdatasync */
  25. argv += optind;
  26. if (!*argv) {
  27. bb_show_usage();
  28. }
  29. status = EXIT_SUCCESS;
  30. do {
  31. int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY);
  32. if (fd == -1) {
  33. status = EXIT_FAILURE;
  34. continue;
  35. }
  36. if ((opts ? fdatasync(fd) : fsync(fd))) {
  37. //status = EXIT_FAILURE; - do we want this?
  38. bb_simple_perror_msg(*argv);
  39. }
  40. close(fd);
  41. } while (*++argv);
  42. return status;
  43. }