void rpmInitMacros(rpmMacroContext mc, const char * macrofiles) { ARGV_t pattern, globs = NULL; if (macrofiles == NULL) return; argvSplit(&globs, macrofiles, ":"); for (pattern = globs; *pattern; pattern++) { ARGV_t path, files = NULL; /* Glob expand the macro file path element, expanding ~ to $HOME. */ if (rpmGlob(*pattern, NULL, &files) != 0) { continue; } /* Read macros from each file. */ for (path = files; *path; path++) { if (rpmFileHasSuffix(*path, ".rpmnew") || rpmFileHasSuffix(*path, ".rpmsave") || rpmFileHasSuffix(*path, ".rpmorig")) { continue; } (void) rpmLoadMacroFile(mc, *path); } argvFree(files); } argvFree(globs); /* Reload cmdline macros */ rpmLoadMacros(rpmCLIMacroContext, RMIL_CMDLINE); }
int headerFindSpec(Header h) { struct rpmtd_s filenames; int specix = -1; if (headerGet(h, RPMTAG_BASENAMES, &filenames, HEADERGET_MINMEM)) { struct rpmtd_s td; const char *str; /* Try to find spec by file flags */ if (headerGet(h, RPMTAG_FILEFLAGS, &td, HEADERGET_MINMEM)) { rpmfileAttrs *flags; while (specix < 0 && (flags = rpmtdNextUint32(&td))) { if (*flags & RPMFILE_SPECFILE) specix = rpmtdGetIndex(&td); } rpmtdFreeData(&td); } /* Still no spec? Look by filename. */ while (specix < 0 && (str = rpmtdNextString(&filenames))) { if (rpmFileHasSuffix(str, ".spec")) specix = rpmtdGetIndex(&filenames); } rpmtdFreeData(&filenames); } return specix; }
rpmRC rpmInstallSourcePackage(rpmts ts, FD_t fd, char ** specFilePtr, char ** cookie) { rpmfi fi = NULL; char * specFile = NULL; const char *rootdir = rpmtsRootDir(ts); Header h = NULL; rpmpsm psm = NULL; rpmte te = NULL; rpmRC rpmrc; int specix = -1; struct rpmtd_s filenames; rpmtdReset(&filenames); rpmrc = rpmReadPackageFile(ts, fd, NULL, &h); switch (rpmrc) { case RPMRC_NOTTRUSTED: case RPMRC_NOKEY: case RPMRC_OK: break; default: goto exit; break; } if (h == NULL) goto exit; rpmrc = RPMRC_FAIL; /* assume failure */ if (!headerIsSource(h)) { rpmlog(RPMLOG_ERR, _("source package expected, binary found\n")); goto exit; } /* src.rpm install can require specific rpmlib features, check them */ if (!rpmlibDeps(h)) goto exit; if (headerGet(h, RPMTAG_BASENAMES, &filenames, HEADERGET_ALLOC)) { struct rpmtd_s td; const char *str; const char *_cookie = headerGetString(h, RPMTAG_COOKIE); if (cookie && _cookie) *cookie = xstrdup(_cookie); /* Try to find spec by file flags */ if (_cookie && headerGet(h, RPMTAG_FILEFLAGS, &td, HEADERGET_MINMEM)) { rpmfileAttrs *flags; while (specix < 0 && (flags = rpmtdNextUint32(&td))) { if (*flags & RPMFILE_SPECFILE) specix = rpmtdGetIndex(&td); } } /* Still no spec? Look by filename. */ while (specix < 0 && (str = rpmtdNextString(&filenames))) { if (rpmFileHasSuffix(str, ".spec")) specix = rpmtdGetIndex(&filenames); } } if (rootdir && rstreq(rootdir, "/")) rootdir = NULL; /* Macros need to be added before trying to create directories */ rpmInstallLoadMacros(h); if (specix >= 0) { const char *bn; headerDel(h, RPMTAG_BASENAMES); headerDel(h, RPMTAG_DIRNAMES); headerDel(h, RPMTAG_DIRINDEXES); rpmtdInit(&filenames); for (int i = 0; (bn = rpmtdNextString(&filenames)); i++) { int spec = (i == specix); char *fn = rpmGenPath(rpmtsRootDir(ts), spec ? "%{_specdir}" : "%{_sourcedir}", bn); headerPutString(h, RPMTAG_OLDFILENAMES, fn); if (spec) specFile = xstrdup(fn); free(fn); } headerConvert(h, HEADERCONV_COMPRESSFILELIST); } else { rpmlog(RPMLOG_ERR, _("source package contains no .spec file\n")); goto exit; }; if (rpmtsAddInstallElement(ts, h, NULL, 0, NULL)) { goto exit; } te = rpmtsElement(ts, 0); if (te == NULL) { /* XXX can't happen */ goto exit; } rpmteSetFd(te, fd); rpmteSetHeader(te, h); fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, RPMFI_KEEPHEADER); h = headerFree(h); if (fi == NULL) { goto exit; } fi->apath = filenames.data; /* Ick */ rpmteSetFI(te, fi); fi = rpmfiFree(fi); if (rpmMkdirs(rpmtsRootDir(ts), "%{_topdir}:%{_sourcedir}:%{_specdir}")) { goto exit; } { /* set all files to be installed */ rpmfs fs = rpmteGetFileStates(te); int i; unsigned int fc = rpmfiFC(fi); for (i=0; i<fc; i++) rpmfsSetAction(fs, i, FA_CREATE); } psm = rpmpsmNew(ts, te); psm->goal = PKG_INSTALL; /* FIX: psm->fi->dnl should be owned. */ if (rpmpsmStage(psm, PSM_PROCESS) == RPMRC_OK) rpmrc = RPMRC_OK; (void) rpmpsmStage(psm, PSM_FINI); rpmpsmFree(psm); exit: if (specFilePtr && specFile && rpmrc == RPMRC_OK) *specFilePtr = specFile; else free(specFile); headerFree(h); rpmfiFree(fi); /* XXX nuke the added package(s). */ rpmtsClean(ts); return rpmrc; }
int rpmFileIsCompressed(const char * file, rpmCompressedMagic * compressed) { FD_t fd; ssize_t nb; int rc = -1; unsigned char magic[13]; *compressed = COMPRESSED_NOT; fd = Fopen(file, "r.ufdio"); if (fd == NULL || Ferror(fd)) { /* XXX Fstrerror */ rpmlog(RPMLOG_ERR, _("File %s: %s\n"), file, Fstrerror(fd)); if (fd) (void) Fclose(fd); return 1; } nb = Fread(magic, sizeof(magic[0]), sizeof(magic), fd); if (nb < 0) { rpmlog(RPMLOG_ERR, _("File %s: %s\n"), file, Fstrerror(fd)); rc = 1; } else if (nb < sizeof(magic)) { rpmlog(RPMLOG_ERR, _("File %s is smaller than %u bytes\n"), file, (unsigned)sizeof(magic)); rc = 0; } (void) Fclose(fd); if (rc >= 0) return rc; rc = 0; if ((magic[0] == 'B') && (magic[1] == 'Z')) { *compressed = COMPRESSED_BZIP2; } else if ((magic[0] == 'P') && (magic[1] == 'K') && (((magic[2] == 3) && (magic[3] == 4)) || ((magic[2] == '0') && (magic[3] == '0')))) { /* pkzip */ *compressed = COMPRESSED_ZIP; } else if ((magic[0] == 0xfd) && (magic[1] == 0x37) && (magic[2] == 0x7a) && (magic[3] == 0x58) && (magic[4] == 0x5a) && (magic[5] == 0x00)) { /* new style xz (lzma) with magic */ *compressed = COMPRESSED_XZ; } else if ((magic[0] == 'L') && (magic[1] == 'Z') && (magic[2] == 'I') && (magic[3] == 'P')) { *compressed = COMPRESSED_LZIP; } else if ((magic[0] == 'L') && (magic[1] == 'R') && (magic[2] == 'Z') && (magic[3] == 'I')) { *compressed = COMPRESSED_LRZIP; } else if (((magic[0] == 0037) && (magic[1] == 0213)) || /* gzip */ ((magic[0] == 0037) && (magic[1] == 0236)) || /* old gzip */ ((magic[0] == 0037) && (magic[1] == 0036)) || /* pack */ ((magic[0] == 0037) && (magic[1] == 0240)) || /* SCO lzh */ ((magic[0] == 0037) && (magic[1] == 0235)) /* compress */ ) { *compressed = COMPRESSED_OTHER; } else if ((magic[0] == '7') && (magic[1] == 'z') && (magic[2] == 0xbc) && (magic[3] == 0xaf) && (magic[4] == 0x27) && (magic[5] == 0x1c)) { *compressed = COMPRESSED_7ZIP; } else if (rpmFileHasSuffix(file, ".lzma")) { *compressed = COMPRESSED_LZMA; } return rc; }