@@ -303,6 +303,14 @@ dir_scan_results (struct dirent *entry, const char *container, ddb_cover_info_t
303303 return -1 ;
304304}
305305
306+ static
307+ int _dirent_alpha_cmp_func (const void * a , const void * b ) {
308+ struct dirent * const * fa = a ;
309+ struct dirent * const * fb = b ;
310+
311+ return strcmp ((* fa )-> d_name , (* fb )-> d_name );
312+ }
313+
306314static int
307315scan_local_path (const char * local_path , const char * uri , DB_vfs_t * vfsplug , ddb_cover_info_t * cover ) {
308316 struct dirent * * files = NULL ;
@@ -322,7 +330,13 @@ scan_local_path (const char *local_path, const char *uri, DB_vfs_t *vfsplug, ddb
322330
323331 int err = -1 ;
324332
325- if (files != NULL ) {
333+ if (files != NULL && files_count > 0 ) {
334+
335+ // sort resulting files alphabetically, to ensure that numbered covers are prioritized correctly
336+ struct dirent * * sorted_files = calloc (files_count , sizeof (struct dirent * ));
337+ memcpy (sorted_files , files , files_count * sizeof (struct dirent * ));
338+ qsort (sorted_files , files_count , sizeof (struct dirent * ), _dirent_alpha_cmp_func );
339+
326340 const char * filemask_end = filemask + strlen (filemask );
327341 char * p ;
328342 while ((p = strrchr (filemask , ';' ))) {
@@ -331,12 +345,12 @@ scan_local_path (const char *local_path, const char *uri, DB_vfs_t *vfsplug, ddb
331345
332346 for (char * mask = filemask ; mask < filemask_end ; mask += strlen (mask ) + 1 ) {
333347 for (int i = 0 ; i < files_count ; i ++ ) {
334- if (!fnmatch (mask , files [i ]-> d_name , FNM_CASEFOLD )) {
348+ if (!fnmatch (mask , sorted_files [i ]-> d_name , FNM_CASEFOLD )) {
335349 if (uri ) {
336- err = vfs_scan_results (files [i ], uri , cover , mask );
350+ err = vfs_scan_results (sorted_files [i ], uri , cover , mask );
337351 }
338352 else {
339- err = dir_scan_results (files [i ], local_path , cover );
353+ err = dir_scan_results (sorted_files [i ], local_path , cover );
340354 }
341355 }
342356 if (!err ) {
@@ -347,6 +361,7 @@ scan_local_path (const char *local_path, const char *uri, DB_vfs_t *vfsplug, ddb
347361 break ;
348362 }
349363 }
364+ free (sorted_files );
350365 for (size_t i = 0 ; i < files_count ; i ++ ) {
351366 free (files [i ]);
352367 }
0 commit comments