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 void apply_fadvise(const char *path
, off_t offset
, off_t len
, int advice
)
19 if ((fd
= open(path
, O_RDONLY
)) < 0) {
20 fprintf(stderr
, "%s: open(): %s\n", path
, strerror(errno
));
25 /* for compatibility with kernels < 2.6.6 */
28 if (fstat(fd
, &sb
) < 0) {
29 fprintf(stderr
, "%s: fstat(%d): %s\n",
30 path
, fd
, strerror(errno
));
36 if (posix_fadvise(fd
, offset
, len
, advice
) < 0)
39 "offset=%lu,len=%lu,advice=%d): %s\n",
41 (unsigned long)offset
,
43 advice
, strerror(errno
));
49 int main(int argc
, char * const argv
[])
53 int advice
= POSIX_FADV_NORMAL
;
57 while ((opt
= getopt(argc
, argv
, "o:l:a:")) != -1) {
64 offset
= strtol(optarg
, &err
, 10);
65 if (*err
|| offset
< 0) {
66 fprintf(stderr
, "offset must be >= 0\n");
71 len
= strtol(optarg
, &err
, 10);
72 if (*err
|| len
< 0) {
73 fprintf(stderr
, "length must be >= 0\n");
78 for (i
= 0; i
< ARRAY_SIZE(advice_table
); ++i
) {
79 if (strcmp(advice_table
[i
].name
, optarg
))
81 advice
= advice_table
[i
].nr
;
84 if (i
< ARRAY_SIZE(advice_table
))
86 fprintf(stderr
, "advice '%s' not understood\n", optarg
);
90 "Usage: %s [-a advice] [-o offset] "
91 "[-l length] <files>\n", argv
[0]);
92 fprintf(stderr
, "\nPossible values for advice:\n");
93 for (i
= 0; i
< ARRAY_SIZE(advice_table
); ++i
)
94 fprintf(stderr
, "\t%s\n", advice_table
[i
].name
);
99 for (; argi
< argc
; ++argi
)
100 apply_fadvise(argv
[argi
], offset
, len
, advice
);