Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Compile it using: valac --pkg gtk+-3.0 --pkg glib-2.0 --pkg gio-2.0 del-apt-cache.vala
- using Gtk;
- using GLib;
- private int64[] get_info_and_clean (File file, string space = "", Cancellable? cancellable = null) throws Error
- {
- int64 files = 0;
- int64 size = 0;
- int64[] data = new int64[2];
- Array<string> paths = new Array<string> ();
- FileInfo info = null;
- FileEnumerator enumerator;
- try {//This Try/Catch is to ignore the permissions of '/var/cache/apt/archives/partial'
- enumerator = file.enumerate_children (
- "standard::*",
- FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
- cancellable);
- } catch (IOError e) {
- stderr.printf ("WARNING: Unable to get size of dir '%s': %s\n", file.get_path (), e.message);
- data[0] = 0;
- data[1] = 0;
- return data;
- }
- while (cancellable.is_cancelled () == false && ((info = enumerator.next_file (cancellable)) != null)) {
- if (info.get_file_type () == FileType.DIRECTORY) {
- File subdir = file.resolve_relative_path (info.get_name ());
- get_info_and_clean (subdir, space + " ", cancellable);
- } else {
- files += 1;//Sum Files
- size += info.get_size ();//Accumulates Size
- paths.append_val (file.get_uri () + "/" + info.get_name ());
- }
- }
- if (cancellable.is_cancelled ()) {
- throw new IOError.CANCELLED ("Operation was cancelled");
- }
- data[0] = files;
- data[1] = size;
- File apt_file;
- for (int i = 0; i < paths.length; i++) {
- apt_file = File.new_for_uri (paths.index (i));
- stdout.printf ("FILE: %s", paths.index (i));
- try {
- apt_file.delete ();
- stdout.printf (" [DELETED]\n");
- } catch (Error e) {
- stdout.printf (" [ERROR: %s]\n\n", e.message);
- }
- }
- stdout.printf ("APT CACHE FILES: %s\n", files.to_string());
- stdout.printf ("APT CACHE SIZE: %s\n", size.to_string());
- return data;
- }
- public static int main (string[] args) {
- Gtk.init (ref args);
- File APT_CACHE_PATH = File.new_for_path ("/var/cache/apt/archives");
- try {
- get_info_and_clean (APT_CACHE_PATH, "", new Cancellable ());
- } catch (Error e) {
- stdout.printf ("ERROR: %s\n", e.message);
- }
- Gtk.main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement