From f111c0d6ab664e51b4421d05d15aeabccfc4980e Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 8 May 2015 21:11:30 +0000 Subject: [PATCH] Limiting the number of backup files to one hundred thousand, well before finding a unused filename takes an annoying amount of time. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5225 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 4 ++++ src/files.c | 20 +++++++++----------- src/proto.h | 1 - src/utils.c | 15 --------------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 67b3665f..9eb69f13 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,10 @@ * src/browser.c (browser_refresh): Take the distant possibility of terabyte files into account, and in the bargain get rid of the need to calculate the number of digits in UINT_MAX. + * src/files.c (get_next_filename): Limit the number of backup files + to one hundred thousand -- which should be far more than enough -- + before finding a unused filename takes an annoying amount of time. + * src/utils.c (digits): Delete this now unneeded function. 2015-05-03 Benno Schulenberg * src/browser.c (browser_refresh): Display an ellipsis only when the diff --git a/src/files.c b/src/files.c index dd4b2c55..44550185 100644 --- a/src/files.c +++ b/src/files.c @@ -991,20 +991,17 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f) * extension exists, we return "". */ char *get_next_filename(const char *name, const char *suffix) { - static int ulmax_digits = -1; unsigned long i = 0; char *buf; - size_t namelen, suffixlen; + size_t wholenamelen; assert(name != NULL && suffix != NULL); - if (ulmax_digits == -1) - ulmax_digits = digits(ULONG_MAX); + wholenamelen = strlen(name) + strlen(suffix); - namelen = strlen(name); - suffixlen = strlen(suffix); - - buf = charalloc(namelen + suffixlen + ulmax_digits + 2); + /* Reserve space for: the name plus the suffix plus a dot plus + * possibly five digits plus a null byte. */ + buf = charalloc(wholenamelen + 7); sprintf(buf, "%s%s", name, suffix); while (TRUE) { @@ -1012,11 +1009,12 @@ char *get_next_filename(const char *name, const char *suffix) if (stat(buf, &fs) == -1) return buf; - if (i == ULONG_MAX) + + /* Limit the number of backup files to a hundred thousand. */ + if (++i == 100000) break; - i++; - sprintf(buf + namelen + suffixlen, ".%lu", i); + sprintf(buf + wholenamelen, ".%lu", i); } /* We get here only if there is no possible save file. Blank out diff --git a/src/proto.h b/src/proto.h index f90b18eb..337211d8 100644 --- a/src/proto.h +++ b/src/proto.h @@ -695,7 +695,6 @@ void do_wordlinechar_count(void); void do_verbatim_input(void); /* All functions in utils.c. */ -int digits(size_t n); void get_homedir(void); bool parse_num(const char *str, ssize_t *val); bool parse_line_column(const char *str, ssize_t *line, ssize_t *column); diff --git a/src/utils.c b/src/utils.c index 1d7899d8..9c936a5f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -30,21 +30,6 @@ #include #include -/* Return the number of decimal digits in n. */ -int digits(size_t n) -{ - int i; - - if (n == 0) - i = 1; - else { - for (i = 0; n != 0; n /= 10, i++) - ; - } - - return i; -} - /* Return the user's home directory. We use $HOME, and if that fails, * we fall back on the home directory of the effective user ID. */ void get_homedir(void)