在glib或libc中是否有类似boost :: filesystem :: remove_all(path)的函数?

问题描述:

我不能使用boost,但只能使用glib和libc函数。在glib或libc中是否有类似boost :: filesystem :: remove_all(path)的函数?

如果您检查glib,您会发现g_remove,g_rmdir和g_unlink,并且它们都不会删除非空目录。

我看过一篇文章,它实现了一个函数递归删除Linux命令“rm -rf path”等目录内的所有文件和子目录。

我更喜欢使用在C.

您推荐哪家执行/ API井测试实施?

谢谢。

+0

你可以使用函数从GIO以及耍贫嘴? – ptomato

没有什么目前GIO它实现的rm -rf相当于是的,但你可以建立一些东西,这是否很容易:

/* Recursively delete @file and its children. @file may be a file or a directory. */ 
static gboolean 
rm_rf (GFile   *file, 
     GCancellable *cancellable, 
     GError  **error) 
{ 
    g_autoptr(GFileEnumerator) enumerator = NULL; 

    enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 
              G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, 
              cancellable, NULL); 

    while (enumerator != NULL) 
    { 
     GFile *child; 

     if (!g_file_enumerator_iterate (enumerator, NULL, &child, cancellable, error)) 
     return FALSE; 
     if (child == NULL) 
     break; 
     if (!rm_rf (child, cancellable, error)) 
     return FALSE; 
    } 

    return g_file_delete (file, cancellable, error); 
}