static void trimChangelog(Header h) { static int oneshot; static int cuttime, minnum, maxnum; int * times; char ** names = 0, ** texts = 0; int i, keep, count = 0; if (!oneshot) { char *binarychangelogtrim = rpmExpand("%{?_binarychangelogtrim}", NULL); oneshot = 1; if (binarychangelogtrim && *binarychangelogtrim) { maxnum = atoi(binarychangelogtrim); binarychangelogtrim = strchr(binarychangelogtrim, ','); if (binarychangelogtrim) binarychangelogtrim++; } if (binarychangelogtrim && *binarychangelogtrim) { cuttime = atoi(binarychangelogtrim); binarychangelogtrim = strchr(binarychangelogtrim, ','); if (binarychangelogtrim) binarychangelogtrim++; } if (binarychangelogtrim && *binarychangelogtrim) { minnum = atoi(binarychangelogtrim); binarychangelogtrim = strchr(binarychangelogtrim, ','); } } if (!cuttime && !minnum && !maxnum) { return; } if (!headerGetEntry(h, RPMTAG_CHANGELOGTIME, NULL, (void **) ×, &count)) return; if ((!cuttime || count <= minnum) && (!maxnum || count <= maxnum)) { return; } keep = count; if (maxnum && keep > maxnum) keep = maxnum; if (cuttime) { for (i = 0; i < keep; i++) { if (i >= minnum && times[i] < cuttime) break; } keep = i; } if (keep >= count) return; headerGetEntry(h, RPMTAG_CHANGELOGNAME, NULL, (void **) &names, &count); headerGetEntry(h, RPMTAG_CHANGELOGTEXT, NULL, (void **) &texts, &count); headerModifyEntry(h, RPMTAG_CHANGELOGTIME, RPM_INT32_TYPE, times, keep); headerModifyEntry(h, RPMTAG_CHANGELOGNAME, RPM_STRING_ARRAY_TYPE, names, keep); headerModifyEntry(h, RPMTAG_CHANGELOGTEXT, RPM_STRING_ARRAY_TYPE, texts, keep); free(names); free(texts); }
int Save_HR_SW_info(int ix) { SWI_t *swi = &_myswi; /* XXX static for now */ #ifdef HAVE_LIBRPM /* * XXX Watchout: ix starts with 1 */ if (1 <= ix && ix <= swi->swi_nrec && ix != swi->swi_prevx) { int offset; Header h; char *n, *v, *r; offset = swi->swi_recs[ix - 1]; { rpmdbMatchIterator mi; mi = rpmtsInitIterator(swi->swi_rpmts, RPMDBI_PACKAGES, &offset, sizeof(offset)); if ((h = rpmdbNextIterator(mi)) != NULL) h = headerLink(h); rpmdbFreeIterator(mi); } if (h == NULL) { DEBUGMSGTL(("host/hr_swinst", "RPM cache has probably expired when reading entry %d, " "reloading...\n", ix)); swi->swi_timestamp = 0; return -1; } if (swi->swi_h != NULL) headerFree(swi->swi_h); swi->swi_h = h; swi->swi_prevx = ix; headerGetEntry(swi->swi_h, RPMTAG_NAME, NULL, (void **) &n, NULL); headerGetEntry(swi->swi_h, RPMTAG_VERSION, NULL, (void **) &v, NULL); headerGetEntry(swi->swi_h, RPMTAG_RELEASE, NULL, (void **) &r, NULL); snprintf(swi->swi_name, sizeof(swi->swi_name), "%s-%s-%s", n, v, r); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; } #else snprintf(swi->swi_name, sizeof(swi->swi_name), "%s", swi->swi_dep->d_name); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; #endif return 0; }
void Save_HR_SW_info(int ix) { SWI_t *swi = &_myswi; /* XXX static for now */ #ifdef HAVE_LIBRPM /* * XXX Watchout: ix starts with 1 */ if (1 <= ix && ix <= swi->swi_nrec && ix != swi->swi_prevx) { int offset; Header h; char *n, *v, *r; offset = swi->swi_recs[ix - 1]; #if defined(RPMDBI_PACKAGES) { rpmdbMatchIterator mi; mi = rpmdbInitIterator(swi->swi_rpmdb, RPMDBI_PACKAGES, &offset, sizeof(offset)); if ((h = rpmdbNextIterator(mi)) != NULL) h = headerLink(h); rpmdbFreeIterator(mi); } #else h = rpmdbGetRecord(swi->swi_rpmdb, offset); #endif if (h == NULL) return; if (swi->swi_h != NULL) headerFree(swi->swi_h); swi->swi_h = h; swi->swi_prevx = ix; headerGetEntry(swi->swi_h, RPMTAG_NAME, NULL, (void **) &n, NULL); headerGetEntry(swi->swi_h, RPMTAG_VERSION, NULL, (void **) &v, NULL); headerGetEntry(swi->swi_h, RPMTAG_RELEASE, NULL, (void **) &r, NULL); snprintf(swi->swi_name, sizeof(swi->swi_name), "%s-%s-%s", n, v, r); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; } #else snprintf(swi->swi_name, sizeof(swi->swi_name), swi->swi_dep->d_name); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; #endif }
int main( int argc, char **argv ) { rpmmi mi; Header h; int_32 type, count; char *name; rpmdb db; rpmReadConfigFiles( NULL, NULL ); if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) { fprintf( stderr, "cannot open database!\n" ); exit(EXIT_FAILURE); } mi = rpmmiInit(db, RPMDBI_PACKAGES, NULL, 0); while ((h = rpmmiNext(mi)) != NULL) { headerGetEntry( h, RPMTAG_NAME, &type, (void**)&name, &count ); if( strcmp(name,argv[1]) == 0 ) headerDump( h, stdout, HEADER_DUMP_INLINE, rpmTagTable ); /* * Note that the header reference is "owned" by the iterator, * so no headerFree() is necessary. */ } mi = rpmmiFree(mi); rpmdbClose( db ); return 0; }
/* Get the size of the file */ static size_t RPMSize(install_info *info, const char *path) { FD_t fdi; Header hd; size_t size = 0; int_32 type, c; void *p; int rc; fdi = fdOpen(path, O_RDONLY, 0644); rc = rpmReadPackageHeader(fdi, &hd, NULL, NULL, NULL); if ( rc ) { log_warning(_("RPM error: %s"), rpmErrorString()); fdClose(fdi); return 0; } headerGetEntry(hd, RPMTAG_SIZE, &type, &p, &c); if(type==RPM_INT32_TYPE){ size = *(int_32*) p; } fdClose(fdi); return size; }
/* --------------------------------------------------------------------- */ int netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) { rpmts ts; rpmdbMatchIterator mi; Header h; char *n, *v, *r, *g; int32_t *t; time_t install_time; size_t date_len; int i = 1; netsnmp_swinst_entry *entry; ts = rpmtsCreate(); rpmtsSetVSFlags( ts, (_RPMVSF_NOSIGNATURES|_RPMVSF_NODIGESTS)); mi = rpmtsInitIterator( ts, RPMDBI_PACKAGES, NULL, 0); if (mi == NULL) NETSNMP_LOGONCE((LOG_ERR, "rpmdbOpen() failed\n")); while (NULL != (h = rpmdbNextIterator( mi ))) { const u_char *dt; entry = netsnmp_swinst_entry_create( i++ ); if (NULL == entry) continue; /* error already logged by function */ CONTAINER_INSERT(container, entry); h = headerLink( h ); headerGetEntry( h, RPMTAG_NAME, NULL, (void**)&n, NULL); headerGetEntry( h, RPMTAG_VERSION, NULL, (void**)&v, NULL); headerGetEntry( h, RPMTAG_RELEASE, NULL, (void**)&r, NULL); headerGetEntry( h, RPMTAG_GROUP, NULL, (void**)&g, NULL); headerGetEntry( h, RPMTAG_INSTALLTIME, NULL, (void**)&t, NULL); entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s-%s-%s", n, v, r); if (entry->swName_len > sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName); entry->swType = (NULL != strstr( g, "System Environment")) ? 2 /* operatingSystem */ : 4; /* application */ install_time = *t; dt = date_n_time( &install_time, &date_len ); if (date_len != 8 && date_len != 11) { snmp_log(LOG_ERR, "Bogus length from date_n_time for %s", entry->swName); entry->swDate_len = 0; } else { entry->swDate_len = date_len; memcpy(entry->swDate, dt, entry->swDate_len); } headerFree( h ); } rpmdbFreeIterator( mi ); rpmtsFree( ts ); DEBUGMSGTL(("swinst:load:arch", "loaded %d entries\n", (int)CONTAINER_SIZE(container))); return 0; }
u_char * var_hrswinst(struct variable * vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method) { SWI_t *swi = &_myswi; /* XXX static for now */ int sw_idx = 0; static char string[SNMP_MAXPATH]; u_char *ret = NULL; struct stat stat_buf; if (vp->magic < HRSWINST_INDEX) { if (header_hrswinst(vp, name, length, exact, var_len, write_method) == MATCH_FAILED) return NULL; } else { sw_idx = header_hrswInstEntry(vp, name, length, exact, var_len, write_method); if (sw_idx == MATCH_FAILED) return NULL; } switch (vp->magic) { case HRSWINST_CHANGE: case HRSWINST_UPDATE: string[0] = '\0'; if (swi->swi_directory != NULL) { strncpy(string, swi->swi_directory, sizeof(string)); string[ sizeof(string)-1 ] = 0; } if (*string && (stat(string, &stat_buf) != -1)) { if (stat_buf.st_mtime > starttime.tv_sec) /* * changed 'recently' - i.e. since this agent started */ long_return = (stat_buf.st_mtime - starttime.tv_sec) * 100; else long_return = 0; /* predates this agent */ } else #if NETSNMP_NO_DUMMY_VALUES return NULL; #else long_return = 363136200; #endif ret = (u_char *) & long_return; break; case HRSWINST_INDEX: long_return = sw_idx; ret = (u_char *) & long_return; break; case HRSWINST_NAME: { strncpy(string, swi->swi_name, sizeof(string) - 1); /* * This will be unchanged from the initial "null" * value, if swi->swi_name is not defined */ string[sizeof(string) - 1] = '\0'; *var_len = strlen(string); ret = (u_char *) string; } break; case HRSWINST_ID: *var_len = nullOidLen; ret = (u_char *) nullOid; break; case HRSWINST_TYPE: { #ifdef HAVE_PKGINFO /* * at least on solaris2 this works */ char *catg = pkgparam(swi->swi_name, "CATEGORY"); if (catg == NULL) { long_return = 1; /* unknown */ } else { if (strstr(catg, "system") != NULL) { long_return = 2; /* operatingSystem */ } else if (strstr(catg, "application") != NULL) { long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ } free(catg); } #else # ifdef HAVE_LIBRPM char *rpm_groups; if ( headerGetEntry(swi->swi_h, RPMTAG_GROUP, NULL, (void **) &rpm_groups, NULL) ) { if ( strstr(rpm_groups, "System Environment") != NULL ) long_return = 2; /* operatingSystem */ else long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ } # else long_return = 1; /* unknown */ # endif #endif ret = (u_char *) & long_return; } break; case HRSWINST_DATE: { #ifdef HAVE_LIBRPM int_32 *rpm_data; if ( headerGetEntry(swi->swi_h, RPMTAG_INSTALLTIME, NULL, (void **) &rpm_data, NULL) ) { time_t installTime = *rpm_data; ret = date_n_time(&installTime, var_len); } else { ret = date_n_time(0, var_len); } #else if (swi->swi_directory != NULL) { snprintf(string, sizeof(string), "%s/%s", swi->swi_directory, swi->swi_name); string[ sizeof(string)-1 ] = 0; stat(string, &stat_buf); ret = date_n_time(&stat_buf.st_mtime, var_len); } else { #if NETSNMP_NO_DUMMY_VALUES return NULL; #endif sprintf(string, "back in the mists of time"); *var_len = strlen(string); ret = (u_char *) string; } #endif } break; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswinst\n", vp->magic)); ret = NULL; break; } Release_HRSW_token(); return ret; }
/* Extract the file */ static size_t RPMCopy(install_info *info, const char *path, const char *dest, const char *current_option_name, xmlNodePtr node, int (*update)(install_info *info, const char *path, size_t progress, size_t size, const char *current)) { FD_t fdi; Header hd; size_t size; int_32 type, c; int rc, isSource; void *p; const char *reloc = xmlGetProp(node, "relocate"); const char *autorm = xmlGetProp(node, "autoremove"); const char *depsoff = xmlGetProp(node, "nodeps"); int relocate = (reloc && !strcasecmp(reloc, "true")); int autoremove = (autorm && !strcasecmp(autorm, "true")); int nodeps = (depsoff && !strcasecmp(depsoff, "true")); fdi = fdOpen(path, O_RDONLY, 0644); rc = rpmReadPackageHeader(fdi, &hd, &isSource, NULL, NULL); if ( rc ) { log_warning(_("RPM error: %s"), rpmErrorString()); return 0; } size = 0; if ( rpm_access && ! force_manual ) { /* We can call RPM directly */ char cmd[300]; FILE *fp; float percent = 0.0; double bytes_copied = 0.0; double previous_bytes = 0.0; char *name = "", *version = "", *release = ""; char *options = (char *) malloc(PATH_MAX); options[0] = '\0'; headerGetEntry(hd, RPMTAG_SIZE, &type, &p, &c); if(type==RPM_INT32_TYPE){ size = *(int_32*) p; } headerGetEntry(hd, RPMTAG_RELEASE, &type, &p, &c); if(type==RPM_STRING_TYPE){ release = (char *) p; } headerGetEntry(hd, RPMTAG_NAME, &type, &p, &c); if(type==RPM_STRING_TYPE){ name = (char*)p; } headerGetEntry(hd, RPMTAG_VERSION, &type, &p, &c); if(type==RPM_STRING_TYPE){ version = (char*)p; } fdClose(fdi); if (relocate) { /* force relocating RPM to install directory */ sprintf(options, " --relocate /=%s --badreloc ", dest); } if (nodeps) { strcat(options, " --nodeps "); } snprintf(cmd,sizeof(cmd),"rpm -U --percent --root %s %s %s", rpm_root, options, path); fp = popen(cmd, "r"); while ( percent < 100.0 ) { if(!fp || feof(fp)){ pclose(fp); free(options); log_warning(_("Unable to install RPM file: '%s'"), path); return 0; } fscanf(fp,"%s", cmd); if(strcmp(cmd,"%%")){ pclose(fp); free(options); log_warning(_("Unable to install RPM file: '%s'"), path); return 0; } fscanf(fp,"%f", &percent); /* calculate the bytes installed in this pass of the loop */ bytes_copied = (percent/100.0)*size - previous_bytes; previous_bytes += bytes_copied; info->installed_bytes += bytes_copied; if ( ! update(info, path, (percent/100.0)*size, size, current_option_name) ) break; } pclose(fp); free (options); /* Log the RPM installation */ add_rpm_entry(info, current_option, name, version, atoi(release), autoremove); } else { /* Manually install the RPM file */ gzFile gzdi = NULL; BZFILE *bzdi = NULL; FILE *fd = NULL; unsigned char magic[2]; stream *cpio; if(headerIsEntry(hd, RPMTAG_PREIN)){ headerGetEntry(hd, RPMTAG_PREIN, &type, &p, &c); if(type==RPM_STRING_TYPE) run_script(info, (char*)p, 1, 1); } /* Identify the type of compression for the archive */ if ( fdRead(fdi, magic, 2) < 2 ) { return 0; } lseek(fdFileno(fdi), -2L, SEEK_CUR); if ( magic[0]==037 && magic[1]==0213 ) { gzdi = gzdopen(fdFileno(fdi), "r"); /* XXX gzdi == fdi */ } else if ( magic[0]=='B' && magic[1]=='Z' ) { bzdi = BZDOPEN(fdFileno(fdi), "r"); } else { /* Assume not compressed */ fd = fdopen(fdFileno(fdi), "r"); } cpio = file_fdopen(info, path, fd, gzdi, bzdi, "r"); /* if relocate="true", copy the files into dest instead of rpm_root */ if (relocate) { size = copy_cpio_stream(info, cpio, dest, current_option_name, node, update); } else { size = copy_cpio_stream(info, cpio, rpm_root, current_option_name, node, update); } if(headerIsEntry(hd, RPMTAG_POSTIN)){ headerGetEntry(hd, RPMTAG_POSTIN, &type, &p, &c); if(type==RPM_STRING_TYPE) run_script(info, (char*)p, 1, 1); } /* Append the uninstall scripts to the uninstall */ if(headerIsEntry(hd, RPMTAG_PREUN)){ headerGetEntry(hd, RPMTAG_PREUN, &type, &p, &c); if(type==RPM_STRING_TYPE) add_script_entry(info, current_option, (char*)p, 0); } if(headerIsEntry(hd, RPMTAG_POSTUN)){ headerGetEntry(hd, RPMTAG_POSTUN, &type, &p, &c); if(type==RPM_STRING_TYPE) add_script_entry(info, current_option, (char*)p, 1); } fdClose(fdi); } return size; }
int main ( int argc, char * * argv ) { int rc = -1; rpmts pts = NULL; /* Transaction Set */ FD_t pfd = NULL; Header pheader = NULL; char * pszPath = "/home/knaka/rpm/RPMS/i686/file-4.14-1.i686.rpm"; char * psz = NULL; char * * ppsz = NULL; int iType; void * pv; int_32 nCount; int_32 iTag; int_32 aiTag[] = { RPMTAG_NAME, RPMTAG_REQUIRENAME }; int i; int nNumTag = (sizeof (aiTag)) / sizeof (int_32); if (! (pfd = Fopen(pszPath, "r.ufdio"))) { fprintf(stderr, "Failed to open file.\n"); goto except; } if (! (pts = rpmtsCreate())) { fprintf(stderr, "Failed to create transaction set.\n"); goto except; } rpmtsSetVSFlags(pts, RPMVSF_NOMD5 | RPMVSF_NODSA | RPMVSF_NORSA); if (rpmReadPackageFile(pts, pfd, pszPath, & pheader) != RPMRC_OK) { fprintf(stderr, "Failed to read file.\n"); goto except; } for (i = 0; i < nNumTag; ++ i) { iTag = aiTag[i]; /* return: 1 on success, 0 on failure */ if (headerIsEntry(pheader, iTag) == 0) { fprintf(stderr, "No such entry.\n"); goto except; } /* headerGetEntryMinMemory() allocates only for the pointer vector of RPM_STRING_ARRAY_TYPE */ /* return: 1 on success, 0 on failure */ /* if (headerGetEntryMinMemory(pheader, iTag, & iType, */ if (headerGetEntry(pheader, iTag, & iType, (void *) & pv, & nCount ) == 0) { fprintf(stderr, "Failed to get size.\n"); goto except; } switch (iType) { case RPM_STRING_ARRAY_TYPE: { int i; const char * * ppszSrc = (const char * *) pv; ppsz = (char * *) malloc((sizeof (char *)) * (nCount + 1)); fprintf(stderr, "d2-0: %d\n", nCount); for (i = 0; i < nCount; ++ i) { ppsz[i] = strdup(ppszSrc[i]); fprintf(stderr, "d2 (%d, %p): %s\n", i, ppsz[i], ppsz[i]); /* free(ppszSrc[i]); */ } ppsz[i] = NULL; fprintf(stderr, "d3 (%p)\n", pv); free(pv); break; } case RPM_STRING_TYPE: case RPM_I18NSTRING_TYPE: { psz = strdup(pv); fprintf(stderr, "d1: %s\n", psz); free(pv); break; } case RPM_BIN_TYPE: { /* free(pv); */ break; } default: { fprintf(stderr, "Unknown type.\n"); goto except; } } if (ppsz) { int i; for (i = 0; ppsz[i]; ++ i) { free(ppsz[i]); } free(ppsz); ppsz = NULL; } if (psz) { free(psz); psz = NULL; } } rc = 0; except: if (ppsz) { int i; for (i = 0; ppsz[i]; ++ i) { free(ppsz[i]); } free(ppsz); } if (psz) { free(psz); } if (pheader) { headerFree(pheader); } if (pts) { rpmtsFree(pts); } if (pfd) { Fclose(pfd); } return (rc); }