Advertisement
Guest User

delete-files-in-apt-cache.vala

a guest
Aug 29th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.34 KB | None | 0 0
  1. //Compile it using: valac --pkg gtk+-3.0 --pkg glib-2.0 --pkg gio-2.0 del-apt-cache.vala
  2. using Gtk;
  3. using GLib;
  4.  
  5. private int64[] get_info_and_clean (File file, string space = "", Cancellable? cancellable = null) throws Error
  6. {
  7.     int64 files = 0;
  8.     int64 size  = 0;
  9.     int64[] data = new int64[2];
  10.  
  11.     Array<string> paths = new Array<string> ();
  12.  
  13.     FileInfo info = null;
  14.     FileEnumerator enumerator;
  15.  
  16.     try {//This Try/Catch is to ignore the permissions of '/var/cache/apt/archives/partial'
  17.         enumerator = file.enumerate_children (
  18.             "standard::*",
  19.             FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
  20.             cancellable);
  21.     } catch (IOError e) {
  22.         stderr.printf ("WARNING: Unable to get size of dir '%s': %s\n", file.get_path (), e.message);
  23.         data[0] = 0;
  24.         data[1] = 0;
  25.         return data;
  26.     }
  27.  
  28.     while (cancellable.is_cancelled () == false && ((info = enumerator.next_file (cancellable)) != null)) {
  29.         if (info.get_file_type () == FileType.DIRECTORY) {
  30.             File subdir = file.resolve_relative_path (info.get_name ());
  31.             get_info_and_clean (subdir, space + " ", cancellable);
  32.         } else {
  33.             files += 1;//Sum Files
  34.             size  += info.get_size ();//Accumulates Size
  35.             paths.append_val (file.get_uri () + "/" + info.get_name ());
  36.         }
  37.     }
  38.  
  39.     if (cancellable.is_cancelled ()) {
  40.         throw new IOError.CANCELLED ("Operation was cancelled");
  41.     }
  42.  
  43.     data[0] = files;
  44.     data[1] = size;
  45.  
  46.     File apt_file;
  47.  
  48.     for (int i = 0; i < paths.length; i++) {
  49.         apt_file = File.new_for_uri (paths.index (i));
  50.         stdout.printf ("FILE:  %s", paths.index (i));
  51.         try {
  52.             apt_file.delete ();
  53.             stdout.printf (" [DELETED]\n");
  54.         } catch (Error e) {
  55.             stdout.printf (" [ERROR: %s]\n\n", e.message);
  56.         }
  57.     }
  58.  
  59.     stdout.printf ("APT CACHE FILES: %s\n", files.to_string());
  60.     stdout.printf ("APT CACHE SIZE: %s\n", size.to_string());
  61.  
  62.     return data;
  63. }
  64.  
  65. public static int main (string[] args) {
  66.     Gtk.init (ref args);
  67.    
  68.     File APT_CACHE_PATH = File.new_for_path ("/var/cache/apt/archives");
  69.     try {
  70.         get_info_and_clean (APT_CACHE_PATH, "", new Cancellable ());
  71.     } catch (Error e) {
  72.         stdout.printf ("ERROR: %s\n", e.message);    
  73.     }
  74.     Gtk.main ();
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement