1 #include "compat-util.h"
3 static struct fa_advice
{
7 { "normal", POSIX_FADV_NORMAL
},
8 { "sequential", POSIX_FADV_SEQUENTIAL
},
9 { "random", POSIX_FADV_RANDOM
},
10 { "noreuse", POSIX_FADV_NOREUSE
},
11 { "willneed", POSIX_FADV_WILLNEED
},
12 { "dontneed" , POSIX_FADV_DONTNEED
}
15 static int usage(const char * argv0
)
20 "Usage: %s [-a ADVICE] [-o OFFSET] "
21 "[-l LENGTH] FILE...\n", argv0
);
22 fprintf(stderr
, "\nPossible values for advice:\n");
23 for (i
= 0; i
< ARRAY_SIZE(advice_table
); ++i
)
24 fprintf(stderr
, "\t%s\n", advice_table
[i
].name
);
29 static void apply_fadvise(const char *path
, off_t offset
, off_t len
, int advice
)
33 if ((fd
= open(path
, O_RDONLY
|O_NOATIME
)) < 0) {
34 if (errno
== EPERM
&& O_NOATIME
!= 0) {
35 fd
= open(path
, O_RDONLY
);
39 fprintf(stderr
, "%s: open(): %s\n", path
, strerror(errno
));
45 /* for compatibility with kernels < 2.6.6 */
48 if (fstat(fd
, &sb
) < 0) {
49 fprintf(stderr
, "%s: fstat(%d): %s\n",
50 path
, fd
, strerror(errno
));
56 if (posix_fadvise(fd
, offset
, len
, advice
) < 0)
59 "offset=%lu,len=%lu,advice=%d): %s\n",
61 (unsigned long)offset
,
63 advice
, strerror(errno
));
69 int main(int argc
, char * const argv
[])
73 int advice
= POSIX_FADV_NORMAL
;
77 while ((opt
= getopt(argc
, argv
, "o:l:a:h")) != -1) {
84 offset
= cstr_to_off_t(optarg
, &err
, 10);
85 if (*err
|| offset
< 0) {
86 fprintf(stderr
, "offset must be >= 0\n");
91 len
= cstr_to_off_t(optarg
, &err
, 10);
92 if (*err
|| len
< 0) {
93 fprintf(stderr
, "length must be >= 0\n");
98 for (i
= 0; i
< ARRAY_SIZE(advice_table
); ++i
) {
99 if (strcmp(advice_table
[i
].name
, optarg
))
101 advice
= advice_table
[i
].nr
;
104 if (i
< ARRAY_SIZE(advice_table
))
106 fprintf(stderr
, "advice '%s' not understood\n", optarg
);
109 return usage(argv
[0]);
114 return usage(argv
[0]);
116 for (; argi
< argc
; ++argi
)
117 apply_fadvise(argv
[argi
], offset
, len
, advice
);