annotate 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
rev   line source
paulo@0 1 /*
paulo@0 2 * $Id: gt_share_file.c,v 1.17 2003/07/23 18:43:32 hipnod Exp $
paulo@0 3 *
paulo@0 4 * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
paulo@0 5 *
paulo@0 6 * This program is free software; you can redistribute it and/or modify it
paulo@0 7 * under the terms of the GNU General Public License as published by the
paulo@0 8 * Free Software Foundation; either version 2, or (at your option) any
paulo@0 9 * later version.
paulo@0 10 *
paulo@0 11 * This program is distributed in the hope that it will be useful, but
paulo@0 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
paulo@0 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
paulo@0 14 * General Public License for more details.
paulo@0 15 */
paulo@0 16
paulo@0 17 #include "gt_gnutella.h"
paulo@0 18
paulo@0 19 #include "sha1.h"
paulo@0 20 #if 0
paulo@0 21 #include "src/mime.h"
paulo@0 22 #endif
paulo@0 23
paulo@0 24 #include "gt_share_file.h"
paulo@0 25 #include "gt_query_route.h"
paulo@0 26
paulo@0 27 #include "gt_search.h"
paulo@0 28 #include "gt_search_exec.h"
paulo@0 29
paulo@0 30 /******************************************************************************/
paulo@0 31
paulo@0 32 unsigned int gt_share_unref (Share *file)
paulo@0 33 {
paulo@0 34 GtShare *share;
paulo@0 35
paulo@0 36 if ((share = share_get_udata (file, GT->name)))
paulo@0 37 {
paulo@0 38 if (file->ref <= 1)
paulo@0 39 {
paulo@0 40 gt_share_free_data (file, share);
paulo@0 41 share_set_udata (file, GT->name, NULL);
paulo@0 42 }
paulo@0 43 }
paulo@0 44
paulo@0 45 return share_unref (file);
paulo@0 46 }
paulo@0 47
paulo@0 48 unsigned int gt_share_ref (Share *file)
paulo@0 49 {
paulo@0 50 return share_ref (file);
paulo@0 51 }
paulo@0 52
paulo@0 53 /******************************************************************************/
paulo@0 54
paulo@0 55 /* this is duplicated with tokenize in gt_query_route.c for the time being */
paulo@0 56 GtTokenSet *gt_share_tokenize (char *hpath)
paulo@0 57 {
paulo@0 58 GtTokenSet *token_set;
paulo@0 59 char *str;
paulo@0 60 char *str0;
paulo@0 61 char *next;
paulo@0 62
paulo@0 63 if (!(str0 = str = STRDUP (hpath)))
paulo@0 64 return NULL;
paulo@0 65
paulo@0 66 if (!(token_set = gt_token_set_new ()))
paulo@0 67 {
paulo@0 68 free (str0);
paulo@0 69 return NULL;
paulo@0 70 }
paulo@0 71
paulo@0 72 while ((next = string_sep_set (&str, QRP_DELIMITERS)) != NULL)
paulo@0 73 {
paulo@0 74 uint32_t tok;
paulo@0 75
paulo@0 76 if (string_isempty (next))
paulo@0 77 continue;
paulo@0 78
paulo@0 79 tok = gt_query_router_hash_str (next, 32);
paulo@0 80 gt_token_set_append (token_set, tok);
paulo@0 81 }
paulo@0 82
paulo@0 83 free (str0);
paulo@0 84 return token_set;
paulo@0 85 }
paulo@0 86
paulo@0 87 /******************************************************************************/
paulo@0 88
paulo@0 89 GtShare *gt_share_new_data (Share *file, uint32_t index)
paulo@0 90 {
paulo@0 91 GtShare *share;
paulo@0 92 char *basename;
paulo@0 93
paulo@0 94 if (!file)
paulo@0 95 return NULL;
paulo@0 96
paulo@0 97 if (!(share = malloc (sizeof (GtShare))))
paulo@0 98 return NULL;
paulo@0 99
paulo@0 100 assert (SHARE_DATA(file) != NULL);
paulo@0 101
paulo@0 102 if (!(basename = file_basename (SHARE_DATA(file)->path)))
paulo@0 103 {
paulo@0 104 GT->DBGFN (GT, "bad basename for %s", SHARE_DATA(file)->path);
paulo@0 105 free (share);
paulo@0 106 return NULL;
paulo@0 107 }
paulo@0 108
paulo@0 109 share->index = index;
paulo@0 110 share->filename = basename; /* share mem with Share */
paulo@0 111 share->tokens = gt_share_tokenize (share_get_hpath (file));
paulo@0 112
paulo@0 113 return share;
paulo@0 114 }
paulo@0 115
paulo@0 116 void gt_share_free_data (Share *file, GtShare *share)
paulo@0 117 {
paulo@0 118 if (!file)
paulo@0 119 {
paulo@0 120 assert (share != NULL);
paulo@0 121 return;
paulo@0 122 }
paulo@0 123
paulo@0 124 if (!share)
paulo@0 125 return;
paulo@0 126
paulo@0 127 gt_token_set_free (share->tokens);
paulo@0 128
paulo@0 129 /* don't free share->filename */
paulo@0 130
paulo@0 131 free (share);
paulo@0 132 }
paulo@0 133
paulo@0 134 Share *gt_share_new (char *filename, uint32_t index, off_t size,
paulo@0 135 unsigned char *sha1)
paulo@0 136 {
paulo@0 137 Share *file;
paulo@0 138 GtShare *share;
paulo@0 139 #if 0
paulo@0 140 char *mime;
paulo@0 141
paulo@0 142 /* choose mime type based on extension */
paulo@0 143 mime = mime_type (filename);
paulo@0 144 #endif
paulo@0 145
paulo@0 146 /* TODO: parse path out of filename. Nodes don't put paths in the
paulo@0 147 * search result string yet but may in the future */
paulo@0 148 if (!(file = share_new (filename)))
paulo@0 149 return NULL;
paulo@0 150
paulo@0 151 #if 0
paulo@0 152 share_set_mime (file, mime);
paulo@0 153 #endif
paulo@0 154
paulo@0 155 file->size = size;
paulo@0 156
paulo@0 157 /*
paulo@0 158 * Set the hash if it was passed in, but also accept a null hash
paulo@0 159 * TODO: need a generic urn: type to stop hardcoding sha1
paulo@0 160 */
paulo@0 161 if (sha1 && !share_set_hash (file, "SHA1", sha1, SHA1_BINSIZE, TRUE))
paulo@0 162 {
paulo@0 163 gt_share_unref (file);
paulo@0 164 return NULL;
paulo@0 165 }
paulo@0 166
paulo@0 167 if (!(share = gt_share_new_data (file, index)))
paulo@0 168 {
paulo@0 169 gt_share_unref (file);
paulo@0 170 return NULL;
paulo@0 171 }
paulo@0 172
paulo@0 173 share_set_udata (file, GT->name, share);
paulo@0 174
paulo@0 175 return file;
paulo@0 176 }
paulo@0 177
paulo@0 178 void gt_share_free (Share *file)
paulo@0 179 {
paulo@0 180 gt_share_free_data (file, share_get_udata (file, GT->name));
paulo@0 181 share_set_udata (file, GT->name, NULL);
paulo@0 182
paulo@0 183 share_free (file);
paulo@0 184 }