mirror of
https://code.blicky.net/yorhel/ncdu.git
synced 2026-01-13 01:08:41 -09:00
Clip directory sizes to make sure they are positive
I realized that I used addparentstats() with negative values when removing stuff, so it had to be done this way (without rewriting everything). It's a simple solution, anyway.
This commit is contained in:
parent
ad84603bee
commit
28c0b58c2e
2 changed files with 8 additions and 5 deletions
|
|
@ -186,8 +186,8 @@ static void freedir_hlnk(struct dir *d) {
|
||||||
if(pt==par)
|
if(pt==par)
|
||||||
i=0;
|
i=0;
|
||||||
if(i) {
|
if(i) {
|
||||||
par->size -= d->size;
|
par->size = adds64(par->size, -d->size);
|
||||||
par->asize -= d->asize;
|
par->asize = adds64(par->size, -d->asize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,11 +81,14 @@ char *getpath(struct dir *);
|
||||||
/* returns the root element of the given dir struct */
|
/* returns the root element of the given dir struct */
|
||||||
struct dir *getroot(struct dir *);
|
struct dir *getroot(struct dir *);
|
||||||
|
|
||||||
/* Add two positive signed 64-bit integers. Returns INT64_MAX if the result
|
/* Add two signed 64-bit integers. Returns INT64_MAX if the result would
|
||||||
* would overflow.
|
* overflow, or 0 if it would be negative. At least one of the integers must be
|
||||||
|
* positive.
|
||||||
* I use uint64_t's to detect the overflow, as (a + b < 0) relies on undefined
|
* I use uint64_t's to detect the overflow, as (a + b < 0) relies on undefined
|
||||||
* behaviour, and (INT64_MAX - b >= a) didn't work for some reason. */
|
* behaviour, and (INT64_MAX - b >= a) didn't work for some reason. */
|
||||||
#define adds64(a, b) ((uint64_t)(a) + (uint64_t)(b) > (uint64_t)INT64_MAX ? INT64_MAX : (a)+(b))
|
#define adds64(a, b) ((a) > 0 && (b) > 0\
|
||||||
|
? ((uint64_t)(a) + (uint64_t)(b) > (uint64_t)INT64_MAX ? INT64_MAX : (a)+(b))\
|
||||||
|
: (a)+(b) < 0 ? 0 : (a)+(b))
|
||||||
|
|
||||||
/* Adds a value to the size, asize and items fields of *d and its parents */
|
/* Adds a value to the size, asize and items fields of *d and its parents */
|
||||||
void addparentstats(struct dir *, int64_t, int64_t, int);
|
void addparentstats(struct dir *, int64_t, int64_t, int);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue