void init(DWORD netbase, DWORD recv, DWORD send) { if (recv) { pfn_ws2_recv = reinterpret_cast<void*>(*reinterpret_cast<DWORD*>(recv)); *reinterpret_cast<DWORD*>(recv) = reinterpret_cast<DWORD>(ws2_recv); rbuf_clear(); } if (send) { pfn_ws2_send = reinterpret_cast<void*>(*reinterpret_cast<DWORD*>(send)); *reinterpret_cast<DWORD*>(send) = reinterpret_cast<DWORD>(ws2_send); sbuf_clear(); } { DWORD address = config::get_n("packet", "plt_address"); bool as = false; if (address == 0) { address = netbase; as = true; } packet = new CPacket; packet->initialize(address, as); } dbgprintf(0, "recv_a=%08X recv_o=%08X recv_m=%08X send_a=%08X send_o=%08X send_m=%08X\n", recv, pfn_ws2_recv, ws2_recv, send, pfn_ws2_send, ws2_send); }
void vfs_mountroot(void) { struct mount *mp; struct sbuf *sb; struct thread *td; time_t timebase; int error; td = curthread; vfs_mountroot_wait(); sb = sbuf_new_auto(); vfs_mountroot_conf0(sb); sbuf_finish(sb); error = vfs_mountroot_devfs(td, &mp); while (!error) { error = vfs_mountroot_parse(sb, mp); if (!error) { error = vfs_mountroot_shuffle(td, mp); if (!error) { sbuf_clear(sb); error = vfs_mountroot_readconf(td, sb); sbuf_finish(sb); } } } sbuf_delete(sb); /* * Iterate over all currently mounted file systems and use * the time stamp found to check and/or initialize the RTC. * Call inittodr() only once and pass it the largest of the * timestamps we encounter. */ timebase = 0; mtx_lock(&mountlist_mtx); mp = TAILQ_FIRST(&mountlist); while (mp != NULL) { if (mp->mnt_time > timebase) timebase = mp->mnt_time; mp = TAILQ_NEXT(mp, mnt_list); } mtx_unlock(&mountlist_mtx); inittodr(timebase); /* Keep prison0's root in sync with the global rootvnode. */ mtx_lock(&prison0.pr_mtx); prison0.pr_root = rootvnode; vref(prison0.pr_root); mtx_unlock(&prison0.pr_mtx); mtx_lock(&mountlist_mtx); atomic_store_rel_int(&root_mount_complete, 1); wakeup(&root_mount_complete); mtx_unlock(&mountlist_mtx); }
void job_status_end(struct sbuf *msg) { sbuf_finish(msg); printf("%s\n", sbuf_data(msg)); /*printf("\033]0; %s\007", sbuf_data(msg));*/ sbuf_clear(msg); }
void sbuf_init(struct sbuf **buf) { if (*buf == NULL) *buf = sbuf_new_auto(); else sbuf_clear(*buf); }
void sbuf_reset(struct sbuf *buf) { if (buf != NULL) { sbuf_clear(buf); sbuf_finish(buf); } }
static void khttpd_ktr_logging(struct sbuf *sbuf) { struct uio auio; struct iovec aiov; struct ktr_entry *ep; struct thread *td; int error, fd, i, n; KHTTPD_ASSERT_CURPROC_IS_KHTTPD(); td = curthread; error = kern_openat(td, AT_FDCWD, KHTTPD_KTR_FILE, UIO_SYSSPACE, O_WRONLY | O_APPEND, 0666); if (error != 0) { log(LOG_WARNING, "khttpd: failed to open ktr file '%s' (error %d)", KHTTPD_KTR_FILE, error); return; } fd = td->td_retval[0]; sbuf_clear(sbuf); n = ktr_entries; for (i = khttpd_ktr_logging_idx; i != ktr_idx; i = i == n - 1 ? 0 : i + 1) { ep = &ktr_buf[i]; sbuf_printf(sbuf, "%lld %p %d ", (long long)ep->ktr_timestamp, ep->ktr_thread, ep->ktr_cpu); sbuf_printf(sbuf, ep->ktr_desc, ep->ktr_parms[0], ep->ktr_parms[1], ep->ktr_parms[2], ep->ktr_parms[3], ep->ktr_parms[4], ep->ktr_parms[5]); sbuf_cat(sbuf, "\n"); } sbuf_finish(sbuf); khttpd_ktr_logging_idx = i; aiov.iov_base = sbuf_data(sbuf); aiov.iov_len = sbuf_len(sbuf); auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_resid = aiov.iov_len; auio.uio_segflg = UIO_SYSSPACE; error = kern_writev(td, fd, &auio); if (error != 0) log(LOG_WARNING, "khttpd: KTR flush failed " "(error: %d)", error); kern_close(td, fd); }
/* * Copy a string into an sbuf. */ int sbuf_cpy(struct sbuf *s, const char *str) { assert_sbuf_integrity(s); assert_sbuf_state(s, 0); sbuf_clear(s); return (sbuf_cat(s, str)); }
/* * Copy a byte string into an sbuf. */ int sbuf_bcpy(struct sbuf *s, const char *str, size_t len) { assert_sbuf_integrity(s); assert_sbuf_state(s, 0); sbuf_clear(s); return (sbuf_bcat(s, str, len)); }
/* * Copy a byte string into an sbuf. */ int sbuf_bcpy(struct sbuf *s, const void *buf, size_t len) { assert_sbuf_integrity(s); assert_sbuf_state(s, 0); sbuf_clear(s); return (sbuf_bcat(s, buf, len)); }
void fin() { if (packet) { delete packet; packet = NULL; } rbuf_clear(); sbuf_clear(); }
int packing_append_tree(struct packing *pack, const char *treepath, const char *newroot) { FTS *fts = NULL; FTSENT *fts_e = NULL; size_t treelen; struct sbuf *sb; char *paths[2] = { __DECONST(char *, treepath), NULL }; treelen = strlen(treepath); fts = fts_open(paths, FTS_PHYSICAL | FTS_XDEV, NULL); if (fts == NULL) goto cleanup; sb = sbuf_new_auto(); while ((fts_e = fts_read(fts)) != NULL) { switch(fts_e->fts_info) { case FTS_D: case FTS_DEFAULT: case FTS_F: case FTS_SL: case FTS_SLNONE: /* Entries not within this tree are irrelevant. */ if (fts_e->fts_pathlen <= treelen) break; sbuf_clear(sb); /* Strip the prefix to obtain the target path */ if (newroot) /* Prepend a root if one is specified */ sbuf_cat(sb, newroot); /* +1 = skip trailing slash */ sbuf_cat(sb, fts_e->fts_path + treelen + 1); sbuf_finish(sb); packing_append_file(pack, fts_e->fts_name, sbuf_get(sb)); break; case FTS_DC: case FTS_DNR: case FTS_ERR: case FTS_NS: /* XXX error cases, check fts_e->fts_errno and * bubble up the call chain */ break; default: break; } } sbuf_free(sb); cleanup: fts_close(fts); return EPKG_OK; }
static char * sbuf_json_escape(struct sbuf *buf, const char *str) { sbuf_clear(buf); while (str != NULL && *str != '\0') { if (*str == '"' || *str == '\\') sbuf_putc(buf, '\\'); sbuf_putc(buf, *str); str++; } sbuf_finish(buf); return (sbuf_data(buf)); }
void job_status_begin(struct sbuf *msg) { int n; sbuf_clear(msg); #ifdef HAVE_LIBJAIL static char hostname[MAXHOSTNAMELEN] = ""; static int jailed = -1; size_t intlen; if (jailed == -1) { intlen = sizeof(jailed); if (sysctlbyname("security.jail.jailed", &jailed, &intlen, NULL, 0) == -1) jailed = 0; } if (jailed == 1) { if (hostname[0] == '\0') gethostname(hostname, sizeof(hostname)); sbuf_printf(msg, "[%s] ", hostname); } #endif /* Only used for pkg-add right now. */ if (add_deps_depth) { if (add_deps_depth > 1) { for (n = 0; n < (2 * add_deps_depth); ++n) { if (n % 4 == 0 && n < (2 * add_deps_depth)) sbuf_cat(msg, "|"); else sbuf_cat(msg, " "); } } sbuf_cat(msg, "`-- "); } if (nbactions > 0 && nbdone > 0) sbuf_printf(msg, "[%d/%d] ", nbdone, nbactions); }
static int xendebug_filter(void *arg) { #if defined(STACK) && defined(DDB) struct stack st; struct trapframe *frame; frame = arg; stack_zero(&st); stack_save(&st); mtx_lock_spin(&lock); sbuf_clear(buf); xc_printf("Printing stack trace vCPU%d\n", PCPU_GET(vcpu_id)); stack_sbuf_print_ddb(buf, &st); sbuf_finish(buf); mtx_unlock_spin(&lock); #endif return (FILTER_HANDLED); }
static int urlencode(const char *src, struct sbuf **dest) { size_t len; size_t i; if (*dest == NULL) *dest = sbuf_new_auto(); else sbuf_clear(*dest); len = strlen(src); for (i = 0; i < len; i++) { if (!isascii(src[i]) || src[i] == '%') sbuf_printf(*dest, "%%%.2x", (unsigned char)src[i]); else sbuf_putc(*dest, src[i]); } sbuf_finish(*dest); return (EPKG_OK); }
bool ucl_object_emit_sbuf(const ucl_object_t *obj, enum ucl_emitter emit_type, struct sbuf **buf) { bool ret = false; struct ucl_emitter_functions func = { .ucl_emitter_append_character = ucl_sbuf_append_character, .ucl_emitter_append_len = ucl_sbuf_append_len, .ucl_emitter_append_int = ucl_sbuf_append_int, .ucl_emitter_append_double = ucl_sbuf_append_double }; if (*buf == NULL) *buf = sbuf_new_auto(); else sbuf_clear(*buf); func.ud = *buf; ret = ucl_object_emit_full(obj, emit_type, &func); sbuf_finish(*buf); return (ret); }
struct t_rename_pattern * t_rename_parse(const char *source) { const char sep = '%'; const char *c = source; struct t_rename_pattern *pattern = NULL; struct t_rename_token *token; struct sbuf *sb = NULL; enum { PARSING_STRING, PARSING_SIMPLE_TAG, PARSING_BRACE_TAG } state; sb = sbuf_new_auto(); if (sb == NULL) goto error_label; pattern = malloc(sizeof(struct t_rename_pattern)); if (pattern == NULL) goto error_label; TAILQ_INIT(pattern); state = PARSING_STRING; while (*c != '\0') { /* if we parse a litteral string, check if this is the start of a tag */ if (state == PARSING_STRING && *c == sep) { /* avoid to add a empty token. This can happen when when parsing two consecutive tags like `%tag%tag' */ if (sbuf_len(sb) > 0) { if (sbuf_finish(sb) == -1) goto error_label; token = t_rename_token_new(T_STRING, sbuf_data(sb)); if (token == NULL) goto error_label; TAILQ_INSERT_TAIL(pattern, token, entries); } sbuf_clear(sb); c += 1; if (*c == '{') { c += 1; state = PARSING_BRACE_TAG; } else { state = PARSING_SIMPLE_TAG; } /* if we parse a tag, check for the end of it */ } else if ((state == PARSING_SIMPLE_TAG && (*c == sep || !isalnum(*c))) || (state == PARSING_BRACE_TAG && *c == '}')) { if (sbuf_len(sb) == 0) warnx("empty tag in rename pattern"); if (sbuf_finish(sb) == -1) goto error_label; token = t_rename_token_new(T_TAG, sbuf_data(sb)); if (token == NULL) goto error_label; sbuf_clear(sb); TAILQ_INSERT_TAIL(pattern, token, entries); if (state == PARSING_BRACE_TAG) { /* eat the closing `}' */ c += 1; } state = PARSING_STRING; } else { /* default case for both string and tags. `\' escape everything */ if (*c == '\\') { c += 1; if (*c == '\0') break; } sbuf_putc(sb, *c); c += 1; } } /* we've hit the end of the source. Check in which state we are and try to finish cleany */ switch (state) { case PARSING_BRACE_TAG: warnx("missing closing `}' at the end of the rename pattern"); goto error_label; case PARSING_SIMPLE_TAG: if (sbuf_len(sb) == 0) warnx("empty tag at the end of the rename pattern"); case PARSING_STRING: /* FALLTHROUGH */ default: /* all is right */; } /* finish the last tag unless it is the empty string */ if (state != PARSING_STRING || sbuf_len(sb) > 0) { if (sbuf_finish(sb) == -1) goto error_label; token = t_rename_token_new( (state == PARSING_STRING ? T_STRING : T_TAG), sbuf_data(sb)); if (token == NULL) goto error_label; TAILQ_INSERT_TAIL(pattern, token, entries); } sbuf_delete(sb); return (pattern); /* NOTREACHED */ error_label: sbuf_delete(sb); t_rename_pattern_delete(pattern); return (NULL); }
static void format_str(struct pkg *pkg, struct sbuf *dest, const char *qstr, void *data) { char size[7]; const char *tmp; bool automatic; int64_t flatsize; lic_t licenselogic; sbuf_clear(dest); while (qstr[0] != '\0') { if (qstr[0] == '%') { qstr++; switch (qstr[0]) { case 'n': pkg_get(pkg, PKG_NAME, &tmp); sbuf_cat(dest, tmp); break; case 'v': pkg_get(pkg, PKG_VERSION, &tmp); sbuf_cat(dest, tmp); break; case 'o': pkg_get(pkg, PKG_ORIGIN, &tmp); sbuf_cat(dest, tmp); break; case 'p': pkg_get(pkg, PKG_PREFIX, &tmp); sbuf_cat(dest, tmp); break; case 'm': pkg_get(pkg, PKG_MAINTAINER, &tmp); sbuf_cat(dest, tmp); break; case 'c': pkg_get(pkg, PKG_COMMENT, &tmp); sbuf_cat(dest, tmp); break; case 'w': pkg_get(pkg, PKG_WWW, &tmp); sbuf_cat(dest, tmp); break; case 'a': pkg_get(pkg, PKG_AUTOMATIC, &automatic); sbuf_printf(dest, "%d", automatic); break; case 's': qstr++; pkg_get(pkg, PKG_FLATSIZE, &flatsize); if (qstr[0] == 'h') { humanize_number(size, sizeof(size), flatsize, "B", HN_AUTOSCALE, 0); sbuf_cat(dest, size); } else if (qstr[0] == 'b') { sbuf_printf(dest, "%" PRId64, flatsize); } break; case '?': qstr++; switch (qstr[0]) { case 'd': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_DEPS)); break; case 'r': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_RDEPS)); break; case 'C': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_CATEGORIES)); break; case 'F': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_FILES)); break; case 'O': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_OPTIONS)); break; case 'D': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_DIRS)); break; case 'L': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_LICENSES)); break; case 'U': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_USERS)); break; case 'G': sbuf_printf(dest, "%d", !pkg_list_is_empty(pkg, PKG_GROUPS)); break; } break; case 'l': pkg_get(pkg, PKG_LICENSE_LOGIC, &licenselogic); switch (licenselogic) { case LICENSE_SINGLE: sbuf_cat(dest, "single"); break; case LICENSE_OR: sbuf_cat(dest, "or"); break; case LICENSE_AND: sbuf_cat(dest, "and"); break; } break; case 'd': qstr++; if (qstr[0] == 'n') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_NAME)); else if (qstr[0] == 'o') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_ORIGIN)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_VERSION)); break; case 'r': qstr++; if (qstr[0] == 'n') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_NAME)); else if (qstr[0] == 'o') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_ORIGIN)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_dep_get((struct pkg_dep *)data, PKG_DEP_VERSION)); break; case 'C': sbuf_cat(dest, pkg_category_name((struct pkg_category *)data)); break; case 'F': qstr++; if (qstr[0] == 'p') sbuf_cat(dest, pkg_file_get((struct pkg_file *)data, PKG_FILE_PATH)); else if (qstr[0] == 's') sbuf_cat(dest, pkg_file_get((struct pkg_file *)data, PKG_FILE_SUM)); break; case 'S': sbuf_cat(dest, pkg_script_data((struct pkg_script *)data)); break; case 'O': qstr++; if (qstr[0] == 'k') sbuf_cat(dest, pkg_option_opt((struct pkg_option *)data)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_option_value((struct pkg_option *)data)); break; case 'D': sbuf_cat(dest, pkg_dir_path((struct pkg_dir *)data)); break; case 'L': sbuf_cat(dest, pkg_license_name((struct pkg_license *)data)); break; case 'U': sbuf_cat(dest, pkg_user_name((struct pkg_user *)data)); break; case 'G': sbuf_cat(dest, pkg_group_name((struct pkg_group *)data)); break; case '%': sbuf_putc(dest, '%'); break; } } else if (qstr[0] == '\\') { qstr++; switch (qstr[0]) { case 'n': sbuf_putc(dest, '\n'); break; case 'a': sbuf_putc(dest, '\a'); break; case 'b': sbuf_putc(dest, '\b'); break; case 'f': sbuf_putc(dest, '\f'); break; case 'r': sbuf_putc(dest, '\r'); break; case '\\': sbuf_putc(dest, '\\'); break; case 't': sbuf_putc(dest, '\t'); break; } } else { sbuf_putc(dest, qstr[0]); } qstr++; } sbuf_finish(dest); }
static void attempt_to_merge(bool renamed, struct pkg_config_file *rcf, struct pkg *local, char *pathname, const char *path, struct sbuf *newconf) { const struct pkg_file *lf = NULL; struct pkg_config_file *lcf = NULL; char *localconf = NULL; size_t sz; char *localsum; if (!renamed) { pkg_debug(3, "Not renamed"); return; } if (rcf == NULL) { pkg_debug(3, "No remote config file"); return; } if (local == NULL) { pkg_debug(3, "No local package"); return; } if (!pkg_is_config_file(local, path, &lf, &lcf)) { pkg_debug(3, "No local package"); return; } if (lcf->content == NULL) { pkg_debug(3, "Empty configuration content for local package"); return; } pkg_debug(1, "Config file found %s", pathname); file_to_buffer(pathname, &localconf, &sz); pkg_debug(2, "size: %d vs %d", sz, strlen(lcf->content)); if (sz == strlen(lcf->content)) { pkg_debug(2, "Ancient vanilla and deployed conf are the same size testing checksum"); localsum = pkg_checksum_data(localconf, sz, PKG_HASH_TYPE_SHA256_HEX); if (localsum && strcmp(localsum, lf->sum) == 0) { pkg_debug(2, "Checksum are the same %d", strlen(localconf)); free(localconf); free(localsum); return; } free(localsum); pkg_debug(2, "Checksum are different %d", strlen(localconf)); } pkg_debug(1, "Attempting to merge %s", pathname); if (merge_3way(lcf->content, localconf, rcf->content, newconf) != 0) { pkg_emit_error("Impossible to merge configuration file"); sbuf_clear(newconf); strlcat(pathname, ".pkgnew", MAXPATHLEN); } free(localconf); }
static int do_extract(struct archive *a, struct archive_entry *ae, const char *location, int nfiles, struct pkg *pkg, struct pkg *local) { int retcode = EPKG_OK; int ret = 0, cur_file = 0; char path[MAXPATHLEN], pathname[MAXPATHLEN], rpath[MAXPATHLEN]; struct stat st; const struct stat *aest; bool renamed = false; const struct pkg_file *rf; struct pkg_config_file *rcf; struct sbuf *newconf; bool automerge = pkg_object_bool(pkg_config_get("AUTOMERGE")); unsigned long set, clear; #ifndef HAVE_ARC4RANDOM srand(time(NULL)); #endif if (nfiles == 0) return (EPKG_OK); pkg_emit_extract_begin(pkg); pkg_emit_progress_start(NULL); newconf = sbuf_new_auto(); do { ret = ARCHIVE_OK; sbuf_clear(newconf); rf = NULL; rcf = NULL; pkg_absolutepath(archive_entry_pathname(ae), path, sizeof(path)); snprintf(pathname, sizeof(pathname), "%s%s%s", location ? location : "", *path == '/' ? "" : "/", path ); strlcpy(rpath, pathname, sizeof(rpath)); aest = archive_entry_stat(ae); archive_entry_fflags(ae, &set, &clear); if (lstat(rpath, &st) != -1) { /* * We have an existing file on the path, so handle it */ if (!S_ISDIR(aest->st_mode)) { pkg_debug(2, "Old version found, renaming"); pkg_add_file_random_suffix(rpath, sizeof(rpath), 12); renamed = true; } if (!S_ISDIR(st.st_mode) && S_ISDIR(aest->st_mode)) { if (S_ISLNK(st.st_mode)) { if (stat(rpath, &st) == -1) { pkg_emit_error("Dead symlink %s", rpath); } else { pkg_debug(2, "Directory is a symlink, use it"); pkg_emit_progress_tick(cur_file++, nfiles); continue; } } } } archive_entry_set_pathname(ae, rpath); /* load in memory the content of config files */ if (pkg_is_config_file(pkg, path, &rf, &rcf)) { pkg_debug(1, "Populating config_file %s", pathname); size_t len = archive_entry_size(ae); rcf->content = malloc(len); archive_read_data(a, rcf->content, len); if (renamed && (!automerge || local == NULL)) strlcat(pathname, ".pkgnew", sizeof(pathname)); } /* * check if the file is already provided by previous package */ if (!automerge) attempt_to_merge(renamed, rcf, local, pathname, path, newconf); if (sbuf_len(newconf) == 0 && (rcf == NULL || rcf->content == NULL)) { pkg_debug(1, "Extracting: %s", archive_entry_pathname(ae)); int install_as_user = (getenv("INSTALL_AS_USER") != NULL); int extract_flags = EXTRACT_ARCHIVE_FLAGS; if (install_as_user) { /* when installing as user don't try to set file ownership */ extract_flags &= ~ARCHIVE_EXTRACT_OWNER; } ret = archive_read_extract(a, ae, extract_flags); } else { if (sbuf_len(newconf) == 0) { sbuf_cat(newconf, rcf->content); sbuf_finish(newconf); } pkg_debug(2, "Writing conf in %s", pathname); unlink(rpath); FILE *f = fopen(rpath, "w+"); fprintf(f, "%s", sbuf_data(newconf)); fclose(f); } if (ret != ARCHIVE_OK) { /* * show error except when the failure is during * extracting a directory and that the directory already * exists. * this allow to install packages linux_base from * package for example */ if (archive_entry_filetype(ae) != AE_IFDIR || !is_dir(pathname)) { pkg_emit_error("archive_read_extract(): %s", archive_error_string(a)); retcode = EPKG_FATAL; goto cleanup; } } /* Reapply modes to the directories to work around a problem on FreeBSD 9 */ if (archive_entry_filetype(ae) == AE_IFDIR) chmod(pathname, aest->st_mode); pkg_emit_progress_tick(cur_file++, nfiles); /* Rename old file */ if (renamed) { pkg_debug(1, "Renaming %s -> %s", rpath, pathname); #ifdef HAVE_CHFLAGS bool old = false; if (set & NOCHANGESFLAGS) chflags(rpath, 0); if (lstat(pathname, &st) != -1) { old = true; if (st.st_flags & NOCHANGESFLAGS) chflags(pathname, 0); } #endif if (rename(rpath, pathname) == -1) { #ifdef HAVE_CHFLAGS /* restore flags */ if (old) chflags(pathname, st.st_flags); #endif pkg_emit_error("cannot rename %s to %s: %s", rpath, pathname, strerror(errno)); retcode = EPKG_FATAL; goto cleanup; } #ifdef HAVE_CHFLAGS /* Restore flags */ chflags(pathname, set); #endif } if (string_end_with(pathname, ".pkgnew")) pkg_emit_notice("New configuration file: %s", pathname); renamed = false; } while ((ret = archive_read_next_header(a, &ae)) == ARCHIVE_OK); if (ret != ARCHIVE_EOF) { pkg_emit_error("archive_read_next_header(): %s", archive_error_string(a)); retcode = EPKG_FATAL; } cleanup: pkg_emit_progress_tick(nfiles, nfiles); pkg_emit_extract_finished(pkg); if (renamed && retcode == EPKG_FATAL) { #ifdef HAVE_CHFLAGS if (set & NOCHANGESFLAGS) chflags(rpath, set & ~NOCHANGESFLAGS); #endif unlink(rpath); } return (retcode); }
static void format_str(struct pkg *pkg, struct sbuf *dest, const char *qstr, const void *data) { bool automatic; bool locked; sbuf_clear(dest); while (qstr[0] != '\0') { if (qstr[0] == '%') { qstr++; switch (qstr[0]) { case 'n': pkg_sbuf_printf(dest, "%n", pkg); break; case 'v': pkg_sbuf_printf(dest, "%v", pkg); break; case 'o': pkg_sbuf_printf(dest, "%o", pkg); break; case 'R': pkg_sbuf_printf(dest, "%N", pkg); break; case 'p': pkg_sbuf_printf(dest, "%p", pkg); break; case 'm': pkg_sbuf_printf(dest, "%m", pkg); break; case 'c': pkg_sbuf_printf(dest, "%c", pkg); break; case 'w': pkg_sbuf_printf(dest, "%w", pkg); break; case 'a': pkg_get(pkg, PKG_AUTOMATIC, &automatic); sbuf_printf(dest, "%d", automatic); break; case 'k': pkg_get(pkg, PKG_LOCKED, &locked); sbuf_printf(dest, "%d", locked); break; case 't': pkg_sbuf_printf(dest, "%t", pkg); break; case 's': qstr++; if (qstr[0] == 'h') pkg_sbuf_printf(dest, "%#sB", pkg); else if (qstr[0] == 'b') pkg_sbuf_printf(dest, "%s", pkg); break; case 'e': pkg_sbuf_printf(dest, "%e", pkg); break; case '?': qstr++; switch (qstr[0]) { case 'd': pkg_sbuf_printf(dest, "%?d", pkg); break; case 'r': pkg_sbuf_printf(dest, "%?r", pkg); break; case 'C': pkg_sbuf_printf(dest, "%?C", pkg); break; case 'F': pkg_sbuf_printf(dest, "%?F", pkg); break; case 'O': pkg_sbuf_printf(dest, "%?O", pkg); break; case 'D': pkg_sbuf_printf(dest, "%?D", pkg); break; case 'L': pkg_sbuf_printf(dest, "%?L", pkg); break; case 'U': pkg_sbuf_printf(dest, "%?U", pkg); break; case 'G': pkg_sbuf_printf(dest, "%?G", pkg); break; case 'B': pkg_sbuf_printf(dest, "%?B", pkg); break; case 'b': pkg_sbuf_printf(dest, "%?b", pkg); break; case 'A': pkg_sbuf_printf(dest, "%?A", pkg); break; } break; case '#': qstr++; switch (qstr[0]) { case 'd': pkg_sbuf_printf(dest, "%#d", pkg); break; case 'r': pkg_sbuf_printf(dest, "%#r", pkg); break; case 'C': pkg_sbuf_printf(dest, "%#C", pkg); break; case 'F': pkg_sbuf_printf(dest, "%#F", pkg); break; case 'O': pkg_sbuf_printf(dest, "%#O", pkg); break; case 'D': pkg_sbuf_printf(dest, "%#D", pkg); break; case 'L': pkg_sbuf_printf(dest, "%#L", pkg); break; case 'U': pkg_sbuf_printf(dest, "%#U", pkg); break; case 'G': pkg_sbuf_printf(dest, "%#G", pkg); break; case 'B': pkg_sbuf_printf(dest, "%#B", pkg); break; case 'b': pkg_sbuf_printf(dest, "%#b", pkg); break; case 'A': pkg_sbuf_printf(dest, "%#A", pkg); break; } break; case 'q': pkg_sbuf_printf(dest, "%q", pkg); break; case 'l': pkg_sbuf_printf(dest, "%l", pkg); break; case 'd': qstr++; if (qstr[0] == 'n') pkg_sbuf_printf(dest, "%dn", data); else if (qstr[0] == 'o') pkg_sbuf_printf(dest, "%do", data); else if (qstr[0] == 'v') pkg_sbuf_printf(dest, "%dv", data); break; case 'r': qstr++; if (qstr[0] == 'n') pkg_sbuf_printf(dest, "%rn", data); else if (qstr[0] == 'o') pkg_sbuf_printf(dest, "%ro", data); else if (qstr[0] == 'v') pkg_sbuf_printf(dest, "%rv", data); break; case 'C': pkg_sbuf_printf(dest, "%Cn", data); break; case 'F': qstr++; if (qstr[0] == 'p') pkg_sbuf_printf(dest, "%Fn", data); else if (qstr[0] == 's') pkg_sbuf_printf(dest, "%Fs", data); break; case 'O': qstr++; if (qstr[0] == 'k') pkg_sbuf_printf(dest, "%On", data); else if (qstr[0] == 'v') pkg_sbuf_printf(dest, "%Ov", data); else if (qstr[0] == 'd') /* default value */ pkg_sbuf_printf(dest, "%Od", data); else if (qstr[0] == 'D') /* description */ pkg_sbuf_printf(dest, "%OD", data); break; case 'D': pkg_sbuf_printf(dest, "%Dn", data); break; case 'L': pkg_sbuf_printf(dest, "%Ln", data); break; case 'U': pkg_sbuf_printf(dest, "%Un", data); break; case 'G': pkg_sbuf_printf(dest, "%Gn", data); break; case 'B': pkg_sbuf_printf(dest, "%Bn", data); break; case 'b': pkg_sbuf_printf(dest, "%bn", data); break; case 'A': qstr++; if (qstr[0] == 't') pkg_sbuf_printf(dest, "%An", data); else if (qstr[0] == 'v') pkg_sbuf_printf(dest, "%Av", data); break; case 'M': if (pkg_has_message(pkg)) pkg_sbuf_printf(dest, "%M", pkg); break; case '%': sbuf_putc(dest, '%'); break; } } else if (qstr[0] == '\\') { qstr++; switch (qstr[0]) { case 'n': sbuf_putc(dest, '\n'); break; case 'a': sbuf_putc(dest, '\a'); break; case 'b': sbuf_putc(dest, '\b'); break; case 'f': sbuf_putc(dest, '\f'); break; case 'r': sbuf_putc(dest, '\r'); break; case '\\': sbuf_putc(dest, '\\'); break; case 't': sbuf_putc(dest, '\t'); break; } } else { sbuf_putc(dest, qstr[0]); } qstr++; } sbuf_finish(dest); }
static void format_str(struct pkg *pkg, struct sbuf *dest, const char *qstr, void *data) { char size[7]; const char *tmp; bool tmp2; int64_t flatsize; int64_t timestamp; lic_t licenselogic; sbuf_clear(dest); while (qstr[0] != '\0') { if (qstr[0] == '%') { qstr++; switch (qstr[0]) { case 'n': pkg_get(pkg, PKG_NAME, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'v': pkg_get(pkg, PKG_VERSION, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'o': pkg_get(pkg, PKG_ORIGIN, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'R': pkg_get(pkg, PKG_REPONAME, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'p': pkg_get(pkg, PKG_PREFIX, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'm': pkg_get(pkg, PKG_MAINTAINER, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'c': pkg_get(pkg, PKG_COMMENT, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'w': pkg_get(pkg, PKG_WWW, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'i': pkg_get(pkg, PKG_INFOS, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case 'a': pkg_get(pkg, PKG_AUTOMATIC, &tmp2); sbuf_printf(dest, "%d", tmp2); break; case 'k': pkg_get(pkg, PKG_LOCKED, &tmp2); sbuf_printf(dest, "%d", tmp2); break; case 't': pkg_get(pkg, PKG_TIME, ×tamp); sbuf_printf(dest, "%" PRId64, timestamp); break; case 's': qstr++; pkg_get(pkg, PKG_FLATSIZE, &flatsize); if (qstr[0] == 'h') { humanize_number(size, sizeof(size), flatsize, "B", HN_AUTOSCALE, 0); sbuf_cat(dest, size); } else if (qstr[0] == 'b') { sbuf_printf(dest, "%" PRId64, flatsize); } break; case 'e': pkg_get(pkg, PKG_DESC, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case '?': qstr++; switch (qstr[0]) { case 'd': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_DEPS) > 0); break; case 'r': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_RDEPS) > 0); break; case 'C': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_CATEGORIES) > 0); break; case 'F': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_FILES) > 0); break; case 'O': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_OPTIONS) > 0); break; case 'D': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_DIRS) > 0); break; case 'L': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_LICENSES) > 0); break; case 'U': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_USERS) > 0); break; case 'G': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_GROUPS) > 0); break; case 'B': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_SHLIBS_REQUIRED) > 0); break; case 'b': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_SHLIBS_PROVIDED) > 0); break; case 'A': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_ANNOTATIONS) > 0); break; } break; case '#': qstr++; switch (qstr[0]) { case 'd': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_DEPS)); break; case 'r': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_RDEPS)); break; case 'C': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_CATEGORIES)); break; case 'F': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_FILES)); break; case 'O': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_OPTIONS)); break; case 'D': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_DIRS)); break; case 'L': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_LICENSES)); break; case 'U': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_USERS)); break; case 'G': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_GROUPS)); break; case 'B': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_SHLIBS_REQUIRED)); break; case 'b': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_SHLIBS_PROVIDED)); break; case 'A': sbuf_printf(dest, "%d", pkg_list_count(pkg, PKG_ANNOTATIONS)); break; } break; case 'l': pkg_get(pkg, PKG_LICENSE_LOGIC, &licenselogic); switch (licenselogic) { case LICENSE_SINGLE: sbuf_cat(dest, "single"); break; case LICENSE_OR: sbuf_cat(dest, "or"); break; case LICENSE_AND: sbuf_cat(dest, "and"); break; } break; case 'd': qstr++; if (qstr[0] == 'n') sbuf_cat(dest, pkg_dep_name((struct pkg_dep *)data)); else if (qstr[0] == 'o') sbuf_cat(dest, pkg_dep_origin((struct pkg_dep *)data)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_dep_version((struct pkg_dep *)data)); break; case 'r': qstr++; if (qstr[0] == 'n') sbuf_cat(dest, pkg_dep_name((struct pkg_dep *)data)); else if (qstr[0] == 'o') sbuf_cat(dest, pkg_dep_origin((struct pkg_dep *)data)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_dep_version((struct pkg_dep *)data)); break; case 'C': sbuf_cat(dest, pkg_category_name((struct pkg_category *)data)); break; case 'F': qstr++; if (qstr[0] == 'p') sbuf_cat(dest, pkg_file_path((struct pkg_file *)data)); else if (qstr[0] == 's') sbuf_cat(dest, pkg_file_cksum((struct pkg_file *)data)); break; case 'O': qstr++; if (qstr[0] == 'k') sbuf_cat(dest, pkg_option_opt((struct pkg_option *)data)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_option_value((struct pkg_option *)data)); break; case 'D': sbuf_cat(dest, pkg_dir_path((struct pkg_dir *)data)); break; case 'L': sbuf_cat(dest, pkg_license_name((struct pkg_license *)data)); break; case 'U': sbuf_cat(dest, pkg_user_name((struct pkg_user *)data)); break; case 'G': sbuf_cat(dest, pkg_group_name((struct pkg_group *)data)); break; case 'B': case 'b': sbuf_cat(dest, pkg_shlib_name((struct pkg_shlib *)data)); break; case 'A': qstr++; if (qstr[0] == 't') sbuf_cat(dest, pkg_annotation_tag((struct pkg_note *)data)); else if (qstr[0] == 'v') sbuf_cat(dest, pkg_annotation_value((struct pkg_note *)data)); break; case 'M': pkg_get(pkg, PKG_MESSAGE, &tmp); if (tmp != NULL) sbuf_cat(dest, tmp); break; case '%': sbuf_putc(dest, '%'); break; } } else if (qstr[0] == '\\') { qstr++; switch (qstr[0]) { case 'n': sbuf_putc(dest, '\n'); break; case 'a': sbuf_putc(dest, '\a'); break; case 'b': sbuf_putc(dest, '\b'); break; case 'f': sbuf_putc(dest, '\f'); break; case 'r': sbuf_putc(dest, '\r'); break; case '\\': sbuf_putc(dest, '\\'); break; case 't': sbuf_putc(dest, '\t'); break; } } else { sbuf_putc(dest, qstr[0]); } qstr++; } sbuf_finish(dest); }
static int convert_to_old(const char *pkg_add_dbdir, bool dry_run) { struct pkgdb *db = NULL; struct pkg *pkg = NULL; struct pkg_dep *dep = NULL; struct pkgdb_it *it = NULL; char *content, *name, *version, *buf; const char *tmp; int ret = EX_OK; char path[MAXPATHLEN]; int query_flags = PKG_LOAD_DEPS | PKG_LOAD_FILES | PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS | PKG_LOAD_MTREE | PKG_LOAD_USERS | PKG_LOAD_GROUPS | PKG_LOAD_RDEPS; FILE *fp, *rq; struct sbuf *install_script = sbuf_new_auto(); struct sbuf *deinstall_script = sbuf_new_auto(); if (mkdir(pkg_add_dbdir, 0755) != 0 && errno != EEXIST) err(EX_CANTCREAT, "%s", pkg_add_dbdir); if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) { pkgdb_close(db); return (EX_IOERR); } if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) { ret = EPKG_FATAL; goto cleanup; } while (pkgdb_it_next(it, &pkg, query_flags) == EPKG_OK) { rq = NULL; pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version); printf("Converting %s-%s...", name, version); if (dry_run) { printf("\n"); continue; } pkg_to_old(pkg); pkg_old_emit_content(pkg, &content); snprintf(path, MAXPATHLEN, "%s/%s-%s", pkg_add_dbdir, name, version); mkdir(path, 0755); snprintf(path, MAXPATHLEN, "%s/%s-%s/+CONTENTS", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(content, fp); fclose(fp); pkg_get(pkg, PKG_DESC, &buf); snprintf(path, MAXPATHLEN, "%s/%s-%s/+DESC", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(buf, fp); fclose(fp); pkg_get(pkg, PKG_COMMENT, &buf); snprintf(path, MAXPATHLEN, "%s/%s-%s/+COMMENT", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fprintf(fp, "%s\n", buf); fclose(fp); pkg_get(pkg, PKG_MESSAGE, &buf); if (buf != NULL && buf[0] != '\0') { snprintf(path, MAXPATHLEN, "%s/%s-%s/+DISPLAY", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(buf, fp); fclose(fp); } pkg_get(pkg, PKG_MTREE, &buf); if (buf != NULL && buf[0] != '\0') { snprintf(path, MAXPATHLEN, "%s/%s-%s/+MTREE_DIRS", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(buf, fp); fclose(fp); } sbuf_clear(install_script); tmp = pkg_script_get(pkg, PKG_SCRIPT_PRE_INSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_printf(install_script, "if [ \"$2\" = \"PRE-INSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_INSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_cat(install_script, tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_POST_INSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_printf(install_script, "if [ \"$2\" = \"POST-INSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } if (sbuf_len(install_script) > 0) { sbuf_finish(install_script); snprintf(path, MAXPATHLEN, "%s/%s-%s/+INSTALL", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(sbuf_data(install_script), fp); fclose(fp); } sbuf_clear(deinstall_script); tmp = pkg_script_get(pkg, PKG_SCRIPT_PRE_DEINSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_printf(deinstall_script, "if [ \"$2\" = \"DEINSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_DEINSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_cat(deinstall_script, tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_POST_DEINSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_printf(deinstall_script, "if [ \"$2\" = \"POST-DEINSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } if (sbuf_len(deinstall_script) > 0) { sbuf_finish(deinstall_script); snprintf(path, MAXPATHLEN, "%s/%s-%s/+DEINSTALL", pkg_add_dbdir, name, version); fp = fopen(path, "w"); fputs(sbuf_data(deinstall_script), fp); fclose(fp); } snprintf(path, MAXPATHLEN, "%s/%s-%s/+REQUIRED_BY", pkg_add_dbdir, name, version); while (pkg_rdeps(pkg, &dep) == EPKG_OK) { if (rq == NULL) rq = fopen(path, "w"); fprintf(rq, "%s-%s\n", pkg_dep_name(dep), pkg_dep_version(dep)); } if (rq != NULL) fclose(rq); printf("done.\n"); free(content); } sbuf_delete(install_script); sbuf_delete(deinstall_script); cleanup: pkg_free(pkg); pkgdb_it_free(it); pkgdb_close(db); return (ret); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; int ret; struct stat st; int64_t flatsize = 0; int64_t nfiles; const char *relocation; hardlinks_t *hardlinks; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } relocation = pkg_kv_get(&pkg->annotations, "relocated"); if (relocation == NULL) relocation = ""; if (pkg_rootdir != NULL) relocation = pkg_rootdir; /* * Get / compute size / checksum if not provided in the manifest */ nfiles = kh_count(pkg->filehash); counter_init("file sizes/checksums", nfiles); hardlinks = kh_init_hardlinks(); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); if (lstat(fpath, &st) == -1) { pkg_emit_error("file '%s' is missing", fpath); return (EPKG_FATAL); } if (file->size == 0) file->size = (int64_t)st.st_size; if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) { flatsize += file->size; } file->sum = pkg_checksum_generate_file(fpath, PKG_HASH_TYPE_SHA256_HEX); if (file->sum == NULL) return (EPKG_FATAL); counter_count(); } kh_destroy_hardlinks(hardlinks); counter_end(); pkg->flatsize = flatsize; if (pkg->type == PKG_OLD_FILE) { pkg_emit_error("Cannot create an old format package"); return (EPKG_FATAL); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_analyse_files(NULL, pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } counter_init("packing files", nfiles); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); ret = packing_append_file_attr(pkg_archive, fpath, file->path, file->uname, file->gname, file->perm, file->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); nfiles = kh_count(pkg->dirhash); counter_init("packing directories", nfiles); while (pkg_dirs(pkg, &dir) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, dir->path); ret = packing_append_file_attr(pkg_archive, fpath, dir->path, dir->uname, dir->gname, dir->perm, dir->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); return (EPKG_OK); }
int sys_abort2(struct thread *td, struct abort2_args *uap) { struct proc *p = td->td_proc; struct sbuf *sb; void *uargs[16]; int error, i, sig; /* * Do it right now so we can log either proper call of abort2(), or * note, that invalid argument was passed. 512 is big enough to * handle 16 arguments' descriptions with additional comments. */ sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); sbuf_clear(sb); sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", p->p_comm, p->p_pid, td->td_ucred->cr_uid); /* * Since we can't return from abort2(), send SIGKILL in cases, where * abort2() was called improperly */ sig = SIGKILL; /* Prevent from DoSes from user-space. */ if (uap->nargs < 0 || uap->nargs > 16) goto out; if (uap->nargs > 0) { if (uap->args == NULL) goto out; error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); if (error != 0) goto out; } /* * Limit size of 'reason' string to 128. Will fit even when * maximal number of arguments was chosen to be logged. */ if (uap->why != NULL) { error = sbuf_copyin(sb, uap->why, 128); if (error < 0) goto out; } else { sbuf_printf(sb, "(null)"); } if (uap->nargs > 0) { sbuf_printf(sb, "("); for (i = 0;i < uap->nargs; i++) sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); sbuf_printf(sb, ")"); } /* * Final stage: arguments were proper, string has been * successfully copied from userspace, and copying pointers * from user-space succeed. */ sig = SIGABRT; out: if (sig == SIGKILL) { sbuf_trim(sb); sbuf_printf(sb, " (Reason text inaccessible)"); } sbuf_cat(sb, "\n"); sbuf_finish(sb); log(LOG_INFO, "%s", sbuf_data(sb)); sbuf_delete(sb); exit1(td, W_EXITCODE(0, sig)); return (0); }
static int scan(FILE *fp, const char *name, bool quiet) { int c; bool hasid = false; bool subversion = false; analyzer_states state = INIT; struct sbuf *id = sbuf_new_auto(); locale_t l; l = newlocale(LC_ALL_MASK, "C", NULL); if (name != NULL) printf("%s:\n", name); while ((c = fgetc(fp)) != EOF) { switch (state) { case INIT: if (c == '$') { /* Transit to DELIM_SEEN if we see $ */ state = DELIM_SEEN; } else { /* Otherwise, stay in INIT state */ continue; } break; case DELIM_SEEN: if (isalpha_l(c, l)) { /* Transit to KEYWORD if we see letter */ sbuf_clear(id); sbuf_putc(id, '$'); sbuf_putc(id, c); state = KEYWORD; continue; } else if (c == '$') { /* Or, stay in DELIM_SEEN if more $ */ continue; } else { /* Otherwise, transit back to INIT */ state = INIT; } break; case KEYWORD: sbuf_putc(id, c); if (isalpha_l(c, l)) { /* * Stay in KEYWORD if additional letter is seen */ continue; } else if (c == ':') { /* * See ':' for the first time, transit to * PUNC_SEEN. */ state = PUNC_SEEN; subversion = false; } else if (c == '$') { /* * Incomplete ident. Go back to DELIM_SEEN * state because we see a '$' which could be * the beginning of a keyword. */ state = DELIM_SEEN; } else { /* * Go back to INIT state otherwise. */ state = INIT; } break; case PUNC_SEEN: case PUNC_SEEN_SVN: sbuf_putc(id, c); switch (c) { case ':': /* * If we see '::' (seen : in PUNC_SEEN), * activate subversion treatment and transit * to PUNC_SEEN_SVN state. * * If more than two :'s were seen, the ident * is invalid and we would therefore go back * to INIT state. */ if (state == PUNC_SEEN) { state = PUNC_SEEN_SVN; subversion = true; } else { state = INIT; } break; case ' ': /* * A space after ':' or '::' indicates we are at the * last component of potential ident. */ state = TEXT; break; default: /* All other characters are invalid */ state = INIT; break; } break; case TEXT: sbuf_putc(id, c); if (iscntrl_l(c, l)) { /* Control characters are not allowed in this state */ state = INIT; } else if (c == '$') { sbuf_finish(id); /* * valid ident should end with a space. * * subversion extension uses '#' to indicate that * the keyword expansion have exceeded the fixed * width, so it is also permitted if we are in * subversion mode. No length check is enforced * because GNU RCS ident(1) does not do it either. */ c = sbuf_data(id)[sbuf_len(id) - 2]; if (c == ' ' || (subversion && c == '#')) { printf(" %s\n", sbuf_data(id)); hasid = true; } state = INIT; } /* Other characters: stay in the state */ break; } } sbuf_delete(id); freelocale(l); if (!hasid) { if (!quiet) fprintf(stderr, "%s warning: no id keywords in %s\n", getprogname(), name ? name : "standard input"); return (EXIT_FAILURE); } return (EXIT_SUCCESS); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *m; int ret; const char *mtree; bool developer; struct stat st; char sha256[SHA256_DIGEST_LENGTH * 2 + 1]; int64_t flatsize = 0; ucl_object_t *obj; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } obj = pkg_annotation_lookup(pkg, "relocated"); /* * Get / compute size / checksum if not provided in the manifest */ while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); const char *pkg_sum = pkg_file_cksum(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); if (lstat(fpath, &st) != 0 || S_ISLNK(st.st_mode)) continue; if (file->size == 0) file->size = (int64_t)st.st_size; flatsize += file->size; if (pkg_sum == NULL || pkg_sum[0] == '\0') { if (pkg->type == PKG_OLD_FILE) { if (md5_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } else { if (sha256_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } strlcpy(file->sum, sha256, sizeof(file->sum)); } } pkg_set(pkg, PKG_FLATSIZE, flatsize); if (pkg->type == PKG_OLD_FILE) { const char *desc, *display, *comment; char oldcomment[BUFSIZ]; pkg_old_emit_content(pkg, &m); packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m)); free(m); pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment); packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc)); packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display)); snprintf(oldcomment, sizeof(oldcomment), "%s\n", comment); packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment)); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_register_shlibs(pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } pkg_get(pkg, PKG_MTREE, &mtree); if (mtree != NULL) packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS", strlen(mtree)); while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, file->uname, file->gname, file->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { const char *pkg_path = pkg_dir_path(dir); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, dir->uname, dir->gname, dir->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } return (EPKG_OK); }
static void config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype) { struct sbuf *buf = sbuf_new_auto(); const ucl_object_t *cur, *seq; ucl_object_iter_t it = NULL, itseq = NULL; struct config_entry *temp_config; struct config_value *cv; const char *key; int i; size_t j; /* Temporary config for configs that may be disabled. */ temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry)); while ((cur = ucl_iterate_object(obj, &it, true))) { key = ucl_object_key(cur); if (key == NULL) continue; sbuf_clear(buf); if (conftype == CONFFILE_PKG) { for (j = 0; j < strlen(key); ++j) sbuf_putc(buf, key[j]); sbuf_finish(buf); } else if (conftype == CONFFILE_REPO) { if (strcasecmp(key, "url") == 0) sbuf_cpy(buf, "PACKAGESITE"); else if (strcasecmp(key, "mirror_type") == 0) sbuf_cpy(buf, "MIRROR_TYPE"); else if (strcasecmp(key, "signature_type") == 0) sbuf_cpy(buf, "SIGNATURE_TYPE"); else if (strcasecmp(key, "fingerprints") == 0) sbuf_cpy(buf, "FINGERPRINTS"); else if (strcasecmp(key, "enabled") == 0) { if ((cur->type != UCL_BOOLEAN) || !ucl_object_toboolean(cur)) goto cleanup; } else continue; sbuf_finish(buf); } for (i = 0; i < CONFIG_SIZE; i++) { if (strcmp(sbuf_data(buf), c[i].key) == 0) break; } /* Silently skip unknown keys to be future compatible. */ if (i == CONFIG_SIZE) continue; /* env has priority over config file */ if (c[i].envset) continue; /* Parse sequence value ["item1", "item2"] */ switch (c[i].type) { case PKG_CONFIG_LIST: if (cur->type != UCL_ARRAY) { warnx("Skipping invalid array " "value for %s.\n", c[i].key); continue; } temp_config[i].list = malloc(sizeof(*temp_config[i].list)); STAILQ_INIT(temp_config[i].list); while ((seq = ucl_iterate_object(cur, &itseq, true))) { if (seq->type != UCL_STRING) continue; cv = malloc(sizeof(struct config_value)); cv->value = strdup(ucl_object_tostring(seq)); STAILQ_INSERT_TAIL(temp_config[i].list, cv, next); } break; case PKG_CONFIG_BOOL: temp_config[i].value = strdup(ucl_object_toboolean(cur) ? "yes" : "no"); break; default: /* Normal string value. */ temp_config[i].value = strdup(ucl_object_tostring(cur)); break; } } /* Repo is enabled, copy over all settings from temp_config. */ for (i = 0; i < CONFIG_SIZE; i++) { if (c[i].envset) continue; /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */ if (conftype != CONFFILE_PKG && c[i].main_only == true) continue; switch (c[i].type) { case PKG_CONFIG_LIST: c[i].list = temp_config[i].list; break; default: c[i].value = temp_config[i].value; break; } } cleanup: free(temp_config); sbuf_delete(buf); }
int write_userconfig(char const * file) { int fd; int i, j; struct sbuf *buf; FILE *fp; if (file == NULL) file = _PATH_PW_CONF; if ((fd = open(file, O_CREAT|O_RDWR|O_TRUNC|O_EXLOCK, 0644)) == -1) return (0); if ((fp = fdopen(fd, "w")) == NULL) { close(fd); return (0); } buf = sbuf_new_auto(); for (i = _UC_NONE; i < _UC_FIELDS; i++) { int quote = 1; sbuf_clear(buf); switch (i) { case _UC_DEFAULTPWD: sbuf_cat(buf, boolean_str(config.default_password)); break; case _UC_REUSEUID: sbuf_cat(buf, boolean_str(config.reuse_uids)); break; case _UC_REUSEGID: sbuf_cat(buf, boolean_str(config.reuse_gids)); break; case _UC_NISPASSWD: sbuf_cat(buf, config.nispasswd ? config.nispasswd : ""); quote = 0; break; case _UC_DOTDIR: sbuf_cat(buf, config.dotdir ? config.dotdir : boolean_str(0)); break; case _UC_NEWMAIL: sbuf_cat(buf, config.newmail ? config.newmail : boolean_str(0)); break; case _UC_LOGFILE: sbuf_cat(buf, config.logfile ? config.logfile : boolean_str(0)); break; case _UC_HOMEROOT: sbuf_cat(buf, config.home); break; case _UC_HOMEMODE: sbuf_printf(buf, "%04o", config.homemode); quote = 0; break; case _UC_SHELLPATH: sbuf_cat(buf, config.shelldir); break; case _UC_SHELLS: for (j = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) sbuf_printf(buf, "%s\"%s\"", j ? "," : "", system_shells[j]); quote = 0; break; case _UC_DEFAULTSHELL: sbuf_cat(buf, config.shell_default ? config.shell_default : bourne_shell); break; case _UC_DEFAULTGROUP: sbuf_cat(buf, config.default_group ? config.default_group : ""); break; case _UC_EXTRAGROUPS: for (j = 0; config.groups != NULL && j < (int)config.groups->sl_cur; j++) sbuf_printf(buf, "%s\"%s\"", j ? "," : "", config.groups->sl_str[j]); quote = 0; break; case _UC_DEFAULTCLASS: sbuf_cat(buf, config.default_class ? config.default_class : ""); break; case _UC_MINUID: sbuf_printf(buf, "%u", config.min_uid); quote = 0; break; case _UC_MAXUID: sbuf_printf(buf, "%u", config.max_uid); quote = 0; break; case _UC_MINGID: sbuf_printf(buf, "%u", config.min_gid); quote = 0; break; case _UC_MAXGID: sbuf_printf(buf, "%u", config.max_gid); quote = 0; break; case _UC_EXPIRE: sbuf_printf(buf, "%d", config.expire_days); quote = 0; break; case _UC_PASSWORD: sbuf_printf(buf, "%d", config.password_days); quote = 0; break; case _UC_NONE: break; } sbuf_finish(buf); if (comments[i]) fputs(comments[i], fp); if (*kwds[i]) { if (quote) fprintf(fp, "%s = \"%s\"\n", kwds[i], sbuf_data(buf)); else fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf)); #if debugging printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf)); #endif } } sbuf_delete(buf); return (fclose(fp) != EOF); }
int pkg_register_old(struct pkg *pkg) { FILE *fp; char *content; const char *pkgdbdir, *tmp; char path[MAXPATHLEN]; struct sbuf *install_script = sbuf_new_auto(); struct sbuf *deinstall_script = sbuf_new_auto(); struct pkg_dep *dep = NULL; pkg_to_old(pkg); pkg_old_emit_content(pkg, &content); pkgdbdir = pkg_object_string(pkg_config_get("PKG_DBDIR")); pkg_snprintf(path, sizeof(path), "%S/%n-%v", pkgdbdir, pkg, pkg); mkdir(path, 0755); pkg_snprintf(path, sizeof(path), "%S/%n-%v/+CONTENTS", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); fputs(content, fp); fclose(fp); pkg_snprintf(path, sizeof(path), "%S/%n-%v/+DESC", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); pkg_fprintf(fp, "%e", pkg); fclose(fp); pkg_snprintf(path, sizeof(path), "%s/%n-%v/+COMMENT", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); pkg_fprintf(fp, "%c\n", pkg); fclose(fp); if (pkg_has_message(pkg)) { pkg_snprintf(path, sizeof(path), "%s/%n-%v/+DISPLAY", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); pkg_fprintf(fp, "%M", pkg); fclose(fp); } sbuf_clear(install_script); tmp = pkg_script_get(pkg, PKG_SCRIPT_PRE_INSTALL); if (tmp != NULL && *tmp != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_printf(install_script, "if [ \"$2\" = \"PRE-INSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_INSTALL); if (tmp != NULL && *tmp != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_cat(install_script, tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_POST_INSTALL); if (tmp != NULL && *tmp != '\0') { if (sbuf_len(install_script) == 0) sbuf_cat(install_script, "#!/bin/sh\n\n"); sbuf_printf(install_script, "if [ \"$2\" = \"POST-INSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } if (sbuf_len(install_script) > 0) { sbuf_finish(install_script); pkg_snprintf(path, sizeof(path), "%s/%n-%v/+INSTALL", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); fputs(sbuf_data(install_script), fp); fclose(fp); } sbuf_clear(deinstall_script); tmp = pkg_script_get(pkg, PKG_SCRIPT_PRE_DEINSTALL); if (tmp != NULL && *tmp != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_printf(deinstall_script, "if [ \"$2\" = \"DEINSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_DEINSTALL); if (tmp != NULL && *tmp != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_cat(deinstall_script, tmp); } tmp = pkg_script_get(pkg, PKG_SCRIPT_POST_DEINSTALL); if (tmp != NULL && tmp[0] != '\0') { if (sbuf_len(deinstall_script) == 0) sbuf_cat(deinstall_script, "#!/bin/sh\n\n"); sbuf_printf(deinstall_script, "if [ \"$2\" = \"POST-DEINSTALL\" ]; then\n" "%s\n" "fi\n", tmp); } if (sbuf_len(deinstall_script) > 0) { sbuf_finish(deinstall_script); pkg_snprintf(path, sizeof(path), "%s/%n-%v/+DEINSTALL", pkgdbdir, pkg, pkg); fp = fopen(path, "w"); fputs(sbuf_data(deinstall_script), fp); fclose(fp); } while (pkg_deps(pkg, &dep)) { snprintf(path, sizeof(path), "%s/%s-%s/+REQUIRED_BY", pkgdbdir, pkg_dep_name(dep), pkg_dep_version(dep)); fp = fopen(path, "a"); pkg_fprintf(fp, "%n-%v\n", pkg, pkg); fclose(fp); } return (EPKG_OK); }