<commit_before>/*
  $NiH: zip_close.c,v 1.37 2003/10/06 22:44:06 dillo Exp $

  zip_close.c -- close zip archive and update changes
  Copyright (C) 1999 <NAME> and <NAME>

  This file is part of libzip, a library to manipulate ZIP archives.
  The authors can be contacted at <<EMAIL>>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. 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.
  3. The names of the authors may not be used to endorse or promote
     products derived from this software without specific prior
     written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
*/




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "zip.h"
#include "zipint.h"

static int _zip_entry_copy(struct zip *dest, struct zip *src,
			  int entry_no, struct zip_meta *meta);
static int _zip_entry_add(struct zip *dest, struct zip_entry *se);
static int _zip_writecdir(struct zip *zfp);
static void _zip_write2(FILE *fp, int i);
static void _zip_write4(FILE *fp, int i);
static void _zip_writestr(FILE *fp, char *str, int len);
static int _zip_writecdentry(FILE *fp, struct zip_entry *zfe, int localp);
static struct zip_entry *_zip_create_entry(struct zip *dest, 
			                   struct zip_entry *src_entry,
			                   char *name, struct zip_meta *meta);
static void _zip_u2d_time(time_t time, int *ddate, int *dtime);
static int _zip_fwrite(char *b, int s, int n, FILE *f);




/* zip_close:
   Tries to commit all changes and close the zipfile; if it fails,
   zip_err (and errno) are set and *zf is unchanged, except for
   problems in _zip_free. */ 

int
zip_close(struct zip *zf)
{
    int i, count, tfd, ret, survivors;
    char *temp;
    FILE *tfp;
    struct zip *tzf;
    mode_t mask;

    count = survivors = 0;
    for (i=0; i<zf->nentry; i++) {
	if (zf->entry[i].state != ZIP_ST_UNCHANGED)
	    count = 1;
	if (zf->entry[i].state != ZIP_ST_DELETED)
	    survivors = 1;
	if (survivors && count)
	    break;
    }

    /* no changes */
    if (count == 0) {
	_zip_free(zf);
	return 0;
    }

    /* don't create zip files with no entries */
    if (survivors == 0) {
	ret = 0;
	if (zf->zn)
	    ret = remove(zf->zn);
	_zip_free(zf);
	/* XXX: inconsistent: zf freed, returned -1 */
	return ret;
    }	       
	
    temp = (char *)malloc(strlen(zf->zn)+8);
    if (!temp) {
	_zip_error_set(&zf->error, ZERR_MEMORY, 0);
	return -1;
    }

    sprintf(temp, "%s.XXXXXX", zf->zn);

    tfd = mkstemp(temp);
    if (tfd == -1) {
	_zip_error_set(&zf->error, ZERR_TMPOPEN, errno);
	free(temp);
	return -1;
    }
    
    if ((tfp=fdopen(tfd, "r+b")) == NULL) {
	_zip_error_set(&zf->error, ZERR_TMPOPEN, errno);
	close(tfd);
	remove(temp);
	free(temp);
	return -1;
    }

    if ((tzf=_zip_new(&zf->error.zip_err)) == NULL) {
	fclose(tfp);
	remove(temp);
	free(temp);
	return -1;
    }
    tzf->zp = tfp;
    tzf->zn = temp;
    tzf->comlen = zf->comlen;
    if ((tzf->com=(unsigned char *)_zip_memdup(zf->com, zf->comlen)) == NULL) {
	_zip_error_set(&zf->error, ZERR_MEMORY, 0);
	remove(tzf->zn);
	_zip_free(tzf);
	return -1;
    }

#if 0
    count = 0;
    if (zf->entry) {
	for (i=0; i<zf->nentry; i++) {
	    switch (zf->entry[i].state) {
	    case ZIP_ST_UNCHANGED:
	    case ZIP_ST_RENAMED:
		if (_zip_local_header_read(zf, i) < 0) {
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}
		if (_zip_entry_copy(tzf, zf, i, zf->entry[i].ch_meta)) {
		    /* zip_err set by _zip_entry_copy */
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}
		break;
	    case ZIP_ST_DELETED:
		break;
	    case ZIP_ST_REPLACED:
	    case ZIP_ST_ADDED:
		if (_zip_entry_add(tzf, zf->entry+i)) {
		    /* zip_err set by _zip_entry_add */
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}<commit_msg>comment out more code so this compiles<commit_after>/*
  $NiH: zip_close.c,v 1.37.4.1 2004/03/20 09:54:05 dillo Exp $

  zip_close.c -- close zip archive and update changes
  Copyright (C) 1999 <NAME> and <NAME>

  This file is part of libzip, a library to manipulate ZIP archives.
  The authors can be contacted at <<EMAIL>>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. 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.
  3. The names of the authors may not be used to endorse or promote
     products derived from this software without specific prior
     written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
*/




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "zip.h"
#include "zipint.h"

static int _zip_entry_copy(struct zip *dest, struct zip *src,
			   int entry_no, struct zip_meta *meta);
static int _zip_entry_add(struct zip *dest, struct zip_entry *se);
static int _zip_writecdir(struct zip *zfp);
static void _zip_write2(FILE *fp, int i);
static void _zip_write4(FILE *fp, int i);
static void _zip_writestr(FILE *fp, char *str, int len);
static int _zip_writecdentry(FILE *fp, struct zip_entry *zfe, int localp);
static struct zip_entry *_zip_create_entry(struct zip *dest, 
			                   struct zip_entry *src_entry,
			                   char *name, struct zip_meta *meta);
static void _zip_u2d_time(time_t time, int *ddate, int *dtime);
static int _zip_fwrite(char *b, int s, int n, FILE *f);




/* zip_close:
   Tries to commit all changes and close the zipfile; if it fails,
   zip_err (and errno) are set and *zf is unchanged, except for
   problems in _zip_free. */ 

int
zip_close(struct zip *zf)
{
    int i, count, tfd, ret, survivors;
    char *temp;
    FILE *tfp;
    struct zip *tzf;
    mode_t mask;

    count = survivors = 0;
    for (i=0; i<zf->nentry; i++) {
	if (zf->entry[i].state != ZIP_ST_UNCHANGED)
	    count = 1;
	if (zf->entry[i].state != ZIP_ST_DELETED)
	    survivors = 1;
	if (survivors && count)
	    break;
    }

    /* no changes */
    if (count == 0) {
	_zip_free(zf);
	return 0;
    }

    /* don't create zip files with no entries */
    if (survivors == 0) {
	ret = 0;
	if (zf->zn)
	    ret = remove(zf->zn);
	_zip_free(zf);
	/* XXX: inconsistent: zf freed, returned -1 */
	return ret;
    }	       
	
    temp = (char *)malloc(strlen(zf->zn)+8);
    if (!temp) {
	_zip_error_set(&zf->error, ZERR_MEMORY, 0);
	return -1;
    }

#if 0
    sprintf(temp, "%s.XXXXXX", zf->zn);

    tfd = mkstemp(temp);
    if (tfd == -1) {
	_zip_error_set(&zf->error, ZERR_TMPOPEN, errno);
	free(temp);
	return -1;
    }
    
    if ((tfp=fdopen(tfd, "r+b")) == NULL) {
	_zip_error_set(&zf->error, ZERR_TMPOPEN, errno);
	close(tfd);
	remove(temp);
	free(temp);
	return -1;
    }

    if ((tzf=_zip_new(&zf->error.zip_err)) == NULL) {
	fclose(tfp);
	remove(temp);
	free(temp);
	return -1;
    }
    tzf->zp = tfp;
    tzf->zn = temp;
    tzf->comlen = zf->comlen;
    if ((tzf->com=(unsigned char *)_zip_memdup(zf->com, zf->comlen)) == NULL) {
	_zip_error_set(&zf->error, ZERR_MEMORY, 0);
	remove(tzf->zn);
	_zip_free(tzf);
	return -1;
    }

    count = 0;
    if (zf->entry) {
	for (i=0; i<zf->nentry; i++) {
	    switch (zf->entry[i].state) {
	    case ZIP_ST_UNCHANGED:
	    case ZIP_ST_RENAMED:
		if (_zip_local_header_read(zf, i) < 0) {
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}
		if (_zip_entry_copy(tzf, zf, i, zf->entry[i].ch_meta)) {
		    /* zip_err set by _zip_entry_copy */
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}
		break;
	    case ZIP_ST_DELETED:
		break;
	    case ZIP_ST_REPLACED:
	    case ZIP_ST_ADDED:
		if (_zip_entry_add(tzf, zf->entry+i)) {
		    /* zip_err set by _zip_entry_add */
		    remove(tzf->zn);
		    _zip_free(tzf);
		    return -1;
		}
{'fraction_non_alphanumeric': 0.11641490433031218, 'fraction_numerical': 0.010674723061430011, 'mean_word_length': 3.1973795435333896, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '755542', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3534, 'n_words': 1140}