Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:57b7866ab553 |
---|---|
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 */ | |
16 | |
17 #include "gt_gnutella.h" | |
18 | |
19 #include "sha1.h" | |
20 #if 0 | |
21 #include "src/mime.h" | |
22 #endif | |
23 | |
24 #include "gt_share_file.h" | |
25 #include "gt_query_route.h" | |
26 | |
27 #include "gt_search.h" | |
28 #include "gt_search_exec.h" | |
29 | |
30 /******************************************************************************/ | |
31 | |
32 unsigned int gt_share_unref (Share *file) | |
33 { | |
34 GtShare *share; | |
35 | |
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 } | |
44 | |
45 return share_unref (file); | |
46 } | |
47 | |
48 unsigned int gt_share_ref (Share *file) | |
49 { | |
50 return share_ref (file); | |
51 } | |
52 | |
53 /******************************************************************************/ | |
54 | |
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; | |
62 | |
63 if (!(str0 = str = STRDUP (hpath))) | |
64 return NULL; | |
65 | |
66 if (!(token_set = gt_token_set_new ())) | |
67 { | |
68 free (str0); | |
69 return NULL; | |
70 } | |
71 | |
72 while ((next = string_sep_set (&str, QRP_DELIMITERS)) != NULL) | |
73 { | |
74 uint32_t tok; | |
75 | |
76 if (string_isempty (next)) | |
77 continue; | |
78 | |
79 tok = gt_query_router_hash_str (next, 32); | |
80 gt_token_set_append (token_set, tok); | |
81 } | |
82 | |
83 free (str0); | |
84 return token_set; | |
85 } | |
86 | |
87 /******************************************************************************/ | |
88 | |
89 GtShare *gt_share_new_data (Share *file, uint32_t index) | |
90 { | |
91 GtShare *share; | |
92 char *basename; | |
93 | |
94 if (!file) | |
95 return NULL; | |
96 | |
97 if (!(share = malloc (sizeof (GtShare)))) | |
98 return NULL; | |
99 | |
100 assert (SHARE_DATA(file) != NULL); | |
101 | |
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 } | |
108 | |
109 share->index = index; | |
110 share->filename = basename; /* share mem with Share */ | |
111 share->tokens = gt_share_tokenize (share_get_hpath (file)); | |
112 | |
113 return share; | |
114 } | |
115 | |
116 void gt_share_free_data (Share *file, GtShare *share) | |
117 { | |
118 if (!file) | |
119 { | |
120 assert (share != NULL); | |
121 return; | |
122 } | |
123 | |
124 if (!share) | |
125 return; | |
126 | |
127 gt_token_set_free (share->tokens); | |
128 | |
129 /* don't free share->filename */ | |
130 | |
131 free (share); | |
132 } | |
133 | |
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; | |
141 | |
142 /* choose mime type based on extension */ | |
143 mime = mime_type (filename); | |
144 #endif | |
145 | |
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; | |
150 | |
151 #if 0 | |
152 share_set_mime (file, mime); | |
153 #endif | |
154 | |
155 file->size = size; | |
156 | |
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 } | |
166 | |
167 if (!(share = gt_share_new_data (file, index))) | |
168 { | |
169 gt_share_unref (file); | |
170 return NULL; | |
171 } | |
172 | |
173 share_set_udata (file, GT->name, share); | |
174 | |
175 return file; | |
176 } | |
177 | |
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); | |
182 | |
183 share_free (file); | |
184 } |