@@ -386,21 +386,30 @@ static int add_entries_count(void *args, int count, char **data, char **columns)
386386 (void ) count ; (void ) columns ;
387387
388388 size_t * total = (size_t * ) args ;
389- size_t rows = 0 ;
390- if (sscanf (data [0 ], "%zu" , & rows ) != 1 ) {
389+
390+ /* handle index root, which does not have summary data */
391+ if (!data [0 ]) {
392+ return 0 ;
393+ }
394+
395+ if (sscanf (data [0 ], "%zu" , total ) != 1 ) {
391396 return 1 ;
392397 }
393- * total += rows ;
398+
394399 return 0 ;
395400}
396401
397- /* get number of non-dirs in this directory from the pentries table */
402+ /* get number of non-dirs in this directory (including rolled up non-dirs) */
398403static int get_nondirs (const char * name , sqlite3 * dst , size_t * subnondir_count ) {
404+ * subnondir_count = 0 ;
405+
406+ /* reduce number of times add_entries_count is called to 1 per directory by using SUM() */
399407 char * err = NULL ;
400- const int exec_rc = sqlite3_exec (dst , "SELECT COUNT(* ) FROM pentries " ,
408+ const int exec_rc = sqlite3_exec (dst , "SELECT SUM(totfiles + totlinks ) FROM summary; " ,
401409 add_entries_count , subnondir_count , & err );
402410 if (exec_rc != SQLITE_OK ) {
403- sqlite_print_err_and_free (err , stderr , "Warning: Failed to get entries row count from \"%s\": %s\n" , name , err );
411+ sqlite_print_err_and_free (err , stderr , "Warning: Failed to get non-directory count from \"%s\": %s\n" ,
412+ name , err );
404413 }
405414 return exec_rc ;
406415}
@@ -523,7 +532,6 @@ static int can_rollup(struct input *in,
523532 int legal = 0 ;
524533
525534 /* get count of number of non-directories in the current directory */
526- ds -> subnondir_count = 0 ;
527535 get_nondirs (rollup -> data .name , dst , & ds -> subnondir_count );
528536
529537 /* the current directory has too many immediate files/links, don't roll up */
0 commit comments