2016-08-29 16 views
1

мне нужно удалить файлы из кэша из APT и с функциями файлов, Vala обеспечивает не позволяет мне.удалить файлы с корневой собственностью в Vala

Кто-нибудь, кто может дать мне руку?

Код следующее:

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

Когда я запускаю программу, я получаю следующее сообщение об ошибке:

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

ответ

1

Там нет ничего Вала делать, если операционная система отказывает вам разрешение. Вам нужно запустить свою программу Vala как root либо с помощью sudo, либо установить бит «setuid» в приложении и изменить владельца на root.

+0

Кроме того, приложение может использовать механизм, например 'PolicyKit' или' gksu', для запуска задания 'rm' с повышенными привилегиями. –