예제 #1
0
/**
 * Grab random data from file and feed them to the specified routine.
 *
 * @param path		pathname where random data are expected
 * @param rfd		random filling routine to feed data
 * @param len		amount of random bytes to read
 *
 * @return the amount of bytes fed if OK, a short count or -1 on error,
 * with errno set.
 */
ssize_t
frand_restore(const char *path, feed_fn_t rfd, size_t len)
{
    char buf[256];
    int fd;
    ssize_t bytes_read = 0;

    fd = file_open_missing(path, O_RDONLY);

    if (-1 == fd)
        return -1;

    while (len != 0) {
        size_t n = MIN(len, sizeof buf);
        ssize_t r;

        r = read(fd, buf, n);
        if (-1 == r)
            break;
        bytes_read += r;
        (*rfd)(buf, r);
        if (UNSIGNED(r) != n)
            break;
        len -= n;
    }

    ZERO(buf);		/* Leave no memory trail */
    close(fd);
    return bytes_read;
}
예제 #2
0
파일: dump.c 프로젝트: Haxe/gtk-gnutella
/**
 * Initialize RX dumping.
 *
 * @return TRUE if initialized.
 */
static gboolean
dump_initialize(struct dump *dump)
{
	char *pathname;

	if (dump->initialized)
		return TRUE;

	pathname = make_pathname(settings_config_dir(), dump->filename);
	dump->fd = file_open_missing(pathname, O_WRONLY | O_APPEND | O_NONBLOCK);
	HFREE_NULL(pathname);

	/*
	 * If the dump "file" is actually a named pipe, we'd block quickly
	 * if there was no reader.  So set the file as non-blocking and
	 * we'll disable dumping as soon as we can't write all the data
	 * we want.
	 */

	if (dump->fd < 0) {
		g_warning("can't open %s -- disabling dumping", dump->filename);
		dump_disable(dump);
		return FALSE;
	}

	fd_set_nonblocking(dump->fd);

	dump->slist = slist_new();
	dump->fill = 0;
	dump->initialized = TRUE;

	return TRUE;
}
예제 #3
0
static int
tth_cache_file_open(const struct tth *tth)
{
	char *pathname;
	int fd;

	g_return_val_if_fail(tth, -1);

	pathname = tth_cache_pathname(tth);
	fd = file_open_missing(pathname, O_RDONLY);
	HFREE_NULL(pathname);
	return fd;
}
예제 #4
0
/**
 * Open and parse the first line of a given file as the ASCII representation
 * of an unsigned 64-bit integer.
 *
 * @param path			file path
 * @param missing		whether file may be missing (to shut up warnings)
 * @param errptr		if non-NULL, filled with error number (0 means OK)
 *
 * @return the value we parsed on the first line, 0 otherwise.
 *
 * @note
 * The ``errptr'' parameter needs to be used to distinguish between
 * a file containing "0" and a file which could not be parsed correctly.
 */
guint64
filehead_uint64(const char *path, gboolean missing, int *errptr)
{
	int fd;
	guint64 value;
	char data[FILEHEAD_LINE_MAXLEN + 1];
	ssize_t r;
	int error;

	fd = missing ?  file_open_missing(path, O_RDONLY) :
		file_open(path, O_RDONLY, 0);

	if (-1 == fd)
		goto error;

	r = read(fd, data, sizeof data - 1); /* reserve one byte for NUL */

	if ((ssize_t) -1 == r)
		goto error_close;

	g_assert(r >= 0 && UNSIGNED(r) < sizeof data);

	fd_close(&fd);
	data[r] = '\0';
	value = parse_uint64(data, NULL, 10, &error);

	if (error) {
		errno = error;
		goto error;
	}

	if (errptr != NULL)
		*errptr = 0;

	return value;

error_close:
	fd_close(&fd);
error:
	if (errptr != NULL)
		*errptr = errno;

	return 0;
}