Lines Matching refs:ctx

85 static int create_temp_output(struct read_file *ctx);
115 struct read_file *ctx; in _zip_source_file_or_p() local
123 if ((ctx=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) { in _zip_source_file_or_p()
128 ctx->fname = NULL; in _zip_source_file_or_p()
130 if ((ctx->fname=strdup(fname)) == NULL) { in _zip_source_file_or_p()
132 free(ctx); in _zip_source_file_or_p()
136 ctx->f = file; in _zip_source_file_or_p()
137 ctx->start = start; in _zip_source_file_or_p()
138 ctx->end = (len < 0 ? 0 : start+(zip_uint64_t)len); in _zip_source_file_or_p()
140 memcpy(&ctx->st, st, sizeof(ctx->st)); in _zip_source_file_or_p()
141 ctx->st.name = NULL; in _zip_source_file_or_p()
142 ctx->st.valid &= ~ZIP_STAT_NAME; in _zip_source_file_or_p()
145 zip_stat_init(&ctx->st); in _zip_source_file_or_p()
148 ctx->tmpname = NULL; in _zip_source_file_or_p()
149 ctx->fout = NULL; in _zip_source_file_or_p()
151 zip_error_init(&ctx->error); in _zip_source_file_or_p()
153ctx->supports = ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS,… in _zip_source_file_or_p()
154 if (ctx->fname) { in _zip_source_file_or_p()
157 if (stat(ctx->fname, &sb) < 0 || S_ISREG(sb.st_mode)) { in _zip_source_file_or_p()
158 ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE; in _zip_source_file_or_p()
161 else if (fseeko(ctx->f, 0, SEEK_CUR) == 0) { in _zip_source_file_or_p()
162 ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE; in _zip_source_file_or_p()
165 if ((zs=zip_source_function_create(read_file, ctx, error)) == NULL) { in _zip_source_file_or_p()
166 free(ctx->fname); in _zip_source_file_or_p()
167 free(ctx); in _zip_source_file_or_p()
176 create_temp_output(struct read_file *ctx) in create_temp_output() argument
183 if ((temp=(char *)malloc(strlen(ctx->fname)+8)) == NULL) { in create_temp_output()
184 zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); in create_temp_output()
187 sprintf(temp, "%s.XXXXXX", ctx->fname); in create_temp_output()
191 zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); in create_temp_output()
199 zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, errno); in create_temp_output()
214 ctx->fout = tfp; in create_temp_output()
215 ctx->tmpname = temp; in create_temp_output()
224 struct read_file *ctx; in read_file() local
229 ctx = (struct read_file *)state; in read_file()
234 if (ctx->fname == NULL) { in read_file()
235 zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); in read_file()
238 return create_temp_output(ctx); in read_file()
243 if (fclose(ctx->fout) < 0) { in read_file()
244 ctx->fout = NULL; in read_file()
245 zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); in read_file()
247 ctx->fout = NULL; in read_file()
248 if (rename(ctx->tmpname, ctx->fname) < 0) { in read_file()
249 zip_error_set(&ctx->error, ZIP_ER_RENAME, errno); in read_file()
255 (void)chmod(ctx->fname, 0666&~mask); in read_file()
256 free(ctx->tmpname); in read_file()
257 ctx->tmpname = NULL; in read_file()
262 if (ctx->fname) { in read_file()
263 fclose(ctx->f); in read_file()
264 ctx->f = NULL; in read_file()
269 return zip_error_to_data(&ctx->error, data, len); in read_file()
272 free(ctx->fname); in read_file()
273 free(ctx->tmpname); in read_file()
274 if (ctx->f) in read_file()
275 fclose(ctx->f); in read_file()
276 free(ctx); in read_file()
280 if (ctx->fname) { in read_file()
281 if ((ctx->f=fopen(ctx->fname, "rb")) == NULL) { in read_file()
282 zip_error_set(&ctx->error, ZIP_ER_OPEN, errno); in read_file()
287 if (ctx->start > 0) { in read_file()
288 if (_zip_fseek_u(ctx->f, ctx->start, SEEK_SET, &ctx->error) < 0) { in read_file()
292 ctx->current = ctx->start; in read_file()
296 if (ctx->end > 0) { in read_file()
297 n = ctx->end-ctx->current; in read_file()
309 if ((i=fread(buf, 1, (size_t)n, ctx->f)) == 0) { in read_file()
310 if (ferror(ctx->f)) { in read_file()
311 zip_error_set(&ctx->error, ZIP_ER_READ, errno); in read_file()
315 ctx->current += i; in read_file()
320 if (remove(ctx->fname) < 0) { in read_file()
321 zip_error_set(&ctx->error, ZIP_ER_REMOVE, errno); in read_file()
327 if (ctx->fout) { in read_file()
328 fclose(ctx->fout); in read_file()
329 ctx->fout = NULL; in read_file()
331 (void)remove(ctx->tmpname); in read_file()
332 free(ctx->tmpname); in read_file()
333 ctx->tmpname = NULL; in read_file()
339 …zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); in read_file()
352 if (ctx->end == 0) { in read_file()
353 if (_zip_fseek(ctx->f, args->offset, SEEK_END, &ctx->error) < 0) { in read_file()
356 if ((new_current = ftello(ctx->f)) < 0) { in read_file()
357 zip_error_set(&ctx->error, ZIP_ER_SEEK, errno); in read_file()
363 new_current = (zip_int64_t)ctx->end + args->offset; in read_file()
367 new_current = (zip_int64_t)ctx->current + args->offset; in read_file()
371 zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); in read_file()
375 …w_current < 0 || (zip_uint64_t)new_current < ctx->start || (ctx->end != 0 && (zip_uint64_t)new_cur… in read_file()
376 zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); in read_file()
380 ctx->current = (zip_uint64_t)new_current; in read_file()
383 if (_zip_fseek_u(ctx->f, ctx->current, SEEK_SET, &ctx->error) < 0) { in read_file()
393 args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); in read_file()
398 if (_zip_fseek(ctx->fout, args->offset, args->whence, &ctx->error) < 0) { in read_file()
405 if (len < sizeof(ctx->st)) in read_file()
408 if (ctx->st.valid != 0) in read_file()
409 memcpy(data, &ctx->st, sizeof(ctx->st)); in read_file()
415 if (ctx->f) in read_file()
416 err = fstat(fileno(ctx->f), &fst); in read_file()
418 err = stat(ctx->fname, &fst); in read_file()
421 zip_error_set(&ctx->error, ZIP_ER_READ, errno); in read_file()
430 if (ctx->end != 0) { in read_file()
431 st->size = ctx->end - ctx->start; in read_file()
439 return sizeof(ctx->st); in read_file()
443 return ctx->supports; in read_file()
446 return (zip_int64_t)ctx->current; in read_file()
450 off_t ret = ftello(ctx->fout); in read_file()
453 zip_error_set(&ctx->error, ZIP_ER_TELL, errno); in read_file()
463 clearerr(ctx->fout); in read_file()
464 ret = fwrite(data, 1, len, ctx->fout); in read_file()
465 if (ret != len || ferror(ctx->fout)) { in read_file()
466 zip_error_set(&ctx->error, ZIP_ER_WRITE, errno); in read_file()
474 zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); in read_file()