[Thunar-dev] show total directory sizes?

Benedikt Meurer benedikt.meurer at unix-ag.uni-siegen.de
Thu Mar 23 15:34:04 CET 2006


Jean-François Wauthy wrote:
>>It should be a simple job, which uses one or more threads to determine
>>the size of a directory using opendir(), readdir(), closedir() and lstat().
> 
> You can start with the xfburn code (taken from gnomebaker in fact). I'm
> sure you'll find ways to improve it.
> 
> guint64
> xfburn_calc_dirsize (const gchar * dirname)
> {
>   /* copied from GnomeBaker */
>   guint64 size = 0;
> 
>   GDir *dir = g_dir_open (dirname, 0, NULL);
>   if (dir != NULL) {
>     const gchar *name = g_dir_read_name (dir);
>     while (name != NULL) {
>       /* build up the full path to the name */
>       gchar *fullname = g_build_filename (dirname, name, NULL);
>       struct stat s;
> 
>       if (stat (fullname, &s) == 0) {
>         /* see if the name is actually a directory or a regular file */
>         if (s.st_mode & S_IFDIR)
>           size += (guint64) s.st_size + xfburn_calc_dirsize (fullname);
>         else if (s.st_mode & S_IFREG)
>           size += (guint64) s.st_size;
>       }
> 
>       g_free (fullname);
>       name = g_dir_read_name (dir);
>     }
> 
>     g_dir_close (dir);
>   }
> 
>   return size;
> }

This is definitely the simplest solution, but it's not suitable for
Thunar, as it uses too much memory when calculating the total size of a
large directory tree.

I.e. for a tree a/b/c/d/e/f, when determining the size of f, it'll have
DIR handles for a, b, c, d and e, and each DIR handle uses a not
necessarily small buffer to reduce the amount of getdirentries() syscalls.

Benedikt



More information about the Thunar-dev mailing list