Return-Path: X-Original-To: apmail-avro-commits-archive@www.apache.org Delivered-To: apmail-avro-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AEBC286F for ; Mon, 18 Jun 2012 21:40:48 +0000 (UTC) Received: (qmail 4827 invoked by uid 500); 18 Jun 2012 21:40:48 -0000 Delivered-To: apmail-avro-commits-archive@avro.apache.org Received: (qmail 4762 invoked by uid 500); 18 Jun 2012 21:40:48 -0000 Mailing-List: contact commits-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list commits@avro.apache.org Received: (qmail 4752 invoked by uid 99); 18 Jun 2012 21:40:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Jun 2012 21:40:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Jun 2012 21:40:47 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E5E3A2388C8A for ; Mon, 18 Jun 2012 21:40:26 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1351497 - in /avro/trunk: CHANGES.txt lang/c/src/CMakeLists.txt lang/c/src/avroappend.c Date: Mon, 18 Jun 2012 21:40:26 -0000 To: commits@avro.apache.org From: dcreager@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120618214026.E5E3A2388C8A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dcreager Date: Mon Jun 18 21:40:26 2012 New Revision: 1351497 URL: http://svn.apache.org/viewvc?rev=1351497&view=rev Log: AVRO-1104. C: avroappend utility Contributed by Lucas Martin-King. Added: avro/trunk/lang/c/src/avroappend.c Modified: avro/trunk/CHANGES.txt avro/trunk/lang/c/src/CMakeLists.txt Modified: avro/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1351497&r1=1351496&r2=1351497&view=diff ============================================================================== --- avro/trunk/CHANGES.txt (original) +++ avro/trunk/CHANGES.txt Mon Jun 18 21:40:26 2012 @@ -13,6 +13,8 @@ Avro 1.7.1 (unreleased) AVRO-1108. Java: Add support for reflect API to newer mapreduce API. (cutting) + AVRO-1104. C: avroappend utility. (Lucas Martin-King via dcreager) + IMPROVEMENTS BUG FIXES Modified: avro/trunk/lang/c/src/CMakeLists.txt URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/CMakeLists.txt?rev=1351497&r1=1351496&r2=1351497&view=diff ============================================================================== --- avro/trunk/lang/c/src/CMakeLists.txt (original) +++ avro/trunk/lang/c/src/CMakeLists.txt Mon Jun 18 21:40:26 2012 @@ -148,6 +148,10 @@ add_executable(avrocat avrocat.c) target_link_libraries(avrocat avro-static) install(TARGETS avrocat RUNTIME DESTINATION bin) +add_executable(avroappend avroappend.c) +target_link_libraries(avroappend avro-static) +install(TARGETS avroappend RUNTIME DESTINATION bin) + if (NOT WIN32) #TODO: Port getopt() to Windows to compile avropipe.c and avromod.c add_executable(avropipe avropipe.c) Added: avro/trunk/lang/c/src/avroappend.c URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avroappend.c?rev=1351497&view=auto ============================================================================== --- avro/trunk/lang/c/src/avroappend.c (added) +++ avro/trunk/lang/c/src/avroappend.c Mon Jun 18 21:40:26 2012 @@ -0,0 +1,165 @@ +/* + * 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 +#include +#include +#include +#include + +#include "avro.h" + +int process_file(const char *in_filename, const char *out_filename) +{ + avro_file_reader_t reader; + avro_file_writer_t writer; + + if (in_filename == NULL) { + if (avro_file_reader_fp(stdin, "", 0, &reader)) { + fprintf(stderr, "Error opening :\n %s\n", + avro_strerror()); + return 1; + } + } else { + if (avro_file_reader(in_filename, &reader)) { + fprintf(stderr, "Error opening %s:\n %s\n", + in_filename, avro_strerror()); + return 1; + } + } + + avro_schema_t wschema; + wschema = avro_file_reader_get_writer_schema(reader); + + /* Check that the reader schema is the same as the writer schema */ + { + avro_schema_t oschema; + avro_file_reader_t oreader; + + if (avro_file_reader(out_filename, &oreader)) { + fprintf(stderr, "Error opening %s:\n %s\n", + out_filename, avro_strerror()); + avro_file_reader_close(reader); + return 1; + } + + oschema = avro_file_reader_get_writer_schema(oreader); + + if (avro_schema_equal(oschema, wschema) == 0) { + fprintf(stderr, "Error: reader and writer schema are not equal.\n"); + avro_file_reader_close(oreader); + avro_file_reader_close(reader); + return 1; + } + + avro_file_reader_close(oreader); + } + + if (avro_file_writer_open(out_filename, &writer)) { + fprintf(stderr, "Error opening %s:\n %s\n", + out_filename, avro_strerror()); + avro_file_reader_close(reader); + return 1; + } + + avro_value_iface_t *iface; + avro_value_t value; + + iface = avro_generic_class_from_schema(wschema); + avro_generic_value_new(iface, &value); + + while (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()); + return 1; + } + avro_value_reset(&value); + } + + avro_file_reader_close(reader); + avro_file_writer_close(writer); + avro_value_decref(&value); + avro_value_iface_decref(iface); + + return 0; +} + +static void usage(void) +{ + fprintf(stderr, + "Usage: avroappend [] \n"); +} + +static int check_filenames(const char *in_filename, const char *out_filename) +{ + if (in_filename == NULL) { + return 0; + } + + struct stat in_stat; + struct stat out_stat; + + if (stat(in_filename, &in_stat) == -1) { + fprintf(stderr, "stat error on %s: %s\n", in_filename, strerror(errno)); + return 2; + } + + if (stat(out_filename, &out_stat) == -1) { + fprintf(stderr, "stat error on %s: %s\n", out_filename, strerror(errno)); + return 2; + } + + if (in_stat.st_dev == out_stat.st_dev && in_stat.st_ino == out_stat.st_ino) { + return 1; + } + + return 0; +} + +int main(int argc, char **argv) +{ + char *in_filename; + char *out_filename; + + argc--; + argv++; + + if (argc == 2) { + in_filename = argv[0]; + out_filename = argv[1]; + } else if (argc == 1) { + in_filename = NULL; + out_filename = argv[0]; + } else { + fprintf(stderr, "Not enough arguments\n\n"); + usage(); + exit(1); + } + + int ret = check_filenames(in_filename, out_filename); + + if (ret == 1) { + fprintf(stderr, "Files are the same.\n"); + } + + if (ret > 0) { + exit(1); + } + + exit(process_file(in_filename, out_filename)); +}