Files
cnmcdee 05b33a9c33 Pilot batch: banner, bigfind, emptybig LightGUI apps + repo index and MIT license
First 3 of the 5 pilot apps, all built and screenshot-tested on hot_dallas:
- banner (Python): text + font combo + scrolling output, 12/12 GUI-vs-CLI
- bigfind (C++): top-N largest files table, exact CLI match
- emptybig (C++): sparse-file finder, exact CLI match on fixture
Top-level README indexes all 50 drawn apps; LICENSE is MIT (util_box
itself is unlicensed; MIT matches lzply and lightgui).
datecalc and certcheck are blocked pending execution-risk approval.
2026-12-05 04:38:49 +00:00

296 lines
11 KiB
C++

// emptybig - LightGUI front end for the util_box emptybig utility.
// Finds large files that are mostly zeros (sparse / zero-filled files)
// under a path, using the same detection logic as the CLI:
// * only regular files at least --min-size (default 1M) are considered
// * a file is "sparse" when its on-disk blocks use less than half its size
// * otherwise up to 16 64 KiB blocks spread across the file are sampled
// and the file is flagged when the zero fraction is >= --min-zero %
// Results are sorted by size, largest first, with SIZE / ON DISK / ZERO%
// columns identical to the CLI output.
// Build: make (see Makefile)
// Run: ./emptybig (under Xvfb or a real X server)
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#include "GtkButton/GtkButton.h"
#include "GtkEntry/GtkEntry.h"
#include "GtkLabel/GtkLabel.h"
#include "GtkSpinButton/GtkSpinButton.h"
#include "TStringGrid/TStringGrid.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
namespace {
// ---- CLI logic, ported 1:1 from /root/fileutils/src/emptybig.c ----
long long g_minsize = 1024 * 1024;
int g_minzero = 50; /* percent */
void human(long long b, char* out, size_t n) {
const char* u[] = {"B", "K", "M", "G", "T"};
double v = (double)b;
int i = 0;
while (v >= 1024 && i < 4) { v /= 1024; i++; }
if (i == 0) snprintf(out, n, "%lldB", b);
else snprintf(out, n, "%.1f%s", v, u[i]);
}
/* 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);
}
struct Hit {
std::string path;
long long size;
long long blocks_bytes;
int zero;
};
std::vector<Hit> g_hits;
void walk(const char* path) {
DIR* d = opendir(path);
if (!d) return;
struct dirent* e;
while ((e = readdir(d))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
char full[PATH_MAX];
snprintf(full, sizeof full, "%s/%s", path, e->d_name);
struct stat st;
if (lstat(full, &st)) continue;
if (S_ISLNK(st.st_mode)) continue;
if (S_ISREG(st.st_mode)) {
if (st.st_size < g_minsize) continue;
long long disk = (long long)st.st_blocks * 512;
int sparse = st.st_size > 0 && disk * 2 < st.st_size; /* uses < half its size on disk */
int z = -1;
if (!sparse) z = zero_fraction(full, st.st_size);
if (sparse || z >= g_minzero)
g_hits.push_back({full, st.st_size, disk, sparse ? 100 : z});
} else if (S_ISDIR(st.st_mode)) {
walk(full);
}
}
closedir(d);
}
long long parse_size(const char* s) {
char* e;
long long v = strtoll(s, &e, 10);
if (*e == 75 || *e == 107) v *= 1024LL;
else if (*e == 77 || *e == 109) v *= 1024LL * 1024;
else if (*e == 71 || *e == 103) v *= 1024LL * 1024 * 1024;
return v;
}
void run_scan(const std::string& path) {
g_hits.clear();
walk(path.c_str());
std::sort(g_hits.begin(), g_hits.end(),
[](const Hit& a, const Hit& b) { return a.size > b.size; });
}
long long total_waste() {
long long w = 0;
for (const auto& h : g_hits) w += h.size - h.blocks_bytes;
return w;
}
} // namespace
int main() {
Display* display = XOpenDisplay(nullptr);
if (!display) return 1;
int screen = DefaultScreen(display);
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
100, 100, 900, 640, 1,
BlackPixel(display, screen),
WhitePixel(display, screen));
XStoreName(display, window, "emptybig - util_boxgui");
XSelectInput(display, window,
ExposureMask | KeyPressMask | StructureNotifyMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
XMapWindow(display, window);
GC gc = DefaultGC(display, screen);
SStyle_Manager styles(display, window, screen, gc);
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.set_active_style_byName("default");
GtkLabel lblPath(display, screen, window, gc, &styles, 16, 18, 40, 20);
lblPath.set_text("Path:");
GtkEntry entPath(display, screen, window, gc, &styles, 60, 12, 420, 30);
entPath.set_text("/tmp/emptybig-test");
GtkLabel lblMinSize(display, screen, window, gc, &styles, 496, 18, 70, 20);
lblMinSize.set_text("Min size:");
GtkEntry entMinSize(display, screen, window, gc, &styles, 570, 12, 70, 30);
entMinSize.set_text("1M");
GtkLabel lblMinZero(display, screen, window, gc, &styles, 656, 18, 80, 20);
lblMinZero.set_text("Min zero %:");
GtkSpinButton spinZero(display, screen, window, gc, &styles, 740, 12, 70, 30);
spinZero.set_range(0, 100);
spinZero.set_increments(1, 10);
spinZero.set_digits(0);
spinZero.set_value(50);
GtkButton btnScan(display, screen, window, gc, &styles, 824, 12, 60, 30);
btnScan.set_text("Scan");
GtkLabel lblStatus(display, screen, window, gc, &styles, 16, 52, 868, 20);
TStringGrid grid(display, screen, window, gc, &styles, 16, 80, 868, 520);
grid.set_col_count(4);
grid.set_col_width(0, 90);
grid.set_col_width(1, 90);
grid.set_col_width(2, 70);
grid.set_col_width(3, 618);
grid.set_fixed_rows(1);
GtkLabel lblSummary(display, screen, window, gc, &styles, 16, 610, 868, 20);
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);
};
btnScan.on_click = [&]() { refresh(); };
refresh(); // scan on launch so the table is populated immediately
auto draw_all = [&]() {
lblPath.draw();
entPath.draw();
lblMinSize.draw();
entMinSize.draw();
lblMinZero.draw();
spinZero.draw();
btnScan.draw();
lblStatus.draw();
grid.draw();
lblSummary.draw();
};
draw_all();
XFlush(display);
bool running = true;
while (running) {
XEvent event;
XNextEvent(display, &event);
bool handled = false;
switch (event.type) {
case Expose:
handled = true;
break;
case ButtonPress:
handled |= entPath.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= entMinSize.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= spinZero.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= btnScan.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= grid.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
break;
case ButtonRelease:
handled |= entPath.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= entMinSize.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= spinZero.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= btnScan.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
handled |= grid.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
break;
case MotionNotify:
handled |= entPath.on_motion(event.xmotion.x, event.xmotion.y);
handled |= entMinSize.on_motion(event.xmotion.x, event.xmotion.y);
handled |= spinZero.on_motion(event.xmotion.x, event.xmotion.y);
handled |= btnScan.on_motion(event.xmotion.x, event.xmotion.y);
handled |= grid.on_motion(event.xmotion.x, event.xmotion.y);
break;
case KeyPress: {
char buffer[32];
KeySym key = 0;
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
buffer[n] = 0;
handled |= entPath.on_key(key, buffer);
handled |= entMinSize.on_key(key, buffer);
handled |= spinZero.on_key(key, buffer);
handled |= btnScan.on_key(key, buffer);
handled |= grid.on_key(key, buffer);
break;
}
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
running = false;
break;
}
if (handled) {
XClearWindow(display, window);
draw_all();
XFlush(display);
}
}
XCloseDisplay(display);
return 0;
}