From 333dc83d52e014a0b532e316ea8cd93b048f1ac6 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker Date: Sat, 27 Jul 2013 14:25:28 +0200 Subject: du: add --inodes option This new option can be used to find directories with a huge amount of files. The GNU find utility has the printf format "%h" which prints the number of entries in a directory, but this is non-cumulative and doesn't handle hard links. * src/du.c (struct duinfo): Add new member for counting inodes. (duinfo_init): Initialize inodes member with Zero. (duinfo_set): Set inodes counter to 1. (duinfo_add): Sum up the 2 given inodes counters. (opt_inodes): Add new boolean flag to remember if the --inodes option has been specified. (INODES_OPTION): Add new enum value to be used ... (long_options): ... here. (usage): Add description of the new option. (print_size): Pass inodes counter or size to print_only_size, depending on the inodes mode. (process_file): Adapt threshold handling: with --inodes, print or elide the entries according to the struct member inodes. (main): Add a case for accepting the new INODES_OPTION. Print a warning diagnostic when --inodes is used together with the option --apparent-size or -b. Reset the output_block_size to 1 ... and thus ignoring the options -m and -k. * tests/du/inodes.sh: Add a new test. * tests/local.mk (all_tests): Mention it. * doc/coreutils.texi (du invocation): Document the new option. * NEWS: Mention the new option. --- tests/du/inodes.sh | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100755 tests/du/inodes.sh (limited to 'tests/du') diff --git a/tests/du/inodes.sh b/tests/du/inodes.sh new file mode 100755 index 000000000..2069e2b2b --- /dev/null +++ b/tests/du/inodes.sh @@ -0,0 +1,133 @@ +#!/bin/sh +# exercise du's --inodes option + +# Copyright (C) 2010-2013 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ du + +# An empty directory uses only 1 inode. +mkdir d || framework_failure_ +printf '1\td\n' > exp || framework_failure_ + +du --inodes d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Add a regular file: 2 inodes used. +touch d/f || framework_failure_ +printf '2\td\n' > exp || framework_failure_ + +du --inodes d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Add a hardlink to the file: still only 2 inodes used. +ln -v d/f d/h || framework_failure_ +du --inodes d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Now count also hardlinks (-l,--count-links): 3 inodes. +printf '3\td\n' > exp || framework_failure_ +du --inodes -l d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Create a directory and summarize: 3 inodes. +mkdir d/d || framework_failure_ +du --inodes -s d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Count inodes separated: 1-2. +printf '1\td/d\n2\td\n' > exp || framework_failure_ +du --inodes -S d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Count inodes cumulative (default): 1-3. +printf '1\td/d\n3\td\n' > exp || framework_failure_ +du --inodes d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Count all items: 1-1-3. +printf '1\td/d\n1\td/h\n3\td\n' > exp || framework_failure_ +du --inodes -a d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Count all items and hardlinks again: 1-1-1-4 +printf '1\td/d\n1\td/h\n1\td/f\n4\td\n' > exp || framework_failure_ +du --inodes -al d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Run with total (-c) line: 1-3-3 +printf '1\td/d\n3\td\n3\ttotal\n' > exp || framework_failure_ +du --inodes -c d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Create another file in the subdirectory: 2-4 +touch d/d/f || framework_failure_ +printf '2\td/d\n4\td\n' > exp || framework_failure_ +du --inodes d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Ensure human output (-h, --si) works. +rm -rf d || framework_failure_ +mkdir d || framework_failure_ +seq --format="d/file%g" 1023 | xargs touch || framework_failure_ +printf '1.0K\td\n' > exp || framework_failure_ +du --inodes -h d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +printf '1.1k\td\n' > exp || framework_failure_ +du --inodes --si d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Verify --inodes ignores -B. +printf '1024\td\n' > exp || framework_failure_ +du --inodes -B10 d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Verify --inodes works with --threshold. +printf '1024\td\n' > exp || framework_failure_ +du --inodes --threshold=1000 d > out 2>err || fail=1 +compare exp out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +du --inodes --threshold=-1000 d > out 2>err || fail=1 +compare /dev/null out || { cat out; fail=1; } +compare /dev/null err || fail=1 + +# Verify --inodes raises a warning for --apparent-size and -b. +du --inodes -b d > out 2>err || fail=1 +grep ' ineffective ' err >/dev/null || { fail=1; cat out err; } + +du --inodes --apparent-size d > out 2>err || fail=1 +grep ' ineffective ' err >/dev/null || { fail=1; cat out err; } + +# Ensure that --inodes is mentioned in the usage. +du --help > out || fail=1 +grep ' --inodes ' out >/dev/null || { fail=1; cat out; } +Exit $fail -- cgit v1.2.3-54-g00ecf