static int archive_write_mtree_header(struct archive_write *a, struct archive_entry *entry) { struct mtree_writer *mtree= a->format_data; struct archive_string *str; const char *path; mtree->entry = archive_entry_clone(entry); path = archive_entry_pathname(mtree->entry); if (mtree->first) { mtree->first = 0; archive_strcat(&mtree->buf, "#mtree\n"); } if (mtree->set.output) set_global(mtree, entry); archive_string_empty(&mtree->ebuf); str = (mtree->indent)? &mtree->ebuf : &mtree->buf; if (!mtree->dironly || archive_entry_filetype(entry) == AE_IFDIR) mtree_quote(str, path); mtree->entry_bytes_remaining = archive_entry_size(entry); if ((mtree->keys & F_CKSUM) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_CKSUM; mtree->crc = 0; mtree->crc_len = 0; } else mtree->compute_sum &= ~F_CKSUM; #ifdef ARCHIVE_HAS_MD5 if ((mtree->keys & F_MD5) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_MD5; archive_md5_init(&mtree->md5ctx); } else mtree->compute_sum &= ~F_MD5; #endif #ifdef ARCHIVE_HAS_RMD160 if ((mtree->keys & F_RMD160) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_RMD160; archive_rmd160_init(&mtree->rmd160ctx); } else mtree->compute_sum &= ~F_RMD160; #endif #ifdef ARCHIVE_HAS_SHA1 if ((mtree->keys & F_SHA1) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA1; archive_sha1_init(&mtree->sha1ctx); } else mtree->compute_sum &= ~F_SHA1; #endif #ifdef ARCHIVE_HAS_SHA256 if ((mtree->keys & F_SHA256) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA256; archive_sha256_init(&mtree->sha256ctx); } else mtree->compute_sum &= ~F_SHA256; #endif #ifdef ARCHIVE_HAS_SHA384 if ((mtree->keys & F_SHA384) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA384; archive_sha384_init(&mtree->sha384ctx); } else mtree->compute_sum &= ~F_SHA384; #endif #ifdef ARCHIVE_HAS_SHA512 if ((mtree->keys & F_SHA512) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA512; archive_sha512_init(&mtree->sha512ctx); } else mtree->compute_sum &= ~F_SHA512; #endif return (ARCHIVE_OK); }
/* * A single file can have multiple lines contribute specifications. * Parse as many lines as necessary, then pull additional information * from a backing file on disk as necessary. */ static int parse_file(struct archive_read *a, struct archive_entry *entry, struct mtree *mtree, struct mtree_entry *mentry, int *use_next) { const char *path; struct stat st_storage, *st; struct mtree_entry *mp; struct archive_entry *sparse_entry; int r = ARCHIVE_OK, r1, parsed_kws; mentry->used = 1; /* Initialize reasonable defaults. */ archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_size(entry, 0); archive_string_empty(&mtree->contents_name); /* Parse options from this line. */ parsed_kws = 0; r = parse_line(a, entry, mtree, mentry, &parsed_kws); if (mentry->full) { archive_entry_copy_pathname(entry, mentry->name); /* * "Full" entries are allowed to have multiple lines * and those lines aren't required to be adjacent. We * don't support multiple lines for "relative" entries * nor do we make any attempt to merge data from * separate "relative" and "full" entries. (Merging * "relative" and "full" entries would require dealing * with pathname canonicalization, which is a very * tricky subject.) */ for (mp = mentry->next; mp != NULL; mp = mp->next) { if (mp->full && !mp->used && strcmp(mentry->name, mp->name) == 0) { /* Later lines override earlier ones. */ mp->used = 1; r1 = parse_line(a, entry, mtree, mp, &parsed_kws); if (r1 < r) r = r1; } } } else { /* * Relative entries require us to construct * the full path and possibly update the * current directory. */ size_t n = archive_strlen(&mtree->current_dir); if (n > 0) archive_strcat(&mtree->current_dir, "/"); archive_strcat(&mtree->current_dir, mentry->name); archive_entry_copy_pathname(entry, mtree->current_dir.s); if (archive_entry_filetype(entry) != AE_IFDIR) mtree->current_dir.length = n; } if (mtree->checkfs) { /* * Try to open and stat the file to get the real size * and other file info. It would be nice to avoid * this here so that getting a listing of an mtree * wouldn't require opening every referenced contents * file. But then we wouldn't know the actual * contents size, so I don't see a really viable way * around this. (Also, we may want to someday pull * other unspecified info from the contents file on * disk.) */ mtree->fd = -1; if (archive_strlen(&mtree->contents_name) > 0) path = mtree->contents_name.s; else path = archive_entry_pathname(entry); if (archive_entry_filetype(entry) == AE_IFREG || archive_entry_filetype(entry) == AE_IFDIR) { mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC); __archive_ensure_cloexec_flag(mtree->fd); if (mtree->fd == -1 && (errno != ENOENT || archive_strlen(&mtree->contents_name) > 0)) { archive_set_error(&a->archive, errno, "Can't open %s", path); r = ARCHIVE_WARN; } } st = &st_storage; if (mtree->fd >= 0) { if (fstat(mtree->fd, st) == -1) { archive_set_error(&a->archive, errno, "Could not fstat %s", path); r = ARCHIVE_WARN; /* If we can't stat it, don't keep it open. */ close(mtree->fd); mtree->fd = -1; st = NULL; } } else if (lstat(path, st) == -1) { st = NULL; } /* * Check for a mismatch between the type in the specification * and the type of the contents object on disk. */ if (st != NULL) { if (((st->st_mode & S_IFMT) == S_IFREG && archive_entry_filetype(entry) == AE_IFREG) #ifdef S_IFLNK ||((st->st_mode & S_IFMT) == S_IFLNK && archive_entry_filetype(entry) == AE_IFLNK) #endif #ifdef S_IFSOCK ||((st->st_mode & S_IFSOCK) == S_IFSOCK && archive_entry_filetype(entry) == AE_IFSOCK) #endif #ifdef S_IFCHR ||((st->st_mode & S_IFMT) == S_IFCHR && archive_entry_filetype(entry) == AE_IFCHR) #endif #ifdef S_IFBLK ||((st->st_mode & S_IFMT) == S_IFBLK && archive_entry_filetype(entry) == AE_IFBLK) #endif ||((st->st_mode & S_IFMT) == S_IFDIR && archive_entry_filetype(entry) == AE_IFDIR) #ifdef S_IFIFO ||((st->st_mode & S_IFMT) == S_IFIFO && archive_entry_filetype(entry) == AE_IFIFO) #endif ) { /* Types match. */ } else { /* Types don't match; bail out gracefully. */ if (mtree->fd >= 0) close(mtree->fd); mtree->fd = -1; if (parsed_kws & MTREE_HAS_OPTIONAL) { /* It's not an error for an optional * entry to not match disk. */ *use_next = 1; } else if (r == ARCHIVE_OK) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "mtree specification has different" " type for %s", archive_entry_pathname(entry)); r = ARCHIVE_WARN; } return (r); } } /* * If there is a contents file on disk, pick some of the * metadata from that file. For most of these, we only * set it from the contents if it wasn't already parsed * from the specification. */ if (st != NULL) { if (((parsed_kws & MTREE_HAS_DEVICE) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) && (archive_entry_filetype(entry) == AE_IFCHR || archive_entry_filetype(entry) == AE_IFBLK)) archive_entry_set_rdev(entry, st->st_rdev); if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_gid(entry, st->st_gid); if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_uid(entry, st->st_uid); if ((parsed_kws & MTREE_HAS_MTIME) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) { #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtimespec.tv_nsec); #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtim.tv_nsec); #elif HAVE_STRUCT_STAT_ST_MTIME_N archive_entry_set_mtime(entry, st->st_mtime, st->st_mtime_n); #elif HAVE_STRUCT_STAT_ST_UMTIME archive_entry_set_mtime(entry, st->st_mtime, st->st_umtime*1000); #elif HAVE_STRUCT_STAT_ST_MTIME_USEC archive_entry_set_mtime(entry, st->st_mtime, st->st_mtime_usec*1000); #else archive_entry_set_mtime(entry, st->st_mtime, 0); #endif } if ((parsed_kws & MTREE_HAS_NLINK) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_nlink(entry, st->st_nlink); if ((parsed_kws & MTREE_HAS_PERM) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_perm(entry, st->st_mode); if ((parsed_kws & MTREE_HAS_SIZE) == 0 || (parsed_kws & MTREE_HAS_NOCHANGE) != 0) archive_entry_set_size(entry, st->st_size); archive_entry_set_ino(entry, st->st_ino); archive_entry_set_dev(entry, st->st_dev); archive_entry_linkify(mtree->resolver, &entry, &sparse_entry); } else if (parsed_kws & MTREE_HAS_OPTIONAL) { /* * Couldn't open the entry, stat it or the on-disk type * didn't match. If this entry is optional, just * ignore it and read the next header entry. */ *use_next = 1; return ARCHIVE_OK; } } mtree->cur_size = archive_entry_size(entry); mtree->offset = 0; return r; }
static int archive_write_shar_finish_entry(struct archive_write *a) { const char *g, *p, *u; struct shar *shar; int ret; shar = (struct shar *)a->format_data; if (shar->entry == NULL) return (0); if (shar->dump) { /* Finish uuencoded data. */ if (shar->has_data) { if (shar->outpos > 0) uuencode_line(a, shar, shar->outbuff, shar->outpos); archive_strcat(&shar->work, "`\nend\n"); archive_strcat(&shar->work, "SHAR_END\n"); } /* Restore file mode, owner, flags. */ /* * TODO: Don't immediately restore mode for * directories; defer that to end of script. */ archive_string_sprintf(&shar->work, "chmod %o ", (unsigned int)(archive_entry_mode(shar->entry) & 07777)); shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1); archive_strcat(&shar->work, "\n"); u = archive_entry_uname(shar->entry); g = archive_entry_gname(shar->entry); if (u != NULL || g != NULL) { archive_strcat(&shar->work, "chown "); if (u != NULL) shar_quote(&shar->work, u, 1); if (g != NULL) { archive_strcat(&shar->work, ":"); shar_quote(&shar->work, g, 1); } shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1); archive_strcat(&shar->work, "\n"); } if ((p = archive_entry_fflags_text(shar->entry)) != NULL) { archive_string_sprintf(&shar->work, "chflags %s ", p); shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1); archive_strcat(&shar->work, "\n"); } /* TODO: restore ACLs */ } else { if (shar->has_data) { /* Finish sed-encoded data: ensure last line ends. */ if (!shar->end_of_line) archive_strappend_char(&shar->work, '\n'); archive_strcat(&shar->work, "SHAR_END\n"); } } archive_entry_free(shar->entry); shar->entry = NULL; if (shar->work.length < 65536) return (ARCHIVE_OK); ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); archive_string_empty(&shar->work); return (ARCHIVE_OK); }
static ssize_t archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n) { static const size_t ensured = 65533; struct shar *shar; const char *src; char *buf, *buf_end; int ret; size_t written = n; shar = (struct shar *)a->format_data; if (!shar->has_data || n == 0) return (0); src = (const char *)buff; /* * ensure is the number of bytes in buffer before expanding the * current character. Each operation writes the current character * and optionally the start-of-new-line marker. This can happen * twice before entering the loop, so make sure three additional * bytes can be written. */ if (archive_string_ensure(&shar->work, ensured + 3) == NULL) { archive_set_error(&a->archive, ENOMEM, "Out of memory"); return (ARCHIVE_FATAL); } if (shar->work.length > ensured) { ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); archive_string_empty(&shar->work); } buf = shar->work.s + shar->work.length; buf_end = shar->work.s + ensured; if (shar->end_of_line) { *buf++ = 'X'; shar->end_of_line = 0; } while (n-- != 0) { if ((*buf++ = *src++) == '\n') { if (n == 0) shar->end_of_line = 1; else *buf++ = 'X'; } if (buf >= buf_end) { shar->work.length = buf - shar->work.s; ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); archive_string_empty(&shar->work); buf = shar->work.s; } } shar->work.length = buf - shar->work.s; return (written); }
static int archive_write_shar_header(struct archive_write *a, struct archive_entry *entry) { const char *linkname; const char *name; char *p, *pp; struct shar *shar; shar = (struct shar *)a->format_data; if (!shar->wrote_header) { archive_strcat(&shar->work, "#!/bin/sh\n"); archive_strcat(&shar->work, "# This is a shell archive\n"); shar->wrote_header = 1; } /* Save the entry for the closing. */ if (shar->entry) archive_entry_free(shar->entry); shar->entry = archive_entry_clone(entry); name = archive_entry_pathname(entry); /* Handle some preparatory issues. */ switch(archive_entry_filetype(entry)) { case AE_IFREG: /* Only regular files have non-zero size. */ break; case AE_IFDIR: archive_entry_set_size(entry, 0); /* Don't bother trying to recreate '.' */ if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0) return (ARCHIVE_OK); break; case AE_IFIFO: case AE_IFCHR: case AE_IFBLK: /* All other file types have zero size in the archive. */ archive_entry_set_size(entry, 0); break; default: archive_entry_set_size(entry, 0); if (archive_entry_hardlink(entry) == NULL && archive_entry_symlink(entry) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "shar format cannot archive this"); return (ARCHIVE_WARN); } } archive_string_empty(&shar->quoted_name); shar_quote(&shar->quoted_name, name, 1); /* Stock preparation for all file types. */ archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s); if (archive_entry_filetype(entry) != AE_IFDIR) { /* Try to create the dir. */ p = strdup(name); pp = strrchr(p, '/'); /* If there is a / character, try to create the dir. */ if (pp != NULL) { *pp = '\0'; /* Try to avoid a lot of redundant mkdir commands. */ if (strcmp(p, ".") == 0) { /* Don't try to "mkdir ." */ free(p); } else if (shar->last_dir == NULL) { archive_strcat(&shar->work, "mkdir -p "); shar_quote(&shar->work, p, 1); archive_strcat(&shar->work, " > /dev/null 2>&1\n"); shar->last_dir = p; } else if (strcmp(p, shar->last_dir) == 0) { /* We've already created this exact dir. */ free(p); } else if (strlen(p) < strlen(shar->last_dir) && strncmp(p, shar->last_dir, strlen(p)) == 0) { /* We've already created a subdir. */ free(p); } else { archive_strcat(&shar->work, "mkdir -p "); shar_quote(&shar->work, p, 1); archive_strcat(&shar->work, " > /dev/null 2>&1\n"); shar->last_dir = p; } } else { free(p); } } /* Handle file-type specific issues. */ shar->has_data = 0; if ((linkname = archive_entry_hardlink(entry)) != NULL) { archive_strcat(&shar->work, "ln -f "); shar_quote(&shar->work, linkname, 1); archive_string_sprintf(&shar->work, " %s\n", shar->quoted_name.s); } else if ((linkname = archive_entry_symlink(entry)) != NULL) { archive_strcat(&shar->work, "ln -fs "); shar_quote(&shar->work, linkname, 1); archive_string_sprintf(&shar->work, " %s\n", shar->quoted_name.s); } else { switch(archive_entry_filetype(entry)) { case AE_IFREG: if (archive_entry_size(entry) == 0) { /* More portable than "touch." */ archive_string_sprintf(&shar->work, "test -e \"%s\" || :> \"%s\"\n", shar->quoted_name.s, shar->quoted_name.s); } else { if (shar->dump) { unsigned int mode = archive_entry_mode(entry) & 0777; archive_string_sprintf(&shar->work, "uudecode -p > %s << 'SHAR_END'\n", shar->quoted_name.s); archive_string_sprintf(&shar->work, "begin %o ", mode); shar_quote(&shar->work, name, 0); archive_strcat(&shar->work, "\n"); } else { archive_string_sprintf(&shar->work, "sed 's/^X//' > %s << 'SHAR_END'\n", shar->quoted_name.s); } shar->has_data = 1; shar->end_of_line = 1; shar->outpos = 0; } break; case AE_IFDIR: archive_string_sprintf(&shar->work, "mkdir -p %s > /dev/null 2>&1\n", shar->quoted_name.s); /* Record that we just created this directory. */ if (shar->last_dir != NULL) free(shar->last_dir); shar->last_dir = strdup(name); /* Trim a trailing '/'. */ pp = strrchr(shar->last_dir, '/'); if (pp != NULL && pp[1] == '\0') *pp = '\0'; /* * TODO: Put dir name/mode on a list to be fixed * up at end of archive. */ break; case AE_IFIFO: archive_string_sprintf(&shar->work, "mkfifo %s\n", shar->quoted_name.s); break; case AE_IFCHR: archive_string_sprintf(&shar->work, "mknod %s c %ju %ju\n", shar->quoted_name.s, (uintmax_t)archive_entry_rdevmajor(entry), (uintmax_t)archive_entry_rdevminor(entry)); break; case AE_IFBLK: archive_string_sprintf(&shar->work, "mknod %s b %ju %ju\n", shar->quoted_name.s, (uintmax_t)archive_entry_rdevmajor(entry), (uintmax_t)archive_entry_rdevminor(entry)); break; default: return (ARCHIVE_WARN); } } 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); 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_copy_error(&(a->archive), ar); archive_read_free(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 something error happend, 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); }
void archive_clear_error(struct archive *a) { archive_string_empty(&a->error_string); a->error = NULL; }