From 74a13c645d6df961e69744ffac3528348078b6b7 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Tue, 11 Apr 2017 10:17:50 +0530 Subject: [PATCH] Maximize rlimit, switch to detail view in du mode --- Makefile | 2 +- nnn.c | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 4800e8d4..a1dcb6e0 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION = 0.6 PREFIX = /usr/local MANPREFIX = $(PREFIX)/man -CFLAGS += -O3 -march=native -Wall -Wextra +CFLAGS += -O3 -march=native -Wall -Wextra -Wno-unused-parameter LDLIBS = -lcurses DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE diff --git a/nnn.c b/nnn.c index 78d159c1..9dc7afbc 100644 --- a/nnn.c +++ b/nnn.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -123,6 +124,7 @@ static char *fallback_opener; static char *copier; static off_t blk_size; static size_t fs_free; +static int open_max; static const double div_2_pow_10 = 1.0 / 1024.0; static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"}; @@ -167,6 +169,28 @@ xrealloc(void *p, size_t size) } #endif +static rlim_t +max_openfds() +{ + struct rlimit rl; + rlim_t limit; + + limit = getrlimit(RLIMIT_NOFILE, &rl); + if (limit != 0) + return 32; + + limit = rl.rlim_cur; + rl.rlim_cur = rl.rlim_max; + + if (setrlimit(RLIMIT_NOFILE, &rl) == 0) + return rl.rlim_max - 64; + + if (limit > 128) + return limit - 64; + + return 32; +} + static size_t xstrlcpy(char *dest, const char *src, size_t n) { @@ -936,14 +960,11 @@ show_help(void) } static int -sum_sizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) +sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - if (!fpath || !ftwbuf) - printmsg("fpath or ftwbuf NULL"); /* TODO: on %s", fpath); */ - /* Handle permission problems */ if(typeflag == FTW_NS) { - printmsg("No stats (permissions ?)"); /* TODO: on %s", fpath); */ + printmsg("No stats (permissions ?)"); return 0; } @@ -1023,8 +1044,8 @@ dentfill(char *path, struct entry **dents, while ((dp = readdir(dirp)) != NULL) { /* Skip self and parent */ - if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || - (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == '\0')) + if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || + (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))) continue; if (filter(re, dp->d_name) == 0) continue; @@ -1042,8 +1063,8 @@ dentfill(char *path, struct entry **dents, if (bsizeorder) { if (S_ISDIR(sb.st_mode)) { blk_size = 0; - if (nftw(newpath, sum_sizes, 128, FTW_MOUNT | FTW_PHYS) == -1) { - printmsg("nftw(3) failed"); /* TODO: , newpath); */ + if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) { + printmsg("nftw(3) failed"); (*dents)[n].bsize = sb.st_blocks; } else (*dents)[n].bsize = blk_size; @@ -1507,6 +1528,10 @@ nochange: goto begin; case SEL_BSIZE: bsizeorder = !bsizeorder; + if (bsizeorder) { + showdetail = 1; + printptr = &printent_long; + } mtimeorder = 0; sizeorder = 0; /* Save current */ @@ -1619,6 +1644,8 @@ main(int argc, char *argv[]) } } + open_max = max_openfds(); + if (getuid() == 0) showhidden = 1; initfilter(showhidden, &ifilter);