Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add human readable removed files size
From 17191a1c1850668d756d8d25ce15bed68e306d00 Mon Sep 17 00:00:00 2001
From: Fredrik Strandin <fredrik@strandin.name>
Date: Thu, 24 Aug 2017 15:05:44 +0200
Subject: [PATCH] Add human readable removed files size
---
.SRCINFO | 8 +++-----
PKGBUILD | 6 +++---
pkgcacheclean.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/.SRCINFO b/.SRCINFO
index 0012b29..75e47af 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,16 +1,14 @@
-# Generated by mksrcinfo v8
-# Sun May 21 07:52:19 UTC 2017
pkgbase = pkgcacheclean
pkgdesc = Application to clean the pacman cache
- pkgver = 1.8.2
- pkgrel = 5
+ pkgver = 1.9.0
+ pkgrel = 1
url = https://bbs.archlinux.org/viewtopic.php?pid=841774
arch = any
license = GPL
depends = pacman>=5
source = pkgcacheclean.c
source = pkgcacheclean.8
- md5sums = d79fac7e27381249774fc8e6f15d7f2e
+ md5sums = 111a5c26c4c6536e1fda310fbd8cb824
md5sums = 965889f755e4611c12f8c9ac0048372d
pkgname = pkgcacheclean
diff --git a/PKGBUILD b/PKGBUILD
index 2913490..9eeb720 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -1,8 +1,8 @@
# Maintainer: Auguste Pop <auguste [at] gmail [dot] com>
pkgname=pkgcacheclean
-pkgver=1.8.2
-pkgrel=5
+pkgver=1.9.0
+pkgrel=1
pkgdesc="Application to clean the pacman cache"
arch=('any')
url='https://bbs.archlinux.org/viewtopic.php?pid=841774'
@@ -10,7 +10,7 @@ license=('GPL')
depends=('pacman>=5')
source=($pkgname.c
$pkgname.8)
-md5sums=('d79fac7e27381249774fc8e6f15d7f2e'
+md5sums=('111a5c26c4c6536e1fda310fbd8cb824'
'965889f755e4611c12f8c9ac0048372d')
build()
diff --git a/pkgcacheclean.c b/pkgcacheclean.c
index 90c3d79..36996df 100644
--- a/pkgcacheclean.c
+++ b/pkgcacheclean.c
@@ -15,6 +15,15 @@
#define DBPATH "/var/lib/pacman/"
#define CACHEDIR "/var/cache/pacman/pkg/"
+/*
+ * This is a very stupid and simple limit of the longest human readable file
+ * size. Maximum file size that will be representable will be:
+ * 999.9 YiB\0
+ * which "should be enough".
+ */
+#define LONGEST_HUMAN_READABLE 10
+#define NUM_HUMAN_READABLE_UNITS 9
+
const char *argp_program_version = "pkgcacheclean "VERSION;
const char *argp_program_bug_address = "auguste@gmail.com";
@@ -35,6 +44,7 @@ static struct argp_option options[] =
{ .name = "all-as-installed", .key = 'k',
.doc = "Treat not-installed packages as installed" },
{ .name = "verbose", .key = 'v', .doc = "Verbose output" },
+ { .name = "human-readable", .key = 'h', .doc = "Human readable file size" },
{ .name = "quiet", .key = 'q', .doc = "Suppress output, default" },
{ .doc = NULL }
};
@@ -55,6 +65,7 @@ struct arguments
int preserve;
int keep;
int verbose;
+ int human_readable;
char *cachedir;
};
@@ -143,6 +154,33 @@ static off_t get_file_size(const char *filename)
return st.st_size;
}
+static void human_readable(off_t total_size, char* buf)
+{
+ const char* units[NUM_HUMAN_READABLE_UNITS] = {
+ "B",
+ "KiB",
+ "MiB",
+ "GiB",
+ "TiB",
+ "PiB",
+ "EiB",
+ "ZiB",
+ "YiB"
+ };
+ unsigned char num_divisions = 0;
+ float human_size = total_size;
+
+ while (human_size > 1024 && num_divisions < (NUM_HUMAN_READABLE_UNITS - 1))
+ {
+ human_size /= 1024;
+ num_divisions++;
+ }
+
+ snprintf(buf, LONGEST_HUMAN_READABLE, "%.1f %s",
+ human_size,
+ units[num_divisions]);
+}
+
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *argument = (struct arguments *)(state -> input);
@@ -155,6 +193,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 'v':
argument->verbose = 1;
break;
+ case 'h':
+ argument->human_readable = 1;
+ break;
case 'k':
argument->keep = 1;
case 'q':
@@ -190,10 +231,12 @@ int main(const int argc, char ** __restrict__ argv)
struct pkginfo **hit = NULL;
const char *current = "", *name;
char cachedir[PATH_MAX] = CACHEDIR;
+ char human_readable_buf[LONGEST_HUMAN_READABLE];
struct argp arg_parser = { .options = options, .parser = parse_opt,
.args_doc = args_doc, .doc = doc };
struct arguments args = { .dry_run = 0, .preserve = 0, .keep = 0,
- .verbose = 0, .cachedir = NULL };
+ .verbose = 0, .human_readable = 0,
+ .cachedir = NULL };
argp_parse(&arg_parser, argc, argv, 0, NULL, &args);
if (!args.preserve)
@@ -275,17 +318,21 @@ int main(const int argc, char ** __restrict__ argv)
{
strcpy(cachedir + len, cachepkg[i]->filename);
if (args.verbose)
- {
printf("remove: %s\n", cachepkg[i]->filename);
+ if (args.verbose || args.human_readable)
total_size += get_file_size(cachedir);
- }
if (!args.dry_run)
unlink(cachedir);
}
}
}
- if (args.verbose)
+ if (args.human_readable)
+ {
+ human_readable(total_size, human_readable_buf);
+ printf("\ntotal: %s\n", human_readable_buf);
+ }
+ else if (args.verbose)
printf("\ntotal: %"PRIuMAX" bytes\n", (uintmax_t)total_size);
free_pkginfo_array(cachepkg, n);
--
2.14.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.