删除Vala中具有root所有权的文件

删除Vala中具有root所有权的文件,root,delete-file,vala,owner,Root,Delete File,Vala,Owner,我需要从APT的缓存中删除文件,并使用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 =

我需要APT的缓存中删除文件,并使用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;
}
//编译时使用:valac--pkg gtk+-3.0--pkg glib-2.0--pkg gio-2.0 del-apt-cache.vala
使用Gtk;
使用油嘴滑舌;
private int64[]get_info_and_clean(文件文件,字符串空格=),cancelable?cancelable=null)抛出错误
{
int64文件=0;
int64大小=0;
int64[]数据=新的int64[2];
数组路径=新数组();
FileInfo=null;
文件枚举器枚举器;
try{//此try/Catch忽略“/var/cache/apt/archives/partial”的权限
枚举器=file.enumerate\u子项(
“标准::*”,
FileQueryInfoFlags.NOFOLLOW_符号链接,
可取消);
}捕获(IOE错误){
stderr.printf(“警告:无法获取目录“%s”的大小:%s\n”,file.get_path(),e.message);
数据[0]=0;
数据[1]=0;
返回数据;
}
while(cancelable.is_cancelled()==false&((info=enumerator.next_file(cancelable))!=null)){
if(info.get_file_type()==FileType.DIRECTORY){
File subdir=File.resolve_relative_path(info.get_name());
获取信息和清理(子目录,空格+“”,可取消);
}否则{
files+=1;//对文件求和
size+=info.get_size();//累积大小
paths.append_val(file.get_uri()+“/”+info.get_name());
}
}
if(cancelable.is_cancelled()){
抛出新IOError.CANCELLED(“操作已取消”);
}
数据[0]=文件;
数据[1]=大小;
文件apt_文件;
for(int i=0;i
当我运行该程序时,出现以下错误:

文件:file:///var/cache/apt/archives/libdbus-1-3_1.10.6-1ubuntu3_amd64.deb [错误:删除文件失败:权限被拒绝]


如果操作系统拒绝您的许可,Vala将无能为力。您需要使用
sudo
或在应用程序上设置“setuid”位并将所有者更改为root用户,以root用户身份运行Vala程序。

此外,应用程序还可以使用类似
PolicyKit
gksu
的机制以提升权限运行
rm
作业。