Ejemplo n.º 1
0
static size_t
tth_cache_get_leaves(const struct tth *tth,
	struct tth leaves[TTH_MAX_LEAVES], size_t n)
{
	int fd, num_leaves = 0;

	g_return_val_if_fail(tth, 0);
	g_return_val_if_fail(leaves, 0);

	fd = tth_cache_file_open(tth);
	if (fd >= 0) {
		filestat_t sb;

		if (fstat(fd, &sb)) {
			g_warning("%s(%s): fstat() failed: %m", G_STRFUNC, tth_base32(tth));
		} else {
			size_t n_leaves;
		
			n_leaves = tth_cache_leave_count(tth, &sb);
			n_leaves = MIN(n, n_leaves);
			if (n_leaves > 0) {
				size_t size;
				ssize_t ret;

				STATIC_ASSERT(TTH_RAW_SIZE == sizeof(leaves[0]));

				size = TTH_RAW_SIZE * n_leaves;
				ret = read(fd, &leaves[0].data, size);
				if ((size_t) ret == size) {
					num_leaves = n_leaves;
				}
			}
		}
		fd_forget_and_close(&fd);
	}
	return num_leaves;
}
Ejemplo n.º 2
0
/**
 * Get amount of leaves stored in the cached TTH entry.
 *
 * @param tth		the TTH for which we want the information
 *
 * @return 0 if the entry could not be located, the amount of leaves otherwise.
 */
size_t
tth_cache_get_nleaves(const struct tth *tth)
{
	int fd;
	filesize_t nleaves = 0;

	g_return_val_if_fail(tth != NULL, 0);

	fd = tth_cache_file_open(tth);

	if (fd >= 0) {
		filestat_t sb;

		if (fstat(fd, &sb)) {
			g_warning("%s(%s): fstat() failed: %m", G_STRFUNC, tth_base32(tth));
		} else {
			nleaves = tth_cache_leave_count(tth, &sb);
		}

		fd_forget_and_close(&fd);
	}

	return nleaves;
}