summaryrefslogtreecommitdiffstats
path: root/misc-utils/fadvise.c
blob: 9606fb44ec965c958a8bf211c53c30f68b8fd40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * fadvise - utility to use the posix_fadvise(2)
 *
 * Copyright (C) 2022 Red Hat, Inc. All rights reserved.
 * Written by Masatake YAMATO <yamato@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>

#include "c.h"
#include "nls.h"
#include "strutils.h"

static const struct advice {
	const char *name;
	int num;
} advices [] = {
	{ "normal",     POSIX_FADV_NORMAL,     },
	{ "sequential", POSIX_FADV_SEQUENTIAL, },
	{ "random",     POSIX_FADV_RANDOM,     },
	{ "noreuse",    POSIX_FADV_NOREUSE,    },
	{ "willneeded", POSIX_FADV_WILLNEED,   },
	{ "dontneed",   POSIX_FADV_DONTNEED,   },
};

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;
	size_t i;

	fputs(USAGE_HEADER, out);
	fprintf(out, _(" %s [options] file\n"), program_invocation_short_name);
	fprintf(out, _(" %s [options] --fd|-d file-descriptor\n"), program_invocation_short_name);

	fputs(USAGE_OPTIONS, out);
	fputs(_(" -a, --advice <advice> applying advice to the file (default: \"dontneed\")\n"), out);
	fputs(_(" -l, --length <num>    length for range operations, in bytes\n"), out);
	fputs(_(" -o, --offset <num>    offset for range operations, in bytes\n"), out);

	fputs(USAGE_SEPARATOR, out);
	printf(USAGE_HELP_OPTIONS(23));

	fputs(_("\nAvailable values for advice:\n"), out);
	for (i = 0; i < ARRAY_SIZE(advices); i++) {
		fprintf(out, "  %s\n",
			advices[i].name);
	}

	printf(USAGE_MAN_TAIL("fadvise(1)"));

	exit(EXIT_SUCCESS);
}

int main(int argc, char ** argv)
{
	int c;
	int rc;
	bool do_close = false;

	int fd = -1;
	off_t offset = 0;
	off_t len = 0;
	int advice = POSIX_FADV_DONTNEED;

	static const struct option longopts[] = {
		{ "advice",     required_argument, NULL, 'a' },
		{ "fd",         required_argument, NULL, 'd' },
		{ "length",     required_argument, NULL, 'l' },
		{ "offset",     required_argument, NULL, 'o' },
		{ "version",    no_argument,       NULL, 'V' },
		{ "help",	no_argument,       NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	while ((c = getopt_long (argc, argv, "a:d:hl:o:V", longopts, NULL)) != -1) {
		switch (c) {
		case 'a':
			advice = -1;
			for (size_t i = 0; i < ARRAY_SIZE(advices); i++) {
				if (strcmp(optarg, advices[i].name) == 0) {
					advice = advices[i].num;
					break;
				}
			}
			if (advice == -1)
				errx(EXIT_FAILURE, "invalid advice argument: '%s'", optarg);
			break;
		case 'd':
			fd = strtos32_or_err(optarg,
					     _("invalid fd argument"));
			break;
		case 'l':
			len = strtosize_or_err(optarg,
					       _("invalid length argument"));
			break;
		case 'o':
			offset = strtosize_or_err(optarg,
						  _("invalid offset argument"));
			break;
		case 'V':
			print_version(EXIT_SUCCESS);
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if (optind == argc && fd == -1) {
		warnx(_("no file specified"));
		errtryhelp(EXIT_FAILURE);
	}

	if (argc - optind > 0 && fd != -1) {
		warnx(_("specify either file descriptor or file name"));
		errtryhelp(EXIT_FAILURE);
	}

	if (argc - optind > 1) {
		warnx(_("specify one file descriptor or file name"));
		errtryhelp(EXIT_FAILURE);
	}

	if (fd == -1) {
		fd = open(argv[optind], O_RDONLY);
		if (fd < 0)
			err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
		do_close = true;
	}

	rc = posix_fadvise(fd,
			   offset, len,
			   advice);
	if (rc != 0)
		warnx(_("failed to advise: %s"), strerror(rc));

	if (do_close)
		close(fd);

	return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}