diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gt_share_file.c	Sat Feb 20 21:18:28 2010 -0800
     1.3 @@ -0,0 +1,184 @@
     1.4 +/*
     1.5 + * $Id: gt_share_file.c,v 1.17 2003/07/23 18:43:32 hipnod Exp $
     1.6 + *
     1.7 + * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or modify it
    1.10 + * under the terms of the GNU General Public License as published by the
    1.11 + * Free Software Foundation; either version 2, or (at your option) any
    1.12 + * later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful, but
    1.15 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 + * General Public License for more details.
    1.18 + */
    1.19 +
    1.20 +#include "gt_gnutella.h"
    1.21 +
    1.22 +#include "sha1.h"
    1.23 +#if 0
    1.24 +#include "src/mime.h"
    1.25 +#endif
    1.26 +
    1.27 +#include "gt_share_file.h"
    1.28 +#include "gt_query_route.h"
    1.29 +
    1.30 +#include "gt_search.h"
    1.31 +#include "gt_search_exec.h"
    1.32 +
    1.33 +/******************************************************************************/
    1.34 +
    1.35 +unsigned int gt_share_unref (Share *file)
    1.36 +{
    1.37 +	GtShare *share;
    1.38 +
    1.39 +	if ((share = share_get_udata (file, GT->name)))
    1.40 +	{
    1.41 +		if (file->ref <= 1)
    1.42 +		{
    1.43 +			gt_share_free_data (file, share);
    1.44 +			share_set_udata (file, GT->name, NULL);
    1.45 +		}
    1.46 +	}
    1.47 +
    1.48 +	return share_unref (file);
    1.49 +}
    1.50 +
    1.51 +unsigned int gt_share_ref (Share *file)
    1.52 +{
    1.53 +	return share_ref (file);
    1.54 +}
    1.55 +
    1.56 +/******************************************************************************/
    1.57 +
    1.58 +/* this is duplicated with tokenize in gt_query_route.c for the time being */
    1.59 +GtTokenSet *gt_share_tokenize (char *hpath)
    1.60 +{
    1.61 +	GtTokenSet *token_set;
    1.62 +	char       *str;
    1.63 +	char       *str0;
    1.64 +	char       *next;
    1.65 +
    1.66 +	if (!(str0 = str = STRDUP (hpath)))
    1.67 +		return NULL;
    1.68 +
    1.69 +	if (!(token_set = gt_token_set_new ()))
    1.70 +	{
    1.71 +		free (str0);
    1.72 +		return NULL;
    1.73 +	}
    1.74 +
    1.75 +	while ((next = string_sep_set (&str, QRP_DELIMITERS)) != NULL)
    1.76 +	{
    1.77 +		uint32_t tok;
    1.78 +
    1.79 +		if (string_isempty (next))
    1.80 +			continue;
    1.81 +
    1.82 +		tok = gt_query_router_hash_str (next, 32);
    1.83 +		gt_token_set_append (token_set, tok);
    1.84 +	}
    1.85 +
    1.86 +	free (str0);
    1.87 +	return token_set;
    1.88 +}
    1.89 +
    1.90 +/******************************************************************************/
    1.91 +
    1.92 +GtShare *gt_share_new_data (Share *file, uint32_t index)
    1.93 +{
    1.94 +	GtShare  *share;
    1.95 +	char     *basename;
    1.96 +
    1.97 +	if (!file)
    1.98 +		return NULL;
    1.99 +
   1.100 +	if (!(share = malloc (sizeof (GtShare))))
   1.101 +		return NULL;
   1.102 +
   1.103 +	assert (SHARE_DATA(file) != NULL);
   1.104 +
   1.105 +	if (!(basename = file_basename (SHARE_DATA(file)->path)))
   1.106 +	{
   1.107 +		GT->DBGFN (GT, "bad basename for %s", SHARE_DATA(file)->path);
   1.108 +		free (share);
   1.109 +		return NULL;
   1.110 +	}
   1.111 +
   1.112 +	share->index    = index;
   1.113 +	share->filename = basename;            /* share mem with Share */
   1.114 +	share->tokens   = gt_share_tokenize (share_get_hpath (file));
   1.115 +
   1.116 +	return share;
   1.117 +}
   1.118 +
   1.119 +void gt_share_free_data (Share *file, GtShare *share)
   1.120 +{
   1.121 +	if (!file)
   1.122 +	{
   1.123 +		assert (share != NULL);
   1.124 +		return;
   1.125 +	}
   1.126 +
   1.127 +	if (!share)
   1.128 +		return;
   1.129 +
   1.130 +	gt_token_set_free (share->tokens);
   1.131 +
   1.132 +	/* don't free share->filename */
   1.133 +
   1.134 +	free (share);
   1.135 +}
   1.136 +
   1.137 +Share *gt_share_new (char *filename, uint32_t index, off_t size,
   1.138 +                     unsigned char *sha1)
   1.139 +{
   1.140 +	Share *file;
   1.141 +	GtShare   *share;
   1.142 +#if 0
   1.143 +	char      *mime;
   1.144 +
   1.145 +	/* choose mime type based on extension */
   1.146 +	mime = mime_type (filename);
   1.147 +#endif
   1.148 +
   1.149 +	/* TODO: parse path out of filename. Nodes don't put paths in the
   1.150 +	 *       search result string yet but may in the future */
   1.151 +	if (!(file = share_new (filename)))
   1.152 +		return NULL;
   1.153 +
   1.154 +#if 0
   1.155 +	share_set_mime (file, mime);
   1.156 +#endif
   1.157 +
   1.158 +	file->size = size;
   1.159 +
   1.160 +	/* 
   1.161 +	 * Set the hash if it was passed in, but also accept a null hash
   1.162 +	 * TODO: need a generic urn: type to stop hardcoding sha1 
   1.163 +	 */
   1.164 +	if (sha1 && !share_set_hash (file, "SHA1", sha1, SHA1_BINSIZE, TRUE))
   1.165 +	{
   1.166 +		gt_share_unref (file);
   1.167 +		return NULL;
   1.168 +	}
   1.169 +
   1.170 +	if (!(share = gt_share_new_data (file, index)))
   1.171 +	{
   1.172 +		gt_share_unref (file);
   1.173 +		return NULL;
   1.174 +	}
   1.175 +
   1.176 +	share_set_udata (file, GT->name, share);
   1.177 +
   1.178 +	return file;
   1.179 +}
   1.180 +
   1.181 +void gt_share_free (Share *file)
   1.182 +{
   1.183 +	gt_share_free_data (file, share_get_udata (file, GT->name));
   1.184 +	share_set_udata (file, GT->name, NULL);
   1.185 +
   1.186 +	share_free (file);
   1.187 +}