Ejemplo n.º 1
0
ssize_t knet_read(knetFile *fp, void *buf, size_t len)
{
	off_t l = 0;
	if (fp->fd == -1) return 0;
	if (fp->type == KNF_TYPE_FTP) {
		if (fp->is_ready == 0) {
			if (!fp->no_reconnect) kftp_reconnect(fp);
			kftp_connect_file(fp);
		}
	} else if (fp->type == KNF_TYPE_HTTP) {
		if (fp->is_ready == 0)
			khttp_connect_file(fp);
	}
	if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX
		size_t rest = len;
		ssize_t curr;
		while (rest) {
			do {
				curr = read(fp->fd, (void*)((char*)buf + l), rest);
			} while (curr < 0 && EINTR == errno);
			if (curr < 0) return -1;
			if (curr == 0) break;
			l += curr; rest -= curr;
		}
	} else l = my_netread(fp->fd, buf, len);
	fp->offset += l;
	return l;
}
Ejemplo n.º 2
0
off_t knet_read(knetFile *fp, void *buf, off_t len)
{
    off_t l = 0;
    if (fp->fd == -1) return 0;
    if (fp->type == KNF_TYPE_FTP) {
        if (fp->is_ready == 0) {
            if (!fp->no_reconnect) kftp_reconnect(fp);
            kftp_connect_file(fp);
        }
    } else if (fp->type == KNF_TYPE_HTTP) {
        if (fp->is_ready == 0)
            khttp_connect_file(fp);
    }
    if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX
        off_t rest = len, curr;
        while (rest) {
            curr = read(fp->fd, buf + l, rest);
            if (curr == 0) break;
            l += curr;
            rest -= curr;
        }
    } else l = my_netread(fp->fd, buf, len);
    fp->offset += l;
    return l;
}
Ejemplo n.º 3
0
knetFile *knet_open(const char *fn, const char *mode)
{
	knetFile *fp = 0;
	if (mode[0] != 'r') {
		fprintf(stderr, "[kftp_open] only mode \"r\" is supported.\n");
		return 0;
	}
	if (strstr(fn, "ftp://") == fn) {
		fp = kftp_parse_url(fn, mode);
		if (fp == 0) return 0;
		if (kftp_connect(fp) == -1) {
			knet_close(fp);
			return 0;
		}
		kftp_connect_file(fp);
	} else if (strstr(fn, "http://") == fn) {
		fp = khttp_parse_url(fn, mode);
		if (fp == 0) return 0;
		khttp_connect_file(fp);
	} else { // local file
#ifdef _WIN32
		/* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may
		 * be undefined on some systems, although it is defined on my
		 * Mac and the Linux I have tested on. */
		FILE *fpf = fopen(fn, "rb");
#else		
		FILE *fpf = fopen(fn, "r");
#endif
		if (!fpf) {
			perror("open");
			return 0;
		}
		fp = (knetFile*)calloc(1, sizeof(knetFile));
		fp->type = KNF_TYPE_LOCAL;
		fp->fp = fpf;
		fp->fd = -1;
		fp->ctrl_fd = -1;
		if (g_block_size > 0) {
			fp->buffer = malloc(g_block_size << 10);
			setvbuf(fp->fp, fp->buffer, _IOFBF, g_block_size << 10);
		}
	}
	if (fp && fp->type != KNF_TYPE_LOCAL && fp->fd == -1) {
		knet_close(fp);
		return 0;
	}
	if (fp && fp->type == KNF_TYPE_LOCAL && !fp->fp) {
		knet_close(fp);
		return 0;
	}
	return fp;
}
Ejemplo n.º 4
0
knetFile *knet_open(const char *fn, const char *mode)
{
#ifdef KNETFILE_HOOKS
	if (alt_open)
		return alt_open(fn, mode);
#endif
	knetFile *fp = 0;
	if (mode[0] != 'r') {
		fprintf(stderr, "[kftp_open] only mode \"r\" is supported.\n");
		return 0;
	}
	if (strstr(fn, "ftp://") == fn) {
		fp = kftp_parse_url(fn, mode);
		if (fp == 0) return 0;
		if (kftp_connect(fp) == -1) {
			knet_close(fp);
			return 0;
		}
		kftp_connect_file(fp);
	} else if (strstr(fn, "http://") == fn) {
		fp = khttp_parse_url(fn, mode);
		if (fp == 0) return 0;
		khttp_connect_file(fp);
	} else { // local file
#ifdef _WIN32
		/* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may
		 * be undefined on some systems, although it is defined on my
		 * Mac and the Linux I have tested on. */
		int fd = open(fn, O_RDONLY | O_BINARY);
#else		
		int fd = open(fn, O_RDONLY);
#endif
		if (fd == -1) {
			perror("open");
			return 0;
		}
		fp = (knetFile*)calloc(1, sizeof(knetFile));
		fp->type = KNF_TYPE_LOCAL;
		fp->fd = fd;
		fp->ctrl_fd = -1;
	}
	if (fp && fp->fd == -1) {
		knet_close(fp);
		return 0;
	}
	return fp;
}