X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fzip-writer.c;h=b8240efe6bb8ba194308cb1aff64dc0229ce70e1;hb=a09e5b9e6c18d1fb2466f9192fe2172958c05d63;hp=e286a7eb1c5f281ddc739d88ab9c3a33e57c512b;hpb=e2da62d735c597afeef2e0e9b36e5a4a83d7da94;p=pspp diff --git a/src/libpspp/zip-writer.c b/src/libpspp/zip-writer.c index e286a7eb1c..b8240efe6b 100644 --- a/src/libpspp/zip-writer.c +++ b/src/libpspp/zip-writer.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010, 2012 Free Software Foundation, Inc. + Copyright (C) 2010, 2012, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,12 +23,14 @@ #include #include #include +#include #include "gl/crc.h" #include "gl/fwriteerror.h" #include "gl/xalloc.h" #include "libpspp/message.h" +#include "libpspp/temp-file.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -37,9 +39,12 @@ struct zip_writer { char *file_name; /* File name, for use in error mesages. */ FILE *file; /* Output stream. */ + uint32_t offset; /* Offset in output stream. */ uint16_t date, time; /* Date and time in MS-DOS format. */ + bool ok; + /* Members already added to the file, so that we can summarize them to the central directory at the end of the ZIP file. */ struct zip_member *members; @@ -58,6 +63,7 @@ static void put_bytes (struct zip_writer *zw, const void *p, size_t n) { fwrite (p, 1, n, zw->file); + zw->offset += n; } static void @@ -88,16 +94,32 @@ zip_writer_create (const char *file_name) time_t now; FILE *file; - file = fopen (file_name, "wb"); - if (file == NULL) + if (strcmp (file_name, "-")) + { + file = fopen (file_name, "wb"); + if (file == NULL) + { + msg_error (errno, _("%s: error opening output file"), file_name); + return NULL; + } + } + else { - msg_error (errno, _("%s: error opening output file"), file_name); - return NULL; + if (isatty (STDOUT_FILENO)) + { + msg (ME, _("%s: not writing ZIP file to terminal"), file_name); + return NULL; + } + + file = stdout; } zw = xmalloc (sizeof *zw); zw->file_name = xstrdup (file_name); zw->file = file; + zw->offset = 0; + + zw->ok = true; now = time (NULL); tm = localtime (&now); @@ -111,30 +133,37 @@ zip_writer_create (const char *file_name) return zw; } +static void +put_local_header (struct zip_writer *zw, const char *member_name, uint32_t crc, + uint32_t size, int flag) +{ + put_u32 (zw, MAGIC_LHDR); /* local file header signature */ + put_u16 (zw, 10); /* version needed to extract */ + put_u16 (zw, flag); /* general purpose bit flag */ + put_u16 (zw, 0); /* compression method */ + put_u16 (zw, zw->time); /* last mod file time */ + put_u16 (zw, zw->date); /* last mod file date */ + put_u32 (zw, crc); /* crc-32 */ + put_u32 (zw, size); /* compressed size */ + put_u32 (zw, size); /* uncompressed size */ + put_u16 (zw, strlen (member_name)); /* file name length */ + put_u16 (zw, 0); /* extra field length */ + put_bytes (zw, member_name, strlen (member_name)); +} + /* Adds the contents of FILE, with name MEMBER_NAME, to ZW. */ void zip_writer_add (struct zip_writer *zw, FILE *file, const char *member_name) { struct zip_member *member; - uint32_t offset, size; + uint32_t size; size_t bytes_read; uint32_t crc; char buf[4096]; /* Local file header. */ - offset = ftello (zw->file); - put_u32 (zw, MAGIC_LHDR); /* local file header signature */ - put_u16 (zw, 10); /* version needed to extract */ - put_u16 (zw, 1 << 3); /* general purpose bit flag */ - put_u16 (zw, 0); /* compression method */ - put_u16 (zw, zw->time); /* last mod file time */ - put_u16 (zw, zw->date); /* last mod file date */ - put_u32 (zw, 0); /* crc-32 */ - put_u32 (zw, 0); /* compressed size */ - put_u32 (zw, 0); /* uncompressed size */ - put_u16 (zw, strlen (member_name)); /* file name length */ - put_u16 (zw, 0); /* extra field length */ - put_bytes (zw, member_name, strlen (member_name)); + uint32_t offset = zw->offset; + put_local_header (zw, member_name, 0, 0, 1 << 3); /* File data. */ size = crc = 0; @@ -146,11 +175,27 @@ zip_writer_add (struct zip_writer *zw, FILE *file, const char *member_name) crc = crc32_update (crc, buf, bytes_read); } - /* Data descriptor. */ - put_u32 (zw, MAGIC_DDHD); - put_u32 (zw, crc); - put_u32 (zw, size); - put_u32 (zw, size); + /* Try to seek back to the local file header. If successful, overwrite it + with the correct file size and CRC. Otherwise, write data descriptor. */ + if (fseeko (zw->file, offset, SEEK_SET) == 0) + { + uint32_t save_offset = zw->offset; + put_local_header (zw, member_name, crc, size, 0); + if (fseeko (zw->file, size, SEEK_CUR) + && zw->ok) + { + msg_error (errno, _("%s: error seeking in output file"), zw->file_name); + zw->ok = false; + } + zw->offset = save_offset; + } + else + { + put_u32 (zw, MAGIC_DDHD); + put_u32 (zw, crc); + put_u32 (zw, size); + put_u32 (zw, size); + } /* Add to set of members. */ if (zw->n_members >= zw->allocated_members) @@ -163,6 +208,33 @@ zip_writer_add (struct zip_writer *zw, FILE *file, const char *member_name) member->name = xstrdup (member_name); } +/* Adds a member named MEMBER_NAME whose contents is the null-terminated string + CONTENT. */ +void +zip_writer_add_string (struct zip_writer *zw, const char *member_name, + const char *content) +{ + zip_writer_add_memory (zw, member_name, content, strlen (content)); +} + +/* Adds a member named MEMBER_NAME whose contents is the SIZE bytes of + CONTENT. */ +void +zip_writer_add_memory (struct zip_writer *zw, const char *member_name, + const void *content, size_t size) +{ + FILE *fp = create_temp_file (); + if (fp == NULL) + { + msg_error (errno, _("error creating temporary file")); + zw->ok = false; + return; + } + fwrite (content, size, 1, fp); + zip_writer_add (zw, fp, member_name); + close_temp_file (fp); +} + /* Finalizes the contents of ZW and closes it. Returns true if successful, false if a write error occurred while finalizing the file or at any earlier time. */ @@ -176,7 +248,7 @@ zip_writer_close (struct zip_writer *zw) if (zw == NULL) return true; - dir_start = ftello (zw->file); + dir_start = zw->offset; for (i = 0; i < zw->n_members; i++) { struct zip_member *m = &zw->members[i]; @@ -203,7 +275,7 @@ zip_writer_close (struct zip_writer *zw) free (m->name); } free (zw->members); - dir_end = ftello (zw->file); + dir_end = zw->offset; /* End of central directory record. */ put_u32 (zw, MAGIC_EOCD); /* end of central dir signature */ @@ -220,9 +292,8 @@ zip_writer_close (struct zip_writer *zw) the starting disk number */ put_u16 (zw, 0); /* .ZIP file comment length */ - if (!fwriteerror (zw->file)) - ok = true; - else + ok = zw->ok; + if (ok && zw->file != stdout && fwriteerror (zw->file)) { msg_error (errno, _("%s: write failed"), zw->file_name); ok = false;