blob: e22aaa46887bb0beb67d0a805111ec69ba7dfb59 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley0e4690d2006-08-20 22:12:18 +000011 */
12
Pere Orga5bc8c002011-04-11 03:29:49 +020013//usage:#define readahead_trivial_usage
14//usage: "[FILE]..."
15//usage:#define readahead_full_usage "\n\n"
16//usage: "Preload FILEs to RAM"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Rob Landley0e4690d2006-08-20 22:12:18 +000019
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000020int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010021int readahead_main(int argc UNUSED_PARAM, char **argv)
Rob Landley0e4690d2006-08-20 22:12:18 +000022{
Rob Landley0e4690d2006-08-20 22:12:18 +000023 int retval = EXIT_SUCCESS;
24
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010025 if (!argv[1]) {
26 bb_show_usage();
27 }
Rob Landley0e4690d2006-08-20 22:12:18 +000028
29 while (*++argv) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000030 int fd = open_or_warn(*argv, O_RDONLY);
31 if (fd >= 0) {
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000032 off_t len;
33 int r;
34
35 /* fdlength was reported to be unreliable - use seek */
36 len = xlseek(fd, 0, SEEK_END);
37 xlseek(fd, 0, SEEK_SET);
38 r = readahead(fd, 0, len);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000039 close(fd);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000040 if (r >= 0)
41 continue;
Rob Landley0e4690d2006-08-20 22:12:18 +000042 }
43 retval = EXIT_FAILURE;
44 }
45
46 return retval;
47}