int __archive_mktemp(const char *tmpdir) { static const char num[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; struct archive_string temp_name; struct stat st; int fd; char *tp, *ep; fd = -1; archive_string_init(&temp_name); if (tmpdir == NULL) { if (get_tempdir(&temp_name) != ARCHIVE_OK) goto exit_tmpfile; } else archive_strcpy(&temp_name, tmpdir); if (temp_name.s[temp_name.length-1] == '/') { temp_name.s[temp_name.length-1] = '\0'; temp_name.length --; } if (stat(temp_name.s, &st) < 0) goto exit_tmpfile; if (!S_ISDIR(st.st_mode)) { errno = ENOTDIR; goto exit_tmpfile; } archive_strcat(&temp_name, "/libarchive_"); tp = temp_name.s + archive_strlen(&temp_name); archive_strcat(&temp_name, "XXXXXXXXXX"); ep = temp_name.s + archive_strlen(&temp_name); do { char *p; p = tp; archive_random(p, ep - p); while (p < ep) { int d = *((unsigned char *)p) % sizeof(num); *p++ = num[d]; } fd = open(temp_name.s, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600); } while (fd < 0 && errno == EEXIST); if (fd < 0) goto exit_tmpfile; __archive_ensure_cloexec_flag(fd); unlink(temp_name.s); exit_tmpfile: archive_string_free(&temp_name); return (fd); }
int __archive_mktemp(const char *tmpdir) { struct archive_string temp_name; int fd = -1; archive_string_init(&temp_name); if (tmpdir == NULL) { if (get_tempdir(&temp_name) != ARCHIVE_OK) goto exit_tmpfile; } else { archive_strcpy(&temp_name, tmpdir); if (temp_name.s[temp_name.length-1] != '/') archive_strappend_char(&temp_name, '/'); } archive_strcat(&temp_name, "libarchive_XXXXXX"); fd = mkstemp(temp_name.s); if (fd < 0) goto exit_tmpfile; __archive_ensure_cloexec_flag(fd); unlink(temp_name.s); exit_tmpfile: archive_string_free(&temp_name); return (fd); }
/* * Destroy the archive structure. * * Be careful: user might just call write_new and then write_free. * Don't assume we actually wrote anything or performed any non-trivial * initialization. */ static int _archive_write_free(struct archive *_a) { struct archive_write *a = (struct archive_write *)_a; int r = ARCHIVE_OK, r1; if (_a == NULL) return (ARCHIVE_OK); /* It is okay to call free() in state FATAL. */ archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free"); if (a->archive.state != ARCHIVE_STATE_FATAL) r = archive_write_close(&a->archive); /* Release format resources. */ if (a->format_free != NULL) { r1 = (a->format_free)(a); if (r1 < r) r = r1; } __archive_write_filters_free(_a); /* Release various dynamic buffers. */ free((void *)(uintptr_t)(const void *)a->nulls); archive_string_free(&a->archive.error_string); a->archive.magic = 0; __archive_clean(&a->archive); free(a); return (r); }
static int archive_read_format_zip_cleanup(struct archive_read *a) { struct zip *zip; zip = (struct zip *)(a->format->data); #ifdef HAVE_ZLIB_H if (zip->stream_valid) inflateEnd(&zip->stream); #endif free(zip->uncompressed_buffer); archive_string_free(&(zip->pathname)); archive_string_free(&(zip->extra)); free(zip); (a->format->data) = NULL; return (ARCHIVE_OK); }
static int archive_write_shar_free(struct archive_write *a) { struct shar *shar; shar = (struct shar *)a->format_data; if (shar == NULL) return (ARCHIVE_OK); archive_entry_free(shar->entry); free(shar->last_dir); archive_string_free(&(shar->work)); archive_string_free(&(shar->quoted_name)); free(shar); a->format_data = NULL; return (ARCHIVE_OK); }
static int archive_read_format_iso9660_cleanup(struct archive_read *a) { struct iso9660 *iso9660; struct file_info *file; iso9660 = (struct iso9660 *)(a->format->data); while ((file = next_entry(iso9660)) != NULL) release_file(iso9660, file); archive_string_free(&iso9660->pathname); archive_string_free(&iso9660->previous_pathname); if (iso9660->pending_files) free(iso9660->pending_files); free(iso9660); (a->format->data) = NULL; return (ARCHIVE_OK); }
static int archive_compressor_program_free(struct archive_write_filter *f) { struct private_data *data = (struct private_data *)f->data; free(data->cmd); archive_string_free(&data->description); __archive_write_program_free(data->pdata); free(data); return (ARCHIVE_OK); }
/* * Set up command line arguments. * Returns ARCHIVE_OK if everything okey. * Returns ARCHIVE_FAILED if there is a lack of the `"' terminator or an * empty command line. * Returns ARCHIVE_FATAL if no memory. */ int __archive_cmdline_parse(struct archive_cmdline *data, const char *cmd) { struct archive_string as; const char *p; ssize_t al; int r; archive_string_init(&as); /* Get first argument as a command path. */ al = get_argument(&as, cmd); if (al < 0) { r = ARCHIVE_FAILED;/* Invalid sequence. */ goto exit_function; } if (archive_strlen(&as) == 0) { r = ARCHIVE_FAILED;/* An empty command path. */ goto exit_function; } r = cmdline_set_path(data, as.s); if (r != ARCHIVE_OK) goto exit_function; p = strrchr(as.s, '/'); if (p == NULL) p = as.s; else p++; r = cmdline_add_arg(data, p); if (r != ARCHIVE_OK) goto exit_function; cmd += al; for (;;) { al = get_argument(&as, cmd); if (al < 0) { r = ARCHIVE_FAILED;/* Invalid sequence. */ goto exit_function; } if (al == 0) break; cmd += al; if (archive_strlen(&as) == 0 && *cmd == '\0') break; r = cmdline_add_arg(data, as.s); if (r != ARCHIVE_OK) goto exit_function; } r = ARCHIVE_OK; exit_function: archive_string_free(&as); return (r); }
int archive_read_open_filename_w(struct archive *a, const wchar_t *wfilename, size_t block_size) { enum fnt_e filename_type; if (wfilename == NULL || wfilename[0] == L'\0') { filename_type = FNT_STDIN; } else { #if defined(_WIN32) && !defined(__CYGWIN__) filename_type = FNT_WCS; #else /* * POSIX system does not support a wchar_t interface for * open() system call, so we have to translate a whcar_t * filename to multi-byte one and use it. */ struct archive_string fn; int r; archive_string_init(&fn); if (archive_string_append_from_wcs(&fn, wfilename, wcslen(wfilename)) != 0) { if (errno == ENOMEM) archive_set_error(a, errno, "Can't allocate memory"); else archive_set_error(a, EINVAL, "Failed to convert a wide-character" " filename to a multi-byte filename"); archive_string_free(&fn); return (ARCHIVE_FATAL); } r = file_open_filename(a, FNT_MBS, fn.s, block_size); archive_string_free(&fn); return (r); #endif } return (file_open_filename(a, filename_type, wfilename, block_size)); }
static int _warc_cleanup(struct archive_read *a) { struct warc_s *w = a->format->data; if (w->pool.len > 0U) { free(w->pool.str); } archive_string_free(&w->sver); free(w); a->format->data = NULL; return (ARCHIVE_OK); }
/* * Release memory and other resources. */ static int _archive_read_free(struct archive *_a) { struct archive_read *a = (struct archive_read *)_a; int i, n; int slots; int r = ARCHIVE_OK; if (_a == NULL) return (ARCHIVE_OK); archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free"); if (a->archive.state != ARCHIVE_STATE_CLOSED && a->archive.state != ARCHIVE_STATE_FATAL) r = archive_read_close(&a->archive); /* Call cleanup functions registered by optional components. */ if (a->cleanup_archive_extract != NULL) r = (a->cleanup_archive_extract)(a); /* Cleanup format-specific data. */ slots = sizeof(a->formats) / sizeof(a->formats[0]); for (i = 0; i < slots; i++) { a->format = &(a->formats[i]); if (a->formats[i].cleanup) (a->formats[i].cleanup)(a); } /* Free the filters */ __archive_read_free_filters(a); /* Release the bidder objects. */ n = sizeof(a->bidders)/sizeof(a->bidders[0]); for (i = 0; i < n; i++) { if (a->bidders[i].free != NULL) { int r1 = (a->bidders[i].free)(&a->bidders[i]); if (r1 < r) r = r1; } } archive_string_free(&a->archive.error_string); if (a->entry) archive_entry_free(a->entry); a->archive.magic = 0; __archive_clean(&a->archive); free(a->client.dataset); free(a); return (r); }
static int program_filter_close(struct archive_read_filter *self) { struct program_filter *state; int e; state = (struct program_filter *)self->data; e = child_stop(self, state); /* Release our private data. */ free(state->out_buf); archive_string_free(&state->description); free(state); return (e); }
static void release_file(struct iso9660 *iso9660, struct file_info *file) { struct file_info *parent; if (file->refcount == 0) { parent = file->parent; if (file->name) free(file->name); archive_string_free(&file->symlink); free(file); if (parent != NULL) { parent->refcount--; release_file(iso9660, parent); } } }
/* * Destroy the archive structure. */ static int _archive_write_finish(struct archive *_a) { struct archive_write *a = (struct archive_write *)_a; int r = ARCHIVE_OK; __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_finish"); if (a->archive.state != ARCHIVE_STATE_CLOSED) r = archive_write_close(&a->archive); /* Release various dynamic buffers. */ free((void *)(uintptr_t)(const void *)a->nulls); archive_string_free(&a->archive.error_string); a->archive.magic = 0; free(a); return (r); }
static int archive_read_format_cpio_cleanup(struct archive_read *a) { struct cpio *cpio; cpio = (struct cpio *)(a->format->data); /* Free inode->name map */ while (cpio->links_head != NULL) { struct links_entry *lp = cpio->links_head->next; if (cpio->links_head->name) free(cpio->links_head->name); free(cpio->links_head); cpio->links_head = lp; } archive_string_free(&cpio->entry_name); free(cpio); (a->format->data) = NULL; return (ARCHIVE_OK); }
static int archive_write_lrzip_open(struct archive_write_filter *f) { struct write_lrzip *data = (struct write_lrzip *)f->data; struct archive_string as; int r; archive_string_init(&as); archive_strcpy(&as, "lrzip -q"); /* Specify compression type. */ switch (data->compression) { case lzma:/* default compression */ break; case bzip2: archive_strcat(&as, " -b"); break; case gzip: archive_strcat(&as, " -g"); break; case lzo: archive_strcat(&as, " -l"); break; case none: archive_strcat(&as, " -n"); break; case zpaq: archive_strcat(&as, " -z"); break; } /* Specify compression level. */ if (data->compression_level > 0) { archive_strcat(&as, " -L "); archive_strappend_char(&as, '0' + data->compression_level); } r = __archive_write_program_open(f, data->pdata, as.s); archive_string_free(&as); return (r); }
static int archive_compressor_bzip2_open(struct archive_write_filter *f) { struct private_data *data = (struct private_data *)f->data; struct archive_string as; int r; archive_string_init(&as); archive_strcpy(&as, "bzip2"); /* Specify compression level. */ if (data->compression_level > 0) { archive_strcat(&as, " -"); archive_strappend_char(&as, '0' + data->compression_level); } f->write = archive_compressor_bzip2_write; r = __archive_write_program_open(f, data->pdata, as.s); archive_string_free(&as); return (r); }
int __archive_read_program(struct archive_read_filter *self, const char *cmd) { struct program_filter *state; static const size_t out_buf_len = 65536; char *out_buf; const char *prefix = "Program: "; pid_t child; size_t l; l = strlen(prefix) + strlen(cmd) + 1; state = (struct program_filter *)calloc(1, sizeof(*state)); out_buf = (char *)malloc(out_buf_len); if (state == NULL || out_buf == NULL || archive_string_ensure(&state->description, l) == NULL) { archive_set_error(&self->archive->archive, ENOMEM, "Can't allocate input data"); if (state != NULL) { archive_string_free(&state->description); free(state); } free(out_buf); return (ARCHIVE_FATAL); } archive_strcpy(&state->description, prefix); archive_strcat(&state->description, cmd); self->code = ARCHIVE_FILTER_PROGRAM; self->name = state->description.s; state->out_buf = out_buf; state->out_buf_len = out_buf_len; child = __archive_create_child(cmd, &state->child_stdin, &state->child_stdout); if (child == -1) { free(state->out_buf); free(state); archive_set_error(&self->archive->archive, EINVAL, "Can't initialize filter; unable to run program \"%s\"", cmd); return (ARCHIVE_FATAL); } #if defined(_WIN32) && !defined(__CYGWIN__) state->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child); if (state->child == NULL) { child_stop(self, state); free(state->out_buf); free(state); archive_set_error(&self->archive->archive, EINVAL, "Can't initialize filter; unable to run program \"%s\"", cmd); return (ARCHIVE_FATAL); } #else state->child = child; #endif self->data = state; self->read = program_filter_read; self->skip = NULL; self->close = program_filter_close; /* XXX Check that we can read at least one byte? */ return (ARCHIVE_OK); }
static int add_pattern_from_file(struct archive_match *a, struct match_list *mlist, int mbs, const void *pathname, int nullSeparator) { struct archive *ar; struct archive_entry *ae; struct archive_string as; const void *buff; size_t size; int64_t offset; int r; ar = archive_read_new(); if (ar == NULL) { archive_set_error(&(a->archive), ENOMEM, "No memory"); return (ARCHIVE_FATAL); } r = archive_read_support_format_raw(ar); r = archive_read_support_format_empty(ar); if (r != ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); return (r); } if (mbs) r = archive_read_open_filename(ar, pathname, 512*20); else r = archive_read_open_filename_w(ar, pathname, 512*20); if (r != ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); return (r); } r = archive_read_next_header(ar, &ae); if (r != ARCHIVE_OK) { archive_read_free(ar); if (r == ARCHIVE_EOF) { return (ARCHIVE_OK); } else { archive_copy_error(&(a->archive), ar); return (r); } } archive_string_init(&as); while ((r = archive_read_data_block(ar, &buff, &size, &offset)) == ARCHIVE_OK) { const char *b = (const char *)buff; while (size) { const char *s = (const char *)b; size_t length = 0; int found_separator = 0; while (length < size) { if (nullSeparator) { if (*b == '\0') { found_separator = 1; break; } } else { if (*b == 0x0d || *b == 0x0a) { found_separator = 1; break; } } b++; length++; } if (!found_separator) { archive_strncat(&as, s, length); /* Read next data block. */ break; } b++; size -= length + 1; archive_strncat(&as, s, length); /* If the line is not empty, add the pattern. */ if (archive_strlen(&as) > 0) { /* Add pattern. */ r = add_pattern_mbs(a, mlist, as.s); if (r != ARCHIVE_OK) { archive_read_free(ar); archive_string_free(&as); return (r); } archive_string_empty(&as); } } } /* If an error occurred, report it immediately. */ if (r < ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); archive_string_free(&as); return (r); } /* If the line is not empty, add the pattern. */ if (r == ARCHIVE_EOF && archive_strlen(&as) > 0) { /* Add pattern. */ r = add_pattern_mbs(a, mlist, as.s); if (r != ARCHIVE_OK) { archive_read_free(ar); archive_string_free(&as); return (r); } } archive_read_free(ar); archive_string_free(&as); return (ARCHIVE_OK); }
/* * The Mac OS "copyfile()" API copies the extended metadata for a * file into a separate file in AppleDouble format (see RFC 1740). * * Mac OS tar and cpio implementations store this extended * metadata as a separate entry just before the regular entry * with a "._" prefix added to the filename. * * Note that this is currently done unconditionally; the tar program has * an option to discard this information before the archive is written. * * TODO: If there's a failure, report it and return ARCHIVE_WARN. */ static int setup_mac_metadata(struct archive_read_disk *a, struct archive_entry *entry, int *fd) { int tempfd = -1; int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR; struct stat copyfile_stat; int ret = ARCHIVE_OK; void *buff = NULL; int have_attrs; const char *name, *tempdir; struct archive_string tempfile; (void)fd; /* UNUSED */ name = archive_entry_sourcepath(entry); if (name == NULL) name = archive_entry_pathname(entry); if (name == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Can't open file to read extended attributes: No name"); return (ARCHIVE_WARN); } if (a->tree != NULL) { if (a->tree_enter_working_dir(a->tree) != 0) { archive_set_error(&a->archive, errno, "Couldn't change dir"); return (ARCHIVE_FAILED); } } /* Short-circuit if there's nothing to do. */ have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK); if (have_attrs == -1) { archive_set_error(&a->archive, errno, "Could not check extended attributes"); return (ARCHIVE_WARN); } if (have_attrs == 0) return (ARCHIVE_OK); tempdir = NULL; if (issetugid() == 0) tempdir = getenv("TMPDIR"); if (tempdir == NULL) tempdir = _PATH_TMP; archive_string_init(&tempfile); archive_strcpy(&tempfile, tempdir); archive_strcat(&tempfile, "tar.md.XXXXXX"); tempfd = mkstemp(tempfile.s); if (tempfd < 0) { archive_set_error(&a->archive, errno, "Could not open extended attribute file"); ret = ARCHIVE_WARN; goto cleanup; } __archive_ensure_cloexec_flag(tempfd); /* XXX I wish copyfile() could pack directly to a memory * buffer; that would avoid the temp file here. For that * matter, it would be nice if fcopyfile() actually worked, * that would reduce the many open/close races here. */ if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) { archive_set_error(&a->archive, errno, "Could not pack extended attributes"); ret = ARCHIVE_WARN; goto cleanup; } if (fstat(tempfd, ©file_stat)) { archive_set_error(&a->archive, errno, "Could not check size of extended attributes"); ret = ARCHIVE_WARN; goto cleanup; } buff = malloc(copyfile_stat.st_size); if (buff == NULL) { archive_set_error(&a->archive, errno, "Could not allocate memory for extended attributes"); ret = ARCHIVE_WARN; goto cleanup; } if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) { archive_set_error(&a->archive, errno, "Could not read extended attributes into memory"); ret = ARCHIVE_WARN; goto cleanup; } archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size); cleanup: if (tempfd >= 0) { close(tempfd); unlink(tempfile.s); } archive_string_free(&tempfile); free(buff); return (ret); }
static int _warc_header(struct archive_write *a, struct archive_entry *entry) { struct warc_s *w = a->format_data; struct archive_string hdr; #define MAX_HDR_SIZE 512 /* check whether warcinfo record needs outputting */ if (!w->omit_warcinfo) { ssize_t r; warc_essential_hdr_t wi = { WT_INFO, /*uri*/NULL, /*urn*/NULL, /*rtm*/0, /*mtm*/0, /*cty*/"application/warc-fields", /*len*/sizeof(warcinfo) - 1U, }; wi.rtime = w->now; wi.mtime = w->now; archive_string_init(&hdr); r = _popul_ehdr(&hdr, MAX_HDR_SIZE, wi); if (r >= 0) { /* jackpot! */ /* now also use HDR buffer for the actual warcinfo */ archive_strncat(&hdr, warcinfo, sizeof(warcinfo) -1); /* append end-of-record indicator */ archive_strncat(&hdr, "\r\n\r\n", 4); /* write to output stream */ __archive_write_output(a, hdr.s, archive_strlen(&hdr)); } /* indicate we're done with file header writing */ w->omit_warcinfo = 1U; archive_string_free(&hdr); } if (archive_entry_pathname(entry) == NULL) { archive_set_error(&a->archive, EINVAL, "Invalid filename"); return (ARCHIVE_WARN); } w->typ = archive_entry_filetype(entry); w->populz = 0U; if (w->typ == AE_IFREG) { warc_essential_hdr_t rh = { WT_RSRC, /*uri*/NULL, /*urn*/NULL, /*rtm*/0, /*mtm*/0, /*cty*/NULL, /*len*/0, }; ssize_t r; rh.tgturi = archive_entry_pathname(entry); rh.rtime = w->now; rh.mtime = archive_entry_mtime(entry); rh.cntlen = (size_t)archive_entry_size(entry); archive_string_init(&hdr); r = _popul_ehdr(&hdr, MAX_HDR_SIZE, rh); if (r < 0) { /* don't bother */ archive_set_error( &a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "cannot archive file"); return (ARCHIVE_WARN); } /* otherwise append to output stream */ __archive_write_output(a, hdr.s, r); /* and let subsequent calls to _data() know about the size */ w->populz = rh.cntlen; archive_string_free(&hdr); return (ARCHIVE_OK); } /* just resort to erroring as per Tim's advice */ archive_set_error( &a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "WARC can only process regular files"); return (ARCHIVE_FAILED); }
pid_t __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout) { HANDLE childStdout[2], childStdin[2],childStderr; SECURITY_ATTRIBUTES secAtts; STARTUPINFOA staInfo; PROCESS_INFORMATION childInfo; struct archive_string cmdline; struct archive_string fullpath; struct archive_cmdline *acmd; char *arg0, *ext; int i, l; DWORD fl, fl_old; childStdout[0] = childStdout[1] = INVALID_HANDLE_VALUE; childStdin[0] = childStdin[1] = INVALID_HANDLE_VALUE; childStderr = INVALID_HANDLE_VALUE; archive_string_init(&cmdline); archive_string_init(&fullpath); acmd = __archive_cmdline_allocate(); if (acmd == NULL) goto fail; if (__archive_cmdline_parse(acmd, cmd) != ARCHIVE_OK) goto fail; /* * Search the full path of 'path'. * NOTE: This does not need if we give CreateProcessA 'path' as * a part of the cmdline and give CreateProcessA NULL as first * parameter, but I do not like that way. */ ext = strrchr(acmd->path, '.'); if (ext == NULL || strlen(ext) > 4) /* 'path' does not have a proper extension, so we have to * give SearchPath() ".exe" as the extension. */ ext = ".exe"; else ext = NULL;/* 'path' has an extension. */ fl = MAX_PATH; do { if (archive_string_ensure(&fullpath, fl) == NULL) goto fail; fl_old = fl; fl = SearchPathA(NULL, acmd->path, ext, fl, fullpath.s, &arg0); } while (fl != 0 && fl > fl_old); if (fl == 0) goto fail; /* * Make a command line. */ for (l = 0, i = 0; acmd->argv[i] != NULL; i++) { if (i == 0) continue; l += (int)strlen(acmd->argv[i]) + 1; } if (archive_string_ensure(&cmdline, l + 1) == NULL) goto fail; for (i = 0; acmd->argv[i] != NULL; i++) { if (i == 0) { const char *p, *sp; if ((p = strchr(acmd->argv[i], '/')) != NULL || (p = strchr(acmd->argv[i], '\\')) != NULL) p++; else p = acmd->argv[i]; if ((sp = strchr(p, ' ')) != NULL) archive_strappend_char(&cmdline, '"'); archive_strcat(&cmdline, p); if (sp != NULL) archive_strappend_char(&cmdline, '"'); } else { archive_strappend_char(&cmdline, ' '); archive_strcat(&cmdline, acmd->argv[i]); } } if (i <= 1) { const char *sp; if ((sp = strchr(arg0, ' ')) != NULL) archive_strappend_char(&cmdline, '"'); archive_strcat(&cmdline, arg0); if (sp != NULL) archive_strappend_char(&cmdline, '"'); } secAtts.nLength = sizeof(SECURITY_ATTRIBUTES); secAtts.bInheritHandle = TRUE; secAtts.lpSecurityDescriptor = NULL; if (CreatePipe(&childStdout[0], &childStdout[1], &secAtts, 0) == 0) goto fail; if (!SetHandleInformation(childStdout[0], HANDLE_FLAG_INHERIT, 0)) goto fail; if (CreatePipe(&childStdin[0], &childStdin[1], &secAtts, 0) == 0) goto fail; if (!SetHandleInformation(childStdin[1], HANDLE_FLAG_INHERIT, 0)) goto fail; if (DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &childStderr, 0, TRUE, DUPLICATE_SAME_ACCESS) == 0) goto fail; memset(&staInfo, 0, sizeof(staInfo)); staInfo.cb = sizeof(staInfo); staInfo.hStdError = childStderr; staInfo.hStdOutput = childStdout[1]; staInfo.hStdInput = childStdin[0]; staInfo.wShowWindow = SW_HIDE; staInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; if (CreateProcessA(fullpath.s, cmdline.s, NULL, NULL, TRUE, 0, NULL, NULL, &staInfo, &childInfo) == 0) goto fail; WaitForInputIdle(childInfo.hProcess, INFINITE); CloseHandle(childInfo.hProcess); CloseHandle(childInfo.hThread); *child_stdout = _open_osfhandle((intptr_t)childStdout[0], _O_RDONLY); *child_stdin = _open_osfhandle((intptr_t)childStdin[1], _O_WRONLY); CloseHandle(childStdout[1]); CloseHandle(childStdin[0]); archive_string_free(&cmdline); archive_string_free(&fullpath); __archive_cmdline_free(acmd); return (childInfo.dwProcessId); fail: if (childStdout[0] != INVALID_HANDLE_VALUE) CloseHandle(childStdout[0]); if (childStdout[1] != INVALID_HANDLE_VALUE) CloseHandle(childStdout[1]); if (childStdin[0] != INVALID_HANDLE_VALUE) CloseHandle(childStdin[0]); if (childStdin[1] != INVALID_HANDLE_VALUE) CloseHandle(childStdin[1]); if (childStderr != INVALID_HANDLE_VALUE) CloseHandle(childStderr); archive_string_free(&cmdline); archive_string_free(&fullpath); __archive_cmdline_free(acmd); return (-1); }
static int archive_write_v7tar_header(struct archive_write *a, struct archive_entry *entry) { char buff[512]; int ret, ret2; struct v7tar *v7tar; struct archive_entry *entry_main; struct archive_string_conv *sconv; v7tar = (struct v7tar *)a->format_data; /* Setup default string conversion. */ if (v7tar->opt_sconv == NULL) { if (!v7tar->init_default_conversion) { v7tar->sconv_default = archive_string_default_conversion_for_write( &(a->archive)); v7tar->init_default_conversion = 1; } sconv = v7tar->sconv_default; } else sconv = v7tar->opt_sconv; /* Sanity check. */ if (archive_entry_pathname(entry) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Can't record entry in tar file without pathname"); return (ARCHIVE_FAILED); } /* Only regular files (not hardlinks) have data. */ if (archive_entry_hardlink(entry) != NULL || archive_entry_symlink(entry) != NULL || !(archive_entry_filetype(entry) == AE_IFREG)) archive_entry_set_size(entry, 0); if (AE_IFDIR == archive_entry_filetype(entry)) { const char *p; size_t path_length; /* * Ensure a trailing '/'. Modify the entry so * the client sees the change. */ #if defined(_WIN32) && !defined(__CYGWIN__) const wchar_t *wp; wp = archive_entry_pathname_w(entry); if (wp != NULL && wp[wcslen(wp) -1] != L'/') { struct archive_wstring ws; archive_string_init(&ws); path_length = wcslen(wp); if (archive_wstring_ensure(&ws, path_length + 2) == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate v7tar data"); archive_wstring_free(&ws); return(ARCHIVE_FATAL); } /* Should we keep '\' ? */ if (wp[path_length -1] == L'\\') path_length--; archive_wstrncpy(&ws, wp, path_length); archive_wstrappend_wchar(&ws, L'/'); archive_entry_copy_pathname_w(entry, ws.s); archive_wstring_free(&ws); p = NULL; } else #endif p = archive_entry_pathname(entry); /* * On Windows, this is a backup operation just in * case getting WCS failed. On POSIX, this is a * normal operation. */ if (p != NULL && p[strlen(p) - 1] != '/') { struct archive_string as; archive_string_init(&as); path_length = strlen(p); if (archive_string_ensure(&as, path_length + 2) == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate v7tar data"); archive_string_free(&as); return(ARCHIVE_FATAL); } #if defined(_WIN32) && !defined(__CYGWIN__) /* NOTE: This might break the pathname * if the current code page is CP932 and * the pathname includes a character '\' * as a part of its multibyte pathname. */ if (p[strlen(p) -1] == '\\') path_length--; else #endif archive_strncpy(&as, p, path_length); archive_strappend_char(&as, '/'); archive_entry_copy_pathname(entry, as.s); archive_string_free(&as); } } #if defined(_WIN32) && !defined(__CYGWIN__) /* Make sure the path separators in pahtname, hardlink and symlink * are all slash '/', not the Windows path separator '\'. */ entry_main = __la_win_entry_in_posix_pathseparator(entry); if (entry_main == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate v7tar data"); return(ARCHIVE_FATAL); } if (entry != entry_main) entry = entry_main; else entry_main = NULL; #else entry_main = NULL; #endif ret = format_header_v7tar(a, buff, entry, 1, sconv); if (ret < ARCHIVE_WARN) { if (entry_main) archive_entry_free(entry_main); return (ret); } ret2 = __archive_write_output(a, buff, 512); if (ret2 < ARCHIVE_WARN) { if (entry_main) archive_entry_free(entry_main); return (ret2); } if (ret2 < ret) ret = ret2; v7tar->entry_bytes_remaining = archive_entry_size(entry); v7tar->entry_padding = 0x1ff & (-(int64_t)v7tar->entry_bytes_remaining); if (entry_main) archive_entry_free(entry_main); return (ret); }
/* * Write /set keyword. It means set global datas. * [directory-only mode] * - It is only once to write /set keyword. It is using values of the * first entry. * [normal mode] * - Write /set keyword. It is using values of the first entry whose * filetype is a regular file. * - When a parent directory of the entry whose filetype is the regular * file is changed, check the global datas and write it again if its * values are different from the entry's. */ static void set_global(struct mtree_writer *mtree, struct archive_entry *entry) { struct archive_string setstr; struct archive_string unsetstr; const char *name; int keys, oldkeys, effkeys; mode_t set_type = 0; switch (archive_entry_filetype(entry)) { case AE_IFLNK: case AE_IFSOCK: case AE_IFCHR: case AE_IFBLK: case AE_IFIFO: break; case AE_IFDIR: if (mtree->dironly) set_type = AE_IFDIR; break; case AE_IFREG: default: /* Handle unknown file types as regular files. */ if (!mtree->dironly) set_type = AE_IFREG; break; } if (set_type == 0) return; if (mtree->set.processed && !parent_dir_changed(&mtree->set.parent, entry)) return; /* At first, save a parent directory of the entry for following * entries. */ if (!mtree->set.processed && set_type == AE_IFREG) parent_dir_changed(&mtree->set.parent, entry); archive_string_init(&setstr); archive_string_init(&unsetstr); keys = mtree->keys & (F_FLAGS | F_GID | F_GNAME | F_NLINK | F_MODE | F_TYPE | F_UID | F_UNAME); oldkeys = mtree->set.keys; effkeys = keys; if (mtree->set.processed) { /* * Check the global datas for whether it needs updating. */ effkeys &= ~F_TYPE; if ((oldkeys & (F_UNAME | F_UID)) != 0 && mtree->set.uid == archive_entry_uid(entry)) effkeys &= ~(F_UNAME | F_UID); if ((oldkeys & (F_GNAME | F_GID)) != 0 && mtree->set.gid == archive_entry_gid(entry)) effkeys &= ~(F_GNAME | F_GID); if ((oldkeys & F_MODE) != 0 && mtree->set.mode == (archive_entry_mode(entry) & 07777)) effkeys &= ~F_MODE; if ((oldkeys & F_FLAGS) != 0) { unsigned long fflags_set; unsigned long fflags_clear; archive_entry_fflags(entry, &fflags_set, &fflags_clear); if (fflags_set == mtree->set.fflags_set && fflags_clear == mtree->set.fflags_clear) effkeys &= ~F_FLAGS; } } if ((keys & effkeys & F_TYPE) != 0) { mtree->set.type = set_type; if (set_type == AE_IFDIR) archive_strcat(&setstr, " type=dir"); else archive_strcat(&setstr, " type=file"); } if ((keys & effkeys & F_UNAME) != 0) { if ((name = archive_entry_uname(entry)) != NULL) { archive_strcat(&setstr, " uname="); mtree_quote(&setstr, name); } else if ((oldkeys & F_UNAME) != 0) archive_strcat(&unsetstr, " uname"); else keys &= ~F_UNAME; } if ((keys & effkeys & F_UID) != 0) { mtree->set.uid = archive_entry_uid(entry); archive_string_sprintf(&setstr, " uid=%jd", (intmax_t)mtree->set.uid); } if ((keys & effkeys & F_GNAME) != 0) { if ((name = archive_entry_gname(entry)) != NULL) { archive_strcat(&setstr, " gname="); mtree_quote(&setstr, name); } else if ((oldkeys & F_GNAME) != 0) archive_strcat(&unsetstr, " gname"); else keys &= ~F_GNAME; } if ((keys & effkeys & F_GID) != 0) { mtree->set.gid = archive_entry_gid(entry); archive_string_sprintf(&setstr, " gid=%jd", (intmax_t)mtree->set.gid); } if ((keys & effkeys & F_MODE) != 0) { mtree->set.mode = archive_entry_mode(entry) & 07777; archive_string_sprintf(&setstr, " mode=%o", mtree->set.mode); } if ((keys & effkeys & F_FLAGS) != 0) { if ((name = archive_entry_fflags_text(entry)) != NULL) { archive_strcat(&setstr, " flags="); mtree_quote(&setstr, name); archive_entry_fflags(entry, &mtree->set.fflags_set, &mtree->set.fflags_clear); } else if ((oldkeys & F_FLAGS) != 0) archive_strcat(&unsetstr, " flags"); else keys &= ~F_FLAGS; } if (unsetstr.length > 0) archive_string_sprintf(&mtree->buf, "/unset%s\n", unsetstr.s); archive_string_free(&unsetstr); if (setstr.length > 0) archive_string_sprintf(&mtree->buf, "/set%s\n", setstr.s); archive_string_free(&setstr); mtree->set.keys = keys; mtree->set.processed = 1; /* On directory-only mode, it is only once to write /set keyword. */ if (mtree->dironly) mtree->set.output = 0; }
static int archive_write_gnutar_header(struct archive_write *a, struct archive_entry *entry) { char buff[512]; int r, ret, ret2 = ARCHIVE_OK; int tartype; struct gnutar *gnutar; struct archive_string_conv *sconv; struct archive_entry *entry_main; gnutar = (struct gnutar *)a->format_data; /* Setup default string conversion. */ if (gnutar->opt_sconv == NULL) { if (!gnutar->init_default_conversion) { gnutar->sconv_default = archive_string_default_conversion_for_write( &(a->archive)); gnutar->init_default_conversion = 1; } sconv = gnutar->sconv_default; } else sconv = gnutar->opt_sconv; /* Only regular files (not hardlinks) have data. */ if (archive_entry_hardlink(entry) != NULL || archive_entry_symlink(entry) != NULL || !(archive_entry_filetype(entry) == AE_IFREG)) archive_entry_set_size(entry, 0); if (AE_IFDIR == archive_entry_filetype(entry)) { const char *p; size_t path_length; /* * Ensure a trailing '/'. Modify the entry so * the client sees the change. */ #if defined(_WIN32) && !defined(__CYGWIN__) const wchar_t *wp; wp = archive_entry_pathname_w(entry); if (wp != NULL && wp[wcslen(wp) -1] != L'/') { struct archive_wstring ws; archive_string_init(&ws); path_length = wcslen(wp); if (archive_wstring_ensure(&ws, path_length + 2) == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate ustar data"); archive_wstring_free(&ws); return(ARCHIVE_FATAL); } /* Should we keep '\' ? */ if (wp[path_length -1] == L'\\') path_length--; archive_wstrncpy(&ws, wp, path_length); archive_wstrappend_wchar(&ws, L'/'); archive_entry_copy_pathname_w(entry, ws.s); archive_wstring_free(&ws); p = NULL; } else #endif p = archive_entry_pathname(entry); /* * On Windows, this is a backup operation just in * case getting WCS failed. On POSIX, this is a * normal operation. */ if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') { struct archive_string as; archive_string_init(&as); path_length = strlen(p); if (archive_string_ensure(&as, path_length + 2) == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate ustar data"); archive_string_free(&as); return(ARCHIVE_FATAL); } #if defined(_WIN32) && !defined(__CYGWIN__) /* NOTE: This might break the pathname * if the current code page is CP932 and * the pathname includes a character '\' * as a part of its multibyte pathname. */ if (p[strlen(p) -1] == '\\') path_length--; else #endif archive_strncpy(&as, p, path_length); archive_strappend_char(&as, '/'); archive_entry_copy_pathname(entry, as.s); archive_string_free(&as); } } #if defined(_WIN32) && !defined(__CYGWIN__) /* Make sure the path separators in pathname, hardlink and symlink * are all slash '/', not the Windows path separator '\'. */ entry_main = __la_win_entry_in_posix_pathseparator(entry); if (entry_main == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate ustar data"); return(ARCHIVE_FATAL); } if (entry != entry_main) entry = entry_main; else entry_main = NULL; #else entry_main = NULL; #endif r = archive_entry_pathname_l(entry, &(gnutar->pathname), &(gnutar->pathname_length), sconv); if (r != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Pathame"); ret = ARCHIVE_FATAL; goto exit_write_header; } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate pathname '%s' to %s", archive_entry_pathname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } r = archive_entry_uname_l(entry, &(gnutar->uname), &(gnutar->uname_length), sconv); if (r != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Uname"); ret = ARCHIVE_FATAL; goto exit_write_header; } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate uname '%s' to %s", archive_entry_uname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } r = archive_entry_gname_l(entry, &(gnutar->gname), &(gnutar->gname_length), sconv); if (r != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Gname"); ret = ARCHIVE_FATAL; goto exit_write_header; } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate gname '%s' to %s", archive_entry_gname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } /* If linkname is longer than 100 chars we need to add a 'K' header. */ r = archive_entry_hardlink_l(entry, &(gnutar->linkname), &(gnutar->linkname_length), sconv); if (r != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Linkname"); ret = ARCHIVE_FATAL; goto exit_write_header; } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate linkname '%s' to %s", archive_entry_hardlink(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } if (gnutar->linkname_length == 0) { r = archive_entry_symlink_l(entry, &(gnutar->linkname), &(gnutar->linkname_length), sconv); if (r != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Linkname"); ret = ARCHIVE_FATAL; goto exit_write_header; } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate linkname '%s' to %s", archive_entry_hardlink(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } } if (gnutar->linkname_length > GNUTAR_linkname_size) { size_t length = gnutar->linkname_length + 1; struct archive_entry *temp = archive_entry_new2(&a->archive); /* Uname/gname here don't really matter since no one reads them; * these are the values that GNU tar happens to use on FreeBSD. */ archive_entry_set_uname(temp, "root"); archive_entry_set_gname(temp, "wheel"); archive_entry_set_pathname(temp, "././@LongLink"); archive_entry_set_size(temp, length); ret = archive_format_gnutar_header(a, buff, temp, 'K'); archive_entry_free(temp); if (ret < ARCHIVE_WARN) goto exit_write_header; ret = __archive_write_output(a, buff, 512); if (ret < ARCHIVE_WARN) goto exit_write_header; /* Write name and trailing null byte. */ ret = __archive_write_output(a, gnutar->linkname, length); if (ret < ARCHIVE_WARN) goto exit_write_header; /* Pad to 512 bytes */ ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length)); if (ret < ARCHIVE_WARN) goto exit_write_header; } /* If pathname is longer than 100 chars we need to add an 'L' header. */ if (gnutar->pathname_length > GNUTAR_name_size) { const char *pathname = gnutar->pathname; size_t length = gnutar->pathname_length + 1; struct archive_entry *temp = archive_entry_new2(&a->archive); /* Uname/gname here don't really matter since no one reads them; * these are the values that GNU tar happens to use on FreeBSD. */ archive_entry_set_uname(temp, "root"); archive_entry_set_gname(temp, "wheel"); archive_entry_set_pathname(temp, "././@LongLink"); archive_entry_set_size(temp, length); ret = archive_format_gnutar_header(a, buff, temp, 'L'); archive_entry_free(temp); if (ret < ARCHIVE_WARN) goto exit_write_header; ret = __archive_write_output(a, buff, 512); if(ret < ARCHIVE_WARN) goto exit_write_header; /* Write pathname + trailing null byte. */ ret = __archive_write_output(a, pathname, length); if(ret < ARCHIVE_WARN) goto exit_write_header; /* Pad to multiple of 512 bytes. */ ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length)); if (ret < ARCHIVE_WARN) goto exit_write_header; } if (archive_entry_hardlink(entry) != NULL) { tartype = '1'; } else switch (archive_entry_filetype(entry)) { case AE_IFREG: tartype = '0' ; break; case AE_IFLNK: tartype = '2' ; break; case AE_IFCHR: tartype = '3' ; break; case AE_IFBLK: tartype = '4' ; break; case AE_IFDIR: tartype = '5' ; break; case AE_IFIFO: tartype = '6' ; break; case AE_IFSOCK: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "tar format cannot archive socket"); ret = ARCHIVE_FAILED; goto exit_write_header; default: archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "tar format cannot archive this (mode=0%lo)", (unsigned long)archive_entry_mode(entry)); ret = ARCHIVE_FAILED; goto exit_write_header; } ret = archive_format_gnutar_header(a, buff, entry, tartype); if (ret < ARCHIVE_WARN) goto exit_write_header; if (ret2 < ret) ret = ret2; ret2 = __archive_write_output(a, buff, 512); if (ret2 < ARCHIVE_WARN) { ret = ret2; goto exit_write_header; } if (ret2 < ret) ret = ret2; gnutar->entry_bytes_remaining = archive_entry_size(entry); gnutar->entry_padding = 0x1ff & (-(int64_t)gnutar->entry_bytes_remaining); exit_write_header: archive_entry_free(entry_main); return (ret); }