Ejemplo n.º 1
0
/**
 * Rozpoczyna wysyłanie pliku o danej nazwie.
 *
 * \param sess Struktura sesji
 * \param rcpt Numer odbiorcy
 * \param filename Nazwa pliku w lokalnym systemie plików
 * \param filename1250 Nazwa pliku w kodowaniu CP-1250
 * \param hash Skrót SHA-1 pliku (lub \c NULL jeśli ma być wyznaczony)
 *
 * \return Struktura \c gg_dcc7 lub \c NULL w przypadku błędu
 *
 * \ingroup dcc7
 */
struct gg_dcc7 *gg_dcc7_send_file(struct gg_session *sess, uin_t rcpt, const char *filename, const char *filename1250, const char *hash)
{
	struct gg_dcc7 *dcc = NULL;
	const char *tmp;
	char hash_buf[GG_DCC7_HASH_LEN];
	struct stat st;
	int fd = -1;

	gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_dcc7_send_file(%p, %d, \"%s\", %p)\n", sess, rcpt, filename, hash);

	if (!sess || !rcpt || !filename) {
		gg_debug_session(sess, GG_DEBUG_MISC, "// gg_dcc7_send_file() invalid parameters\n");
		errno = EINVAL;
		goto fail;
	}

	if (!filename1250)
		filename1250 = filename;

	if (stat(filename, &st) == -1) {
		gg_debug_session(sess, GG_DEBUG_MISC, "// gg_dcc7_send_file() stat() failed (%s)\n", strerror(errno));
		goto fail;
	}

	if ((st.st_mode & S_IFDIR)) {
		gg_debug_session(sess, GG_DEBUG_MISC, "// gg_dcc7_send_file() that's a directory\n");
		errno = EINVAL;
		goto fail;
	}

	if ((fd = open(filename, O_RDONLY)) == -1) {
		gg_debug_session(sess, GG_DEBUG_MISC, "// gg_dcc7_send_file() open() failed (%s)\n", strerror(errno));
		goto fail;
	}

	if (!hash) {
		if (gg_file_hash_sha1(fd, (uint8_t*) hash_buf) == -1)
			goto fail;

		hash = hash_buf;
	}

	if ((tmp = strrchr(filename1250, '/')))
		filename1250 = tmp + 1;

	if (!(dcc = gg_dcc7_send_file_common(sess, rcpt, fd, st.st_size, filename1250, hash, 1)))
		goto fail;

	return dcc;

fail:
	if (fd != -1) {
		int errsv = errno;
		close(fd);
		errno = errsv;
	}

	free(dcc);
	return NULL;
}
Ejemplo n.º 2
0
static void test_file_hash(unsigned int megs, const char *expect)
{
	int fd;
	size_t i;
	char name[32];
	uint8_t result[20];

	strcpy(name, "hashdata.XXXXXX");

	fd = gg_mkstemp(name);

	if (fd == -1) {
		fprintf(stderr, "Unable to create temporary file\n");
		exit(1);
	}

	for (i = 1; i <= megs; i++) {
		unsigned char j;

		if (lseek(fd, i * 1048756 - 1, SEEK_SET) == (off_t) -1) {
			fprintf(stderr, "Unable to seek past end of file\n");
			goto fail;
		}

		j = i;

		if (write(fd, &j, sizeof(j)) != sizeof(j)) {
			fprintf(stderr, "Unable to write past end of file\n");
			goto fail;
		}
	}

	if (gg_file_hash_sha1(fd, result) == -1) {
		fprintf(stderr, "gg_file_hash_sha1() failed for %d megs\n", megs);
		goto fail;
	}

	if (!sha1_compare(result, expect)) {
		printf("hash failed for %d mesgs, expected %s, got %s\n", megs, expect, sha1_to_string(result));
		goto fail;
	}

	close(fd);
	unlink(name);
	return;

fail:
	close(fd);
	unlink(name);
	exit(1);
}