LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2007, 11:54 AM   #1
patrickdepingui
Member
 
Registered: Mar 2003
Distribution: Slackware
Posts: 53

Rep: Reputation: 15
zlib data errors


I use zlib to write data structures to a compressed file, using the
gzwrite function. Afterwards I read the data back with gzread. I notice
that this works well when the data written is not that much, but when
there is more data to write, after a while I get data errors when
reading back the data.
Error in main: couldn't read stat
zlib error -3: test512-20070531-18h10m02.stat.gz: data error


I wonder whether I am doing something wrong. The data I write are simply
C data structures, struct acmp_stat, which have a fixed size.

I am using my internal buffer, after noticing that it helped the
problem. However, now, even though I buffer I get problems. Writing is
done with:

Code:
if (data->buflen + sizeof(*s) > data->bufmaxlen)
		if (0 != acmp_flush_buffer(data))
			printf("Error: could not flush buffer\n");

	acmp_buffer_stat(data);
Data being:
Code:
struct acmp_data {
	void *fd;
	void *buf;
	size_t buflen;
	size_t bufmaxlen;

	char *statpath;

	struct acmp_stat **stat;
};
The flush buffer function is:

Code:
static int
acmp_flush_buffer(struct acmp_data *data)
{
	size_t len;

	assert(NULL != data);
	assert(NULL != data->fd);

	len = (size_t)gzwrite((gzFile)data->fd, data->buf, data->buflen);

	if (len != data->buflen)
		return 1;

	data->buflen = 0;
	return 0;
}

and acmp_buffer_stat simply:

Code:
static void
acmp_buffer_stat(struct acmp_data *data)
{
	assert(NULL != data);

	memcpy(data->buf + data->buflen, data->stat[data->curcpu], sizeof(**data->stat));
	data->buflen += sizeof(**data->stat);
}

Of course I have opened and closed the file before and after using this
code. The buffer is currently assigned a size of 1 MiB.

Reading back the data is done with:

Code:
static int
acmp_read_stat(/*@out@*/ struct acmp_stat *stat, void *fd)
{
	int err;
	const char *errmsg;
	size_t len;

	assert(NULL != fd);
	assert(stat != NULL);

	len = (size_t)gzread((gzFile)fd, (void *)stat, sizeof(*stat));
	errmsg = gzerror((gzFile)fd, &err);
	if (0 > err)
		printf("zlib error %d: %s \n", err, errmsg);

	if (sizeof(*stat) != len)
		return 1;

	return 0;
}


The code looks ok to me, and the weird thing is that I only get errors
when the amount of data written is large. I didn't find a clear point
yet, from where it seems to fail.
Also notice that there are no errors when writing, but only when reading
back. The files are not touched between reading and writing, and are
cleanly closed.

Am I doing something wrong here?

Thanks a lot, Thomas
 
Old 06-03-2007, 04:19 AM   #2
patrickdepingui
Member
 
Registered: Mar 2003
Distribution: Slackware
Posts: 53

Original Poster
Rep: Reputation: 15
Does anyone have an idea for this?
Thanks, Thomas
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
All user configuration data reset + weird RAM errors amhso Linux - Software 2 12-16-2006 05:53 PM
no matching comp found: client zlib none, zlib@openssh.com lost connection jorge_ivan Linux - Networking 0 08-14-2006 07:09 PM
data type errors microsoft/linux Programming 33 05-04-2006 10:31 PM
mplex errors (Stream e0: data will arrive too late) BrianW Linux - Software 0 04-02-2006 01:32 PM
Data errors on intranet network J_Szucs Linux - Networking 1 08-12-2002 03:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration