Esempio n. 1
0
static ssize_t _write
  ( timeshift_file_t *tsf, const void *buf, size_t count )
{
  uint8_t *ram;
  size_t alloc;
  if (tsf->ram) {
    pthread_mutex_lock(&tsf->ram_lock);
    if (tsf->ram_size < tsf->woff + count) {
      if (tsf->ram_size >= timeshift_ram_segment_size)
        alloc = MAX(count, 64*1024);
      else
        alloc = MAX(count, 4*1024*1024);
      ram = realloc(tsf->ram, tsf->ram_size + alloc);
      if (ram == NULL) {
        tvhwarn("timeshift", "RAM timeshift memalloc failed");
        pthread_mutex_unlock(&tsf->ram_lock);
        return -1;
      }
      tsf->ram = ram;
      tsf->ram_size += alloc;
    }
    memcpy(tsf->ram + tsf->woff, buf, count);
    tsf->woff += count;
    pthread_mutex_unlock(&tsf->ram_lock);
    return count;
  }
  return _write_fd(tsf->wfd, buf, count);
}
Esempio n. 2
0
static ssize_t _write
  ( timeshift_file_t *tsf, const void *buf, size_t count )
{
  uint8_t *ram;
  size_t alloc;
  ssize_t ret;
  if (tsf->ram) {
    pthread_mutex_lock(&tsf->ram_lock);
    if (tsf->ram_size < tsf->woff + count) {
      if (tsf->ram_size >= timeshift_conf.ram_segment_size)
        alloc = MAX(count, 64*1024);
      else
        alloc = MAX(count, 4*1024*1024);
      ram = realloc(tsf->ram, tsf->ram_size + alloc);
      if (ram == NULL) {
        tvhwarn(LS_TIMESHIFT, "RAM timeshift memalloc failed");
        pthread_mutex_unlock(&tsf->ram_lock);
        return -1;
      }
      memoryinfo_append(&timeshift_memoryinfo_ram, alloc);
      tsf->ram = ram;
      tsf->ram_size += alloc;
    }
    memcpy(tsf->ram + tsf->woff, buf, count);
    tsf->woff += count;
    pthread_mutex_unlock(&tsf->ram_lock);
    return count;
  }
  ret = _write_fd(tsf->wfd, buf, count);
  if (ret > 0)
    tsf->woff += ret;
  return ret;
}
Esempio n. 3
0
static ssize_t _write_msg_fd
  ( int fd, streaming_message_type_t type, int64_t time,
    const void *buf, size_t len )
{
  size_t len2 = len + sizeof(type) + sizeof(time);
  ssize_t err, ret;
  ret = err = _write_fd(fd, &len2, sizeof(len2));
  if (err < 0) return err;
  err = _write_fd(fd, &type, sizeof(type));
  if (err < 0) return err;
  ret += err;
  err = _write_fd(fd, &time, sizeof(time));
  if (err < 0) return err;
  ret += err;
  if (len) {
    err = _write_fd(fd, buf, len);
    if (err < 0) return err;
    ret += err;
  }
  return ret;
}
int capn_write_fd(struct capn *c, ssize_t (*write_fd)(int fd, void *p, size_t count), int fd, int packed)
{
	unsigned char buf[4096];
	struct capn_segment *seg;
	struct capn_ptr root;
	uint32_t headerlen;
	size_t headersz, datasz = 0;
	int ret;
	struct capn_stream z;
	unsigned char *p;

	if (c->segnum == 0)
		return -1;

	root = capn_root(c);
	header_calc(c, &headerlen, &headersz);

	if (sizeof(buf) < headersz)
		return -1;

	ret = header_render(c, root.seg, (uint32_t*)buf, headerlen, &datasz);
	if (ret != 0)
		return -1;

	if (packed) {
		const int headerrem = sizeof(buf) - headersz;
		const int maxpack = headersz + 2;
		if (headerrem < maxpack)
			return -1;

		memset(&z, 0, sizeof(z));
		z.next_in = buf;
		z.avail_in = headersz;
		z.next_out = buf + headersz;
		z.avail_out = headerrem;
		ret = capn_deflate(&z);
		if (ret != 0)
			return -1;

		p = buf + headersz;
		headersz = headerrem - z.avail_out;
	} else {
		p = buf;
	}

	ret = _write_fd(write_fd, fd, p, headersz);
	if (ret < 0)
		return -1;

	datasz = headersz;
	for (seg = root.seg; seg; seg = seg->next) {
		size_t bufsz;
		if (packed) {
			memset(&z, 0, sizeof(z));
			z.next_in = (uint8_t*)seg->data;
			z.avail_in = seg->len;
			z.next_out = buf;
			z.avail_out = sizeof(buf);
			ret = capn_deflate(&z);
			if (ret != 0)
				return -1;
			p = buf;
			bufsz = sizeof(buf) - z.avail_out;
		} else {
			p = (uint8_t*)seg->data;
			bufsz = seg->len;
		}
		ret = _write_fd(write_fd, fd, p, bufsz);
		if (ret < 0)
			return -1;
		datasz += bufsz;
	}

	return datasz;
}