1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
zip_source_file_stdio_named.c -- source for stdio file opened by name
Copyright (C) 1999-2023 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <info@libzip.org>
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 "zipint.h"
#include "zip_source_file.h"
#include "zip_source_file_stdio.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_CLONEFILE
#include <sys/attr.h>
#include <sys/clonefile.h>
#define CAN_CLONE
#endif
#ifdef HAVE_FICLONERANGE
#include <linux/fs.h>
#include <sys/ioctl.h>
#define CAN_CLONE
#endif
static int create_temp_file(zip_source_file_context_t *ctx, bool create_file);
static zip_int64_t _zip_stdio_op_commit_write(zip_source_file_context_t *ctx);
static zip_int64_t _zip_stdio_op_create_temp_output(zip_source_file_context_t *ctx);
#ifdef CAN_CLONE
static zip_int64_t _zip_stdio_op_create_temp_output_cloning(zip_source_file_context_t *ctx, zip_uint64_t offset);
#endif
static bool _zip_stdio_op_open(zip_source_file_context_t *ctx);
static zip_int64_t _zip_stdio_op_remove(zip_source_file_context_t *ctx);
static void _zip_stdio_op_rollback_write(zip_source_file_context_t *ctx);
static char *_zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string);
static zip_int64_t _zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len);
static FILE *_zip_fopen_close_on_exec(const char *name, bool writeable);
/* clang-format off */
static zip_source_file_operations_t ops_stdio_named = {
_zip_stdio_op_close,
_zip_stdio_op_commit_write,
_zip_stdio_op_create_temp_output,
#ifdef CAN_CLONE
_zip_stdio_op_create_temp_output_cloning,
#else
NULL,
#endif
_zip_stdio_op_open,
_zip_stdio_op_read,
_zip_stdio_op_remove,
_zip_stdio_op_rollback_write,
_zip_stdio_op_seek,
_zip_stdio_op_stat,
_zip_stdio_op_strdup,
_zip_stdio_op_tell,
_zip_stdio_op_write
};
/* clang-format on */
ZIP_EXTERN zip_source_t *
zip_source_file(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) {
if (za == NULL)
return NULL;
return zip_source_file_create(fname, start, len, &za->error);
}
ZIP_EXTERN zip_source_t *
zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
if (fname == NULL || length < ZIP_LENGTH_UNCHECKED) {
zip_error_set(error, ZIP_ER_INVAL, 0);
return NULL;
}
return zip_source_file_common_new(fname, NULL, start, length, NULL, &ops_stdio_named, NULL, error);
}
static zip_int64_t
_zip_stdio_op_commit_write(zip_source_file_context_t *ctx) {
if (fclose(ctx->fout) < 0) {
zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
return -1;
}
if (rename(ctx->tmpname, ctx->fname) < 0) {
zip_error_set(&ctx->error, ZIP_ER_RENAME, errno);
return -1;
}
return 0;
}
static zip_int64_t
_zip_stdio_op_create_temp_output(zip_source_file_context_t *ctx) {
int fd = create_temp_file(ctx, true);
if (fd < 0) {
return -1;
}
if ((ctx->fout = fdopen(fd, "r+b")) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
close(fd);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
return 0;
}
#ifdef CAN_CLONE
static zip_int64_t
_zip_stdio_op_create_temp_output_cloning(zip_source_file_context_t *ctx, zip_uint64_t offset) {
FILE *tfp;
if (offset > ZIP_OFF_MAX) {
zip_error_set(&ctx->error, ZIP_ER_SEEK, E2BIG);
return -1;
}
#ifdef HAVE_CLONEFILE
/* clonefile insists on creating the file, so just create a name */
if (create_temp_file(ctx, false) < 0) {
return -1;
}
if (clonefile(ctx->fname, ctx->tmpname, 0) < 0) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
if ((tfp = _zip_fopen_close_on_exec(ctx->tmpname, true)) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
#else
{
int fd;
struct file_clone_range range;
zip_os_stat_t st;
if (zip_os_fstat(fileno(ctx->f), &st) < 0) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
return -1;
}
if ((fd = create_temp_file(ctx, true)) < 0) {
return -1;
}
range.src_fd = fileno(ctx->f);
range.src_offset = 0;
range.src_length = ((offset + st.st_blksize - 1) / st.st_blksize) * st.st_blksize;
if (range.src_length > st.st_size) {
range.src_length = 0;
}
range.dest_offset = 0;
if (ioctl(fd, FICLONERANGE, &range) < 0) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
(void)close(fd);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
if ((tfp = fdopen(fd, "r+b")) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
(void)close(fd);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
}
#endif
if (ftruncate(fileno(tfp), (off_t)offset) < 0) {
(void)fclose(tfp);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
if (zip_os_fseek(tfp, (zip_off_t)offset, SEEK_SET) < 0) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
(void)fclose(tfp);
(void)remove(ctx->tmpname);
free(ctx->tmpname);
ctx->tmpname = NULL;
return -1;
}
ctx->fout = tfp;
return 0;
}
#endif
static bool
_zip_stdio_op_open(zip_source_file_context_t *ctx) {
if ((ctx->f = _zip_fopen_close_on_exec(ctx->fname, false)) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_OPEN, errno);
return false;
}
return true;
}
static zip_int64_t
_zip_stdio_op_remove(zip_source_file_context_t *ctx) {
if (remove(ctx->fname) < 0) {
zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno);
return -1;
}
return 0;
}
static void
_zip_stdio_op_rollback_write(zip_source_file_context_t *ctx) {
if (ctx->fout) {
fclose(ctx->fout);
}
(void)remove(ctx->tmpname);
}
static char *
_zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string) {
return strdup(string);
}
static zip_int64_t
_zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len) {
size_t ret;
clearerr((FILE *)ctx->fout);
ret = fwrite(data, 1, len, (FILE *)ctx->fout);
if (ret != len || ferror((FILE *)ctx->fout)) {
zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
return -1;
}
return (zip_int64_t)ret;
}
static int create_temp_file(zip_source_file_context_t *ctx, bool create_file) {
char *temp;
int mode;
zip_os_stat_t st;
int fd = 0;
char *start, *end;
if (zip_os_stat(ctx->fname, &st) == 0) {
mode = st.st_mode;
}
else {
mode = -1;
}
size_t temp_size = strlen(ctx->fname) + 13;
if ((temp = (char *)malloc(temp_size)) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
return -1;
}
snprintf_s(temp, temp_size, "%s.XXXXXX.part", ctx->fname);
end = temp + strlen(temp) - 5;
start = end - 6;
for (;;) {
zip_uint32_t value = zip_random_uint32();
char *xs = start;
while (xs < end) {
char digit = value % 36;
if (digit < 10) {
*(xs++) = digit + '0';
}
else {
*(xs++) = digit - 10 + 'a';
}
value /= 36;
}
if (create_file) {
if ((fd = open(temp, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, mode == -1 ? 0666 : (mode_t)mode)) >= 0) {
if (mode != -1) {
/* open() honors umask(), which we don't want in this case */
#ifdef HAVE_FCHMOD
(void)fchmod(fd, (mode_t)mode);
#else
(void)chmod(temp, (mode_t)mode);
#endif
}
break;
}
if (errno != EEXIST) {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
free(temp);
return -1;
}
}
else {
if (zip_os_stat(temp, &st) < 0) {
if (errno == ENOENT) {
break;
}
else {
zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno);
free(temp);
return -1;
}
}
}
}
ctx->tmpname = temp;
return fd; /* initialized to 0 if !create_file */
}
/*
* fopen replacement that sets the close-on-exec flag
* some implementations support an fopen 'e' flag for that,
* but e.g. macOS doesn't.
*/
static FILE *_zip_fopen_close_on_exec(const char *name, bool writeable) {
int fd;
int flags;
FILE *fp;
flags = O_CLOEXEC;
if (writeable) {
flags |= O_RDWR;
}
else {
flags |= O_RDONLY;
}
/* mode argument needed on Windows */
if ((fd = open(name, flags, 0666)) < 0) {
return NULL;
}
if ((fp = fdopen(fd, writeable ? "r+b" : "rb")) == NULL) {
return NULL;
}
return fp;
}
|