f67f6898c6
DCTC is a Direct Connect clone, allowing users to share their files and talk (like IRC but more software sharing oriented) using a proprietary protocol.
154 lines
5.0 KiB
Plaintext
154 lines
5.0 KiB
Plaintext
$OpenBSD: patch-src_db_c,v 1.1.1.1 2002/01/31 12:21:54 naddy Exp $
|
|
--- src/db.c.orig Sat Jan 26 09:05:21 2002
|
|
+++ src/db.c Mon Jan 28 12:12:27 2002
|
|
@@ -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;i<shared_dirs->len;i++)
|
|
+ lvl = 0;
|
|
+ li = shared_dirs;
|
|
+ while( li != NULL )
|
|
{
|
|
- 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);
|
|
+ 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;i<shared_dirs->len;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;i<shared_dirs->len;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);
|