1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /*
29 * Copyright (c) 2022 by Information2 Software, Inc. All rights reserved.
30 */
31
32 #include "file_common.h"
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <string.h>
37
38 /*
39 * Call fadvise to prefetch data
40 */
41 static const char *execname = "file_fadvise";
42
43 static void
usage(void)44 usage(void)
45 {
46 (void) fprintf(stderr,
47 "usage: %s -f filename -a advice \n", execname);
48 }
49
50 typedef struct advice_name {
51 const char *name;
52 int value;
53 } advice_name;
54
55 static const struct advice_name table[] = {
56 #define ADV(name) {#name, name}
57 ADV(POSIX_FADV_NORMAL),
58 ADV(POSIX_FADV_RANDOM),
59 ADV(POSIX_FADV_SEQUENTIAL),
60 ADV(POSIX_FADV_WILLNEED),
61 ADV(POSIX_FADV_DONTNEED),
62 ADV(POSIX_FADV_NOREUSE),
63 {NULL}
64 };
65
66 int
main(int argc,char * argv[])67 main(int argc, char *argv[])
68 {
69 char *filename = NULL;
70 int advice = POSIX_FADV_NORMAL;
71 int fd, ch;
72 int err = 0;
73
74 while ((ch = getopt(argc, argv, "a:f:")) != EOF) {
75 switch (ch) {
76 case 'a':
77 advice = -1;
78 for (const advice_name *p = table; p->name; ++p) {
79 if (strcmp(p->name, optarg) == 0)
80 advice = p->value;
81 }
82 break;
83 case 'f':
84 filename = optarg;
85 break;
86 case '?':
87 (void) printf("unknown arg %c\n", optopt);
88 usage();
89 break;
90 }
91 }
92
93 if (!filename) {
94 (void) printf("Filename not specified (-f <file>)\n");
95 err++;
96 }
97
98 if (advice == -1) {
99 (void) printf("advice is invalid\n");
100 err++;
101 }
102
103 if (err) {
104 usage(); /* no return */
105 return (1);
106 }
107
108 if ((fd = open(filename, O_RDWR, 0666)) < 0) {
109 perror("open");
110 return (1);
111 }
112
113 if (posix_fadvise(fd, 0, 0, advice) != 0) {
114 perror("posix_fadvise");
115 close(fd);
116 return (1);
117 }
118
119 close(fd);
120
121 return (0);
122 }
123