Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
view src/gt_share_file.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: gt_share_file.c,v 1.17 2003/07/23 18:43:32 hipnod Exp $
3 *
4 * Copyright (C) 2001-2003 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"
19 #include "sha1.h"
20 #if 0
21 #include "src/mime.h"
22 #endif
24 #include "gt_share_file.h"
25 #include "gt_query_route.h"
27 #include "gt_search.h"
28 #include "gt_search_exec.h"
30 /******************************************************************************/
32 unsigned int gt_share_unref (Share *file)
33 {
34 GtShare *share;
36 if ((share = share_get_udata (file, GT->name)))
37 {
38 if (file->ref <= 1)
39 {
40 gt_share_free_data (file, share);
41 share_set_udata (file, GT->name, NULL);
42 }
43 }
45 return share_unref (file);
46 }
48 unsigned int gt_share_ref (Share *file)
49 {
50 return share_ref (file);
51 }
53 /******************************************************************************/
55 /* this is duplicated with tokenize in gt_query_route.c for the time being */
56 GtTokenSet *gt_share_tokenize (char *hpath)
57 {
58 GtTokenSet *token_set;
59 char *str;
60 char *str0;
61 char *next;
63 if (!(str0 = str = STRDUP (hpath)))
64 return NULL;
66 if (!(token_set = gt_token_set_new ()))
67 {
68 free (str0);
69 return NULL;
70 }
72 while ((next = string_sep_set (&str, QRP_DELIMITERS)) != NULL)
73 {
74 uint32_t tok;
76 if (string_isempty (next))
77 continue;
79 tok = gt_query_router_hash_str (next, 32);
80 gt_token_set_append (token_set, tok);
81 }
83 free (str0);
84 return token_set;
85 }
87 /******************************************************************************/
89 GtShare *gt_share_new_data (Share *file, uint32_t index)
90 {
91 GtShare *share;
92 char *basename;
94 if (!file)
95 return NULL;
97 if (!(share = malloc (sizeof (GtShare))))
98 return NULL;
100 assert (SHARE_DATA(file) != NULL);
102 if (!(basename = file_basename (SHARE_DATA(file)->path)))
103 {
104 GT->DBGFN (GT, "bad basename for %s", SHARE_DATA(file)->path);
105 free (share);
106 return NULL;
107 }
109 share->index = index;
110 share->filename = basename; /* share mem with Share */
111 share->tokens = gt_share_tokenize (share_get_hpath (file));
113 return share;
114 }
116 void gt_share_free_data (Share *file, GtShare *share)
117 {
118 if (!file)
119 {
120 assert (share != NULL);
121 return;
122 }
124 if (!share)
125 return;
127 gt_token_set_free (share->tokens);
129 /* don't free share->filename */
131 free (share);
132 }
134 Share *gt_share_new (char *filename, uint32_t index, off_t size,
135 unsigned char *sha1)
136 {
137 Share *file;
138 GtShare *share;
139 #if 0
140 char *mime;
142 /* choose mime type based on extension */
143 mime = mime_type (filename);
144 #endif
146 /* TODO: parse path out of filename. Nodes don't put paths in the
147 * search result string yet but may in the future */
148 if (!(file = share_new (filename)))
149 return NULL;
151 #if 0
152 share_set_mime (file, mime);
153 #endif
155 file->size = size;
157 /*
158 * Set the hash if it was passed in, but also accept a null hash
159 * TODO: need a generic urn: type to stop hardcoding sha1
160 */
161 if (sha1 && !share_set_hash (file, "SHA1", sha1, SHA1_BINSIZE, TRUE))
162 {
163 gt_share_unref (file);
164 return NULL;
165 }
167 if (!(share = gt_share_new_data (file, index)))
168 {
169 gt_share_unref (file);
170 return NULL;
171 }
173 share_set_udata (file, GT->name, share);
175 return file;
176 }
178 void gt_share_free (Share *file)
179 {
180 gt_share_free_data (file, share_get_udata (file, GT->name));
181 share_set_udata (file, GT->name, NULL);
183 share_free (file);
184 }