Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
view src/transfer/download.c @ 0:d39e1d0d75b6
initial add
author | paulo@hit-nxdomain.opendns.com |
---|---|
date | Sat, 20 Feb 2010 21:18:28 -0800 |
parents | |
children |
line source
1 /*
2 * $Id: download.c,v 1.2 2004/04/17 06:06:46 hipnod Exp $
3 *
4 * Copyright (C) 2004 giFT project (gift.sourceforge.net)
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
17 #include "gt_gnutella.h"
18 #include "transfer/download.h"
20 /*****************************************************************************/
22 static Dataset *gt_downloads;
24 /*****************************************************************************/
26 void gt_download_add (Transfer *transfer, Source *source)
27 {
28 Dataset *d;
30 d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
31 dataset_insert (&d, &source, sizeof(source), source, 0);
33 dataset_insert (>_downloads, &transfer, sizeof(transfer), d, 0);
34 }
36 void gt_download_remove (Transfer *transfer, Source *source)
37 {
38 Dataset *d;
40 d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
41 dataset_remove (d, &source, sizeof(source));
43 if (dataset_length (d) == 0)
44 {
45 dataset_clear (d);
46 dataset_remove (gt_downloads, &transfer, sizeof(transfer));
47 }
49 if (dataset_length (gt_downloads) == 0)
50 {
51 dataset_clear (gt_downloads);
52 gt_downloads = NULL;
53 }
54 }
56 /*****************************************************************************/
58 static int ds_find_hash (ds_data_t *key, ds_data_t *value, void *udata)
59 {
60 Array *a = udata;
61 char *sha1;
62 Source **ret;
63 Source *src = value->data;
64 int n;
66 n = array_list (&a, &sha1, &ret, NULL);
67 assert (n == 2);
69 if (!src->hash)
70 return DS_CONTINUE;
72 /* NOTE: the hash is prefixed with giftd's hash here */
73 if (strcmp (src->hash, sha1) == 0)
74 {
75 *ret = src;
76 return DS_BREAK;
77 }
79 return DS_CONTINUE;
80 }
82 static int ds_traverse_transfer (ds_data_t *key, ds_data_t *value, void *udata)
83 {
84 Dataset *d = value->data;
86 dataset_foreach_ex (d, ds_find_hash, udata);
87 return DS_CONTINUE;
88 }
90 Source *gt_download_lookup (const char *sha1)
91 {
92 Array *a;
93 Source *ret = NULL;
95 a = array_new ((void *)sha1, &ret, NULL);
97 if (!a)
98 return NULL;
100 dataset_foreach_ex (gt_downloads, ds_traverse_transfer, a);
101 array_unset (&a);
103 return ret;
104 }