avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dcrea...@apache.org
Subject svn commit: r1486051 - in /avro/trunk: ./ lang/c/src/ lang/c/src/avro/ lang/c/tests/
Date Fri, 24 May 2013 13:37:36 GMT
Author: dcreager
Date: Fri May 24 13:37:36 2013
New Revision: 1486051

URL: http://svn.apache.org/r1486051
Log:
AVRO-1238. C: EOF detection in avro_file_reader_read_value

This function now returns the builtin `EOF` constant when end-of-file is
detected.  `0` still indicates a successful read, and other results indicate
errors.

Contributed by Michael Cooper.

Added:
    avro/trunk/lang/c/tests/avro-1238-good.avro   (with props)
    avro/trunk/lang/c/tests/avro-1238-truncated.avro   (with props)
    avro/trunk/lang/c/tests/test_avro_1238.c
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/avro/io.h
    avro/trunk/lang/c/src/avrocat.c
    avro/trunk/lang/c/src/avromod.c
    avro/trunk/lang/c/src/avropipe.c
    avro/trunk/lang/c/src/datafile.c
    avro/trunk/lang/c/src/io.c
    avro/trunk/lang/c/tests/CMakeLists.txt

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri May 24 13:37:36 2013
@@ -56,6 +56,9 @@ Trunk (not yet released)
     AVRO-1334. Java: Upgrade snappy-java dependency to 1.0.5
     (scottcarey)
 
+    AVRO-1238. C: EOF detection in avro_file_reader_read_value.
+    (Michael Cooper via dcreager)
+
   BUG FIXES
 
     AVRO-1296. Python: Fix schemas retrieved from protocol types

Modified: avro/trunk/lang/c/src/avro/io.h
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avro/io.h?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avro/io.h (original)
+++ avro/trunk/lang/c/src/avro/io.h Fri May 24 13:37:36 2013
@@ -65,6 +65,8 @@ void avro_writer_flush(avro_writer_t wri
 void avro_writer_dump(avro_writer_t writer, FILE * fp);
 void avro_reader_dump(avro_reader_t reader, FILE * fp);
 
+int avro_reader_is_eof(avro_reader_t reader);
+
 void avro_reader_free(avro_reader_t reader);
 void avro_writer_free(avro_writer_t writer);
 

Modified: avro/trunk/lang/c/src/avrocat.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avrocat.c?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avrocat.c (original)
+++ avro/trunk/lang/c/src/avrocat.c Fri May 24 13:37:36 2013
@@ -65,7 +65,9 @@ process_file(const char *filename)
 	iface = avro_generic_class_from_schema(wschema);
 	avro_generic_value_new(iface, &value);
 
-	while (avro_file_reader_read_value(reader, &value) == 0) {
+	int rval;
+
+	while ((rval = avro_file_reader_read_value(reader, &value)) == 0) {
 		char  *json;
 
 		if (avro_value_to_json(&value, 1, &json)) {
@@ -79,7 +81,9 @@ process_file(const char *filename)
 		avro_value_reset(&value);
 	}
 
-	if (!feof(fp)) {
+	// If it was not an EOF that caused it to fail,
+	// print the error.
+	if (rval != EOF) {
 		fprintf(stderr, "Error: %s\n", avro_strerror());
 	}
 

Modified: avro/trunk/lang/c/src/avromod.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avromod.c?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avromod.c (original)
+++ avro/trunk/lang/c/src/avromod.c Fri May 24 13:37:36 2013
@@ -57,6 +57,7 @@ process_file(const char *in_filename, co
 	avro_schema_t  wschema;
 	avro_value_iface_t  *iface;
 	avro_value_t  value;
+	int rval;
 
 	wschema = avro_file_reader_get_writer_schema(reader);
 	iface = avro_generic_class_from_schema(wschema);
@@ -69,7 +70,7 @@ process_file(const char *in_filename, co
 		exit(1);
 	}
 
-	while (avro_file_reader_read_value(reader, &value) == 0) {
+	while ((rval = avro_file_reader_read_value(reader, &value)) == 0) {
 		if (avro_file_writer_append_value(writer, &value)) {
 			fprintf(stderr, "Error writing to %s:\n  %s\n",
 				out_filename, avro_strerror());
@@ -78,6 +79,10 @@ process_file(const char *in_filename, co
 		avro_value_reset(&value);
 	}
 
+	if (rval != EOF) {
+		fprintf(stderr, "Error reading value: %s", avro_strerror());
+	}
+
 	avro_file_reader_close(reader);
 	avro_file_writer_close(writer);
 	avro_value_decref(&value);

Modified: avro/trunk/lang/c/src/avropipe.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avropipe.c?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avropipe.c (original)
+++ avro/trunk/lang/c/src/avropipe.c Fri May 24 13:37:36 2013
@@ -362,13 +362,18 @@ process_file(const char *filename)
 	avro_generic_value_new(iface, &value);
 
 	size_t  record_number = 0;
+	int rval;
 
-	for (; avro_file_reader_read_value(reader, &value) == 0; record_number++) {
+	for (; (rval = avro_file_reader_read_value(reader, &value)) == 0; record_number++) {
 		create_array_prefix(&prefix, "", record_number);
 		process_value((const char *) avro_raw_string_get(&prefix), &value);
 		avro_value_reset(&value);
 	}
 
+	if (rval != EOF) {
+		fprintf(stderr, "Error reading value: %s", avro_strerror());
+	}
+
 	avro_raw_string_done(&prefix);
 	avro_value_decref(&value);
 	avro_value_iface_decref(iface);

Modified: avro/trunk/lang/c/src/datafile.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datafile.c?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datafile.c (original)
+++ avro/trunk/lang/c/src/datafile.c Fri May 24 13:37:36 2013
@@ -684,9 +684,6 @@ avro_file_reader_read_value(avro_file_re
 	check_param(EINVAL, r, "reader");
 	check_param(EINVAL, value, "value");
 
-	check(rval, avro_value_read(r->block_reader, value));
-	r->blocks_read++;
-
 	if (r->blocks_read == r->blocks_total) {
 		check(rval, avro_read(r->reader, sync, sizeof(sync)));
 		if (memcmp(r->sync, sync, sizeof(r->sync)) != 0) {
@@ -694,9 +691,17 @@ avro_file_reader_read_value(avro_file_re
 			avro_set_error("Incorrect sync bytes");
 			return EILSEQ;
 		}
-		/* For now, ignore errors (e.g. EOF) */
-		file_read_block_count(r);
+
+		/* Did we just hit the end of the file? */
+		if (avro_reader_is_eof(r->reader))
+			return EOF;
+
+		check(rval, file_read_block_count(r));
 	}
+
+	check(rval, avro_value_read(r->block_reader, value));
+	r->blocks_read++;
+
 	return 0;
 }
 

Modified: avro/trunk/lang/c/src/io.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/io.c?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/src/io.c (original)
+++ avro/trunk/lang/c/src/io.c Fri May 24 13:37:36 2013
@@ -223,7 +223,7 @@ avro_read_file(struct _avro_reader_file_
 		if (rval != needed) {
 			avro_set_error("Cannot read %" PRIsz " bytes from file",
 				       (size_t) needed);
-			return -1;
+			return EILSEQ;
 		}
 		return 0;
 	} else if (needed <= bytes_available(reader)) {
@@ -241,7 +241,7 @@ avro_read_file(struct _avro_reader_file_
 		if (rval == 0) {
 			avro_set_error("Cannot read %" PRIsz " bytes from file",
 				       (size_t) needed);
-			return -1;
+			return EILSEQ;
 		}
 		reader->cur = reader->buffer;
 		reader->end = reader->cur + rval;
@@ -249,7 +249,7 @@ avro_read_file(struct _avro_reader_file_
 		if (bytes_available(reader) < needed) {
 			avro_set_error("Cannot read %" PRIsz " bytes from file",
 				       (size_t) needed);
-			return -1;
+			return EILSEQ;
 		}
 		memcpy(p, reader->cur, needed);
 		reader->cur += needed;
@@ -257,7 +257,7 @@ avro_read_file(struct _avro_reader_file_
 	}
 	avro_set_error("Cannot read %" PRIsz " bytes from file",
 		       (size_t) needed);
-	return -1;
+	return EILSEQ;
 }
 
 int avro_read(avro_reader_t reader, void *buf, int64_t len)
@@ -345,7 +345,7 @@ avro_write_file(struct _avro_writer_file
 	if (len > 0) {
 		rval = fwrite(buf, len, 1, writer->fp);
 		if (rval == 0) {
-			return feof(writer->fp) ? -1 : 0;
+			return feof(writer->fp) ? EOF : 0;
 		}
 	}
 	return 0;
@@ -385,7 +385,7 @@ int64_t avro_writer_tell(avro_writer_t w
 	if (is_memory_io(writer)) {
 		return avro_writer_to_memory(writer)->written;
 	}
-	return -1;
+	return EINVAL;
 }
 
 void avro_writer_flush(avro_writer_t writer)
@@ -434,3 +434,11 @@ void avro_writer_free(avro_writer_t writ
 		avro_freet(struct _avro_writer_file_t, writer);
 	}
 }
+
+int avro_reader_is_eof(avro_reader_t reader)
+{
+	if (is_file_io(reader)) {
+		return feof(avro_reader_to_file(reader)->fp);
+	}
+	return 0;
+}

Modified: avro/trunk/lang/c/tests/CMakeLists.txt
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/CMakeLists.txt?rev=1486051&r1=1486050&r2=1486051&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/CMakeLists.txt (original)
+++ avro/trunk/lang/c/tests/CMakeLists.txt Fri May 24 13:37:36 2013
@@ -54,6 +54,7 @@ add_avro_test(test_avro_1034)
 add_avro_test(test_avro_1084)
 add_avro_test(test_avro_1087)
 add_avro_test(test_avro_1165)
+add_avro_test(test_avro_1238)
 add_avro_test(test_avro_data)
 add_avro_test(test_refcount)
 add_avro_test(test_cpp test_cpp.cpp)

Added: avro/trunk/lang/c/tests/avro-1238-good.avro
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/avro-1238-good.avro?rev=1486051&view=auto
==============================================================================
Binary file - no diff available.

Propchange: avro/trunk/lang/c/tests/avro-1238-good.avro
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: avro/trunk/lang/c/tests/avro-1238-truncated.avro
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/avro-1238-truncated.avro?rev=1486051&view=auto
==============================================================================
Binary file - no diff available.

Propchange: avro/trunk/lang/c/tests/avro-1238-truncated.avro
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: avro/trunk/lang/c/tests/test_avro_1238.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/test_avro_1238.c?rev=1486051&view=auto
==============================================================================
--- avro/trunk/lang/c/tests/test_avro_1238.c (added)
+++ avro/trunk/lang/c/tests/test_avro_1238.c Fri May 24 13:37:36 2013
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+#include <avro.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define check_exit(call) \
+	do { \
+		int  __rc = call; \
+		if (__rc != 0) { \
+			fprintf(stderr, "Unexpected error:\n  %s\n  %s\n", \
+				avro_strerror(), #call); \
+			exit(EXIT_FAILURE); \
+		} \
+	} while (0)
+
+#define expect_eof(call) \
+	do { \
+		int  __rc = call; \
+		if (__rc != EOF) { \
+			fprintf(stderr, "Expected EOF:\n  %s\n", #call); \
+			exit(EXIT_FAILURE); \
+		} \
+	} while (0)
+
+#define expect_error(call) \
+	do { \
+		int  __rc = call; \
+		if (__rc == 0) { \
+			fprintf(stderr, "Expected an error:\n  %s\n", #call); \
+			exit(EXIT_FAILURE); \
+		} \
+	} while (0)
+
+#define check_expected_value(actual, expected) \
+	do { \
+		if (!avro_value_equal_fast((actual), (expected))) { \
+			char  *actual_json; \
+			char  *expected_json; \
+			avro_value_to_json((actual), 1, &actual_json); \
+			avro_value_to_json((expected), 1, &expected_json); \
+			fprintf(stderr, "Expected %s\nGot      %s\n", \
+				expected_json, actual_json); \
+			free(actual_json); \
+			free(expected_json); \
+			exit(EXIT_FAILURE); \
+		} \
+	} while (0)
+
+int main(void)
+{
+	avro_schema_t  schema;
+	avro_file_reader_t  reader;
+	avro_value_iface_t  *iface;
+	avro_value_t  actual;
+	avro_value_t  expected;
+	avro_value_t  branch;
+
+	schema = avro_schema_union();
+	avro_schema_union_append(schema, avro_schema_null());
+	avro_schema_union_append(schema, avro_schema_int());
+
+	iface = avro_generic_class_from_schema(schema);
+	avro_generic_value_new(iface, &actual);
+	avro_generic_value_new(iface, &expected);
+
+
+	/* First read the contents of the good file. */
+
+	check_exit(avro_file_reader("avro-1238-good.avro", &reader));
+
+	check_exit(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_value_set_branch(&expected, 0, &branch));
+	check_exit(avro_value_set_null(&branch));
+	check_expected_value(&actual, &expected);
+
+	check_exit(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_value_set_branch(&expected, 1, &branch));
+	check_exit(avro_value_set_int(&branch, 100));
+	check_expected_value(&actual, &expected);
+
+	expect_eof(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_file_reader_close(reader));
+
+
+	/* Then read from the truncated file. */
+
+	check_exit(avro_file_reader("avro-1238-truncated.avro", &reader));
+
+	check_exit(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_value_set_branch(&expected, 0, &branch));
+	check_exit(avro_value_set_null(&branch));
+	check_expected_value(&actual, &expected);
+
+	check_exit(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_value_set_branch(&expected, 1, &branch));
+	check_exit(avro_value_set_int(&branch, 100));
+	check_expected_value(&actual, &expected);
+
+	expect_error(avro_file_reader_read_value(reader, &actual));
+	check_exit(avro_file_reader_close(reader));
+
+
+	/* Clean up and exit */
+	avro_value_decref(&actual);
+	avro_value_decref(&expected);
+	avro_value_iface_decref(iface);
+	avro_schema_decref(schema);
+	exit(EXIT_SUCCESS);
+}



Mime
View raw message