static void metadata_add_container (struct ushare_t *ut, struct upnp_entry_t *entry, const char *container) { struct dirent **namelist; int n,i; if (!entry || !container) return; n = scandir (container, &namelist, 0, alphasort); if (n < 0) { perror ("scandir"); return; } for (i = 0; i < n; i++) { struct stat st; char *fullpath = NULL; if (namelist[i]->d_name[0] == '.') { free (namelist[i]); continue; } fullpath = (char *) malloc (strlen (container) + strlen (namelist[i]->d_name) + 2); sprintf (fullpath, "%s/%s", container, namelist[i]->d_name); log_verbose ("%s/n", fullpath); if (stat (fullpath, &st) < 0) { free (namelist[i]); free (fullpath); continue; } if (S_ISDIR (st.st_mode)) { struct upnp_entry_t *child = NULL; child = upnp_entry_new (ut, namelist[i]->d_name, fullpath, entry, 0, true); if (child) { metadata_add_container (ut, child, fullpath); upnp_entry_add_child (ut, entry, child); } } else metadata_add_file (ut, entry, fullpath, namelist[i]->d_name, &st); free (namelist[i]); free (fullpath); } free (namelist); } 通过scandir扫描container文件夹的文件并排序。 接下来对于每一个文件,看看是文件夹还是文件,如果是文件的话,加入列表,如果是文件夹的话,递归进入该文件夹执行相同的操作。