readahead.c 888 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 tarball for details.
  11. */
  12. #include "libbb.h"
  13. int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  14. int readahead_main(int argc, char **argv)
  15. {
  16. int retval = EXIT_SUCCESS;
  17. if (argc == 1) bb_show_usage();
  18. while (*++argv) {
  19. int fd = open_or_warn(*argv, O_RDONLY);
  20. if (fd >= 0) {
  21. off_t len;
  22. int r;
  23. /* fdlength was reported to be unreliable - use seek */
  24. len = xlseek(fd, 0, SEEK_END);
  25. xlseek(fd, 0, SEEK_SET);
  26. r = readahead(fd, 0, len);
  27. close(fd);
  28. if (r >= 0)
  29. continue;
  30. }
  31. retval = EXIT_FAILURE;
  32. }
  33. return retval;
  34. }