CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libzip/zip_file_replace.c
Views: 1401
/*1zip_file_replace.c -- replace file via callback function2Copyright (C) 1999-2019 Dieter Baron and Thomas Klausner34This file is part of libzip, a library to manipulate ZIP archives.5The authors can be contacted at <[email protected]>67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions9are met:101. Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.122. Redistributions in binary form must reproduce the above copyright13notice, this list of conditions and the following disclaimer in14the documentation and/or other materials provided with the15distribution.163. The names of the authors may not be used to endorse or promote17products derived from this software without specific prior18written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS21OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY24DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE26GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER28IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/323334#include "zipint.h"353637ZIP_EXTERN int38zip_file_replace(zip_t *za, zip_uint64_t idx, zip_source_t *source, zip_flags_t flags) {39if (idx >= za->nentry || source == NULL) {40zip_error_set(&za->error, ZIP_ER_INVAL, 0);41return -1;42}4344if (_zip_file_replace(za, idx, NULL, source, flags) == -1)45return -1;4647return 0;48}495051/* NOTE: Signed due to -1 on error. See zip_add.c for more details. */5253zip_int64_t54_zip_file_replace(zip_t *za, zip_uint64_t idx, const char *name, zip_source_t *source, zip_flags_t flags) {55zip_uint64_t za_nentry_prev;5657if (ZIP_IS_RDONLY(za)) {58zip_error_set(&za->error, ZIP_ER_RDONLY, 0);59return -1;60}6162za_nentry_prev = za->nentry;63if (idx == ZIP_UINT64_MAX) {64zip_int64_t i = -1;6566if (flags & ZIP_FL_OVERWRITE)67i = _zip_name_locate(za, name, flags, NULL);6869if (i == -1) {70/* create and use new entry, used by zip_add */71if ((i = _zip_add_entry(za)) < 0)72return -1;73}74idx = (zip_uint64_t)i;75}7677if (name && _zip_set_name(za, idx, name, flags) != 0) {78if (za->nentry != za_nentry_prev) {79_zip_entry_finalize(za->entry + idx);80za->nentry = za_nentry_prev;81}82return -1;83}8485/* does not change any name related data, so we can do it here;86* needed for a double add of the same file name */87_zip_unchange_data(za->entry + idx);8889if (za->entry[idx].orig != NULL && (za->entry[idx].changes == NULL || (za->entry[idx].changes->changed & ZIP_DIRENT_COMP_METHOD) == 0)) {90if (za->entry[idx].changes == NULL) {91if ((za->entry[idx].changes = _zip_dirent_clone(za->entry[idx].orig)) == NULL) {92zip_error_set(&za->error, ZIP_ER_MEMORY, 0);93return -1;94}95}9697za->entry[idx].changes->comp_method = ZIP_CM_REPLACED_DEFAULT;98za->entry[idx].changes->changed |= ZIP_DIRENT_COMP_METHOD;99}100101za->entry[idx].source = source;102103return (zip_int64_t)idx;104}105106107