+ } else {
+ if (S_ISDIR(st.st_mode)) {
+ DIR *d;
+
+ if (!(d = opendir(filename))) {
+ pa_log_warn("Failed to read '%s': %s", filename, pa_cstrerror(errno));
+ if (*fail)
+ return -1;
+ } else {
+ unsigned i, count;
+ char **sorted_files;
+ struct dirent *de;
+ pa_bool_t failed = FALSE;
+ pa_dynarray *files = pa_dynarray_new();
+
+ while ((de = readdir(d))) {
+ char *extn;
+ size_t flen = strlen(de->d_name);
+
+ if (flen < 4)
+ continue;
+
+ extn = &de->d_name[flen-3];
+ if (strncmp(extn, ".pa", 3) == 0)
+ pa_dynarray_append(files, pa_sprintf_malloc("%s" PA_PATH_SEP "%s", filename, de->d_name));
+ }
+
+ closedir(d);
+
+ count = pa_dynarray_size(files);
+ sorted_files = pa_xnew(char*, count);
+ for (i = 0; i < count; ++i)
+ sorted_files[i] = pa_dynarray_get(files, i);
+ pa_dynarray_free(files, NULL, NULL);
+
+ for (i = 0; i < count; ++i) {
+ for (unsigned j = 0; j < count; ++j) {
+ if (strcmp(sorted_files[i], sorted_files[j]) < 0) {
+ char *tmp = sorted_files[i];
+ sorted_files[i] = sorted_files[j];
+ sorted_files[j] = tmp;
+ }
+ }
+ }
+
+ for (i = 0; i < count; ++i) {
+ if (!failed) {
+ if (pa_cli_command_execute_file(c, sorted_files[i], buf, fail) < 0 && *fail)
+ failed = TRUE;
+ }
+
+ pa_xfree(sorted_files[i]);
+ }
+ pa_xfree(sorted_files);
+ if (failed)
+ return -1;
+ }
+ } else if (pa_cli_command_execute_file(c, filename, buf, fail) < 0 && *fail) {
+ return -1;
+ }
+ }