/* * write data into zio buffer */ static int zio_write_data (zio_t *zio, void *buf, size_t len) { int n = 0; int ndropped = 0; /* * If buffer is empty, first try writing directly to dstfd * to avoid double-copy: */ if (zio_buffer_empty (zio)) { n = write (zio->dstfd, buf, len); if (n < 0) { if (errno == EAGAIN) n = 0; else return (-1); } /* * If we wrote everything, return early */ if (n == len) { if (zio_eof (zio)) zio_close (zio); return (len); } } /* * Otherwise, buffer any remaining data */ if ((len - n) && cbuf_write (zio->buf, buf+n, len-n, &ndropped) < 0) return (-1); return (0); }
static int zio_flux_read_cb (flux_t f, int fd, short revents, zio_t *zio) { int rc; zio_handler_start (zio); rc = zio_read_cb_common (zio); if (rc >= 0 && zio_eof_sent (zio)) { zio_debug (zio, "reader detaching from flux reactor\n"); flux_fdhandler_remove (f, fd, ZMQ_POLLIN|ZMQ_POLLERR); rc = zio_close (zio); } zio_handler_end (zio); return (rc); }
static int zio_zloop_read_cb (zloop_t *zl, zmq_pollitem_t *zp, zio_t *zio) { int rc; zio_handler_start (zio); rc = zio_read_cb_common (zio); if (rc >= 0 && zio_eof_sent (zio)) { zio_debug (zio, "reader detaching from zloop\n"); zloop_poller_end (zl, zp); rc = zio_close (zio); } zio_handler_end (zio); return (rc); }
int zio_write_eof (zio_t *zio) { if ((zio == NULL) || (zio->magic != ZIO_MAGIC) || !zio_writer (zio)) { errno = EINVAL; return (-1); } zio_set_eof (zio); /* If no data is buffered, then we can close the dst fd: */ if (zio_buffer_empty (zio)) zio_close (zio); return (0); }
/* * Callback when zio->dstfd is writeable. Write buffered data to * file descriptor. */ static int zio_writer_cb (zio_t *zio) { int rc = 0; if (cbuf_used (zio->buf)) rc = cbuf_read_to_fd (zio->buf, zio->dstfd, -1); if (rc < 0) { if (errno == EAGAIN) return (0); zio_debug (zio, "cbuf_read_to_fd: %s\n", strerror (errno)); return (-1); } if ((rc == 0) && zio_eof_pending (zio)) rc = zio_close (zio); return (rc); }
static int zio_writer_flush_all (zio_t *zio) { int n = 0; zio_debug (zio, "zio_writer_flush_all: used=%d\n", zio_buffer_used (zio)); while (zio_buffer_used (zio) > 0) { int rc = cbuf_read_to_fd (zio->buf, zio->dstfd, -1); zio_debug (zio, "zio_writer_flush_all: rc=%d\n", rc); if (rc < 0) return (rc); n += rc; } zio_debug (zio, "zio_writer_flush_all: n=%d\n", n); if (zio_buffer_used (zio) == 0 && zio_eof_pending (zio)) zio_close (zio); return (n); }
static void zio_flux_read_cb (flux_reactor_t *r, flux_watcher_t *w, int revents, void *arg) { zio_t *zio = arg; int rc; zio_handler_start (zio); rc = zio_read_cb_common (zio); if (rc >= 0 && zio_eof_sent (zio)) { zio_debug (zio, "reader detaching from flux reactor\n"); flux_watcher_stop (w); rc = zio_close (zio); } zio_handler_end (zio); if (rc < 0) flux_reactor_stop_error (r); }
/* * Internal function for ebzip_unzip_file() and ebzip_unzip_sebxa_start(). * If it succeeds, 0 is returned. Otherwise -1 is returned. */ static int ebzip_unzip_file_internal(const char *out_file_name, const char *in_file_name, Zio_Code in_zio_code, int index_page) { Zio in_zio; unsigned char *buffer = NULL; off_t total_length; int out_file = -1; ssize_t length; struct stat in_status, out_status; unsigned int crc = 1; int progress_interval; int total_slices; int i; zio_initialize(&in_zio); /* * Simply copy a file, when an input file is not compressed. */ if (in_zio_code == ZIO_PLAIN) return ebzip_copy_file(out_file_name, in_file_name); /* * Output file name information. */ if (!ebzip_quiet_flag) { fprintf(stderr, _("==> uncompress %s <==\n"), in_file_name); fprintf(stderr, _("output to %s\n"), out_file_name); fflush(stderr); } /* * Get status of the input file. */ if (stat(in_file_name, &in_status) < 0 || !S_ISREG(in_status.st_mode)) { fprintf(stderr, _("%s: no such file: %s\n"), invoked_name, in_file_name); goto failed; } /* * Do nothing if the `in_file_name' and `out_file_name' are the same. */ if (is_same_file(out_file_name, in_file_name)) { if (!ebzip_quiet_flag) { fprintf(stderr, _("the input and output files are the same, skipped.\n\n")); fflush(stderr); } return 0; } /* * Allocate memories for in/out buffers. */ buffer = (unsigned char *)malloc(EB_SIZE_PAGE << ZIO_MAX_EBZIP_LEVEL); if (buffer == NULL) { fprintf(stderr, _("%s: memory exhausted\n"), invoked_name); goto failed; } /* * If the file `out_file_name' already exists, confirm and unlink it. */ if (!ebzip_test_flag && stat(out_file_name, &out_status) == 0 && S_ISREG(out_status.st_mode)) { if (ebzip_overwrite_mode == EBZIP_OVERWRITE_NO) { if (!ebzip_quiet_flag) { fputs(_("already exists, skip the file\n\n"), stderr); fflush(stderr); } return 0; } else if (ebzip_overwrite_mode == EBZIP_OVERWRITE_CONFIRM) { int y_or_n; fprintf(stderr, _("\nthe file already exists: %s\n"), out_file_name); y_or_n = query_y_or_n(_("do you wish to overwrite (y or n)? ")); fputc('\n', stderr); fflush(stderr); if (!y_or_n) return 0; } if (unlink(out_file_name) < 0) { fprintf(stderr, _("%s: failed to unlink the file: %s\n"), invoked_name, out_file_name); goto failed; } } /* * Open files. */ if (zio_open(&in_zio, in_file_name, in_zio_code) < 0) { fprintf(stderr, _("%s: failed to open the file: %s\n"), invoked_name, in_file_name); goto failed; } if (in_zio_code == ZIO_SEBXA) { off_t index_location; off_t index_base; off_t zio_start_location; off_t zio_end_location; if (get_sebxa_indexes(in_file_name, index_page, &index_location, &index_base, &zio_start_location, &zio_end_location) < 0) { goto failed; } zio_set_sebxa_mode(&in_zio, index_location, index_base, zio_start_location, zio_end_location); } if (!ebzip_test_flag) { trap_file_name = out_file_name; #ifdef SIGHUP signal(SIGHUP, trap); #endif signal(SIGINT, trap); #ifdef SIGQUIT signal(SIGQUIT, trap); #endif #ifdef SIGTERM signal(SIGTERM, trap); #endif #ifdef O_CREAT out_file = open(out_file_name, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666 ^ get_umask()); #else out_file = creat(out_file_name, 0666 ^ get_umask()); #endif if (out_file < 0) { fprintf(stderr, _("%s: failed to open the file: %s\n"), invoked_name, out_file_name); goto failed; } trap_file = out_file; } /* * Read a slice from the input file, uncompress it if required, * and then write it to the output file. */ total_length = 0; total_slices = (in_zio.file_size + in_zio.slice_size - 1) / in_zio.slice_size; progress_interval = EBZIP_PROGRESS_INTERVAL_FACTOR; if (((total_slices + 999) / 1000) > progress_interval) progress_interval = ((total_slices + 999) / 1000); for (i = 0; i < total_slices; i++) { /* * Read a slice. */ if (zio_lseek(&in_zio, total_length, SEEK_SET) < 0) { fprintf(stderr, _("%s: failed to seek the file: %s\n"), invoked_name, in_file_name); goto failed; } length = zio_read(&in_zio, (char *)buffer, in_zio.slice_size); if (length < 0) { fprintf(stderr, _("%s: failed to read from the file: %s\n"), invoked_name, in_file_name); goto failed; } else if (length == 0) { fprintf(stderr, _("%s: unexpected EOF: %s\n"), invoked_name, in_file_name); goto failed; } else if (length != in_zio.slice_size && total_length + length != in_zio.file_size) { fprintf(stderr, _("%s: unexpected EOF: %s\n"), invoked_name, in_file_name); goto failed; } /* * Update CRC. (Calculate adler32 again.) */ if (in_zio.code == ZIO_EBZIP1) crc = adler32((uLong)crc, (Bytef *)buffer, (uInt)length); /* * Write the slice to `out_file'. */ if (!ebzip_test_flag) { if (write(out_file, buffer, length) != length) { fprintf(stderr, _("%s: failed to write to the file, %s: %s\n"), invoked_name, strerror(errno), out_file_name); goto failed; } } total_length += length; /* * Output status information unless `quiet' mode. */ if (!ebzip_quiet_flag && (i + 1) % progress_interval == 0) { #if defined(PRINTF_LL_MODIFIER) fprintf(stderr, _("%4.1f%% done (%llu / %llu bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned long long) total_length, (unsigned long long) in_zio.file_size); #elif defined(PRINTF_I64_MODIFIER) fprintf(stderr, _("%4.1f%% done (%I64u / %I64u bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned __int64) total_length, (unsigned __int64) in_zio.file_size); #else fprintf(stderr, _("%4.1f%% done (%lu / %lu bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned long) total_length, (unsigned long) in_zio.file_size); #endif fflush(stderr); } } /* * Output the result unless quiet mode. */ if (!ebzip_quiet_flag) { #if defined(PRINTF_LL_MODIFIER) fprintf(stderr, _("completed (%llu / %llu bytes)\n"), (unsigned long long) in_zio.file_size, (unsigned long long) in_zio.file_size); #elif defined(PRINTF_I64_MODIFIER) fprintf(stderr, _("completed (%I64u / %I64u bytes)\n"), (unsigned __int64) in_zio.file_size, (unsigned __int64) in_zio.file_size); #else fprintf(stderr, _("%lu -> %lu bytes\n\n"), (unsigned long) in_status.st_size, (unsigned long) total_length); #endif fflush(stderr); } /* * Close files. */ zio_close(&in_zio); zio_finalize(&in_zio); if (!ebzip_test_flag) { close(out_file); out_file = -1; trap_file = -1; trap_file_name = NULL; #ifdef SIGHUP signal(SIGHUP, SIG_DFL); #endif signal(SIGINT, SIG_DFL); #ifdef SIGQUIT signal(SIGQUIT, SIG_DFL); #endif #ifdef SIGTERM signal(SIGTERM, SIG_DFL); #endif } /* * Check for CRC. */ if (in_zio.code == ZIO_EBZIP1 && in_zio.crc != crc) { fprintf(stderr, _("%s: CRC error: %s\n"), invoked_name, out_file_name); goto failed; } /* * Delete an original file unless the keep flag is set. */ if (!ebzip_test_flag && !ebzip_keep_flag) unlink_files_add(in_file_name); /* * Set owner, group, permission, atime and mtime of `out_file'. * We ignore return values of `chown', `chmod' and `utime'. */ if (!ebzip_test_flag) { struct utimbuf utim; utim.actime = in_status.st_atime; utim.modtime = in_status.st_mtime; utime(out_file_name, &utim); } /* * Dispose memories. */ free(buffer); return 0; /* * An error occurs... */ failed: if (buffer != NULL) free(buffer); zio_close(&in_zio); zio_finalize(&in_zio); if (0 <= out_file) { close(out_file); trap_file = -1; trap_file_name = NULL; #ifdef SIGHUP signal(SIGHUP, SIG_DFL); #endif signal(SIGINT, SIG_DFL); #ifdef SIGQUIT signal(SIGQUIT, SIG_DFL); #endif #ifdef SIGTERM signal(SIGTERM, SIG_DFL); #endif } fputc('\n', stderr); fflush(stderr); return -1; }