3
0

readahead.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * readahead implementation for busybox
  4. *
  5. * Preloads the given files in RAM, to reduce access time.
  6. * Does this by calling the readahead(2) system call.
  7. *
  8. * Copyright (C) 2006 Michael Opdenacker <michael@free-electrons.com>
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //usage:#define readahead_trivial_usage
  13. //usage: "[FILE]..."
  14. //usage:#define readahead_full_usage "\n\n"
  15. //usage: "Preload FILEs to RAM"
  16. #include "libbb.h"
  17. int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int readahead_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. int retval = EXIT_SUCCESS;
  21. if (!argv[1]) {
  22. bb_show_usage();
  23. }
  24. while (*++argv) {
  25. int fd = open_or_warn(*argv, O_RDONLY);
  26. if (fd >= 0) {
  27. off_t len;
  28. int r;
  29. /* fdlength was reported to be unreliable - use seek */
  30. len = xlseek(fd, 0, SEEK_END);
  31. xlseek(fd, 0, SEEK_SET);
  32. r = readahead(fd, 0, len);
  33. close(fd);
  34. if (r >= 0)
  35. continue;
  36. }
  37. retval = EXIT_FAILURE;
  38. }
  39. return retval;
  40. }