dev/vala/rm_rf/rm_rf_async.vala

44 lines
1.4 KiB
Vala

async void rm_rf_async (File directory, Cancellable? cancellable) throws Error {
if (cancellable.is_cancelled()) return;
var children = yield directory.enumerate_children_async (FileAttribute.STANDARD_NAME,
FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
Priority.DEFAULT, cancellable);
GLib.List<FileInfo> fileinfos;
while ((fileinfos = yield children.next_files_async (8, Priority.DEFAULT,
cancellable)) != null) {
foreach (var child_finfo in fileinfos) {
var child = directory.get_child (child_finfo.get_name());
var ftype = child.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
if (ftype == FileType.DIRECTORY) {
yield rm_rf_async (child, cancellable);
} else {
print (@"rm $(child.get_path())\n");
child.delete (cancellable);
}
}
}
print (@"rmdir $(directory.get_path())\n");
directory.delete (cancellable);
}
int main () {
//string temp_dir = DirUtils.make_tmp ("my-XXXXXX-dir");
//stdout.printf ("make_tmp (\"my-XXXXXXX-dir\") = %s\n", temp_dir);
// async example
var loop = new MainLoop();
rm_rf_async.begin(File.new_for_path("/tmp/aaa"), null,
(obj, res) => {
try {
rm_rf_async.end(res);
loop.quit();
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
//return -1;
}
});
loop.run();
return 0;
}