我需要从 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;
}
运行程序时,出现以下错误:
FILE:file:///var/cache/apt/archives/libdbus-1-3_1.10.6-1ubuntu3_amd64.deb [错误:删除文件失败:权限被拒绝]
答案 0 :(得分:1)
如果操作系统拒绝您的许可,Vala无法做到。您需要使用192.168.0.00:8080
或在应用程序上设置“setuid”位并将所有者更改为root来以root身份运行Vala程序。