File threadsafeString.patch of Package rpm

Index: rpm-4.14.1/build/spec.c
===================================================================
--- rpm-4.14.1.orig/build/spec.c
+++ rpm-4.14.1/build/spec.c
@@ -28,7 +28,7 @@ static inline
 struct TriggerFileEntry * freeTriggerFiles(struct TriggerFileEntry * p)
 {
     struct TriggerFileEntry *o, *q = p;
-    
+
     while (q != NULL) {
 	o = q;
 	q = q->next;
@@ -107,7 +107,11 @@ rpmRC lookupPackage(rpmSpec spec, const
 
 Package newPackage(const char *name, rpmstrPool pool, Package *pkglist)
 {
-    Package p = xcalloc(1, sizeof(*p));
+    Package p;
+
+    #pragma omp critical(spec)
+    {
+    p = xcalloc(1, sizeof(*p));
     p->header = headerNew();
     p->autoProv = 1;
     p->autoReq = 1;
@@ -118,6 +122,7 @@ Package newPackage(const char *name, rpm
     p->fileRenameMap = NULL;
     p->pool = rpmstrPoolLink(pool);
     p->dpaths = NULL;
+    }
 
     if (name)
 	p->name = rpmstrPoolId(p->pool, name, 1);
@@ -141,7 +146,9 @@ Package newPackage(const char *name, rpm
 static Package freePackage(Package pkg)
 {
     if (pkg == NULL) return NULL;
-    
+
+    #pragma omp critical(spec)
+  	{
     pkg->preInFile = _free(pkg->preInFile);
     pkg->postInFile = _free(pkg->postInFile);
     pkg->preUnFile = _free(pkg->preUnFile);
@@ -171,6 +178,7 @@ static Package freePackage(Package pkg)
     pkg->pool = rpmstrPoolFree(pkg->pool);
 
     free(pkg);
+    }
     return NULL;
 }
 
@@ -204,7 +212,7 @@ rpmds * packageDependencies(Package pkg,
 rpmSpec newSpec(void)
 {
     rpmSpec spec = xcalloc(1, sizeof(*spec));
-    
+
     spec->specFile = NULL;
 
     spec->fileStack = NULL;
@@ -235,7 +243,7 @@ rpmSpec newSpec(void)
     spec->sourceRpmName = NULL;
     spec->sourcePkgId = NULL;
     spec->sourcePackage = NULL;
-    
+
     spec->buildRoot = NULL;
     spec->buildSubdir = NULL;
 
@@ -249,7 +257,7 @@ rpmSpec newSpec(void)
 
     spec->macros = rpmGlobalMacroContext;
     spec->pool = rpmstrPoolCreate();
-    
+
 #ifdef WITH_LUA
     {
     /* make sure patches and sources tables always exist */
@@ -290,7 +298,7 @@ rpmSpec rpmSpecFree(rpmSpec spec)
 	free(rl);
     }
     spec->lbuf = _free(spec->lbuf);
-    
+
     spec->sourceRpmName = _free(spec->sourceRpmName);
     spec->sourcePkgId = _free(spec->sourcePkgId);
     spec->sourcePackage = freePackage(spec->sourcePackage);
@@ -310,13 +318,13 @@ rpmSpec rpmSpecFree(rpmSpec spec)
 #ifdef WITH_LUA
     rpmlua lua = NULL; /* global state */
     rpmluaDelVar(lua, "patches");
-    rpmluaDelVar(lua, "sources");	
+    rpmluaDelVar(lua, "sources");
 #endif
 
     spec->sources = freeSources(spec->sources);
     spec->packages = freePackages(spec->packages);
     spec->pool = rpmstrPoolFree(spec->pool);
-    
+
     spec = _free(spec);
 
     return spec;
Index: rpm-4.14.1/lib/header.c
===================================================================
--- rpm-4.14.1.orig/lib/header.c
+++ rpm-4.14.1/lib/header.c
@@ -49,7 +49,7 @@ static const int typeAlign[16] =  {
 /** \ingroup header
  * Size of header data types.
  */
-static const int typeSizes[16] =  { 
+static const int typeSizes[16] =  {
     0,	/*!< RPM_NULL_TYPE */
     1,	/*!< RPM_CHAR_TYPE */
     1,	/*!< RPM_INT8_TYPE */
@@ -196,6 +196,8 @@ Header headerFree(Header h)
     if (h == NULL || h->nrefs > 0)
 	return NULL;
 
+	#pragma omp critical(header)
+	{
     if (h->index) {
 	indexEntry entry = h->index;
 	int i;
@@ -213,6 +215,7 @@ Header headerFree(Header h)
 	}
 	h->index = _free(h->index);
     }
+	  }
 
     h = _free(h);
     return NULL;
@@ -296,13 +299,16 @@ static int indexCmp(const void * avp, co
 
 static void headerSort(Header h)
 {
+	  #pragma omp critical(header)
+	  {
     if (h->sorted != HEADERSORT_INDEX) {
 	qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
 	h->sorted = HEADERSORT_INDEX;
     }
+	  }
 }
 
-static int offsetCmp(const void * avp, const void * bvp) 
+static int offsetCmp(const void * avp, const void * bvp)
 {
     indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
     int rc = (ap->info.offset - bp->info.offset);
@@ -560,7 +566,7 @@ void * headerExport(Header h, unsigned i
     unsigned len, diff;
     int32_t il = 0;
     int32_t dl = 0;
-    indexEntry entry; 
+    indexEntry entry;
     int i;
     int drlen, ndribbles;
 
@@ -592,7 +598,7 @@ void * headerExport(Header h, unsigned i
 		diff = alignDiff(entry->info.type, dl);
 		if (diff) {
 		    drlen += diff;
-		    dl += diff;    
+		    dl += diff;
 		}
 
 		ndribbles++;
@@ -679,7 +685,7 @@ void * headerExport(Header h, unsigned i
 		memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
 		memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
 		te += rdlen;
-		{  
+		{
 		    entryInfo se = (entryInfo)src;
 		    int off = ntohl(se->offset);
 		    pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
@@ -754,7 +760,7 @@ void * headerExport(Header h, unsigned i
 	}
 	pe++;
     }
-   
+
     /* Insure that there are no memcpy underruns/overruns. */
     if (((char *)pe) != dataStart)
 	goto errxit;
@@ -792,26 +798,28 @@ indexEntry findEntry(Header h, rpmTagVal
     struct indexEntry_s key;
 
     if (h == NULL) return NULL;
+
+		#pragma omp critical(header)
+		{
     if (h->sorted != HEADERSORT_INDEX)
 	headerSort(h);
 
     key.info.tag = tag;
 
     entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
-    if (entry == NULL)
-	return NULL;
 
-    if (type == RPM_NULL_TYPE)
-	return entry;
+    if (type != RPM_NULL_TYPE && entry != NULL)
+		{
+	    /* look backwards */
+	    while (entry->info.tag == tag && entry->info.type != type &&
+		   entry > h->index) entry--;
 
-    /* look backwards */
-    while (entry->info.tag == tag && entry->info.type != type &&
-	   entry > h->index) entry--;
-
-    if (entry->info.tag == tag && entry->info.type == type)
-	return entry;
+	    if (entry->info.tag != tag || entry->info.type != type)
+		     entry = NULL;
+		}
+	  }
 
-    return NULL;
+    return entry;
 }
 
 int headerDel(Header h, rpmTagVal tag)
@@ -824,7 +832,7 @@ int headerDel(Header h, rpmTagVal tag)
     if (!entry) return 1;
 
     /* Make sure entry points to the first occurrence of this tag. */
-    while (entry > h->index && (entry - 1)->info.tag == tag)  
+    while (entry > h->index && (entry - 1)->info.tag == tag)
 	entry--;
 
     /* Free data for tags being removed. */
@@ -854,7 +862,7 @@ int headerDel(Header h, rpmTagVal tag)
 rpmRC hdrblobImport(hdrblob blob, int fast, Header *hdrp, char **emsg)
 {
     Header h = NULL;
-    indexEntry entry; 
+    indexEntry entry;
     int rdlen;
 
     h = headerCreate(blob->ei, blob->il);
@@ -1026,7 +1034,7 @@ int headerIsEntry(Header h, rpmTagVal ta
 {
    		/* FIX: h modified by sort. */
     return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
-   	
+
 }
 
 /* simple heuristic to find out if the header is from * a source rpm
@@ -1042,10 +1050,10 @@ int headerIsSourceHeuristic(Header h)
 /** \ingroup header
  * Retrieve data from header entry.
  * Relevant flags (others are ignored), if neither is set allocation
- * behavior depends on data type(!) 
+ * behavior depends on data type(!)
  *     HEADERGET_MINMEM: return pointers to header memory
  *     HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
- * 
+ *
  * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
  * @param entry		header entry
  * @param td		tag data container
@@ -1176,7 +1184,7 @@ static int copyTdEntry(const indexEntry
 
 /**
  * Does locale match entry in header i18n table?
- * 
+ *
  * \verbatim
  * The range [l,le) contains the next locale to match:
  *    ll[_CC][.EEEEE][@dddd]
@@ -1229,7 +1237,7 @@ static int headerMatchLocale(const char
  * @param flags		flags to control allocation
  * @return		1 always
  */
-static int copyI18NEntry(Header h, indexEntry entry, rpmtd td, 
+static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
 						headerGetFlags flags)
 {
     const char *lang, *l, *le;
@@ -1246,7 +1254,7 @@ static int copyI18NEntry(Header h, index
 	(lang = getenv("LC_MESSAGES")) == NULL &&
 	(lang = getenv("LANG")) == NULL)
 	    goto exit;
-    
+
     if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
 	goto exit;
 
@@ -1271,7 +1279,7 @@ static int copyI18NEntry(Header h, index
 	    if (match == 1) {
 		td->data = ed;
 		goto exit;
-	    } else if (match == 2) { 
+	    } else if (match == 2) {
 		ed_weak = ed;
 	    }
 	}
@@ -1344,7 +1352,7 @@ int headerGet(Header h, rpmTagVal tag, r
 
 /**
  */
-static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr, 
+static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
 		rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
 {
     switch (type) {
@@ -1413,6 +1421,8 @@ static int intAddEntry(Header h, rpmtd t
     if (data == NULL)
 	return 0;
 
+	  #pragma omp critical(header)
+	  {
     /* Allocate more index space if necessary */
     if (h->indexUsed == h->indexAlloced) {
 	h->indexAlloced += INDEX_MALLOC_SIZE;
@@ -1431,7 +1441,7 @@ static int intAddEntry(Header h, rpmtd t
     if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
 	h->sorted = HEADERSORT_NONE;
     h->indexUsed++;
-
+	}
     return 1;
 }
 
@@ -1462,7 +1472,7 @@ static int intAppendEntry(Header h, rpmt
     } else
 	entry->data = xrealloc(entry->data, entry->length + length);
 
-    copyData(td->type, ((char *) entry->data) + entry->length, 
+    copyData(td->type, ((char *) entry->data) + entry->length,
 	     td->data, td->count, length);
 
     entry->length += length;
@@ -1475,7 +1485,7 @@ static int intAppendEntry(Header h, rpmt
 int headerPut(Header h, rpmtd td, headerPutFlags flags)
 {
     int rc;
-    
+
     assert(td != NULL);
     if (flags & HEADERPUT_APPEND) {
 	rc = findEntry(h, td->tag, td->type) ?
@@ -1513,7 +1523,7 @@ int headerAddI18NString(Header h, rpmTag
 	    charArray[count++] = "C";
 	    charArray[count++] = lang;
 	}
-	
+
 	rpmtdReset(&td);
 	td.tag = RPMTAG_HEADERI18NTABLE;
 	td.type = RPM_STRING_ARRAY_TYPE;
@@ -1567,7 +1577,7 @@ int headerAddI18NString(Header h, rpmTag
 	return rc;
     } else if (langNum >= entry->info.count) {
 	ghosts = langNum - entry->info.count;
-	
+
 	length = strlen(string) + 1 + ghosts;
 	if (ENTRY_IN_REGION(entry)) {
 	    char * t = xmalloc(entry->length + length);
@@ -1614,7 +1624,7 @@ int headerAddI18NString(Header h, rpmTag
 	/* Replace i18N string array */
 	entry->length -= strlen(be) + 1;
 	entry->length += sn;
-	
+
 	if (ENTRY_IN_REGION(entry)) {
 	    entry->info.offset = 0;
 	} else
@@ -1642,7 +1652,7 @@ int headerMod(Header h, rpmtd td)
 	return 0;
 
     /* make sure entry points to the first occurrence of this tag */
-    while (entry > h->index && (entry - 1)->info.tag == td->tag)  
+    while (entry > h->index && (entry - 1)->info.tag == td->tag)
 	entry--;
 
     /* free after we've grabbed the new data in case the two are intertwined;
@@ -1736,7 +1746,7 @@ unsigned int headerGetInstance(Header h)
 void headerSetInstance(Header h, unsigned int instance)
 {
     h->instance = instance;
-}    
+}
 
 #define RETRY_ERROR(_err) \
     ((_err) == EINTR || (_err) == EAGAIN || (_err) == EWOULDBLOCK)