diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2023-01-25 13:29:29 +0100 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2023-01-25 13:29:29 +0100 |
commit | 4de60d28ca1a034d9713ed0fc84ae6f93b0ffcc4 (patch) | |
tree | 1ec78a3d5373632c291448d5e294e0816a6bb1e5 /dontneed.c | |
download | dontneed-4de60d28ca1a034d9713ed0fc84ae6f93b0ffcc4.tar.gz dontneed-4de60d28ca1a034d9713ed0fc84ae6f93b0ffcc4.tar.bz2 dontneed-4de60d28ca1a034d9713ed0fc84ae6f93b0ffcc4.zip |
Diffstat (limited to 'dontneed.c')
-rw-r--r-- | dontneed.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/dontneed.c b/dontneed.c new file mode 100644 index 0000000..29b25d8 --- /dev/null +++ b/dontneed.c @@ -0,0 +1,32 @@ +#include <fcntl.h> // posix_fadvise +#include <errno.h> // errno +#include <string.h> // strerror +#include <stdio.h> // fprintf +#include <unistd.h> // open,close + +int usage(char progname[]) { + fprintf(stderr, "Usage: %s <path>...\n", progname); + fprintf(stderr, " Calls posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) on each <path>\n\n"); + fprintf(stderr, " Tip: you may find all cached files from rootfs with the costly one-liner:\n"); + fprintf(stderr, " # find / -mount -type f -print0 | xargs -r0 fincore | grep -vE '^ *0B' | sort -hr | uniq | less\n"); + return 1; +} + +int main(int argc, char** argv) { + int i, fd, advise_errno, errors=0; + if ( argc < 2 ) return usage(argv[0]); + for (i=1; i<argc; i++) { + fd = open(argv[i], O_RDONLY); + if ( fd == -1 ) { + fprintf(stderr, "Can't open '%s': %s\n", argv[i], strerror(errno)); + errors++; continue; + } + advise_errno = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); + if ( advise_errno != 0 ) { + fprintf(stderr, "Can't posix_fadvise '%s' (%i): %s\n", argv[i], advise_errno, strerror(errno)); + errors++; //don't forget to close(fd) anyway + } + close(fd); + } + return (errors != 0); +} |