emptybig — find large files that are mostly zeros

emptybig — find large files that are mostly zeros

emptybig finds large files that are mostly zeros — sparse files, and files somebody wrote full of zeroes and forgot about. A file flagged here reserves far more space than it holds, so it is a candidate for truncation, punching holes with fallocate -d, or deletion. This is a LightGUI front-end for the emptybig utility from util_box, and one of the 50 apps in util_boxgui. The full source is apps/c/emptybig/emptybig.cpp in that repo.

Give it a directory, a minimum size and a minimum zero percentage, press Scan, and the table lists every file that qualifies, largest first, with its size, its on-disk size, and the percentage of sampled bytes that are zero. The app scans once on launch, so the table is populated the moment the window appears.

The GUI layout

emptybig running under Xvfb
emptybig running — the default scan of /tmp/emptybig-test, captured under Xvfb.

The window is 900×640 and holds nine widgets, all placed by hand in code:

  • Path: a GtkLabel and a GtkEntry holding the directory tree to scan, pre-filled with /tmp/emptybig-test — a test fixture, not a real directory (see below).
  • Min size: a GtkLabel and a GtkEntry holding the smallest file worth considering, default 1M. It accepts K, M and G suffixes, same as the CLI.
  • Min zero %: a GtkLabel and a GtkSpinButton (range 0–100, step 1, page step 10, default 50). A non-sparse file is flagged when at least this percent of its sampled bytes are zero.
  • Scan: a GtkButton that re-runs the scan with the current path and thresholds.
  • Status line: a GtkLabel under the controls that mirrors the CLI's header, for example large mostly-empty files in /tmp/emptybig-test (min size 1.0M, min zero 50%):.
  • The table: a TStringGrid with four columns — SIZE (90 px), ON DISK (90 px), ZERO% (70 px), FILE (618 px) — and a fixed header row, showing the same columns as the CLI.
  • Summary line: a GtkLabel at the bottom with the hit count and the apparent waste — size minus on-disk bytes, summed over all hits — for example 2 file(s); apparent waste vs on-disk: 5.0M.

Two details worth knowing before you use it: the grid shows at most 200 rows, but the summary line always reports the full hit count; and the LightGUI GtkEntry widget does not implement Ctrl+A select-all, so clear the Path field with End + Backspace before typing a new path.

The source, in sections

emptybig.cpp is about 295 lines. The detection logic is a direct port of util_box's src/emptybig.c — same scan, same tests, same output values — and the GUI is a thin shell around it.

Detecting a mostly-empty file. Only regular files are considered; symlinks are skipped and directories are walked recursively. A file is sparse when its on-disk blocks use less than half its size (st_blocks * 512 * 2 < st_size); sparse files are flagged with ZERO% 100 and never sampled. Otherwise up to 16 blocks of 64 KiB, spread evenly across the file, are sampled, and the file is flagged when the zero fraction reaches the threshold:

/* sample up to 16 blocks spread across the file; return zero fraction 0..100, or -1 */
int zero_fraction(const char* path, long long size) {
    int fd = open(path, O_RDONLY);
    if (fd < 0) return -1;
    static char buf[65536];
    int nblocks = size < 16LL * 65536 ? (int)(size / 65536) + 1 : 16;
    if (nblocks < 1) nblocks = 1;
    long long zeros = 0, sampled = 0;
    for (int i = 0; i < nblocks; i++) {
        off_t off;
        if (nblocks == 1) off = 0;
        else off = (off_t)((long long)i * (size - 65536) / (nblocks - 1));
        if (off < 0) off = 0;
        if (lseek(fd, off, SEEK_SET) < 0) continue;
        ssize_t r = read(fd, buf, sizeof buf);
        if (r <= 0) continue;
        for (ssize_t k = 0; k < r; k++) if (buf[k] == 0) zeros++;
        sampled += r;
    }
    close(fd);
    if (!sampled) return -1;
    return (int)(zeros * 100 / sampled);
}

Sampling instead of reading the whole file is the point: a 5 GB sparse file is judged from 16 blocks, not 5 GB of reads. Files below the minimum size are skipped before any of this runs, and the walk itself is the same recursive opendir/readdir pattern as the CLI, with symlinks skipped and unreadable directories silently ignored. Results are sorted by size, largest first, and sizes use the CLI's human formatting (5.0M, 2.0M, 0B). The CLI flags map one-to-one onto the GUI: [PATH] is the Path field, --min-size SIZE is Min size, --min-zero PCT is Min zero %.

Filling the table. The refresh() lambda is the whole GUI-to-logic bridge: it reads the three inputs, runs the scan, writes the status line, fills the grid, and writes the summary — all in one place, so the Scan button and the launch-time scan share exactly the same code path:

auto refresh = [&]() {
    std::string path = entPath.get_text();
    if (path.empty()) path = ".";
    g_minsize = parse_size(entMinSize.get_text().c_str());
    if (g_minsize < 0) g_minsize = 0;
    g_minzero = spinZero.get_value_as_int();
    run_scan(path);
    char hb[32];
    human(g_minsize, hb, sizeof hb);
    char status[1024];
    snprintf(status, sizeof status,
             "large mostly-empty files in %s (min size %s, min zero %d%%):",
             path.c_str(), hb, g_minzero);
    lblStatus.set_text(status);
    const size_t shown = std::min<size_t>(g_hits.size(), 200);
    grid.set_row_count((int)shown + 1);
    grid.set_cell(0, 0, "SIZE");
    grid.set_cell(1, 0, "ON DISK");
    grid.set_cell(2, 0, "ZERO%");
    grid.set_cell(3, 0, "FILE");
    for (size_t i = 0; i < shown; i++) {
        char sb[32], db[32], zb[16];
        human(g_hits[i].size, sb, sizeof sb);
        human(g_hits[i].blocks_bytes, db, sizeof db);
        snprintf(zb, sizeof zb, "%d%%", g_hits[i].zero);
        grid.set_cell(0, (int)i + 1, sb);
        grid.set_cell(1, (int)i + 1, db);
        grid.set_cell(2, (int)i + 1, zb);
        grid.set_cell(3, (int)i + 1, g_hits[i].path);
    }
    char rb[32];
    human(total_waste(), rb, sizeof rb);
    char summary[256];
    snprintf(summary, sizeof summary,
             "%zu file(s); apparent waste vs on-disk: %s",
             g_hits.size(), rb);
    lblSummary.set_text(summary);
};

The grid's first row is the fixed header; rows after it are the hits, capped at 200. The summary uses the full hit count, not the capped one, so the waste figure is never understated. The Scan button's on_click is just refresh(), and refresh() is also called once before the event loop starts, so the window opens populated. The event loop itself is the standard XFrame pattern — XNextEvent, dispatch to each widget's on_button_press / on_button_release / on_motion / on_key, redraw when handled, close the window to quit. Note the widget choice: where bigfind uses a TListView, emptybig uses a TStringGrid — a grid with a fixed header row fits a table whose first row is a header, and the cell API (set_cell(col, row, text)) maps directly onto the CLI's column layout.

Building and running it

One make builds it — a single g++ line against the LightGUI checkout (header-only widgets on raw X11). It needs g++, libx11-dev and the LightGUI checkout. Run it under a real X server, or under Xvfb (display :101 is the one used for this app). The default path is /tmp/emptybig-test, a fixture rather than a real directory: no directory on the build machine with real matches was found (a scan of /root found 0 files), so the default points at a fixture with known sparse files:

cd apps/c/emptybig
make
# g++ -std=c++17 -Wall -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets emptybig.cpp -lX11 -o emptybig

Xvfb :101 -screen 0 1024x768x24 &
DISPLAY=:101 ./emptybig

# Recreate the default test fixture:
mkdir -p /tmp/emptybig-test
truncate -s 5M /tmp/emptybig-test/sparse.bin
dd if=/dev/zero of=/tmp/emptybig-test/zeros.bin bs=64K count=32
dd if=/dev/urandom of=/tmp/emptybig-test/dense.bin bs=64K count=32

The include paths point at the LightGUI checkout on the build machine — on your own machine, point them at wherever you cloned LightGUI. sparse.bin (5 MB, nothing written) and zeros.bin (2 MB of zeros) are flagged; dense.bin (2 MB of random bytes) is correctly ignored. The CLI agrees exactly on this fixture:

large mostly-empty files in /tmp/emptybig-test (min size 1.0M, min zero 50%):
      SIZE    ON DISK  ZERO%  FILE
      5.0M         0B   100%  /tmp/emptybig-test/sparse.bin
      2.0M       2.0M   100%  /tmp/emptybig-test/zeros.bin
2 file(s); apparent waste vs on-disk: 5.0M

What the CLI version does differently

Almost nothing, by design. The detection logic is a 1:1 port — same sparse test, same 16-block sampling, same sort, same human formatting — and the CLI agrees exactly on the fixture above. The differences are presentation: the CLI prints a header and a table to stdout, the GUI puts the same header in the status line and the same table in a grid, with the waste total in a summary line instead of a trailing line. The GUI caps the visible grid at 200 rows (the CLI prints everything); the summary always reports the full count. And the GUI's Path field has the one LightGUI quirk noted above: no Ctrl+A, so clear it with End + Backspace.

See also