aboutsummaryrefslogtreecommitdiff
path: root/gnunet-fuse-0.8/read.c
blob: 8aa6000e1e3a00e72ffeb53bc5ec22f4d78cfe94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * read.c - FUSE read function
 *
 * This file is part of gnunet-fuse.
 * Copyright (C) 2007 David Barksdale
 *
 * gnunet-fuse is free software; you can redistribute it and/or
 * modify if under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * gnunet-fuse is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _XOPEN_SOURCE 500
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fuse.h>
#include "gnfs.h"

struct read_data
{
	char *buf;
	guint size;
	guint64 offset;
};

static void dpcb(unsigned long long totalBytes,
	unsigned long long completedBytes, GNUNET_CronTime eta,
	unsigned long long lastBlockOffset, const char *lastBlock,
	unsigned int lastBlockSize, void *cls)
{
	struct read_data *d = cls;
	guint64 block_end = lastBlockOffset + lastBlockSize;
	guint64 buf_end = d->offset + d->size;

	(void)totalBytes;
	(void)completedBytes;
	(void)eta;

	/* Check if this block is entirely before the buffer */
	if(block_end < d->offset)
		return;

	/* Check if this block is entirely after the buffer */
	if(lastBlockOffset > buf_end)
		return;

	/* Chop off residue at beginning of block */
	if(lastBlockOffset < d->offset)
	{
		lastBlock += d->offset - lastBlockOffset;
		lastBlockSize -= d->offset - lastBlockOffset;
		lastBlockOffset = d->offset;
	}
	/* Chop off residue at end of block */
	if(block_end > buf_end)
	{
		lastBlockSize -= block_end - buf_end;
	}
	memcpy(d->buf + (lastBlockOffset - d->offset), lastBlock,
		lastBlockSize);
}

static int tt(void *cls)
{
	(void)cls;
	return fuse_interrupted() ? GNUNET_SYSERR : GNUNET_OK;
}

int gn_read(const char *path, char *buf, size_t size, off_t offset,
	struct fuse_file_info *fi)
{
	struct dirent *de;
	struct read_data d;
	char *special;
	int ret;
	ssize_t slen;
	guint64 len;

	(void)fi;

	GNUNET_GE_LOG(ectx, GNUNET_GE_BULK | GNUNET_GE_DEVELOPER | GNUNET_GE_DEBUG,
		"%s: called for '%s' %u bytes %lld offset\n", __FUNCTION__,
		path, size, offset);

	/* Check for special file */
	special = gn_get_special_file(path);
	if(special != NULL)
	{
		slen = strlen(special);
		if(offset >= slen)
		{
			GNUNET_free(special);
			return 0;
		}
		if( ((ssize_t) (offset + size)) > slen)
		{
			size = slen - offset;
		}
		memcpy(buf, special + offset, size);
		GNUNET_free(special);
		return size;
	}

	/* Lookup dirent for path */
	de = gn_dirent_find(path);
	if(de == NULL)
	{
		GNUNET_GE_LOG(ectx, GNUNET_GE_BULK | GNUNET_GE_DEVELOPER | GNUNET_GE_DEBUG,
			"%s: file not found\n", __FUNCTION__);
		return -ENOENT;
	}
	if(de->de_type != DE_FILE)
	{
		GNUNET_GE_LOG(ectx, GNUNET_GE_BULK | GNUNET_GE_DEVELOPER | GNUNET_GE_DEBUG,
			"%s: not a file\n", __FUNCTION__);
		size = -ENOENT;
		goto out;
	}
	if(GNUNET_semaphore_down(de->de_sema, GNUNET_YES) == GNUNET_SYSERR)
	{
		size = -EIO;
		goto out;
	}
	if(de->de_cached)
	{
		slen = pread(de->de_fd, buf, size, offset);
		if(slen == -1)
			size = -errno;
		else
			size = slen;
		goto out_sema_up;
	}
	len = GNUNET_ECRS_uri_get_file_size(de->de_fi.uri);
	if((guint64)offset >= len)
	{
		size = 0;
		goto out_sema_up;
	}
	if((guint64)offset + size > len)
	{
		size = len - offset;
	}
	d.buf = buf;
	d.size = size;
	d.offset = offset;
	ret = GNUNET_ECRS_file_download_partial(ectx, cfg, de->de_fi.uri, "/dev/null",
		offset, size, anonymity, GNUNET_YES, dpcb, &d, tt, NULL);
	if(ret != GNUNET_OK)
	{
		GNUNET_GE_LOG(ectx, GNUNET_GE_BULK | GNUNET_GE_USER | GNUNET_GE_ERROR,
			"%s: failed to download file\n", __FUNCTION__);
		size = -ENODATA;
	}
out_sema_up:
	GNUNET_semaphore_up(de->de_sema);
out:
	gn_dirent_put(de);
	return size;
}