Selaa lähdekoodia

Improve zip/jar support:
- Don't support par or war since par is not a standard format and outputting to
war doesn't make sense.
- Add boilerplate manifest when outputting to Jar.
- Remove "XXX check conversion" comments because I don't care.
- Add a test.

kenton@google.com 15 vuotta sitten
vanhempi
commit
d2fcbba230

+ 6 - 3
src/Makefile.am

@@ -31,7 +31,8 @@ nobase_dist_proto_DATA = google/protobuf/descriptor.proto \
 clean-local:
 clean-local:
 	rm -f *.loT
 	rm -f *.loT
 
 
-CLEANFILES = $(protoc_outputs) unittest_proto_middleman
+CLEANFILES = $(protoc_outputs) unittest_proto_middleman \
+             testzip.jar testzip.list testzip.proto testzip.zip
 
 
 MAINTAINERCLEANFILES =   \
 MAINTAINERCLEANFILES =   \
   Makefile.in
   Makefile.in
@@ -278,7 +279,8 @@ COMMON_TEST_SOURCES =                                          \
   google/protobuf/testing/file.cc                              \
   google/protobuf/testing/file.cc                              \
   google/protobuf/testing/file.h
   google/protobuf/testing/file.h
 
 
-check_PROGRAMS = protobuf-test protobuf-lazy-descriptor-test protobuf-lite-test test_plugin $(GZCHECKPROGRAMS)
+check_PROGRAMS = protoc protobuf-test protobuf-lazy-descriptor-test \
+                 protobuf-lite-test test_plugin $(GZCHECKPROGRAMS)
 protobuf_test_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la \
 protobuf_test_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la \
                       $(top_builddir)/gtest/lib/libgtest.la       \
                       $(top_builddir)/gtest/lib/libgtest.la       \
                       $(top_builddir)/gtest/lib/libgtest_main.la
                       $(top_builddir)/gtest/lib/libgtest_main.la
@@ -362,4 +364,5 @@ zcgunzip_LDADD = $(PTHREAD_LIBS) libprotobuf.la
 zcgunzip_SOURCES = google/protobuf/testing/zcgunzip.cc
 zcgunzip_SOURCES = google/protobuf/testing/zcgunzip.cc
 endif
 endif
 
 
-TESTS = protobuf-test protobuf-lazy-descriptor-test protobuf-lite-test $(GZTESTS)
+TESTS = protobuf-test protobuf-lazy-descriptor-test protobuf-lite-test \
+        google/protobuf/compiler/zip_output_unittest.sh $(GZTESTS)

+ 16 - 4
src/google/protobuf/compiler/command_line_interface.cc

@@ -227,6 +227,7 @@ class CommandLineInterface::MemoryOutputDirectory : public OutputDirectory {
 
 
   bool WriteAllToDisk(const string& prefix);
   bool WriteAllToDisk(const string& prefix);
   bool WriteAllToZip(const string& filename);
   bool WriteAllToZip(const string& filename);
+  void AddJarManifest();
 
 
   // implements OutputDirectory --------------------------------------
   // implements OutputDirectory --------------------------------------
   io::ZeroCopyOutputStream* Open(const string& filename);
   io::ZeroCopyOutputStream* Open(const string& filename);
@@ -393,6 +394,16 @@ bool CommandLineInterface::MemoryOutputDirectory::WriteAllToZip(
   return true;
   return true;
 }
 }
 
 
+void CommandLineInterface::MemoryOutputDirectory::AddJarManifest() {
+  string** map_slot = &files_["META-INF/MANIFEST.MF"];
+  if (*map_slot == NULL) {
+    *map_slot = new string(
+        "Manifest-Version: 1.0\n"
+        "Created-By: 1.6.0 (protoc)\n"
+        "\n");
+  }
+}
+
 io::ZeroCopyOutputStream* CommandLineInterface::MemoryOutputDirectory::Open(
 io::ZeroCopyOutputStream* CommandLineInterface::MemoryOutputDirectory::Open(
     const string& filename) {
     const string& filename) {
   return new MemoryOutputStream(this, filename);
   return new MemoryOutputStream(this, filename);
@@ -594,11 +605,8 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
   if (mode_ == MODE_COMPILE) {
   if (mode_ == MODE_COMPILE) {
     for (int i = 0; i < output_directives_.size(); i++) {
     for (int i = 0; i < output_directives_.size(); i++) {
       string output_location = output_directives_[i].output_location;
       string output_location = output_directives_[i].output_location;
-      cout << "location: " << output_location << endl;
       if (!HasSuffixString(output_location, ".zip") &&
       if (!HasSuffixString(output_location, ".zip") &&
-          !HasSuffixString(output_location, ".jar") &&
-          !HasSuffixString(output_location, ".war") &&
-          !HasSuffixString(output_location, ".par")) {
+          !HasSuffixString(output_location, ".jar")) {
         AddTrailingSlash(&output_location);
         AddTrailingSlash(&output_location);
       }
       }
       MemoryOutputDirectory** map_slot = &output_directories_[output_location];
       MemoryOutputDirectory** map_slot = &output_directories_[output_location];
@@ -624,6 +632,10 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
         return 1;
         return 1;
       }
       }
     } else {
     } else {
+      if (HasSuffixString(location, ".jar")) {
+        directory->AddJarManifest();
+      }
+
       if (!directory->WriteAllToZip(location)) {
       if (!directory->WriteAllToZip(location)) {
         return 1;
         return 1;
       }
       }

+ 85 - 0
src/google/protobuf/compiler/zip_output_unittest.sh

@@ -0,0 +1,85 @@
+#!/bin/sh
+#
+# Protocol Buffers - Google's data interchange format
+# Copyright 2009 Google Inc.  All rights reserved.
+# http://code.google.com/p/protobuf/
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Author: kenton@google.com (Kenton Varda)
+#
+# Test protoc's zip output mode.
+
+function fail() {
+  echo "$@" >&2
+  exit 1
+}
+
+echo '
+  option java_multiple_files = true;
+  option java_package = "test.jar";
+  option java_outer_classname = "Outer";
+  message Foo {}
+  message Bar {}
+' > testzip.proto
+
+./protoc --cpp_out=testzip.zip --python_out=testzip.zip --java_out=testzip.jar \
+    testzip.proto || fail 'protoc failed.'
+
+echo "Testing output to zip..."
+if unzip -h > /dev/null; then
+  unzip -t testzip.zip > testzip.list || fail 'unzip failed.'
+
+  grep 'testing: testzip\.pb\.cc *OK$' testzip.list > /dev/null \
+    || fail 'testzip.pb.cc not found in output zip.'
+  grep 'testing: testzip\.pb\.h *OK$' testzip.list > /dev/null \
+    || fail 'testzip.pb.h not found in output zip.'
+  grep 'testing: testzip_pb2\.py *OK$' testzip.list > /dev/null \
+    || fail 'testzip_pb2.py not found in output zip.'
+  grep -i 'manifest' testzip.list > /dev/null \
+    && fail 'Zip file contained manifest.'
+else
+  echo "Warning:  'unzip' command not available.  Skipping test."
+fi
+
+echo "Testing output to jar..."
+if jar c testzip.proto > /dev/null; then
+  jar tf testzip.jar > testzip.list || fail 'jar failed.'
+
+  grep '^test/jar/Foo\.java$' testzip.list > /dev/null \
+    || fail 'Foo.java not found in output jar.'
+  grep '^test/jar/Bar\.java$' testzip.list > /dev/null \
+    || fail 'Bar.java not found in output jar.'
+  grep '^test/jar/Outer\.java$' testzip.list > /dev/null \
+    || fail 'Outer.java not found in output jar.'
+  grep '^META-INF/MANIFEST\.MF$' testzip.list > /dev/null \
+    || fail 'Manifest not ofund in output jar.'
+else
+  echo "Warning:  'jar' command not available.  Skipping test."
+fi
+
+echo PASS

+ 7 - 8
src/google/protobuf/compiler/zip_writer.cc

@@ -86,7 +86,6 @@ static const uint32 kCRC32Table[256] = {
   0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
 };
 };
 
 
-// XXX this appears to be broken, but unzip -t accepts it anyway?  wtff?
 static uint32 ComputeCRC32(const string &buf) {
 static uint32 ComputeCRC32(const string &buf) {
   uint32 x = ~0U;
   uint32 x = ~0U;
   for (int i = 0; i < buf.size(); ++i) {
   for (int i = 0; i < buf.size(); ++i) {
@@ -111,9 +110,9 @@ bool ZipWriter::Write(const string& filename, const string& contents) {
   FileInfo info;
   FileInfo info;
 
 
   info.name = filename;
   info.name = filename;
-  uint16 filename_size = filename.size();  // XXX check conversion
-  info.offset = raw_output_->ByteCount();  // XXX check conversion
-  info.size = contents.size();  // XXX check conversion
+  uint16 filename_size = filename.size();
+  info.offset = raw_output_->ByteCount();
+  info.size = contents.size();
   info.crc32 = ComputeCRC32(contents);
   info.crc32 = ComputeCRC32(contents);
 
 
   files_.push_back(info);
   files_.push_back(info);
@@ -138,14 +137,14 @@ bool ZipWriter::Write(const string& filename, const string& contents) {
 }
 }
 
 
 bool ZipWriter::WriteDirectory() {
 bool ZipWriter::WriteDirectory() {
-  uint16 num_entries = files_.size();  // XXX check conversion
-  uint32 dir_ofs = raw_output_->ByteCount();  // XXX check conversion
+  uint16 num_entries = files_.size();
+  uint32 dir_ofs = raw_output_->ByteCount();
 
 
   // write central directory
   // write central directory
   io::CodedOutputStream output(raw_output_);
   io::CodedOutputStream output(raw_output_);
   for (int i = 0; i < num_entries; ++i) {
   for (int i = 0; i < num_entries; ++i) {
     const string &filename = files_[i].name;
     const string &filename = files_[i].name;
-    uint16 filename_size = filename.size();  // XXX check conversion
+    uint16 filename_size = filename.size();
     uint32 crc32 = files_[i].crc32;
     uint32 crc32 = files_[i].crc32;
     uint32 size = files_[i].size;
     uint32 size = files_[i].size;
     uint32 offset = files_[i].offset;
     uint32 offset = files_[i].offset;
@@ -169,7 +168,7 @@ bool ZipWriter::WriteDirectory() {
     output.WriteLittleEndian32(offset);  // local header offset
     output.WriteLittleEndian32(offset);  // local header offset
     output.WriteString(filename);  // file name
     output.WriteString(filename);  // file name
   }
   }
-  uint32 dir_len = output.ByteCount();  // XXX check conversion
+  uint32 dir_len = output.ByteCount();
 
 
   // write end of central directory marker
   // write end of central directory marker
   output.WriteLittleEndian32(0x06054b50);  // magic
   output.WriteLittleEndian32(0x06054b50);  // magic