1

I need to delete files from the cache of APT and with the functions of files that Vala provides not let me.

Someone who can give me a hand?

The code is the following:

//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;
}

When I run the program, I get the following error:

FILE: file:///var/cache/apt/archives/libdbus-1-3_1.10.6-1ubuntu3_amd64.deb [ERROR: Failed to delete file: Permission denied]

lozanotux
  • 115
  • 1
  • 9

1 Answers1

1

There's nothing Vala can do if the operating system is denying you permission. You need to run your Vala program as root either using sudo or setting the “setuid” bit on the application and changing the owner to root.

apmasell
  • 7,033
  • 19
  • 28