How to go through a directory in C

I use glib in my application and I see that glib has convenient wrappers for C remove , unlink and rmdir . But they only work on one file or directory at a time.

As far as I understand, neither the C standard nor glib includes any recursive directory management function. I also do not see a specific way to delete the entire directory tree at once, as in rm -rf .

Why am I doing this, I don’t care about any complications, such as permissions, symbolic tree references (infinite recursion), or anything that excludes very naive implementations ... so I would not mind writing my own function for it.

However, I am curious that this functionality is already somewhere in the standard gtk or glib libraries (or in some other reusable C library) already, and I just didn’t stumble upon it. This topic generates a lot of false results.

Otherwise, my plan is to use this type of algorithm:

 dir_walk(char* path, void* callback(char*) { if(is_dir(path) && has_entries(path)) { entries = get_entries(path); for(entry in intries) { dir_walk(entry, callback); } } else { callback(path) } } dir_walk("/home/user/trash", remove); 

Obviously, I will create some error handling, etc., to interrupt the process as soon as a fatal error occurs.

+7
c glib gtk
source share
5 answers

You can use GFileEnumerator if you want to do this with glib.

+4
source share

Have you looked at <dirent.h> ? AFAIK belongs to the POSIX specification, which should be part of the standard library of most, if not all C compilers. See this link <dirent.h> (separate UNIX Version 2 specification from the Open Group) .

PS , before someone comments on this: No, this does not offer a recursive directory traversal. But then I think that this is best implemented by the developer; requirements can vary greatly, so the recursive traversal function of the same size for all should be very effective. (For example: Are symbolic links preserved? Should recursion be limited?)

+7
source share

Several platforms include ftw and nftw: "(new) file tree." Checking the man page for imac shows that they are deprecated, and new users prefer fts. Portability can be a problem with any of these options.

+5
source share

The standard C libraries are designed to provide primitive functionality. What you are talking about is complex behavior. You can easily implement it using the low-level features available in your API of choice - take a look at this tutorial .

+2
source share

Note that the “convenient wrappers” that you specify for remove (), unlink (), and rmdir (), assuming that you mean those that were declared in <glib / gstdio.h>, are not are "convenient wrappers." What is the convenience of prefixing fully standard functions with "g_"? (And note that I say this even if I introduced them first.)

The only reason these shells exist is because of problems with file names on Windows, where these wrappers actually consist of real code; they accept Unicode file name arguments encoded in UTF-8. The corresponding "deployed" functions of the Microsoft C library accept file names in the system code page.

Unless you specifically write code designed for portability on Windows, there is no reason to use g_remove () wrappers, etc.

-one
source share

All Articles