blob: fb71ce85f010b3f3097b265e09e8f7ba383b58ee [file] [log] [blame]
Rob Landley0e4690d2006-08-20 22:12:18 +00001/* 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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Rob Landley0e4690d2006-08-20 22:12:18 +000014
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley0e4690d2006-08-20 22:12:18 +000016int readahead_main(int argc, char **argv)
17{
Rob Landley0e4690d2006-08-20 22:12:18 +000018 int retval = EXIT_SUCCESS;
19
20 if (argc == 1) bb_show_usage();
21
22 while (*++argv) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000023 int fd = open_or_warn(*argv, O_RDONLY);
24 if (fd >= 0) {
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000025 off_t len;
26 int r;
27
28 /* fdlength was reported to be unreliable - use seek */
29 len = xlseek(fd, 0, SEEK_END);
30 xlseek(fd, 0, SEEK_SET);
31 r = readahead(fd, 0, len);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000032 close(fd);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000033 if (r >= 0)
34 continue;
Rob Landley0e4690d2006-08-20 22:12:18 +000035 }
36 retval = EXIT_FAILURE;
37 }
38
39 return retval;
40}