$OpenBSD: patch-src_db_c,v 1.2 2003/09/01 16:51:39 avsm Exp $ --- src/db.c.orig 2002-01-26 03:05:21.000000000 -0500 +++ src/db.c 2003-08-29 11:47:53.000000000 -0400 @@ -45,7 +45,7 @@ /* list of shared directories */ /******************************/ G_LOCK_DEFINE_STATIC(shared_dirs); -GPtrArray *shared_dirs=NULL; +GSList *shared_dirs=NULL; /********************************************************************/ /* database containing the list of all shared files with their type */ @@ -398,6 +398,29 @@ static GArray *rebuild_dir_database(GArr } /***************************************************************/ +/* determines the level for the next dir given the previous */ +/* dir, based on the common part of the both dirs. */ +/***************************************************************/ +/* output: lvl contains the correct level to use for the next */ +/* dir. Returns the starting point to use with nextdir */ +/***************************************************************/ +char * get_level_for_next_dir(char *prevdir, char *nextdir, int *lvl) +{ + char *startp = nextdir; + + *lvl = 0; + while( *nextdir && *nextdir == *prevdir ) { + if( *nextdir == '/' && nextdir != startp ) /* ignore the first '/' */ + (*lvl)++; + nextdir++; + prevdir++; + } + while( nextdir != startp && *(nextdir - 1) != '/' ) + nextdir--; + return nextdir; +} + +/***************************************************************/ /* fully rebuild shared file database */ /* this rebuilding can be done even if a search is in progress */ /***************************************************************/ @@ -410,6 +433,10 @@ void rebuild_database(void) GByteArray *nw_he3; GByteArray *old_he3; GString *ls_format=NULL; + char *prevdir = NULL; + char *nextdir; + int lvl; + GSList *li; /* rebuild a new database */ ls_format=g_string_sized_new(65536); @@ -419,12 +446,18 @@ void rebuild_database(void) new_shared=g_array_new(FALSE,FALSE,sizeof(DB_ENTRY)); if(shared_dirs!=NULL) { - for(i=0;ilen;i++) - { - int lvl=0; - add_initial_dir_to_ls(&ls_format,&lvl,g_ptr_array_index(shared_dirs,i)); - - new_shared=rebuild_dir_database(new_shared,&sod,g_ptr_array_index(shared_dirs,i),&ls_format,lvl); + lvl = 0; + li = shared_dirs; + while( li != NULL ) + { + nextdir = li->data; + if(prevdir) + nextdir = get_level_for_next_dir(prevdir, nextdir, &lvl ); + + add_initial_dir_to_ls(&ls_format,&lvl,nextdir); + new_shared=rebuild_dir_database(new_shared,&sod,li->data,&ls_format,lvl); + prevdir = li->data; + li = g_slist_next(li); } } @@ -472,6 +505,14 @@ void rebuild_database(void) } /*****************************************************/ +/* comparison function for GSList, char * */ +/*****************************************************/ +gint string_compare(gconstpointer a, gconstpointer b) +{ + return strcmp( (char *)a, (char *)b ); +} + +/*****************************************************/ /* add a directory to the list of shared directories */ /**********************************************************/ /* due to the fact this function runs in the main thread, */ @@ -484,9 +525,6 @@ void add_shared_directory(char *dir) struct stat st; char *str; - if(shared_dirs==NULL) - shared_dirs=g_ptr_array_new(); - if(stat(dir,&st)) { disp_msg(ERR_MSG,"add_shared_directory","invalid dir",dir,NULL); @@ -509,7 +547,7 @@ void add_shared_directory(char *dir) /* add this directory to the list and rebuild database */ G_LOCK(shared_dirs); - g_ptr_array_add(shared_dirs,str); + shared_dirs=g_slist_insert_sorted(shared_dirs,str, &string_compare); G_UNLOCK(shared_dirs); rebuild_database(); @@ -532,15 +570,8 @@ void remove_shared_directory(char *dir) /* remove this directory to the list and rebuild database */ G_LOCK(shared_dirs); - for(i=0;ilen;i++) - { - if(!strcmp(dir, g_ptr_array_index(shared_dirs,i)) ) - { - g_ptr_array_remove_index_fast(shared_dirs,i); - disp_msg(DEBUG_MSG,NULL,"unsharing",dir,NULL); - break; - } - } + disp_msg(DEBUG_MSG,NULL,"unsharing",dir,NULL); + shared_dirs=g_slist_remove( shared_dirs, dir ); G_UNLOCK(shared_dirs); rebuild_database(); @@ -554,17 +585,20 @@ void remove_shared_directory(char *dir) GString *get_shared_directory_list(void) { GString *lst; + GSList *li; int i; lst=g_string_new(""); G_LOCK(shared_dirs); if(shared_dirs!=NULL) { - for(i=0;ilen;i++) + li = shared_dirs; + while( li != NULL ) { - if(i!=0) + if(li!=shared_dirs) lst=g_string_append_c(lst,'|'); - g_string_sprintfa(lst,"%s",(char*)g_ptr_array_index(shared_dirs,i)); + g_string_sprintfa(lst,"%s", li->data); + li = g_slist_next(li); } } G_UNLOCK(shared_dirs); @@ -743,7 +777,7 @@ static void send_a_db_result(int output_ else { char strmd5[512]; - md5tostr(md5sum,strmd5); + md5tostr(md5sum,strmd5,sizeof strmd5); g_string_sprintfa(str,"$SR %s %s\005%lu.%s %d/%d\005%s (%s)", nickname,adapted->str,de->filesize,strmd5, (dl_on?free_dl_slot:0), ttl_dl_slot,