Commit Graph

48 Commits

Author SHA1 Message Date
Michael Forney
f1c3fd4e44 Remove unnecessary initializers of recursor members 2020-06-23 02:20:41 -07:00
Michael Forney
edbcc223ea libutil/recurse: Use a single path buffer, and directory fd
This way, we don't use PATH_MAX bytes on the stack per path component,
and don't have to keep copying the complete path around.
2020-03-05 00:45:53 -08:00
Michael Forney
830ca05c25 du: Don't print 0 entry if stat of named file fails
Previously:

	$ du doesntexist
	du: lstat doesntexist: No such file or directory
	0       doesntexist

Now:

	$ du doesntexist
	du: lstat doesntexist: No such file or directory

Also, just call nblks one time.
2017-07-03 21:03:04 +02:00
Michael Forney
a5612b0d08 Remove st != NULL checks from recursor functions
In the description of 3111908b03, it says
that the functions must be able to handle st being NULL, but recurse
always passes a valid pointer. The only function that was ever passed
NULL was rm(), but this was changed to go through recurse in
2f4ab52739, so now the checks are
pointless.
2017-07-03 21:03:02 +02:00
sin
1be832320c Fix type mismatches
Thanks to Random832 for the patch.
2015-11-26 10:35:46 +00:00
sin
2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00:00
Evan Gates
72d6553b12 include stdint.h for SIZE_MAX 2015-04-29 22:25:03 +01:00
sin
63d863ae53 du: Use off_t instead of uintmax_t 2015-04-29 16:42:49 +01:00
Dionysis Grigoropoulos
a04da01160 du: Use uintmax_t instead of size_t
Fixes wraparound for files greater than 4G on 32-bit systems.
2015-04-28 11:36:58 +01:00
Hiltjo Posthuma
e73f58937d du: staticize du 2015-04-25 11:43:14 +01:00
FRIGN
7b2465c101 Add maxdepth to recurse()
This also makes more sense.
2015-04-20 11:12:40 +01:00
Dionysis Grigoropoulos
ada69106b7 du: Reset size counter for each file read 2015-04-06 08:57:46 +01:00
FRIGN
d8e829a88d Solve little output issue in du(1)
Don't run the size throug nblks() twice.
2015-04-06 08:57:46 +01:00
FRIGN
11e2d472bf Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-05 09:13:56 +01:00
Hiltjo Posthuma
d583d12300 du: fix possible division by zero if $BLOCKSIZE is 0
these kind of $BLOCKSIZE dont make sense, but dont crash atleast.
2015-03-30 19:58:06 +02:00
FRIGN
3111908b03 Refactor recurse() again
Okay, why yet another recurse()-refactor?
The last one added the recursor-struct, which simplified things
on the user-end, but there was still one thing that bugged me a lot:
Previously, all fn()'s were forced to (l)stat the paths themselves.
This does not work well when you try to keep up with H-, L- and P-
flags at the same time, as each utility-function would have to set
the right function-pointer for (l)stat every single time.

This is not desirable. Furthermore, recurse should be easy to use
and not involve trouble finding the right (l)stat-function to do it
right.
So, what we needed was a stat-argument for each fn(), so it is
directly accessible. This was impossible to do though when the
fn()'s are still directly called by the programs to "start" the
recurse.
Thus, the fundamental change is to make recurse() the function to
go, while designing the fn()'s in a way they can "live" with st
being NULL (we don't want a null-pointer-deref).

What you can see in this commit is the result of this work. Why
all this trouble instead of using nftw?
The special thing about recurse() is that you tell the function
when to recurse() in your fn(). You don't need special flags to
tell nftw() to skip the subtree, just to give an example.

The only single downside to this is that now, you are not allowed
to unconditionally call recurse() from your fn(). It has to be
a directory.
However, that is a cost I think is easily weighed up by the
advantages.

Another thing is the history: I added a procedure at the end of
the outmost recurse to free the history. This way we don't leak
memory.

A simple optimization on the side:

-		if (h->dev == st.st_dev && h->ino == st.st_ino)
+		if (h->ino == st.st_ino && h->dev == st.st_dev)

First compare the likely difference in inode-numbers instead of
checking the unlikely condition that the device-numbers are
different.
2015-03-19 01:08:19 +01:00
FRIGN
9fd4a745f8 Add history and config-struct to recurse
For loop detection, a history is mandatory. In the process of also
adding a flexible struct to recurse, the recurse-definition was moved
to fs.h.
The motivation behind the struct is to allow easy extensions to the
recurse-function without having to change the prototypes of all
functions in the process.
Adding flags is really simple as well now.

Using the recursor-struct, it's also easier to see which defaults
apply to a program (for instance, which type of follow, ...).

Another change was to add proper stat-lstat-usage in recurse. It
was wrong before.
2015-03-13 00:29:48 +01:00
FRIGN
3b187f4826 Only call recurse() when path points to a directory in du(1)
This improves performance by ~30%.
2015-03-12 13:29:12 +01:00
FRIGN
01de5df8e6 Audit du(1) and refactor recurse()
While auditing du(1) I realized that there's no way the over 100 lines
of procedures in du() would pass the audit.
Instead, I decided to rewrite this section using recurse() from libutil.
However, the issue was that you'd need some kind of payload to count
the number of bytes in the subdirectories and use them in the higher
hierarchies.
The solution is to add a "void *data" data pointer to each recurse-
function-prototype, which we might also be able to use in other
recurse-applications.
recurse() itself had to be augmented with a recurse_samedev-flag, which
basically prevents recurse from leaving the current device.

Now, let's take a closer look at the audit:
1) Removing the now unnecessary util-functions push, pop, xrealpath,
   rename print() to printpath(), localize some global variables.
2) Only pass the block count to nblks instead of the entire stat-
   pointer.
3) Fix estrtonum to use the minimum of LLONG_MAX and SIZE_MAX.
4) Use idiomatic argv+argc-loop
5) Report proper exit-status.
2015-03-11 23:21:52 +01:00
FRIGN
8c041cd115 Don't terminate du(1) if chdir(2) fails 2015-02-18 22:04:32 +01:00
Quentin Rameau
593effc7c8 Add -x support for du(1) 2015-02-18 20:40:34 +00:00
sin
6c31f1d2a8 du: Update usage and document -P as well 2015-02-17 16:14:31 +00:00
sin
8f068589fb Fix recurse() prototype and convert char to int flags 2015-02-16 16:23:12 +00:00
FRIGN
31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
Tai Chi Minh Ralph Eastwood
1d2d28a8e4 du.c: add symlink dereferencing flags -H and -L 2015-02-09 22:54:53 +00:00
FRIGN
27b770c02c Adjust some limits to more flexibility for strtonum 2015-02-01 01:24:03 +01:00
FRIGN
fd562481f3 Convert estrto{l, ul} to estrtonum
Enough with this insanity!
2015-01-30 16:52:44 +01:00
Hiltjo Posthuma
549669e657 du: fix eprintf parameter 2014-12-22 10:34:29 +00:00
FRIGN
ec8246bbc6 Un-boolify sbase
It actually makes the binaries smaller, the code easier to read
(gems like "val == true", "val == false" are gone) and actually
predictable in the sense of that we actually know what we're
working with (one bitwise operator was quite adventurous and
should now be fixed).

This is also more consistent with the other suckless projects
around which don't use boolean types.
2014-11-14 10:54:20 +00:00
FRIGN
7d2683ddf2 Sort includes and more cleanup and fixes in util/ 2014-11-14 10:54:10 +00:00
FRIGN
eee98ed3a4 Fix coding style
It was about damn time. Consistency is very important in such a
big codebase.
2014-11-13 18:08:43 +00:00
Hiltjo Posthuma
2cf82f4c16 du: add -d flag to specify the max depth to show files or directories
we don't allow to use it with -s (like GNU du). busybox allows it.
2014-10-18 23:57:00 +01:00
Hiltjo Posthuma
b6b8fe9591 separate humansize into a util function
also show 1 decimal of human size string like: 4M -> 4.4M
2014-10-18 23:56:51 +01:00
sin
4608d91c6d Add human readable output to du(1)
Thanks Jeffrey Picard!
2014-10-16 10:07:17 +01:00
sin
0c5b7b9155 Stop using EXIT_{SUCCESS,FAILURE} 2014-10-02 23:46:59 +01:00
Hiltjo Posthuma
eac0f658cf check snprintf error aswell, handle as truncation error
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-06-01 18:02:55 +01:00
dwts
459161481a use always static for usage and keep usage definition above main 2014-04-22 14:49:23 +01:00
sin
94e97f19cd No need to use enprintf() with EXIT_FAILURE
eprintf() does just that.
2014-01-30 14:55:26 +00:00
sin
5be9c21ce4 Use xrealpath() in du(1) 2014-01-23 21:17:24 +00:00
sin
3ef662c988 Check snprintf() return value 2014-01-23 21:17:08 +00:00
sin
b8edf3b4ee Add weprintf() and replace fprintf(stderr, ...) calls
There is still some programs left to be updated for this.

Many of these programs would stop on the first file that they
could not open.
2013-11-13 11:41:43 +00:00
sin
0690c1a003 Always round up to the next blocksize unit in du(1) 2013-10-18 16:42:00 +01:00
sin
a6e5696cbd Fix indentation in du(1) 2013-10-17 18:11:20 +01:00
sin
9e321b69d2 No need for realpath() to call malloc() in du(1)
Print links correctly as well.
2013-10-17 14:15:06 +01:00
sin
ac130cbbe0 Update usage line and manpage for du(1)
Do not allow -a and -s to be specified at the same time.
2013-10-17 11:27:42 +01:00
sin
ac3a5e0091 Add -k support for du(1)
This setting overrides the BLOCKSIZE environment variable.
2013-10-16 19:22:55 +01:00
sin
6e8b79ebd8 Add -s support for du(1) 2013-10-16 19:22:46 +01:00
sin
09fcbfc338 Add primitive du(1)
Defaults to a 512-byte blocksize.
2013-10-16 18:18:02 +01:00