bsdiff_libglnx_submodule.patch 255 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577
  1. diff -Nuar ostree-2018.8.orig/bsdiff/autogen.sh ostree-2018.8/bsdiff/autogen.sh
  2. --- ostree-2018.8.orig/bsdiff/autogen.sh 1970-01-01 02:00:00.000000000 +0200
  3. +++ ostree-2018.8/bsdiff/autogen.sh 2018-05-26 00:37:31.985018846 +0300
  4. @@ -0,0 +1,6 @@
  5. +#!/bin/sh
  6. +
  7. +touch AUTHORS NEWS README ChangeLog
  8. +cp LICENSE COPYING
  9. +
  10. +autoreconf -fis
  11. diff -Nuar ostree-2018.8.orig/bsdiff/bsdiff.c ostree-2018.8/bsdiff/bsdiff.c
  12. --- ostree-2018.8.orig/bsdiff/bsdiff.c 1970-01-01 02:00:00.000000000 +0200
  13. +++ ostree-2018.8/bsdiff/bsdiff.c 2018-05-26 00:37:31.985018846 +0300
  14. @@ -0,0 +1,445 @@
  15. +/*-
  16. + * Copyright 2003-2005 Colin Percival
  17. + * Copyright 2012 Matthew Endsley
  18. + * All rights reserved
  19. + *
  20. + * Redistribution and use in source and binary forms, with or without
  21. + * modification, are permitted providing that the following conditions
  22. + * are met:
  23. + * 1. Redistributions of source code must retain the above copyright
  24. + * notice, this list of conditions and the following disclaimer.
  25. + * 2. Redistributions in binary form must reproduce the above copyright
  26. + * notice, this list of conditions and the following disclaimer in the
  27. + * documentation and/or other materials provided with the distribution.
  28. + *
  29. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  30. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  31. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  33. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. + * POSSIBILITY OF SUCH DAMAGE.
  40. + */
  41. +
  42. +#include "bsdiff.h"
  43. +
  44. +#include <limits.h>
  45. +#include <string.h>
  46. +
  47. +#define MIN(x,y) (((x)<(y)) ? (x) : (y))
  48. +
  49. +static void split(int64_t *I,int64_t *V,int64_t start,int64_t len,int64_t h)
  50. +{
  51. + int64_t i,j,k,x,tmp,jj,kk;
  52. +
  53. + if(len<16) {
  54. + for(k=start;k<start+len;k+=j) {
  55. + j=1;x=V[I[k]+h];
  56. + for(i=1;k+i<start+len;i++) {
  57. + if(V[I[k+i]+h]<x) {
  58. + x=V[I[k+i]+h];
  59. + j=0;
  60. + };
  61. + if(V[I[k+i]+h]==x) {
  62. + tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
  63. + j++;
  64. + };
  65. + };
  66. + for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
  67. + if(j==1) I[k]=-1;
  68. + };
  69. + return;
  70. + };
  71. +
  72. + x=V[I[start+len/2]+h];
  73. + jj=0;kk=0;
  74. + for(i=start;i<start+len;i++) {
  75. + if(V[I[i]+h]<x) jj++;
  76. + if(V[I[i]+h]==x) kk++;
  77. + };
  78. + jj+=start;kk+=jj;
  79. +
  80. + i=start;j=0;k=0;
  81. + while(i<jj) {
  82. + if(V[I[i]+h]<x) {
  83. + i++;
  84. + } else if(V[I[i]+h]==x) {
  85. + tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
  86. + j++;
  87. + } else {
  88. + tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
  89. + k++;
  90. + };
  91. + };
  92. +
  93. + while(jj+j<kk) {
  94. + if(V[I[jj+j]+h]==x) {
  95. + j++;
  96. + } else {
  97. + tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
  98. + k++;
  99. + };
  100. + };
  101. +
  102. + if(jj>start) split(I,V,start,jj-start,h);
  103. +
  104. + for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
  105. + if(jj==kk-1) I[jj]=-1;
  106. +
  107. + if(start+len>kk) split(I,V,kk,start+len-kk,h);
  108. +}
  109. +
  110. +static void qsufsort(int64_t *I,int64_t *V,const uint8_t *old,int64_t oldsize)
  111. +{
  112. + int64_t buckets[256];
  113. + int64_t i,h,len;
  114. +
  115. + for(i=0;i<256;i++) buckets[i]=0;
  116. + for(i=0;i<oldsize;i++) buckets[old[i]]++;
  117. + for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
  118. + for(i=255;i>0;i--) buckets[i]=buckets[i-1];
  119. + buckets[0]=0;
  120. +
  121. + for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
  122. + I[0]=oldsize;
  123. + for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
  124. + V[oldsize]=0;
  125. + for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
  126. + I[0]=-1;
  127. +
  128. + for(h=1;I[0]!=-(oldsize+1);h+=h) {
  129. + len=0;
  130. + for(i=0;i<oldsize+1;) {
  131. + if(I[i]<0) {
  132. + len-=I[i];
  133. + i-=I[i];
  134. + } else {
  135. + if(len) I[i-len]=-len;
  136. + len=V[I[i]]+1-i;
  137. + split(I,V,i,len,h);
  138. + i+=len;
  139. + len=0;
  140. + };
  141. + };
  142. + if(len) I[i-len]=-len;
  143. + };
  144. +
  145. + for(i=0;i<oldsize+1;i++) I[V[i]]=i;
  146. +}
  147. +
  148. +static int64_t matchlen(const uint8_t *old,int64_t oldsize,const uint8_t *new,int64_t newsize)
  149. +{
  150. + int64_t i;
  151. +
  152. + for(i=0;(i<oldsize)&&(i<newsize);i++)
  153. + if(old[i]!=new[i]) break;
  154. +
  155. + return i;
  156. +}
  157. +
  158. +static int64_t search(const int64_t *I,const uint8_t *old,int64_t oldsize,
  159. + const uint8_t *new,int64_t newsize,int64_t st,int64_t en,int64_t *pos)
  160. +{
  161. + int64_t x,y;
  162. +
  163. + if(en-st<2) {
  164. + x=matchlen(old+I[st],oldsize-I[st],new,newsize);
  165. + y=matchlen(old+I[en],oldsize-I[en],new,newsize);
  166. +
  167. + if(x>y) {
  168. + *pos=I[st];
  169. + return x;
  170. + } else {
  171. + *pos=I[en];
  172. + return y;
  173. + }
  174. + };
  175. +
  176. + x=st+(en-st)/2;
  177. + if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
  178. + return search(I,old,oldsize,new,newsize,x,en,pos);
  179. + } else {
  180. + return search(I,old,oldsize,new,newsize,st,x,pos);
  181. + };
  182. +}
  183. +
  184. +static void offtout(int64_t x,uint8_t *buf)
  185. +{
  186. + int64_t y;
  187. +
  188. + if(x<0) y=-x; else y=x;
  189. +
  190. + buf[0]=y%256;y-=buf[0];
  191. + y=y/256;buf[1]=y%256;y-=buf[1];
  192. + y=y/256;buf[2]=y%256;y-=buf[2];
  193. + y=y/256;buf[3]=y%256;y-=buf[3];
  194. + y=y/256;buf[4]=y%256;y-=buf[4];
  195. + y=y/256;buf[5]=y%256;y-=buf[5];
  196. + y=y/256;buf[6]=y%256;y-=buf[6];
  197. + y=y/256;buf[7]=y%256;
  198. +
  199. + if(x<0) buf[7]|=0x80;
  200. +}
  201. +
  202. +static int64_t writedata(struct bsdiff_stream* stream, const void* buffer, int64_t length)
  203. +{
  204. + int64_t result = 0;
  205. +
  206. + while (length > 0)
  207. + {
  208. + const int smallsize = (int)MIN(length, INT_MAX);
  209. + const int writeresult = stream->write(stream, buffer, smallsize);
  210. + if (writeresult == -1)
  211. + {
  212. + return -1;
  213. + }
  214. +
  215. + result += writeresult;
  216. + length -= smallsize;
  217. + buffer = (uint8_t*)buffer + smallsize;
  218. + }
  219. +
  220. + return result;
  221. +}
  222. +
  223. +struct bsdiff_request
  224. +{
  225. + const uint8_t* old;
  226. + int64_t oldsize;
  227. + const uint8_t* new;
  228. + int64_t newsize;
  229. + struct bsdiff_stream* stream;
  230. + int64_t *I;
  231. + uint8_t *buffer;
  232. +};
  233. +
  234. +static int bsdiff_internal(const struct bsdiff_request req)
  235. +{
  236. + int64_t *I,*V;
  237. + int64_t scan,pos,len;
  238. + int64_t lastscan,lastpos,lastoffset;
  239. + int64_t oldscore,scsc;
  240. + int64_t s,Sf,lenf,Sb,lenb;
  241. + int64_t overlap,Ss,lens;
  242. + int64_t i;
  243. + uint8_t *buffer;
  244. + uint8_t buf[8 * 3];
  245. +
  246. + if((V=req.stream->malloc((req.oldsize+1)*sizeof(int64_t)))==NULL) return -1;
  247. + I = req.I;
  248. +
  249. + qsufsort(I,V,req.old,req.oldsize);
  250. + req.stream->free(V);
  251. +
  252. + buffer = req.buffer;
  253. +
  254. + /* Compute the differences, writing ctrl as we go */
  255. + scan=0;len=0;pos=0;
  256. + lastscan=0;lastpos=0;lastoffset=0;
  257. + while(scan<req.newsize) {
  258. + oldscore=0;
  259. +
  260. + for(scsc=scan+=len;scan<req.newsize;scan++) {
  261. + len=search(I,req.old,req.oldsize,req.new+scan,req.newsize-scan,
  262. + 0,req.oldsize,&pos);
  263. +
  264. + for(;scsc<scan+len;scsc++)
  265. + if((scsc+lastoffset<req.oldsize) &&
  266. + (req.old[scsc+lastoffset] == req.new[scsc]))
  267. + oldscore++;
  268. +
  269. + if(((len==oldscore) && (len!=0)) ||
  270. + (len>oldscore+8)) break;
  271. +
  272. + if((scan+lastoffset<req.oldsize) &&
  273. + (req.old[scan+lastoffset] == req.new[scan]))
  274. + oldscore--;
  275. + };
  276. +
  277. + if((len!=oldscore) || (scan==req.newsize)) {
  278. + s=0;Sf=0;lenf=0;
  279. + for(i=0;(lastscan+i<scan)&&(lastpos+i<req.oldsize);) {
  280. + if(req.old[lastpos+i]==req.new[lastscan+i]) s++;
  281. + i++;
  282. + if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
  283. + };
  284. +
  285. + lenb=0;
  286. + if(scan<req.newsize) {
  287. + s=0;Sb=0;
  288. + for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
  289. + if(req.old[pos-i]==req.new[scan-i]) s++;
  290. + if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
  291. + };
  292. + };
  293. +
  294. + if(lastscan+lenf>scan-lenb) {
  295. + overlap=(lastscan+lenf)-(scan-lenb);
  296. + s=0;Ss=0;lens=0;
  297. + for(i=0;i<overlap;i++) {
  298. + if(req.new[lastscan+lenf-overlap+i]==
  299. + req.old[lastpos+lenf-overlap+i]) s++;
  300. + if(req.new[scan-lenb+i]==
  301. + req.old[pos-lenb+i]) s--;
  302. + if(s>Ss) { Ss=s; lens=i+1; };
  303. + };
  304. +
  305. + lenf+=lens-overlap;
  306. + lenb-=lens;
  307. + };
  308. +
  309. + offtout(lenf,buf);
  310. + offtout((scan-lenb)-(lastscan+lenf),buf+8);
  311. + offtout((pos-lenb)-(lastpos+lenf),buf+16);
  312. +
  313. + /* Write control data */
  314. + if (writedata(req.stream, buf, sizeof(buf)))
  315. + return -1;
  316. +
  317. + /* Write diff data */
  318. + for(i=0;i<lenf;i++)
  319. + buffer[i]=req.new[lastscan+i]-req.old[lastpos+i];
  320. + if (writedata(req.stream, buffer, lenf))
  321. + return -1;
  322. +
  323. + /* Write extra data */
  324. + for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
  325. + buffer[i]=req.new[lastscan+lenf+i];
  326. + if (writedata(req.stream, buffer, (scan-lenb)-(lastscan+lenf)))
  327. + return -1;
  328. +
  329. + lastscan=scan-lenb;
  330. + lastpos=pos-lenb;
  331. + lastoffset=pos-scan;
  332. + };
  333. + };
  334. +
  335. + return 0;
  336. +}
  337. +
  338. +int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t newsize, struct bsdiff_stream* stream)
  339. +{
  340. + int result;
  341. + struct bsdiff_request req;
  342. +
  343. + if((req.I=stream->malloc((oldsize+1)*sizeof(int64_t)))==NULL)
  344. + return -1;
  345. +
  346. + if((req.buffer=stream->malloc(newsize+1))==NULL)
  347. + {
  348. + stream->free(req.I);
  349. + return -1;
  350. + }
  351. +
  352. + req.old = old;
  353. + req.oldsize = oldsize;
  354. + req.new = new;
  355. + req.newsize = newsize;
  356. + req.stream = stream;
  357. +
  358. + result = bsdiff_internal(req);
  359. +
  360. + stream->free(req.buffer);
  361. + stream->free(req.I);
  362. +
  363. + return result;
  364. +}
  365. +
  366. +#if defined(BSDIFF_EXECUTABLE)
  367. +
  368. +#include <sys/types.h>
  369. +
  370. +#include <bzlib.h>
  371. +#include <err.h>
  372. +#include <fcntl.h>
  373. +#include <stdio.h>
  374. +#include <stdlib.h>
  375. +#include <unistd.h>
  376. +
  377. +static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size)
  378. +{
  379. + int bz2err;
  380. + BZFILE* bz2;
  381. +
  382. + bz2 = (BZFILE*)stream->opaque;
  383. + BZ2_bzWrite(&bz2err, bz2, (void*)buffer, size);
  384. + if (bz2err != BZ_STREAM_END && bz2err != BZ_OK)
  385. + return -1;
  386. +
  387. + return 0;
  388. +}
  389. +
  390. +int main(int argc,char *argv[])
  391. +{
  392. + int fd;
  393. + int bz2err;
  394. + uint8_t *old,*new;
  395. + off_t oldsize,newsize;
  396. + uint8_t buf[8];
  397. + FILE * pf;
  398. + struct bsdiff_stream stream;
  399. + BZFILE* bz2;
  400. +
  401. + memset(&bz2, 0, sizeof(bz2));
  402. + stream.malloc = malloc;
  403. + stream.free = free;
  404. + stream.write = bz2_write;
  405. +
  406. + if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  407. +
  408. + /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
  409. + that we never try to malloc(0) and get a NULL pointer */
  410. + if(((fd=open(argv[1],O_RDONLY,0))<0) ||
  411. + ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  412. + ((old=malloc(oldsize+1))==NULL) ||
  413. + (lseek(fd,0,SEEK_SET)!=0) ||
  414. + (read(fd,old,oldsize)!=oldsize) ||
  415. + (close(fd)==-1)) err(1,"%s",argv[1]);
  416. +
  417. +
  418. + /* Allocate newsize+1 bytes instead of newsize bytes to ensure
  419. + that we never try to malloc(0) and get a NULL pointer */
  420. + if(((fd=open(argv[2],O_RDONLY,0))<0) ||
  421. + ((newsize=lseek(fd,0,SEEK_END))==-1) ||
  422. + ((new=malloc(newsize+1))==NULL) ||
  423. + (lseek(fd,0,SEEK_SET)!=0) ||
  424. + (read(fd,new,newsize)!=newsize) ||
  425. + (close(fd)==-1)) err(1,"%s",argv[2]);
  426. +
  427. + /* Create the patch file */
  428. + if ((pf = fopen(argv[3], "w")) == NULL)
  429. + err(1, "%s", argv[3]);
  430. +
  431. + /* Write header (signature+newsize)*/
  432. + offtout(newsize, buf);
  433. + if (fwrite("ENDSLEY/BSDIFF43", 16, 1, pf) != 1 ||
  434. + fwrite(buf, sizeof(buf), 1, pf) != 1)
  435. + err(1, "Failed to write header");
  436. +
  437. +
  438. + if (NULL == (bz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)))
  439. + errx(1, "BZ2_bzWriteOpen, bz2err=%d", bz2err);
  440. +
  441. + stream.opaque = bz2;
  442. + if (bsdiff(old, oldsize, new, newsize, &stream))
  443. + err(1, "bsdiff");
  444. +
  445. + BZ2_bzWriteClose(&bz2err, bz2, 0, NULL, NULL);
  446. + if (bz2err != BZ_OK)
  447. + err(1, "BZ2_bzWriteClose, bz2err=%d", bz2err);
  448. +
  449. + if (fclose(pf))
  450. + err(1, "fclose");
  451. +
  452. + /* Free the memory we used */
  453. + free(old);
  454. + free(new);
  455. +
  456. + return 0;
  457. +}
  458. +
  459. +#endif
  460. diff -Nuar ostree-2018.8.orig/bsdiff/bsdiff.h ostree-2018.8/bsdiff/bsdiff.h
  461. --- ostree-2018.8.orig/bsdiff/bsdiff.h 1970-01-01 02:00:00.000000000 +0200
  462. +++ ostree-2018.8/bsdiff/bsdiff.h 2018-05-26 00:37:31.985018846 +0300
  463. @@ -0,0 +1,45 @@
  464. +/*-
  465. + * Copyright 2003-2005 Colin Percival
  466. + * Copyright 2012 Matthew Endsley
  467. + * All rights reserved
  468. + *
  469. + * Redistribution and use in source and binary forms, with or without
  470. + * modification, are permitted providing that the following conditions
  471. + * are met:
  472. + * 1. Redistributions of source code must retain the above copyright
  473. + * notice, this list of conditions and the following disclaimer.
  474. + * 2. Redistributions in binary form must reproduce the above copyright
  475. + * notice, this list of conditions and the following disclaimer in the
  476. + * documentation and/or other materials provided with the distribution.
  477. + *
  478. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  479. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  480. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  481. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  482. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  483. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  484. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  485. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  486. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  487. + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  488. + * POSSIBILITY OF SUCH DAMAGE.
  489. + */
  490. +
  491. +#ifndef BSDIFF_H
  492. +# define BSDIFF_H
  493. +
  494. +# include <stddef.h>
  495. +# include <stdint.h>
  496. +
  497. +struct bsdiff_stream
  498. +{
  499. + void* opaque;
  500. +
  501. + void* (*malloc)(size_t size);
  502. + void (*free)(void* ptr);
  503. + int (*write)(struct bsdiff_stream* stream, const void* buffer, int size);
  504. +};
  505. +
  506. +int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t newsize, struct bsdiff_stream* stream);
  507. +
  508. +#endif
  509. diff -Nuar ostree-2018.8.orig/bsdiff/bspatch.c ostree-2018.8/bsdiff/bspatch.c
  510. --- ostree-2018.8.orig/bsdiff/bspatch.c 1970-01-01 02:00:00.000000000 +0200
  511. +++ ostree-2018.8/bsdiff/bspatch.c 2018-05-26 00:37:31.985018846 +0300
  512. @@ -0,0 +1,187 @@
  513. +/*-
  514. + * Copyright 2003-2005 Colin Percival
  515. + * Copyright 2012 Matthew Endsley
  516. + * All rights reserved
  517. + *
  518. + * Redistribution and use in source and binary forms, with or without
  519. + * modification, are permitted providing that the following conditions
  520. + * are met:
  521. + * 1. Redistributions of source code must retain the above copyright
  522. + * notice, this list of conditions and the following disclaimer.
  523. + * 2. Redistributions in binary form must reproduce the above copyright
  524. + * notice, this list of conditions and the following disclaimer in the
  525. + * documentation and/or other materials provided with the distribution.
  526. + *
  527. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  528. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  529. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  530. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  531. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  532. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  533. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  534. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  535. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  536. + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  537. + * POSSIBILITY OF SUCH DAMAGE.
  538. + */
  539. +
  540. +#include "bspatch.h"
  541. +
  542. +static int64_t offtin(uint8_t *buf)
  543. +{
  544. + int64_t y;
  545. +
  546. + y=buf[7]&0x7F;
  547. + y=y*256;y+=buf[6];
  548. + y=y*256;y+=buf[5];
  549. + y=y*256;y+=buf[4];
  550. + y=y*256;y+=buf[3];
  551. + y=y*256;y+=buf[2];
  552. + y=y*256;y+=buf[1];
  553. + y=y*256;y+=buf[0];
  554. +
  555. + if(buf[7]&0x80) y=-y;
  556. +
  557. + return y;
  558. +}
  559. +
  560. +int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, struct bspatch_stream* stream)
  561. +{
  562. + uint8_t buf[8];
  563. + int64_t oldpos,newpos;
  564. + int64_t ctrl[3];
  565. + int64_t i;
  566. +
  567. + oldpos=0;newpos=0;
  568. + while(newpos<newsize) {
  569. + /* Read control data */
  570. + for(i=0;i<=2;i++) {
  571. + if (stream->read(stream, buf, 8))
  572. + return -1;
  573. + ctrl[i]=offtin(buf);
  574. + };
  575. +
  576. + /* Sanity-check */
  577. + if(newpos+ctrl[0]>newsize)
  578. + return -1;
  579. +
  580. + /* Read diff string */
  581. + if (stream->read(stream, new + newpos, ctrl[0]))
  582. + return -1;
  583. +
  584. + /* Add old data to diff string */
  585. + for(i=0;i<ctrl[0];i++)
  586. + if((oldpos+i>=0) && (oldpos+i<oldsize))
  587. + new[newpos+i]+=old[oldpos+i];
  588. +
  589. + /* Adjust pointers */
  590. + newpos+=ctrl[0];
  591. + oldpos+=ctrl[0];
  592. +
  593. + /* Sanity-check */
  594. + if(newpos+ctrl[1]>newsize)
  595. + return -1;
  596. +
  597. + /* Read extra string */
  598. + if (stream->read(stream, new + newpos, ctrl[1]))
  599. + return -1;
  600. +
  601. + /* Adjust pointers */
  602. + newpos+=ctrl[1];
  603. + oldpos+=ctrl[2];
  604. + };
  605. +
  606. + return 0;
  607. +}
  608. +
  609. +#if defined(BSPATCH_EXECUTABLE)
  610. +
  611. +#include <bzlib.h>
  612. +#include <stdlib.h>
  613. +#include <stdint.h>
  614. +#include <stdio.h>
  615. +#include <string.h>
  616. +#include <err.h>
  617. +#include <unistd.h>
  618. +#include <fcntl.h>
  619. +
  620. +static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length)
  621. +{
  622. + int n;
  623. + int bz2err;
  624. + BZFILE* bz2;
  625. +
  626. + bz2 = (BZFILE*)stream->opaque;
  627. + n = BZ2_bzRead(&bz2err, bz2, buffer, length);
  628. + if (n != length)
  629. + return -1;
  630. +
  631. + return 0;
  632. +}
  633. +
  634. +int main(int argc,char * argv[])
  635. +{
  636. + FILE * f;
  637. + int fd;
  638. + int bz2err;
  639. + uint8_t header[24];
  640. + uint8_t *old, *new;
  641. + int64_t oldsize, newsize;
  642. + BZFILE* bz2;
  643. + struct bspatch_stream stream;
  644. +
  645. + if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  646. +
  647. + /* Open patch file */
  648. + if ((f = fopen(argv[3], "r")) == NULL)
  649. + err(1, "fopen(%s)", argv[3]);
  650. +
  651. + /* Read header */
  652. + if (fread(header, 1, 24, f) != 24) {
  653. + if (feof(f))
  654. + errx(1, "Corrupt patch\n");
  655. + err(1, "fread(%s)", argv[3]);
  656. + }
  657. +
  658. + /* Check for appropriate magic */
  659. + if (memcmp(header, "ENDSLEY/BSDIFF43", 16) != 0)
  660. + errx(1, "Corrupt patch\n");
  661. +
  662. + /* Read lengths from header */
  663. + newsize=offtin(header+16);
  664. + if(newsize<0)
  665. + errx(1,"Corrupt patch\n");
  666. +
  667. + /* Close patch file and re-open it via libbzip2 at the right places */
  668. + if(((fd=open(argv[1],O_RDONLY,0))<0) ||
  669. + ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  670. + ((old=malloc(oldsize+1))==NULL) ||
  671. + (lseek(fd,0,SEEK_SET)!=0) ||
  672. + (read(fd,old,oldsize)!=oldsize) ||
  673. + (close(fd)==-1)) err(1,"%s",argv[1]);
  674. + if((new=malloc(newsize+1))==NULL) err(1,NULL);
  675. +
  676. + if (NULL == (bz2 = BZ2_bzReadOpen(&bz2err, f, 0, 0, NULL, 0)))
  677. + errx(1, "BZ2_bzReadOpen, bz2err=%d", bz2err);
  678. +
  679. + stream.read = bz2_read;
  680. + stream.opaque = bz2;
  681. + if (bspatch(old, oldsize, new, newsize, &stream))
  682. + errx(1, "bspatch");
  683. +
  684. + /* Clean up the bzip2 reads */
  685. + BZ2_bzReadClose(&bz2err, bz2);
  686. + fclose(f);
  687. +
  688. + /* Write the new file */
  689. + if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
  690. + (write(fd,new,newsize)!=newsize) || (close(fd)==-1))
  691. + err(1,"%s",argv[2]);
  692. +
  693. + free(new);
  694. + free(old);
  695. +
  696. + return 0;
  697. +}
  698. +
  699. +#endif
  700. diff -Nuar ostree-2018.8.orig/bsdiff/bspatch.h ostree-2018.8/bsdiff/bspatch.h
  701. --- ostree-2018.8.orig/bsdiff/bspatch.h 1970-01-01 02:00:00.000000000 +0200
  702. +++ ostree-2018.8/bsdiff/bspatch.h 2018-05-26 00:37:31.985018846 +0300
  703. @@ -0,0 +1,42 @@
  704. +/*-
  705. + * Copyright 2003-2005 Colin Percival
  706. + * Copyright 2012 Matthew Endsley
  707. + * All rights reserved
  708. + *
  709. + * Redistribution and use in source and binary forms, with or without
  710. + * modification, are permitted providing that the following conditions
  711. + * are met:
  712. + * 1. Redistributions of source code must retain the above copyright
  713. + * notice, this list of conditions and the following disclaimer.
  714. + * 2. Redistributions in binary form must reproduce the above copyright
  715. + * notice, this list of conditions and the following disclaimer in the
  716. + * documentation and/or other materials provided with the distribution.
  717. + *
  718. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  719. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  720. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  721. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  722. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  723. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  724. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  725. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  726. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  727. + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  728. + * POSSIBILITY OF SUCH DAMAGE.
  729. + */
  730. +
  731. +#ifndef BSPATCH_H
  732. +# define BSPATCH_H
  733. +
  734. +# include <stdint.h>
  735. +
  736. +struct bspatch_stream
  737. +{
  738. + void* opaque;
  739. + int (*read)(const struct bspatch_stream* stream, void* buffer, int length);
  740. +};
  741. +
  742. +int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, struct bspatch_stream* stream);
  743. +
  744. +#endif
  745. +
  746. diff -Nuar ostree-2018.8.orig/bsdiff/configure.ac ostree-2018.8/bsdiff/configure.ac
  747. --- ostree-2018.8.orig/bsdiff/configure.ac 1970-01-01 02:00:00.000000000 +0200
  748. +++ ostree-2018.8/bsdiff/configure.ac 2018-05-26 00:37:31.985018846 +0300
  749. @@ -0,0 +1,30 @@
  750. +# -*- Autoconf -*-
  751. +# Process this file with autoconf to produce a configure script.
  752. +
  753. +AC_PREREQ([2.69])
  754. +AC_INIT([bsdiff], [0.1])
  755. +AC_CONFIG_SRCDIR([bsdiff.c])
  756. +AC_CONFIG_HEADERS([config.h])
  757. +AM_INIT_AUTOMAKE([1.9])
  758. +
  759. +# Checks for programs.
  760. +AC_PROG_CC
  761. +
  762. +# Checks for libraries.
  763. +# FIXME: Replace `main' with a function in `-lbz2':
  764. +AC_CHECK_LIB([bz2], [BZ2_bzReadOpen])
  765. +
  766. +AC_CHECK_HEADERS([fcntl.h limits.h stddef.h stdint.h stdlib.h string.h unistd.h])
  767. +
  768. +# Checks for typedefs, structures, and compiler characteristics.
  769. +AC_TYPE_INT64_T
  770. +AC_TYPE_OFF_T
  771. +AC_TYPE_SIZE_T
  772. +AC_TYPE_UINT8_T
  773. +
  774. +# Checks for library functions.
  775. +AC_FUNC_MALLOC
  776. +AC_CHECK_FUNCS([memset])
  777. +
  778. +AC_CONFIG_FILES([Makefile])
  779. +AC_OUTPUT
  780. diff -Nuar ostree-2018.8.orig/bsdiff/.git ostree-2018.8/bsdiff/.git
  781. --- ostree-2018.8.orig/bsdiff/.git 1970-01-01 02:00:00.000000000 +0200
  782. +++ ostree-2018.8/bsdiff/.git 2018-05-26 00:37:14.345018427 +0300
  783. @@ -0,0 +1 @@
  784. +gitdir: ../.git/modules/bsdiff
  785. diff -Nuar ostree-2018.8.orig/bsdiff/.gitignore ostree-2018.8/bsdiff/.gitignore
  786. --- ostree-2018.8.orig/bsdiff/.gitignore 1970-01-01 02:00:00.000000000 +0200
  787. +++ ostree-2018.8/bsdiff/.gitignore 2018-05-26 00:37:31.984018846 +0300
  788. @@ -0,0 +1,11 @@
  789. +.deps/*
  790. +.libs/*
  791. +*.lo
  792. +*.o
  793. +.dirstamp
  794. +Makefile-bsdiff.am.inc
  795. +AUTHORS
  796. +NEWS
  797. +README
  798. +ChangeLog
  799. +COPYING
  800. diff -Nuar ostree-2018.8.orig/bsdiff/LICENSE ostree-2018.8/bsdiff/LICENSE
  801. --- ostree-2018.8.orig/bsdiff/LICENSE 1970-01-01 02:00:00.000000000 +0200
  802. +++ ostree-2018.8/bsdiff/LICENSE 2018-05-26 00:37:31.984018846 +0300
  803. @@ -0,0 +1,24 @@
  804. + Copyright 2003-2005 Colin Percival
  805. + Copyright 2012 Matthew Endsley
  806. + All rights reserved
  807. +
  808. + Redistribution and use in source and binary forms, with or without
  809. + modification, are permitted providing that the following conditions
  810. + are met:
  811. + 1. Redistributions of source code must retain the above copyright
  812. + notice, this list of conditions and the following disclaimer.
  813. + 2. Redistributions in binary form must reproduce the above copyright
  814. + notice, this list of conditions and the following disclaimer in the
  815. + documentation and/or other materials provided with the distribution.
  816. +
  817. + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  818. + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  819. + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  820. + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  821. + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  822. + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  823. + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  824. + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  825. + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  826. + IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  827. + POSSIBILITY OF SUCH DAMAGE.
  828. diff -Nuar ostree-2018.8.orig/bsdiff/Makefile.am ostree-2018.8/bsdiff/Makefile.am
  829. --- ostree-2018.8.orig/bsdiff/Makefile.am 1970-01-01 02:00:00.000000000 +0200
  830. +++ ostree-2018.8/bsdiff/Makefile.am 2018-05-26 00:37:31.984018846 +0300
  831. @@ -0,0 +1,11 @@
  832. +bin_PROGRAMS = bsdiff bspatch
  833. +
  834. +bsdiff_SOURCES = bsdiff.c
  835. +
  836. +bspatch_SOURCES = bspatch.c
  837. +
  838. +bsdiff_CFLAGS = -DBSDIFF_EXECUTABLE
  839. +bspatch_CFLAGS = -DBSPATCH_EXECUTABLE
  840. +
  841. +EXTRA_DIST = bsdiff.h bspatch.h
  842. +
  843. diff -Nuar ostree-2018.8.orig/bsdiff/Makefile-bsdiff.am ostree-2018.8/bsdiff/Makefile-bsdiff.am
  844. --- ostree-2018.8.orig/bsdiff/Makefile-bsdiff.am 1970-01-01 02:00:00.000000000 +0200
  845. +++ ostree-2018.8/bsdiff/Makefile-bsdiff.am 2018-05-26 00:37:31.984018846 +0300
  846. @@ -0,0 +1,32 @@
  847. +# Copyright (C) 2015 Giuseppe Scrivano <gscrivan@redhat.com>
  848. +#
  849. +# Redistribution and use in source and binary forms, with or without
  850. +# modification, are permitted providing that the following conditions
  851. +# are met:
  852. +# 1. Redistributions of source code must retain the above copyright
  853. +# notice, this list of conditions and the following disclaimer.
  854. +# 2. Redistributions in binary form must reproduce the above copyright
  855. +# notice, this list of conditions and the following disclaimer in the
  856. +# documentation and/or other materials provided with the distribution.
  857. +#
  858. +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  859. +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  860. +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  861. +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  862. +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  863. +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  864. +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  865. +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  866. +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  867. +# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  868. +# POSSIBILITY OF SUCH DAMAGE.
  869. +#
  870. +
  871. +EXTRA_DIST += $(libbsdiff_srcpath)/bsdiff.h $(libbsdiff_srcpath)/bspatch.h $(libbsdiff_srcpath)/LICENSE $(libbsdiff_srcpath)/README.md
  872. +
  873. +libbsdiff_la_SOURCES = \
  874. + $(libbsdiff_srcpath)/bsdiff.c \
  875. + $(libbsdiff_srcpath)/bspatch.c \
  876. + $(NULL)
  877. +
  878. +libbsdiff_la_CFLAGS = $(AM_CFLAGS)
  879. diff -Nuar ostree-2018.8.orig/bsdiff/README.md ostree-2018.8/bsdiff/README.md
  880. --- ostree-2018.8.orig/bsdiff/README.md 1970-01-01 02:00:00.000000000 +0200
  881. +++ ostree-2018.8/bsdiff/README.md 2018-05-26 00:37:31.985018846 +0300
  882. @@ -0,0 +1,129 @@
  883. +bsdiff/bspatch
  884. +==============
  885. +bsdiff and bspatch are libraries for building and applying patches to binary
  886. +files.
  887. +
  888. +The original algorithm and implementation was developed by Colin Percival. The
  889. +algorithm is detailed in his (unpublished) paper, [Naïve Differences of Executable Code](http://www.daemonology.net/papers/bsdiff.pdf). For more information, visit his
  890. +website at <http://www.daemonology.net/bsdiff/>.
  891. +
  892. +I maintain this project seperately from Colin's work, with the goal of making
  893. +the core functionality easily embedable in existing projects.
  894. +
  895. +Contact
  896. +-------
  897. +[@MatthewEndsley](https://twitter.com/#!/MatthewEndsley)
  898. +<https://github.com/mendsley/bsdiff>
  899. +
  900. +License
  901. +-------
  902. +Copyright 2003-2005 Colin Percival
  903. +Copyright 2012 Matthew Endsley
  904. +
  905. +This project is governed by the BSD 2-clause license. For details see the file
  906. +titled LICENSE in the project root folder.
  907. +
  908. +Overview
  909. +--------
  910. +There are two separate libraries in the project, bsdiff and bspatch. Each are
  911. +self contained in bsdiff.c and bspatch.c The easiest way to integrate is to
  912. +simply copy the c file to your source folder and build it.
  913. +
  914. +The overarching goal was to modify the original bsdiff/bspatch code from Colin
  915. +and eliminate external dependencies and provide a simple interface to the core
  916. +functionality.
  917. +
  918. +I've exposed relevant functions via the `_stream` classes. The only external
  919. +dependency not exposed is `memcmp` in `bsdiff`.
  920. +
  921. +This library generates patches that are not compatible with the original bsdiff
  922. +tool. The impompatibilities were motivated by the patching needs for the game
  923. +AirMech <https://www.carbongames.com> and the following requirements:
  924. +
  925. +* Eliminate/minimize any seek operations when applying patches
  926. +* Eliminate any required disk I/O and support embedded streams
  927. +* Ability to easily embed the routines as a library instead of an external binary
  928. +* Compile+run on all platforms we use to build the game (Windows, Linux, NaCl, OSX)
  929. +
  930. +Compiling
  931. +---------
  932. +The libraries should compile warning free in any moderately recent version of
  933. +gcc. The project uses `<stdint.h>` which is technically a C99 file and not
  934. +available in Microsoft Visual Studio. The easiest solution here is to use the
  935. +msinttypes version of stdint.h from <https://code.google.com/p/msinttypes/>.
  936. +The direct link for the lazy people is:
  937. +<https://msinttypes.googlecode.com/svn/trunk/stdint.h>.
  938. +
  939. +If your compiler does not provide an implementation of `<stdint.h>` you can
  940. +remove the header from the bsdiff/bspatch files and provide your own typedefs
  941. +for the following symbols: `uint8_t`, `uint64_t` and `int64_t`.
  942. +
  943. +Examples
  944. +--------
  945. +Each project has an optional main function that serves as an example for using
  946. +the library. Simply defined `BSDIFF_EXECUTABLE` or `BSPATCH_EXECUTABLE` to
  947. +enable building the standalone tools.
  948. +
  949. +Reference
  950. +---------
  951. +### bsdiff
  952. +
  953. + struct bsdiff_stream
  954. + {
  955. + void* opaque;
  956. + void* (*malloc)(size_t size);
  957. + void (*free)(void* ptr);
  958. + int (*write)(struct bsdiff_stream* stream,
  959. + const void* buffer, int size);
  960. + };
  961. +
  962. + int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new,
  963. + int64_t newsize, struct bsdiff_stream* stream);
  964. +
  965. +
  966. +In order to use `bsdiff`, you need to define functions for allocating memory and
  967. +writing binary data. This behavior is controlled by the `stream` parameted
  968. +passed to to `bsdiff(...)`.
  969. +
  970. +The `opaque` field is never read or modified from within the `bsdiff` function.
  971. +The caller can use this field to store custom state data needed for the callback
  972. +functions.
  973. +
  974. +The `malloc` and `free` members should point to functions that behave like the
  975. +standard `malloc` and `free` C functions.
  976. +
  977. +The `write` function is called by bsdiff to write a block of binary data to the
  978. +stream. The return value for `write` should be `0` on success and non-zero if
  979. +the callback failed to write all data. In the default example, bzip2 is used to
  980. +compress output data.
  981. +
  982. +`bsdiff` returns `0` on success and `-1` on failure.
  983. +
  984. +### bspatch
  985. +
  986. + struct bspatch_stream
  987. + {
  988. + void* opaque;
  989. + int (*read)(const struct bspatch_stream* stream,
  990. + void* buffer, int length);
  991. + };
  992. +
  993. + int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new,
  994. + int64_t newsize, struct bspatch_stream* stream);
  995. +
  996. +The `bspatch` function transforms the data for a file using data generated from
  997. +`bsdiff`. The caller takes care of loading the old file and allocating space for
  998. +new file data. The `stream` parameter controls the process for reading binary
  999. +patch data.
  1000. +
  1001. +The `opaque` field is never read or modified from within the bspatch function.
  1002. +The caller can use this field to store custom state data needed for the read
  1003. +function.
  1004. +
  1005. +The `read` function is called by `bspatch` to read a block of binary data from
  1006. +the stream. The return value for `read` should be `0` on success and non-zero
  1007. +if the callback failed to read the requested amount of data. In the default
  1008. +example, bzip2 is used to decompress input data.
  1009. +
  1010. +`bspatch` returns `0` on success and `-1` on failure. On success, `new` contains
  1011. +the data for the patched file.
  1012. diff -Nuar ostree-2018.8.orig/libglnx/COPYING ostree-2018.8/libglnx/COPYING
  1013. --- ostree-2018.8.orig/libglnx/COPYING 1970-01-01 02:00:00.000000000 +0200
  1014. +++ ostree-2018.8/libglnx/COPYING 2018-05-26 00:37:32.006018846 +0300
  1015. @@ -0,0 +1,502 @@
  1016. + GNU LESSER GENERAL PUBLIC LICENSE
  1017. + Version 2.1, February 1999
  1018. +
  1019. + Copyright (C) 1991, 1999 Free Software Foundation, Inc.
  1020. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  1021. + Everyone is permitted to copy and distribute verbatim copies
  1022. + of this license document, but changing it is not allowed.
  1023. +
  1024. +[This is the first released version of the Lesser GPL. It also counts
  1025. + as the successor of the GNU Library Public License, version 2, hence
  1026. + the version number 2.1.]
  1027. +
  1028. + Preamble
  1029. +
  1030. + The licenses for most software are designed to take away your
  1031. +freedom to share and change it. By contrast, the GNU General Public
  1032. +Licenses are intended to guarantee your freedom to share and change
  1033. +free software--to make sure the software is free for all its users.
  1034. +
  1035. + This license, the Lesser General Public License, applies to some
  1036. +specially designated software packages--typically libraries--of the
  1037. +Free Software Foundation and other authors who decide to use it. You
  1038. +can use it too, but we suggest you first think carefully about whether
  1039. +this license or the ordinary General Public License is the better
  1040. +strategy to use in any particular case, based on the explanations below.
  1041. +
  1042. + When we speak of free software, we are referring to freedom of use,
  1043. +not price. Our General Public Licenses are designed to make sure that
  1044. +you have the freedom to distribute copies of free software (and charge
  1045. +for this service if you wish); that you receive source code or can get
  1046. +it if you want it; that you can change the software and use pieces of
  1047. +it in new free programs; and that you are informed that you can do
  1048. +these things.
  1049. +
  1050. + To protect your rights, we need to make restrictions that forbid
  1051. +distributors to deny you these rights or to ask you to surrender these
  1052. +rights. These restrictions translate to certain responsibilities for
  1053. +you if you distribute copies of the library or if you modify it.
  1054. +
  1055. + For example, if you distribute copies of the library, whether gratis
  1056. +or for a fee, you must give the recipients all the rights that we gave
  1057. +you. You must make sure that they, too, receive or can get the source
  1058. +code. If you link other code with the library, you must provide
  1059. +complete object files to the recipients, so that they can relink them
  1060. +with the library after making changes to the library and recompiling
  1061. +it. And you must show them these terms so they know their rights.
  1062. +
  1063. + We protect your rights with a two-step method: (1) we copyright the
  1064. +library, and (2) we offer you this license, which gives you legal
  1065. +permission to copy, distribute and/or modify the library.
  1066. +
  1067. + To protect each distributor, we want to make it very clear that
  1068. +there is no warranty for the free library. Also, if the library is
  1069. +modified by someone else and passed on, the recipients should know
  1070. +that what they have is not the original version, so that the original
  1071. +author's reputation will not be affected by problems that might be
  1072. +introduced by others.
  1073. +
  1074. + Finally, software patents pose a constant threat to the existence of
  1075. +any free program. We wish to make sure that a company cannot
  1076. +effectively restrict the users of a free program by obtaining a
  1077. +restrictive license from a patent holder. Therefore, we insist that
  1078. +any patent license obtained for a version of the library must be
  1079. +consistent with the full freedom of use specified in this license.
  1080. +
  1081. + Most GNU software, including some libraries, is covered by the
  1082. +ordinary GNU General Public License. This license, the GNU Lesser
  1083. +General Public License, applies to certain designated libraries, and
  1084. +is quite different from the ordinary General Public License. We use
  1085. +this license for certain libraries in order to permit linking those
  1086. +libraries into non-free programs.
  1087. +
  1088. + When a program is linked with a library, whether statically or using
  1089. +a shared library, the combination of the two is legally speaking a
  1090. +combined work, a derivative of the original library. The ordinary
  1091. +General Public License therefore permits such linking only if the
  1092. +entire combination fits its criteria of freedom. The Lesser General
  1093. +Public License permits more lax criteria for linking other code with
  1094. +the library.
  1095. +
  1096. + We call this license the "Lesser" General Public License because it
  1097. +does Less to protect the user's freedom than the ordinary General
  1098. +Public License. It also provides other free software developers Less
  1099. +of an advantage over competing non-free programs. These disadvantages
  1100. +are the reason we use the ordinary General Public License for many
  1101. +libraries. However, the Lesser license provides advantages in certain
  1102. +special circumstances.
  1103. +
  1104. + For example, on rare occasions, there may be a special need to
  1105. +encourage the widest possible use of a certain library, so that it becomes
  1106. +a de-facto standard. To achieve this, non-free programs must be
  1107. +allowed to use the library. A more frequent case is that a free
  1108. +library does the same job as widely used non-free libraries. In this
  1109. +case, there is little to gain by limiting the free library to free
  1110. +software only, so we use the Lesser General Public License.
  1111. +
  1112. + In other cases, permission to use a particular library in non-free
  1113. +programs enables a greater number of people to use a large body of
  1114. +free software. For example, permission to use the GNU C Library in
  1115. +non-free programs enables many more people to use the whole GNU
  1116. +operating system, as well as its variant, the GNU/Linux operating
  1117. +system.
  1118. +
  1119. + Although the Lesser General Public License is Less protective of the
  1120. +users' freedom, it does ensure that the user of a program that is
  1121. +linked with the Library has the freedom and the wherewithal to run
  1122. +that program using a modified version of the Library.
  1123. +
  1124. + The precise terms and conditions for copying, distribution and
  1125. +modification follow. Pay close attention to the difference between a
  1126. +"work based on the library" and a "work that uses the library". The
  1127. +former contains code derived from the library, whereas the latter must
  1128. +be combined with the library in order to run.
  1129. +
  1130. + GNU LESSER GENERAL PUBLIC LICENSE
  1131. + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  1132. +
  1133. + 0. This License Agreement applies to any software library or other
  1134. +program which contains a notice placed by the copyright holder or
  1135. +other authorized party saying it may be distributed under the terms of
  1136. +this Lesser General Public License (also called "this License").
  1137. +Each licensee is addressed as "you".
  1138. +
  1139. + A "library" means a collection of software functions and/or data
  1140. +prepared so as to be conveniently linked with application programs
  1141. +(which use some of those functions and data) to form executables.
  1142. +
  1143. + The "Library", below, refers to any such software library or work
  1144. +which has been distributed under these terms. A "work based on the
  1145. +Library" means either the Library or any derivative work under
  1146. +copyright law: that is to say, a work containing the Library or a
  1147. +portion of it, either verbatim or with modifications and/or translated
  1148. +straightforwardly into another language. (Hereinafter, translation is
  1149. +included without limitation in the term "modification".)
  1150. +
  1151. + "Source code" for a work means the preferred form of the work for
  1152. +making modifications to it. For a library, complete source code means
  1153. +all the source code for all modules it contains, plus any associated
  1154. +interface definition files, plus the scripts used to control compilation
  1155. +and installation of the library.
  1156. +
  1157. + Activities other than copying, distribution and modification are not
  1158. +covered by this License; they are outside its scope. The act of
  1159. +running a program using the Library is not restricted, and output from
  1160. +such a program is covered only if its contents constitute a work based
  1161. +on the Library (independent of the use of the Library in a tool for
  1162. +writing it). Whether that is true depends on what the Library does
  1163. +and what the program that uses the Library does.
  1164. +
  1165. + 1. You may copy and distribute verbatim copies of the Library's
  1166. +complete source code as you receive it, in any medium, provided that
  1167. +you conspicuously and appropriately publish on each copy an
  1168. +appropriate copyright notice and disclaimer of warranty; keep intact
  1169. +all the notices that refer to this License and to the absence of any
  1170. +warranty; and distribute a copy of this License along with the
  1171. +Library.
  1172. +
  1173. + You may charge a fee for the physical act of transferring a copy,
  1174. +and you may at your option offer warranty protection in exchange for a
  1175. +fee.
  1176. +
  1177. + 2. You may modify your copy or copies of the Library or any portion
  1178. +of it, thus forming a work based on the Library, and copy and
  1179. +distribute such modifications or work under the terms of Section 1
  1180. +above, provided that you also meet all of these conditions:
  1181. +
  1182. + a) The modified work must itself be a software library.
  1183. +
  1184. + b) You must cause the files modified to carry prominent notices
  1185. + stating that you changed the files and the date of any change.
  1186. +
  1187. + c) You must cause the whole of the work to be licensed at no
  1188. + charge to all third parties under the terms of this License.
  1189. +
  1190. + d) If a facility in the modified Library refers to a function or a
  1191. + table of data to be supplied by an application program that uses
  1192. + the facility, other than as an argument passed when the facility
  1193. + is invoked, then you must make a good faith effort to ensure that,
  1194. + in the event an application does not supply such function or
  1195. + table, the facility still operates, and performs whatever part of
  1196. + its purpose remains meaningful.
  1197. +
  1198. + (For example, a function in a library to compute square roots has
  1199. + a purpose that is entirely well-defined independent of the
  1200. + application. Therefore, Subsection 2d requires that any
  1201. + application-supplied function or table used by this function must
  1202. + be optional: if the application does not supply it, the square
  1203. + root function must still compute square roots.)
  1204. +
  1205. +These requirements apply to the modified work as a whole. If
  1206. +identifiable sections of that work are not derived from the Library,
  1207. +and can be reasonably considered independent and separate works in
  1208. +themselves, then this License, and its terms, do not apply to those
  1209. +sections when you distribute them as separate works. But when you
  1210. +distribute the same sections as part of a whole which is a work based
  1211. +on the Library, the distribution of the whole must be on the terms of
  1212. +this License, whose permissions for other licensees extend to the
  1213. +entire whole, and thus to each and every part regardless of who wrote
  1214. +it.
  1215. +
  1216. +Thus, it is not the intent of this section to claim rights or contest
  1217. +your rights to work written entirely by you; rather, the intent is to
  1218. +exercise the right to control the distribution of derivative or
  1219. +collective works based on the Library.
  1220. +
  1221. +In addition, mere aggregation of another work not based on the Library
  1222. +with the Library (or with a work based on the Library) on a volume of
  1223. +a storage or distribution medium does not bring the other work under
  1224. +the scope of this License.
  1225. +
  1226. + 3. You may opt to apply the terms of the ordinary GNU General Public
  1227. +License instead of this License to a given copy of the Library. To do
  1228. +this, you must alter all the notices that refer to this License, so
  1229. +that they refer to the ordinary GNU General Public License, version 2,
  1230. +instead of to this License. (If a newer version than version 2 of the
  1231. +ordinary GNU General Public License has appeared, then you can specify
  1232. +that version instead if you wish.) Do not make any other change in
  1233. +these notices.
  1234. +
  1235. + Once this change is made in a given copy, it is irreversible for
  1236. +that copy, so the ordinary GNU General Public License applies to all
  1237. +subsequent copies and derivative works made from that copy.
  1238. +
  1239. + This option is useful when you wish to copy part of the code of
  1240. +the Library into a program that is not a library.
  1241. +
  1242. + 4. You may copy and distribute the Library (or a portion or
  1243. +derivative of it, under Section 2) in object code or executable form
  1244. +under the terms of Sections 1 and 2 above provided that you accompany
  1245. +it with the complete corresponding machine-readable source code, which
  1246. +must be distributed under the terms of Sections 1 and 2 above on a
  1247. +medium customarily used for software interchange.
  1248. +
  1249. + If distribution of object code is made by offering access to copy
  1250. +from a designated place, then offering equivalent access to copy the
  1251. +source code from the same place satisfies the requirement to
  1252. +distribute the source code, even though third parties are not
  1253. +compelled to copy the source along with the object code.
  1254. +
  1255. + 5. A program that contains no derivative of any portion of the
  1256. +Library, but is designed to work with the Library by being compiled or
  1257. +linked with it, is called a "work that uses the Library". Such a
  1258. +work, in isolation, is not a derivative work of the Library, and
  1259. +therefore falls outside the scope of this License.
  1260. +
  1261. + However, linking a "work that uses the Library" with the Library
  1262. +creates an executable that is a derivative of the Library (because it
  1263. +contains portions of the Library), rather than a "work that uses the
  1264. +library". The executable is therefore covered by this License.
  1265. +Section 6 states terms for distribution of such executables.
  1266. +
  1267. + When a "work that uses the Library" uses material from a header file
  1268. +that is part of the Library, the object code for the work may be a
  1269. +derivative work of the Library even though the source code is not.
  1270. +Whether this is true is especially significant if the work can be
  1271. +linked without the Library, or if the work is itself a library. The
  1272. +threshold for this to be true is not precisely defined by law.
  1273. +
  1274. + If such an object file uses only numerical parameters, data
  1275. +structure layouts and accessors, and small macros and small inline
  1276. +functions (ten lines or less in length), then the use of the object
  1277. +file is unrestricted, regardless of whether it is legally a derivative
  1278. +work. (Executables containing this object code plus portions of the
  1279. +Library will still fall under Section 6.)
  1280. +
  1281. + Otherwise, if the work is a derivative of the Library, you may
  1282. +distribute the object code for the work under the terms of Section 6.
  1283. +Any executables containing that work also fall under Section 6,
  1284. +whether or not they are linked directly with the Library itself.
  1285. +
  1286. + 6. As an exception to the Sections above, you may also combine or
  1287. +link a "work that uses the Library" with the Library to produce a
  1288. +work containing portions of the Library, and distribute that work
  1289. +under terms of your choice, provided that the terms permit
  1290. +modification of the work for the customer's own use and reverse
  1291. +engineering for debugging such modifications.
  1292. +
  1293. + You must give prominent notice with each copy of the work that the
  1294. +Library is used in it and that the Library and its use are covered by
  1295. +this License. You must supply a copy of this License. If the work
  1296. +during execution displays copyright notices, you must include the
  1297. +copyright notice for the Library among them, as well as a reference
  1298. +directing the user to the copy of this License. Also, you must do one
  1299. +of these things:
  1300. +
  1301. + a) Accompany the work with the complete corresponding
  1302. + machine-readable source code for the Library including whatever
  1303. + changes were used in the work (which must be distributed under
  1304. + Sections 1 and 2 above); and, if the work is an executable linked
  1305. + with the Library, with the complete machine-readable "work that
  1306. + uses the Library", as object code and/or source code, so that the
  1307. + user can modify the Library and then relink to produce a modified
  1308. + executable containing the modified Library. (It is understood
  1309. + that the user who changes the contents of definitions files in the
  1310. + Library will not necessarily be able to recompile the application
  1311. + to use the modified definitions.)
  1312. +
  1313. + b) Use a suitable shared library mechanism for linking with the
  1314. + Library. A suitable mechanism is one that (1) uses at run time a
  1315. + copy of the library already present on the user's computer system,
  1316. + rather than copying library functions into the executable, and (2)
  1317. + will operate properly with a modified version of the library, if
  1318. + the user installs one, as long as the modified version is
  1319. + interface-compatible with the version that the work was made with.
  1320. +
  1321. + c) Accompany the work with a written offer, valid for at
  1322. + least three years, to give the same user the materials
  1323. + specified in Subsection 6a, above, for a charge no more
  1324. + than the cost of performing this distribution.
  1325. +
  1326. + d) If distribution of the work is made by offering access to copy
  1327. + from a designated place, offer equivalent access to copy the above
  1328. + specified materials from the same place.
  1329. +
  1330. + e) Verify that the user has already received a copy of these
  1331. + materials or that you have already sent this user a copy.
  1332. +
  1333. + For an executable, the required form of the "work that uses the
  1334. +Library" must include any data and utility programs needed for
  1335. +reproducing the executable from it. However, as a special exception,
  1336. +the materials to be distributed need not include anything that is
  1337. +normally distributed (in either source or binary form) with the major
  1338. +components (compiler, kernel, and so on) of the operating system on
  1339. +which the executable runs, unless that component itself accompanies
  1340. +the executable.
  1341. +
  1342. + It may happen that this requirement contradicts the license
  1343. +restrictions of other proprietary libraries that do not normally
  1344. +accompany the operating system. Such a contradiction means you cannot
  1345. +use both them and the Library together in an executable that you
  1346. +distribute.
  1347. +
  1348. + 7. You may place library facilities that are a work based on the
  1349. +Library side-by-side in a single library together with other library
  1350. +facilities not covered by this License, and distribute such a combined
  1351. +library, provided that the separate distribution of the work based on
  1352. +the Library and of the other library facilities is otherwise
  1353. +permitted, and provided that you do these two things:
  1354. +
  1355. + a) Accompany the combined library with a copy of the same work
  1356. + based on the Library, uncombined with any other library
  1357. + facilities. This must be distributed under the terms of the
  1358. + Sections above.
  1359. +
  1360. + b) Give prominent notice with the combined library of the fact
  1361. + that part of it is a work based on the Library, and explaining
  1362. + where to find the accompanying uncombined form of the same work.
  1363. +
  1364. + 8. You may not copy, modify, sublicense, link with, or distribute
  1365. +the Library except as expressly provided under this License. Any
  1366. +attempt otherwise to copy, modify, sublicense, link with, or
  1367. +distribute the Library is void, and will automatically terminate your
  1368. +rights under this License. However, parties who have received copies,
  1369. +or rights, from you under this License will not have their licenses
  1370. +terminated so long as such parties remain in full compliance.
  1371. +
  1372. + 9. You are not required to accept this License, since you have not
  1373. +signed it. However, nothing else grants you permission to modify or
  1374. +distribute the Library or its derivative works. These actions are
  1375. +prohibited by law if you do not accept this License. Therefore, by
  1376. +modifying or distributing the Library (or any work based on the
  1377. +Library), you indicate your acceptance of this License to do so, and
  1378. +all its terms and conditions for copying, distributing or modifying
  1379. +the Library or works based on it.
  1380. +
  1381. + 10. Each time you redistribute the Library (or any work based on the
  1382. +Library), the recipient automatically receives a license from the
  1383. +original licensor to copy, distribute, link with or modify the Library
  1384. +subject to these terms and conditions. You may not impose any further
  1385. +restrictions on the recipients' exercise of the rights granted herein.
  1386. +You are not responsible for enforcing compliance by third parties with
  1387. +this License.
  1388. +
  1389. + 11. If, as a consequence of a court judgment or allegation of patent
  1390. +infringement or for any other reason (not limited to patent issues),
  1391. +conditions are imposed on you (whether by court order, agreement or
  1392. +otherwise) that contradict the conditions of this License, they do not
  1393. +excuse you from the conditions of this License. If you cannot
  1394. +distribute so as to satisfy simultaneously your obligations under this
  1395. +License and any other pertinent obligations, then as a consequence you
  1396. +may not distribute the Library at all. For example, if a patent
  1397. +license would not permit royalty-free redistribution of the Library by
  1398. +all those who receive copies directly or indirectly through you, then
  1399. +the only way you could satisfy both it and this License would be to
  1400. +refrain entirely from distribution of the Library.
  1401. +
  1402. +If any portion of this section is held invalid or unenforceable under any
  1403. +particular circumstance, the balance of the section is intended to apply,
  1404. +and the section as a whole is intended to apply in other circumstances.
  1405. +
  1406. +It is not the purpose of this section to induce you to infringe any
  1407. +patents or other property right claims or to contest validity of any
  1408. +such claims; this section has the sole purpose of protecting the
  1409. +integrity of the free software distribution system which is
  1410. +implemented by public license practices. Many people have made
  1411. +generous contributions to the wide range of software distributed
  1412. +through that system in reliance on consistent application of that
  1413. +system; it is up to the author/donor to decide if he or she is willing
  1414. +to distribute software through any other system and a licensee cannot
  1415. +impose that choice.
  1416. +
  1417. +This section is intended to make thoroughly clear what is believed to
  1418. +be a consequence of the rest of this License.
  1419. +
  1420. + 12. If the distribution and/or use of the Library is restricted in
  1421. +certain countries either by patents or by copyrighted interfaces, the
  1422. +original copyright holder who places the Library under this License may add
  1423. +an explicit geographical distribution limitation excluding those countries,
  1424. +so that distribution is permitted only in or among countries not thus
  1425. +excluded. In such case, this License incorporates the limitation as if
  1426. +written in the body of this License.
  1427. +
  1428. + 13. The Free Software Foundation may publish revised and/or new
  1429. +versions of the Lesser General Public License from time to time.
  1430. +Such new versions will be similar in spirit to the present version,
  1431. +but may differ in detail to address new problems or concerns.
  1432. +
  1433. +Each version is given a distinguishing version number. If the Library
  1434. +specifies a version number of this License which applies to it and
  1435. +"any later version", you have the option of following the terms and
  1436. +conditions either of that version or of any later version published by
  1437. +the Free Software Foundation. If the Library does not specify a
  1438. +license version number, you may choose any version ever published by
  1439. +the Free Software Foundation.
  1440. +
  1441. + 14. If you wish to incorporate parts of the Library into other free
  1442. +programs whose distribution conditions are incompatible with these,
  1443. +write to the author to ask for permission. For software which is
  1444. +copyrighted by the Free Software Foundation, write to the Free
  1445. +Software Foundation; we sometimes make exceptions for this. Our
  1446. +decision will be guided by the two goals of preserving the free status
  1447. +of all derivatives of our free software and of promoting the sharing
  1448. +and reuse of software generally.
  1449. +
  1450. + NO WARRANTY
  1451. +
  1452. + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
  1453. +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
  1454. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
  1455. +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
  1456. +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
  1457. +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1458. +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  1459. +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
  1460. +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  1461. +
  1462. + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  1463. +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
  1464. +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
  1465. +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
  1466. +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
  1467. +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
  1468. +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
  1469. +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
  1470. +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  1471. +DAMAGES.
  1472. +
  1473. + END OF TERMS AND CONDITIONS
  1474. +
  1475. + How to Apply These Terms to Your New Libraries
  1476. +
  1477. + If you develop a new library, and you want it to be of the greatest
  1478. +possible use to the public, we recommend making it free software that
  1479. +everyone can redistribute and change. You can do so by permitting
  1480. +redistribution under these terms (or, alternatively, under the terms of the
  1481. +ordinary General Public License).
  1482. +
  1483. + To apply these terms, attach the following notices to the library. It is
  1484. +safest to attach them to the start of each source file to most effectively
  1485. +convey the exclusion of warranty; and each file should have at least the
  1486. +"copyright" line and a pointer to where the full notice is found.
  1487. +
  1488. + <one line to give the library's name and a brief idea of what it does.>
  1489. + Copyright (C) <year> <name of author>
  1490. +
  1491. + This library is free software; you can redistribute it and/or
  1492. + modify it under the terms of the GNU Lesser General Public
  1493. + License as published by the Free Software Foundation; either
  1494. + version 2.1 of the License, or (at your option) any later version.
  1495. +
  1496. + This library is distributed in the hope that it will be useful,
  1497. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1498. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1499. + Lesser General Public License for more details.
  1500. +
  1501. + You should have received a copy of the GNU Lesser General Public
  1502. + License along with this library; if not, write to the Free Software
  1503. + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  1504. +
  1505. +Also add information on how to contact you by electronic and paper mail.
  1506. +
  1507. +You should also get your employer (if you work as a programmer) or your
  1508. +school, if any, to sign a "copyright disclaimer" for the library, if
  1509. +necessary. Here is a sample; alter the names:
  1510. +
  1511. + Yoyodyne, Inc., hereby disclaims all copyright interest in the
  1512. + library `Frob' (a library for tweaking knobs) written by James Random Hacker.
  1513. +
  1514. + <signature of Ty Coon>, 1 April 1990
  1515. + Ty Coon, President of Vice
  1516. +
  1517. +That's all there is to it!
  1518. diff -Nuar ostree-2018.8.orig/libglnx/.git ostree-2018.8/libglnx/.git
  1519. --- ostree-2018.8.orig/libglnx/.git 1970-01-01 02:00:00.000000000 +0200
  1520. +++ ostree-2018.8/libglnx/.git 2018-05-26 00:37:31.964018845 +0300
  1521. @@ -0,0 +1 @@
  1522. +gitdir: ../.git/modules/libglnx
  1523. diff -Nuar ostree-2018.8.orig/libglnx/.gitignore ostree-2018.8/libglnx/.gitignore
  1524. --- ostree-2018.8.orig/libglnx/.gitignore 1970-01-01 02:00:00.000000000 +0200
  1525. +++ ostree-2018.8/libglnx/.gitignore 2018-05-26 00:37:32.005018846 +0300
  1526. @@ -0,0 +1,16 @@
  1527. +# A path ostree writes to work around automake bug with
  1528. +# subdir-objects
  1529. +Makefile-libglnx.am.inc
  1530. +
  1531. +# Some standard bits
  1532. +.deps
  1533. +.libs
  1534. +.dirstamp
  1535. +*.typelib
  1536. +*.la
  1537. +*.lo
  1538. +*.o
  1539. +*.pyc
  1540. +*.stamp
  1541. +*~
  1542. +
  1543. diff -Nuar ostree-2018.8.orig/libglnx/glnx-backport-autocleanups.h ostree-2018.8/libglnx/glnx-backport-autocleanups.h
  1544. --- ostree-2018.8.orig/libglnx/glnx-backport-autocleanups.h 1970-01-01 02:00:00.000000000 +0200
  1545. +++ ostree-2018.8/libglnx/glnx-backport-autocleanups.h 2018-05-26 00:37:32.006018846 +0300
  1546. @@ -0,0 +1,124 @@
  1547. +/*
  1548. + * Copyright © 2015 Canonical Limited
  1549. + *
  1550. + * This library is free software; you can redistribute it and/or
  1551. + * modify it under the terms of the GNU Lesser General Public
  1552. + * License as published by the Free Software Foundation; either
  1553. + * version 2 of the licence, or (at your option) any later version.
  1554. + *
  1555. + * This library is distributed in the hope that it will be useful,
  1556. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1557. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1558. + * Lesser General Public License for more details.
  1559. + *
  1560. + * You should have received a copy of the GNU Lesser General Public
  1561. + * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  1562. + *
  1563. + * Author: Ryan Lortie <desrt@desrt.ca>
  1564. + */
  1565. +
  1566. +#pragma once
  1567. +
  1568. +#include <glnx-backport-autoptr.h>
  1569. +
  1570. +#if !GLIB_CHECK_VERSION(2, 43, 4)
  1571. +
  1572. +static inline void
  1573. +g_autoptr_cleanup_generic_gfree (void *p)
  1574. +{
  1575. + void **pp = (void**)p;
  1576. + if (*pp)
  1577. + g_free (*pp);
  1578. +}
  1579. +
  1580. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GAsyncQueue, g_async_queue_unref)
  1581. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GBookmarkFile, g_bookmark_file_free)
  1582. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GBytes, g_bytes_unref)
  1583. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GChecksum, g_checksum_free)
  1584. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDateTime, g_date_time_unref)
  1585. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDir, g_dir_close)
  1586. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GError, g_error_free)
  1587. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GHashTable, g_hash_table_unref)
  1588. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GHmac, g_hmac_unref)
  1589. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GIOChannel, g_io_channel_unref)
  1590. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GKeyFile, g_key_file_unref)
  1591. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GList, g_list_free)
  1592. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GArray, g_array_unref)
  1593. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GPtrArray, g_ptr_array_unref)
  1594. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMainContext, g_main_context_unref)
  1595. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMainLoop, g_main_loop_unref)
  1596. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSource, g_source_unref)
  1597. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMappedFile, g_mapped_file_unref)
  1598. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMarkupParseContext, g_markup_parse_context_unref)
  1599. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(gchar, g_free)
  1600. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GNode, g_node_destroy)
  1601. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GOptionContext, g_option_context_free)
  1602. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GOptionGroup, g_option_group_free)
  1603. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GPatternSpec, g_pattern_spec_free)
  1604. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GQueue, g_queue_free)
  1605. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GQueue, g_queue_clear)
  1606. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GRand, g_rand_free)
  1607. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GRegex, g_regex_unref)
  1608. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMatchInfo, g_match_info_unref)
  1609. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GScanner, g_scanner_destroy)
  1610. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSequence, g_sequence_free)
  1611. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSList, g_slist_free)
  1612. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GStringChunk, g_string_chunk_free)
  1613. +G_DEFINE_AUTO_CLEANUP_FREE_FUNC(GStrv, g_strfreev, NULL)
  1614. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GThread, g_thread_unref)
  1615. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GMutex, g_mutex_clear)
  1616. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GCond, g_cond_clear)
  1617. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTimer, g_timer_destroy)
  1618. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTimeZone, g_time_zone_unref)
  1619. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTree, g_tree_unref)
  1620. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariant, g_variant_unref)
  1621. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantBuilder, g_variant_builder_unref)
  1622. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GVariantBuilder, g_variant_builder_clear)
  1623. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantIter, g_variant_iter_free)
  1624. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantDict, g_variant_dict_unref)
  1625. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GVariantDict, g_variant_dict_clear)
  1626. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantType, g_variant_type_free)
  1627. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSubprocess, g_object_unref)
  1628. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSubprocessLauncher, g_object_unref)
  1629. +
  1630. +/* Add GObject-based types as needed. */
  1631. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GCancellable, g_object_unref)
  1632. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GConverter, g_object_unref)
  1633. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GConverterOutputStream, g_object_unref)
  1634. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDataInputStream, g_object_unref)
  1635. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFile, g_object_unref)
  1636. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileEnumerator, g_object_unref)
  1637. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileIOStream, g_object_unref)
  1638. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileInfo, g_object_unref)
  1639. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileInputStream, g_object_unref)
  1640. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileMonitor, g_object_unref)
  1641. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GFileOutputStream, g_object_unref)
  1642. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GInputStream, g_object_unref)
  1643. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMemoryInputStream, g_object_unref)
  1644. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMemoryOutputStream, g_object_unref)
  1645. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GOutputStream, g_object_unref)
  1646. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSocket, g_object_unref)
  1647. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSocketAddress, g_object_unref)
  1648. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTask, g_object_unref)
  1649. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTlsCertificate, g_object_unref)
  1650. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTlsDatabase, g_object_unref)
  1651. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTlsInteraction, g_object_unref)
  1652. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDBusConnection, g_object_unref)
  1653. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDBusMessage, g_object_unref)
  1654. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GZlibCompressor, g_object_unref)
  1655. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GZlibDecompressor, g_object_unref)
  1656. +
  1657. +#endif
  1658. +
  1659. +#if !GLIB_CHECK_VERSION(2, 45, 8)
  1660. +
  1661. +static inline void
  1662. +g_autoptr_cleanup_gstring_free (GString *string)
  1663. +{
  1664. + if (string)
  1665. + g_string_free (string, TRUE);
  1666. +}
  1667. +
  1668. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GString, g_autoptr_cleanup_gstring_free)
  1669. +
  1670. +#endif
  1671. diff -Nuar ostree-2018.8.orig/libglnx/glnx-backport-autoptr.h ostree-2018.8/libglnx/glnx-backport-autoptr.h
  1672. --- ostree-2018.8.orig/libglnx/glnx-backport-autoptr.h 1970-01-01 02:00:00.000000000 +0200
  1673. +++ ostree-2018.8/libglnx/glnx-backport-autoptr.h 2018-05-26 00:37:32.006018846 +0300
  1674. @@ -0,0 +1,133 @@
  1675. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1676. + *
  1677. + * Copyright (C) 2015 Colin Walters <walters@verbum.org>
  1678. + *
  1679. + * GLIB - Library of useful routines for C programming
  1680. + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  1681. + *
  1682. + * This library is free software; you can redistribute it and/or
  1683. + * modify it under the terms of the GNU Lesser General Public
  1684. + * License as published by the Free Software Foundation; either
  1685. + * version 2 of the License, or (at your option) any later version.
  1686. + *
  1687. + * This library is distributed in the hope that it will be useful,
  1688. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1689. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1690. + * Lesser General Public License for more details.
  1691. + *
  1692. + * You should have received a copy of the GNU Lesser General Public
  1693. + * License along with this library; if not, write to the
  1694. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1695. + * Boston, MA 02111-1307, USA.
  1696. + */
  1697. +
  1698. +#pragma once
  1699. +
  1700. +#include <gio/gio.h>
  1701. +
  1702. +G_BEGIN_DECLS
  1703. +
  1704. +#if !GLIB_CHECK_VERSION(2, 43, 4)
  1705. +
  1706. +#define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
  1707. +#define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
  1708. +#define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName
  1709. +#define _GLIB_CLEANUP(func) __attribute__((cleanup(func)))
  1710. +#define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
  1711. + typedef ModuleObjName *_GLIB_AUTOPTR_TYPENAME(ModuleObjName); \
  1712. + static inline void _GLIB_AUTOPTR_FUNC_NAME(ModuleObjName) (ModuleObjName **_ptr) { \
  1713. + _GLIB_AUTOPTR_FUNC_NAME(ParentName) ((ParentName **) _ptr); } \
  1714. +
  1715. +
  1716. +/* these macros are API */
  1717. +#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
  1718. + typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \
  1719. + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
  1720. + static inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); } \
  1721. + G_GNUC_END_IGNORE_DEPRECATIONS
  1722. +#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
  1723. + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
  1724. + static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \
  1725. + G_GNUC_END_IGNORE_DEPRECATIONS
  1726. +#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \
  1727. + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
  1728. + static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \
  1729. + G_GNUC_END_IGNORE_DEPRECATIONS
  1730. +#define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
  1731. +#define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
  1732. +#define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
  1733. +
  1734. +/**
  1735. + * g_steal_pointer:
  1736. + * @pp: a pointer to a pointer
  1737. + *
  1738. + * Sets @pp to %NULL, returning the value that was there before.
  1739. + *
  1740. + * Conceptually, this transfers the ownership of the pointer from the
  1741. + * referenced variable to the "caller" of the macro (ie: "steals" the
  1742. + * reference).
  1743. + *
  1744. + * The return value will be properly typed, according to the type of
  1745. + * @pp.
  1746. + *
  1747. + * This can be very useful when combined with g_autoptr() to prevent the
  1748. + * return value of a function from being automatically freed. Consider
  1749. + * the following example (which only works on GCC and clang):
  1750. + *
  1751. + * |[
  1752. + * GObject *
  1753. + * create_object (void)
  1754. + * {
  1755. + * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  1756. + *
  1757. + * if (early_error_case)
  1758. + * return NULL;
  1759. + *
  1760. + * return g_steal_pointer (&obj);
  1761. + * }
  1762. + * ]|
  1763. + *
  1764. + * It can also be used in similar ways for 'out' parameters and is
  1765. + * particularly useful for dealing with optional out parameters:
  1766. + *
  1767. + * |[
  1768. + * gboolean
  1769. + * get_object (GObject **obj_out)
  1770. + * {
  1771. + * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  1772. + *
  1773. + * if (early_error_case)
  1774. + * return FALSE;
  1775. + *
  1776. + * if (obj_out)
  1777. + * *obj_out = g_steal_pointer (&obj);
  1778. + *
  1779. + * return TRUE;
  1780. + * }
  1781. + * ]|
  1782. + *
  1783. + * In the above example, the object will be automatically freed in the
  1784. + * early error case and also in the case that %NULL was given for
  1785. + * @obj_out.
  1786. + *
  1787. + * Since: 2.44
  1788. + */
  1789. +static inline gpointer
  1790. +(g_steal_pointer) (gpointer pp)
  1791. +{
  1792. + gpointer *ptr = (gpointer *) pp;
  1793. + gpointer ref;
  1794. +
  1795. + ref = *ptr;
  1796. + *ptr = NULL;
  1797. +
  1798. + return ref;
  1799. +}
  1800. +
  1801. +/* type safety */
  1802. +#define g_steal_pointer(pp) \
  1803. + (0 ? (*(pp)) : (g_steal_pointer) (pp))
  1804. +
  1805. +#endif /* !GLIB_CHECK_VERSION(2, 43, 3) */
  1806. +
  1807. +G_END_DECLS
  1808. diff -Nuar ostree-2018.8.orig/libglnx/glnx-backports.c ostree-2018.8/libglnx/glnx-backports.c
  1809. --- ostree-2018.8.orig/libglnx/glnx-backports.c 1970-01-01 02:00:00.000000000 +0200
  1810. +++ ostree-2018.8/libglnx/glnx-backports.c 2018-05-26 00:37:32.006018846 +0300
  1811. @@ -0,0 +1,61 @@
  1812. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1813. + *
  1814. + * Copyright (C) 2015 Colin Walters <walters@verbum.org>
  1815. + *
  1816. + * This program is free software: you can redistribute it and/or modify
  1817. + * it under the terms of the GNU Lesser General Public License as published
  1818. + * by the Free Software Foundation; either version 2 of the licence or (at
  1819. + * your option) any later version.
  1820. + *
  1821. + * This library is distributed in the hope that it will be useful,
  1822. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1823. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1824. + * Lesser General Public License for more details.
  1825. + *
  1826. + * You should have received a copy of the GNU Lesser General
  1827. + * Public License along with this library; if not, write to the
  1828. + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  1829. + * Boston, MA 02111-1307, USA.
  1830. + */
  1831. +
  1832. +#include "config.h"
  1833. +
  1834. +#include "glnx-backports.h"
  1835. +
  1836. +#if !GLIB_CHECK_VERSION(2, 44, 0)
  1837. +gboolean
  1838. +glnx_strv_contains (const gchar * const *strv,
  1839. + const gchar *str)
  1840. +{
  1841. + g_return_val_if_fail (strv != NULL, FALSE);
  1842. + g_return_val_if_fail (str != NULL, FALSE);
  1843. +
  1844. + for (; *strv != NULL; strv++)
  1845. + {
  1846. + if (g_str_equal (str, *strv))
  1847. + return TRUE;
  1848. + }
  1849. +
  1850. + return FALSE;
  1851. +}
  1852. +
  1853. +gboolean
  1854. +glnx_set_object (GObject **object_ptr,
  1855. + GObject *new_object)
  1856. +{
  1857. + GObject *old_object = *object_ptr;
  1858. +
  1859. + if (old_object == new_object)
  1860. + return FALSE;
  1861. +
  1862. + if (new_object != NULL)
  1863. + g_object_ref (new_object);
  1864. +
  1865. + *object_ptr = new_object;
  1866. +
  1867. + if (old_object != NULL)
  1868. + g_object_unref (old_object);
  1869. +
  1870. + return TRUE;
  1871. +}
  1872. +#endif
  1873. diff -Nuar ostree-2018.8.orig/libglnx/glnx-backports.h ostree-2018.8/libglnx/glnx-backports.h
  1874. --- ostree-2018.8.orig/libglnx/glnx-backports.h 1970-01-01 02:00:00.000000000 +0200
  1875. +++ ostree-2018.8/libglnx/glnx-backports.h 2018-05-26 00:37:32.006018846 +0300
  1876. @@ -0,0 +1,46 @@
  1877. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1878. + *
  1879. + * Copyright (C) 2015 Colin Walters <walters@verbum.org>
  1880. + *
  1881. + * GLIB - Library of useful routines for C programming
  1882. + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  1883. + *
  1884. + * This library is free software; you can redistribute it and/or
  1885. + * modify it under the terms of the GNU Lesser General Public
  1886. + * License as published by the Free Software Foundation; either
  1887. + * version 2 of the License, or (at your option) any later version.
  1888. + *
  1889. + * This library is distributed in the hope that it will be useful,
  1890. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1891. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1892. + * Lesser General Public License for more details.
  1893. + *
  1894. + * You should have received a copy of the GNU Lesser General Public
  1895. + * License along with this library; if not, write to the
  1896. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1897. + * Boston, MA 02111-1307, USA.
  1898. + */
  1899. +
  1900. +#pragma once
  1901. +
  1902. +#include <gio/gio.h>
  1903. +
  1904. +G_BEGIN_DECLS
  1905. +
  1906. +#if !GLIB_CHECK_VERSION(2, 44, 0)
  1907. +
  1908. +#define g_strv_contains glnx_strv_contains
  1909. +gboolean glnx_strv_contains (const gchar * const *strv,
  1910. + const gchar *str);
  1911. +
  1912. +#define g_set_object(object_ptr, new_object) \
  1913. + (/* Check types match. */ \
  1914. + 0 ? *(object_ptr) = (new_object), FALSE : \
  1915. + glnx_set_object ((GObject **) (object_ptr), (GObject *) (new_object)) \
  1916. + )
  1917. +gboolean glnx_set_object (GObject **object_ptr,
  1918. + GObject *new_object);
  1919. +
  1920. +#endif /* !GLIB_CHECK_VERSION(2, 44, 0) */
  1921. +
  1922. +G_END_DECLS
  1923. diff -Nuar ostree-2018.8.orig/libglnx/glnx-console.c ostree-2018.8/libglnx/glnx-console.c
  1924. --- ostree-2018.8.orig/libglnx/glnx-console.c 1970-01-01 02:00:00.000000000 +0200
  1925. +++ ostree-2018.8/libglnx/glnx-console.c 2018-05-26 00:37:32.006018846 +0300
  1926. @@ -0,0 +1,359 @@
  1927. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1928. + *
  1929. + * Copyright (C) 2013,2014,2015 Colin Walters <walters@verbum.org>
  1930. + *
  1931. + * This program is free software: you can redistribute it and/or modify
  1932. + * it under the terms of the GNU Lesser General Public License as published
  1933. + * by the Free Software Foundation; either version 2 of the licence or (at
  1934. + * your option) any later version.
  1935. + *
  1936. + * This library is distributed in the hope that it will be useful,
  1937. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1938. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1939. + * Lesser General Public License for more details.
  1940. + *
  1941. + * You should have received a copy of the GNU Lesser General
  1942. + * Public License along with this library; if not, write to the
  1943. + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  1944. + * Boston, MA 02111-1307, USA.
  1945. + */
  1946. +
  1947. +#include "config.h"
  1948. +
  1949. +#include "glnx-console.h"
  1950. +
  1951. +#include <unistd.h>
  1952. +#include <string.h>
  1953. +#include <fcntl.h>
  1954. +#include <stdio.h>
  1955. +#include <errno.h>
  1956. +#include <sys/ioctl.h>
  1957. +
  1958. +/* For people with widescreen monitors and maximized terminals, it looks pretty
  1959. + * bad to have an enormous progress bar. For much the same reason as web pages
  1960. + * tend to have a maximum width;
  1961. + * https://ux.stackexchange.com/questions/48982/suggest-good-max-width-for-fluid-width-design
  1962. + */
  1963. +#define MAX_PROGRESSBAR_COLUMNS 20
  1964. +
  1965. +/* Max updates output per second. On a tty there's no point to rendering
  1966. + * extremely fast; and for a non-tty we're probably in a Jenkins job
  1967. + * or whatever and having percentages spam multiple lines there is annoying.
  1968. + */
  1969. +#define MAX_TTY_UPDATE_HZ (5)
  1970. +#define MAX_NONTTY_UPDATE_HZ (1)
  1971. +
  1972. +static gboolean locked;
  1973. +static guint64 last_update_ms; /* monotonic time in millis we last updated */
  1974. +
  1975. +gboolean
  1976. +glnx_stdout_is_tty (void)
  1977. +{
  1978. + static gsize initialized = 0;
  1979. + static gboolean stdout_is_tty_v;
  1980. +
  1981. + if (g_once_init_enter (&initialized))
  1982. + {
  1983. + stdout_is_tty_v = isatty (1);
  1984. + g_once_init_leave (&initialized, 1);
  1985. + }
  1986. +
  1987. + return stdout_is_tty_v;
  1988. +}
  1989. +
  1990. +static volatile guint cached_columns = 0;
  1991. +static volatile guint cached_lines = 0;
  1992. +
  1993. +static int
  1994. +fd_columns (int fd)
  1995. +{
  1996. + struct winsize ws = {};
  1997. +
  1998. + if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
  1999. + return -errno;
  2000. +
  2001. + if (ws.ws_col <= 0)
  2002. + return -EIO;
  2003. +
  2004. + return ws.ws_col;
  2005. +}
  2006. +
  2007. +/**
  2008. + * glnx_console_columns:
  2009. + *
  2010. + * Returns: The number of columns for terminal output
  2011. + */
  2012. +guint
  2013. +glnx_console_columns (void)
  2014. +{
  2015. + if (G_UNLIKELY (cached_columns == 0))
  2016. + {
  2017. + int c;
  2018. +
  2019. + c = fd_columns (STDOUT_FILENO);
  2020. +
  2021. + if (c <= 0)
  2022. + c = 80;
  2023. +
  2024. + if (c > 256)
  2025. + c = 256;
  2026. +
  2027. + cached_columns = c;
  2028. + }
  2029. +
  2030. + return cached_columns;
  2031. +}
  2032. +
  2033. +static int
  2034. +fd_lines (int fd)
  2035. +{
  2036. + struct winsize ws = {};
  2037. +
  2038. + if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
  2039. + return -errno;
  2040. +
  2041. + if (ws.ws_row <= 0)
  2042. + return -EIO;
  2043. +
  2044. + return ws.ws_row;
  2045. +}
  2046. +
  2047. +/**
  2048. + * glnx_console_lines:
  2049. + *
  2050. + * Returns: The number of lines for terminal output
  2051. + */
  2052. +guint
  2053. +glnx_console_lines (void)
  2054. +{
  2055. + if (G_UNLIKELY (cached_lines == 0))
  2056. + {
  2057. + int l;
  2058. +
  2059. + l = fd_lines (STDOUT_FILENO);
  2060. +
  2061. + if (l <= 0)
  2062. + l = 24;
  2063. +
  2064. + cached_lines = l;
  2065. + }
  2066. +
  2067. + return cached_lines;
  2068. +}
  2069. +
  2070. +static void
  2071. +on_sigwinch (int signum)
  2072. +{
  2073. + cached_columns = 0;
  2074. + cached_lines = 0;
  2075. +}
  2076. +
  2077. +void
  2078. +glnx_console_lock (GLnxConsoleRef *console)
  2079. +{
  2080. + static gsize sigwinch_initialized = 0;
  2081. +
  2082. + g_return_if_fail (!locked);
  2083. + g_return_if_fail (!console->locked);
  2084. +
  2085. + console->is_tty = glnx_stdout_is_tty ();
  2086. +
  2087. + locked = console->locked = TRUE;
  2088. +
  2089. + if (console->is_tty)
  2090. + {
  2091. + if (g_once_init_enter (&sigwinch_initialized))
  2092. + {
  2093. + signal (SIGWINCH, on_sigwinch);
  2094. + g_once_init_leave (&sigwinch_initialized, 1);
  2095. + }
  2096. +
  2097. + { static const char initbuf[] = { '\n', 0x1B, 0x37 };
  2098. + (void) fwrite (initbuf, 1, sizeof (initbuf), stdout);
  2099. + }
  2100. + }
  2101. +}
  2102. +
  2103. +static void
  2104. +printpad (const char *padbuf,
  2105. + guint padbuf_len,
  2106. + guint n)
  2107. +{
  2108. + const guint d = n / padbuf_len;
  2109. + const guint r = n % padbuf_len;
  2110. + guint i;
  2111. +
  2112. + for (i = 0; i < d; i++)
  2113. + fwrite (padbuf, 1, padbuf_len, stdout);
  2114. + fwrite (padbuf, 1, r, stdout);
  2115. +}
  2116. +
  2117. +static void
  2118. +text_percent_internal (const char *text,
  2119. + int percentage)
  2120. +{
  2121. + /* Check whether we're trying to render too fast; unless percentage is 100, in
  2122. + * which case we assume this is the last call, so we always render it.
  2123. + */
  2124. + const guint64 current_ms = g_get_monotonic_time () / 1000;
  2125. + if (percentage != 100)
  2126. + {
  2127. + const guint64 diff_ms = current_ms - last_update_ms;
  2128. + if (glnx_stdout_is_tty ())
  2129. + {
  2130. + if (diff_ms < (1000/MAX_TTY_UPDATE_HZ))
  2131. + return;
  2132. + }
  2133. + else
  2134. + {
  2135. + if (diff_ms < (1000/MAX_NONTTY_UPDATE_HZ))
  2136. + return;
  2137. + }
  2138. + }
  2139. + last_update_ms = current_ms;
  2140. +
  2141. + static const char equals[] = "====================";
  2142. + const guint n_equals = sizeof (equals) - 1;
  2143. + static const char spaces[] = " ";
  2144. + const guint n_spaces = sizeof (spaces) - 1;
  2145. + const guint ncolumns = glnx_console_columns ();
  2146. + const guint bar_min = 10;
  2147. +
  2148. + if (text && !*text)
  2149. + text = NULL;
  2150. +
  2151. + const guint input_textlen = text ? strlen (text) : 0;
  2152. +
  2153. + if (!glnx_stdout_is_tty ())
  2154. + {
  2155. + if (text)
  2156. + fprintf (stdout, "%s", text);
  2157. + if (percentage != -1)
  2158. + {
  2159. + if (text)
  2160. + fputc (' ', stdout);
  2161. + fprintf (stdout, "%u%%", percentage);
  2162. + }
  2163. + fputc ('\n', stdout);
  2164. + fflush (stdout);
  2165. + return;
  2166. + }
  2167. +
  2168. + if (ncolumns < bar_min)
  2169. + return; /* TODO: spinner */
  2170. +
  2171. + /* Restore cursor */
  2172. + { const char beginbuf[2] = { 0x1B, 0x38 };
  2173. + (void) fwrite (beginbuf, 1, sizeof (beginbuf), stdout);
  2174. + }
  2175. +
  2176. + if (percentage == -1)
  2177. + {
  2178. + if (text != NULL)
  2179. + fwrite (text, 1, input_textlen, stdout);
  2180. +
  2181. + /* Overwrite remaining space, if any */
  2182. + if (ncolumns > input_textlen)
  2183. + printpad (spaces, n_spaces, ncolumns - input_textlen);
  2184. + }
  2185. + else
  2186. + {
  2187. + const guint textlen = MIN (input_textlen, ncolumns - bar_min);
  2188. + const guint barlen = MIN (MAX_PROGRESSBAR_COLUMNS, ncolumns - (textlen + 1));
  2189. +
  2190. + if (textlen > 0)
  2191. + {
  2192. + fwrite (text, 1, textlen, stdout);
  2193. + fputc (' ', stdout);
  2194. + }
  2195. +
  2196. + {
  2197. + const guint nbraces = 2;
  2198. + const guint textpercent_len = 5;
  2199. + const guint bar_internal_len = barlen - nbraces - textpercent_len;
  2200. + const guint eqlen = bar_internal_len * (percentage / 100.0);
  2201. + const guint spacelen = bar_internal_len - eqlen;
  2202. +
  2203. + fputc ('[', stdout);
  2204. + printpad (equals, n_equals, eqlen);
  2205. + printpad (spaces, n_spaces, spacelen);
  2206. + fputc (']', stdout);
  2207. + fprintf (stdout, " %3d%%", percentage);
  2208. + }
  2209. + }
  2210. +
  2211. + fflush (stdout);
  2212. +}
  2213. +
  2214. +/**
  2215. + * glnx_console_progress_text_percent:
  2216. + * @text: Show this text before the progress bar
  2217. + * @percentage: An integer in the range of 0 to 100
  2218. + *
  2219. + * On a tty, print to the console @text followed by an ASCII art
  2220. + * progress bar whose percentage is @percentage. If stdout is not a
  2221. + * tty, a more basic line by line change will be printed.
  2222. + *
  2223. + * You must have called glnx_console_lock() before invoking this
  2224. + * function.
  2225. + *
  2226. + */
  2227. +void
  2228. +glnx_console_progress_text_percent (const char *text,
  2229. + guint percentage)
  2230. +{
  2231. + g_return_if_fail (percentage <= 100);
  2232. +
  2233. + text_percent_internal (text, percentage);
  2234. +}
  2235. +
  2236. +/**
  2237. + * glnx_console_progress_n_items:
  2238. + * @text: Show this text before the progress bar
  2239. + * @current: An integer for how many items have been processed
  2240. + * @total: An integer for how many items there are total
  2241. + *
  2242. + * On a tty, print to the console @text followed by [@current/@total],
  2243. + * then an ASCII art progress bar, like glnx_console_progress_text_percent().
  2244. + *
  2245. + * You must have called glnx_console_lock() before invoking this
  2246. + * function.
  2247. + */
  2248. +void
  2249. +glnx_console_progress_n_items (const char *text,
  2250. + guint current,
  2251. + guint total)
  2252. +{
  2253. + g_return_if_fail (current <= total);
  2254. + g_return_if_fail (total > 0);
  2255. +
  2256. + g_autofree char *newtext = g_strdup_printf ("%s (%u/%u)", text, current, total);
  2257. + /* Special case current == total to ensure we end at 100% */
  2258. + int percentage = (current == total) ? 100 : (((double)current) / total * 100);
  2259. + glnx_console_progress_text_percent (newtext, percentage);
  2260. +}
  2261. +
  2262. +void
  2263. +glnx_console_text (const char *text)
  2264. +{
  2265. + text_percent_internal (text, -1);
  2266. +}
  2267. +
  2268. +/**
  2269. + * glnx_console_unlock:
  2270. + *
  2271. + * Print a newline, and reset all cached console progress state.
  2272. + *
  2273. + * This function does nothing if stdout is not a tty.
  2274. + */
  2275. +void
  2276. +glnx_console_unlock (GLnxConsoleRef *console)
  2277. +{
  2278. + g_return_if_fail (locked);
  2279. + g_return_if_fail (console->locked);
  2280. +
  2281. + if (console->is_tty)
  2282. + fputc ('\n', stdout);
  2283. +
  2284. + locked = console->locked = FALSE;
  2285. +}
  2286. diff -Nuar ostree-2018.8.orig/libglnx/glnx-console.h ostree-2018.8/libglnx/glnx-console.h
  2287. --- ostree-2018.8.orig/libglnx/glnx-console.h 1970-01-01 02:00:00.000000000 +0200
  2288. +++ ostree-2018.8/libglnx/glnx-console.h 2018-05-26 00:37:32.006018846 +0300
  2289. @@ -0,0 +1,61 @@
  2290. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  2291. + *
  2292. + * Copyright (C) 2013,2014,2015 Colin Walters <walters@verbum.org>
  2293. + *
  2294. + * This program is free software: you can redistribute it and/or modify
  2295. + * it under the terms of the GNU Lesser General Public License as published
  2296. + * by the Free Software Foundation; either version 2 of the licence or (at
  2297. + * your option) any later version.
  2298. + *
  2299. + * This library is distributed in the hope that it will be useful,
  2300. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2301. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2302. + * Lesser General Public License for more details.
  2303. + *
  2304. + * You should have received a copy of the GNU Lesser General
  2305. + * Public License along with this library; if not, write to the
  2306. + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  2307. + * Boston, MA 02111-1307, USA.
  2308. + */
  2309. +
  2310. +#pragma once
  2311. +
  2312. +#include <glnx-backport-autocleanups.h>
  2313. +
  2314. +G_BEGIN_DECLS
  2315. +
  2316. +struct GLnxConsoleRef {
  2317. + gboolean locked;
  2318. + gboolean is_tty;
  2319. +};
  2320. +
  2321. +typedef struct GLnxConsoleRef GLnxConsoleRef;
  2322. +
  2323. +gboolean glnx_stdout_is_tty (void);
  2324. +
  2325. +void glnx_console_lock (GLnxConsoleRef *ref);
  2326. +
  2327. +void glnx_console_text (const char *text);
  2328. +
  2329. +void glnx_console_progress_text_percent (const char *text,
  2330. + guint percentage);
  2331. +
  2332. +void glnx_console_progress_n_items (const char *text,
  2333. + guint current,
  2334. + guint total);
  2335. +
  2336. +void glnx_console_unlock (GLnxConsoleRef *ref);
  2337. +
  2338. +guint glnx_console_lines (void);
  2339. +
  2340. +guint glnx_console_columns (void);
  2341. +
  2342. +static inline void
  2343. +glnx_console_ref_cleanup (GLnxConsoleRef *p)
  2344. +{
  2345. + if (p->locked)
  2346. + glnx_console_unlock (p);
  2347. +}
  2348. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxConsoleRef, glnx_console_ref_cleanup)
  2349. +
  2350. +G_END_DECLS
  2351. diff -Nuar ostree-2018.8.orig/libglnx/glnx-dirfd.c ostree-2018.8/libglnx/glnx-dirfd.c
  2352. --- ostree-2018.8.orig/libglnx/glnx-dirfd.c 1970-01-01 02:00:00.000000000 +0200
  2353. +++ ostree-2018.8/libglnx/glnx-dirfd.c 2018-05-26 00:37:32.006018846 +0300
  2354. @@ -0,0 +1,425 @@
  2355. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  2356. + *
  2357. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  2358. + *
  2359. + * This library is free software; you can redistribute it and/or
  2360. + * modify it under the terms of the GNU Lesser General Public
  2361. + * License as published by the Free Software Foundation; either
  2362. + * version 2 of the License, or (at your option) any later version.
  2363. + *
  2364. + * This library is distributed in the hope that it will be useful,
  2365. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2366. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2367. + * Lesser General Public License for more details.
  2368. + *
  2369. + * You should have received a copy of the GNU Lesser General Public
  2370. + * License along with this library; if not, write to the
  2371. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  2372. + * Boston, MA 02111-1307, USA.
  2373. + */
  2374. +
  2375. +#include "config.h"
  2376. +
  2377. +#include <string.h>
  2378. +
  2379. +#include <glnx-dirfd.h>
  2380. +#include <glnx-fdio.h>
  2381. +#include <glnx-errors.h>
  2382. +#include <glnx-local-alloc.h>
  2383. +#include <glnx-shutil.h>
  2384. +
  2385. +/**
  2386. + * glnx_opendirat_with_errno:
  2387. + * @dfd: File descriptor for origin directory
  2388. + * @name: Pathname, relative to @dfd
  2389. + * @follow: Whether or not to follow symbolic links
  2390. + *
  2391. + * Use openat() to open a directory, using a standard set of flags.
  2392. + * This function sets errno.
  2393. + */
  2394. +int
  2395. +glnx_opendirat_with_errno (int dfd,
  2396. + const char *path,
  2397. + gboolean follow)
  2398. +{
  2399. + int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY;
  2400. + if (!follow)
  2401. + flags |= O_NOFOLLOW;
  2402. +
  2403. + dfd = glnx_dirfd_canonicalize (dfd);
  2404. +
  2405. + return openat (dfd, path, flags);
  2406. +}
  2407. +
  2408. +/**
  2409. + * glnx_opendirat:
  2410. + * @dfd: File descriptor for origin directory
  2411. + * @path: Pathname, relative to @dfd
  2412. + * @follow: Whether or not to follow symbolic links
  2413. + * @error: Error
  2414. + *
  2415. + * Use openat() to open a directory, using a standard set of flags.
  2416. + */
  2417. +gboolean
  2418. +glnx_opendirat (int dfd,
  2419. + const char *path,
  2420. + gboolean follow,
  2421. + int *out_fd,
  2422. + GError **error)
  2423. +{
  2424. + int ret = glnx_opendirat_with_errno (dfd, path, follow);
  2425. + if (ret == -1)
  2426. + return glnx_throw_errno_prefix (error, "opendir(%s)", path);
  2427. + *out_fd = ret;
  2428. + return TRUE;
  2429. +}
  2430. +
  2431. +struct GLnxRealDirfdIterator
  2432. +{
  2433. + gboolean initialized;
  2434. + int fd;
  2435. + DIR *d;
  2436. +};
  2437. +typedef struct GLnxRealDirfdIterator GLnxRealDirfdIterator;
  2438. +
  2439. +/**
  2440. + * glnx_dirfd_iterator_init_at:
  2441. + * @dfd: File descriptor, may be AT_FDCWD or -1
  2442. + * @path: Path, may be relative to @dfd
  2443. + * @follow: If %TRUE and the last component of @path is a symlink, follow it
  2444. + * @out_dfd_iter: (out caller-allocates): A directory iterator, will be initialized
  2445. + * @error: Error
  2446. + *
  2447. + * Initialize @out_dfd_iter from @dfd and @path.
  2448. + */
  2449. +gboolean
  2450. +glnx_dirfd_iterator_init_at (int dfd,
  2451. + const char *path,
  2452. + gboolean follow,
  2453. + GLnxDirFdIterator *out_dfd_iter,
  2454. + GError **error)
  2455. +{
  2456. + glnx_autofd int fd = -1;
  2457. + if (!glnx_opendirat (dfd, path, follow, &fd, error))
  2458. + return FALSE;
  2459. +
  2460. + if (!glnx_dirfd_iterator_init_take_fd (&fd, out_dfd_iter, error))
  2461. + return FALSE;
  2462. +
  2463. + return TRUE;
  2464. +}
  2465. +
  2466. +/**
  2467. + * glnx_dirfd_iterator_init_take_fd:
  2468. + * @dfd: File descriptor - ownership is taken, and the value is set to -1
  2469. + * @dfd_iter: A directory iterator
  2470. + * @error: Error
  2471. + *
  2472. + * Steal ownership of @dfd, using it to initialize @dfd_iter for
  2473. + * iteration.
  2474. + */
  2475. +gboolean
  2476. +glnx_dirfd_iterator_init_take_fd (int *dfd,
  2477. + GLnxDirFdIterator *dfd_iter,
  2478. + GError **error)
  2479. +{
  2480. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  2481. + DIR *d = fdopendir (*dfd);
  2482. + if (!d)
  2483. + return glnx_throw_errno_prefix (error, "fdopendir");
  2484. +
  2485. + real_dfd_iter->fd = glnx_steal_fd (dfd);
  2486. + real_dfd_iter->d = d;
  2487. + real_dfd_iter->initialized = TRUE;
  2488. +
  2489. + return TRUE;
  2490. +}
  2491. +
  2492. +/**
  2493. + * glnx_dirfd_iterator_next_dent:
  2494. + * @dfd_iter: A directory iterator
  2495. + * @out_dent: (out) (transfer none): Pointer to dirent; do not free
  2496. + * @cancellable: Cancellable
  2497. + * @error: Error
  2498. + *
  2499. + * Read the next value from @dfd_iter, causing @out_dent to be
  2500. + * updated. If end of stream is reached, @out_dent will be set
  2501. + * to %NULL, and %TRUE will be returned.
  2502. + */
  2503. +gboolean
  2504. +glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter,
  2505. + struct dirent **out_dent,
  2506. + GCancellable *cancellable,
  2507. + GError **error)
  2508. +{
  2509. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  2510. +
  2511. + g_return_val_if_fail (out_dent, FALSE);
  2512. + g_return_val_if_fail (dfd_iter->initialized, FALSE);
  2513. +
  2514. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  2515. + return FALSE;
  2516. +
  2517. + do
  2518. + {
  2519. + errno = 0;
  2520. + *out_dent = readdir (real_dfd_iter->d);
  2521. + if (*out_dent == NULL && errno != 0)
  2522. + return glnx_throw_errno_prefix (error, "readdir");
  2523. + } while (*out_dent &&
  2524. + (strcmp ((*out_dent)->d_name, ".") == 0 ||
  2525. + strcmp ((*out_dent)->d_name, "..") == 0));
  2526. +
  2527. + return TRUE;
  2528. +}
  2529. +
  2530. +/**
  2531. + * glnx_dirfd_iterator_next_dent_ensure_dtype:
  2532. + * @dfd_iter: A directory iterator
  2533. + * @out_dent: (out) (transfer none): Pointer to dirent; do not free
  2534. + * @cancellable: Cancellable
  2535. + * @error: Error
  2536. + *
  2537. + * A variant of @glnx_dirfd_iterator_next_dent, which will ensure the
  2538. + * `dent->d_type` member is filled in by calling `fstatat`
  2539. + * automatically if the underlying filesystem type sets `DT_UNKNOWN`.
  2540. + */
  2541. +gboolean
  2542. +glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator *dfd_iter,
  2543. + struct dirent **out_dent,
  2544. + GCancellable *cancellable,
  2545. + GError **error)
  2546. +{
  2547. + g_return_val_if_fail (out_dent, FALSE);
  2548. +
  2549. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, out_dent, cancellable, error))
  2550. + return FALSE;
  2551. +
  2552. + struct dirent *ret_dent = *out_dent;
  2553. + if (ret_dent)
  2554. + {
  2555. +
  2556. + if (ret_dent->d_type == DT_UNKNOWN)
  2557. + {
  2558. + struct stat stbuf;
  2559. + if (!glnx_fstatat (dfd_iter->fd, ret_dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW, error))
  2560. + return FALSE;
  2561. + ret_dent->d_type = IFTODT (stbuf.st_mode);
  2562. + }
  2563. + }
  2564. +
  2565. + return TRUE;
  2566. +}
  2567. +
  2568. +/**
  2569. + * glnx_dirfd_iterator_clear:
  2570. + * @dfd_iter: Iterator, will be de-initialized
  2571. + *
  2572. + * Unset @dfd_iter, freeing any resources. If @dfd_iter is not
  2573. + * initialized, do nothing.
  2574. + */
  2575. +void
  2576. +glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter)
  2577. +{
  2578. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  2579. + /* fd is owned by dfd_iter */
  2580. + if (!real_dfd_iter->initialized)
  2581. + return;
  2582. + (void) closedir (real_dfd_iter->d);
  2583. + real_dfd_iter->initialized = FALSE;
  2584. +}
  2585. +
  2586. +/**
  2587. + * glnx_fdrel_abspath:
  2588. + * @dfd: Directory fd
  2589. + * @path: Path
  2590. + *
  2591. + * Turn a fd-relative pair into something that can be used for legacy
  2592. + * APIs expecting absolute paths.
  2593. + *
  2594. + * This is Linux specific, and only valid inside this process (unless
  2595. + * you set up the child process to have the exact same fd number, but
  2596. + * don't try that).
  2597. + */
  2598. +char *
  2599. +glnx_fdrel_abspath (int dfd,
  2600. + const char *path)
  2601. +{
  2602. + dfd = glnx_dirfd_canonicalize (dfd);
  2603. + if (dfd == AT_FDCWD)
  2604. + return g_strdup (path);
  2605. + return g_strdup_printf ("/proc/self/fd/%d/%s", dfd, path);
  2606. +}
  2607. +
  2608. +/**
  2609. + * glnx_gen_temp_name:
  2610. + * @tmpl: (type filename): template directory name, the last 6 characters will be replaced
  2611. + *
  2612. + * Replace the last 6 characters of @tmpl with random ASCII. You must
  2613. + * use this in combination with a mechanism to ensure race-free file
  2614. + * creation such as `O_EXCL`.
  2615. + */
  2616. +void
  2617. +glnx_gen_temp_name (gchar *tmpl)
  2618. +{
  2619. + g_return_if_fail (tmpl != NULL);
  2620. + const size_t len = strlen (tmpl);
  2621. + g_return_if_fail (len >= 6);
  2622. +
  2623. + static const char letters[] =
  2624. + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  2625. + static const int NLETTERS = sizeof (letters) - 1;
  2626. +
  2627. + char *XXXXXX = tmpl + (len - 6);
  2628. + for (int i = 0; i < 6; i++)
  2629. + XXXXXX[i] = letters[g_random_int_range(0, NLETTERS)];
  2630. +}
  2631. +
  2632. +/**
  2633. + * glnx_mkdtempat:
  2634. + * @dfd: Directory fd
  2635. + * @tmpl: (type filename): Initial template directory name, last 6 characters will be replaced
  2636. + * @mode: permissions with which to create the temporary directory
  2637. + * @out_tmpdir: (out caller-allocates): Initialized tempdir structure
  2638. + * @error: Error
  2639. + *
  2640. + * Somewhat similar to g_mkdtemp_full(), but fd-relative, and returns a
  2641. + * structure that uses autocleanups. Note that the supplied @dfd lifetime
  2642. + * must match or exceed that of @out_tmpdir in order to remove the directory.
  2643. + */
  2644. +gboolean
  2645. +glnx_mkdtempat (int dfd, const char *tmpl, int mode,
  2646. + GLnxTmpDir *out_tmpdir, GError **error)
  2647. +{
  2648. + g_return_val_if_fail (tmpl != NULL, FALSE);
  2649. + g_return_val_if_fail (out_tmpdir != NULL, FALSE);
  2650. + g_return_val_if_fail (!out_tmpdir->initialized, FALSE);
  2651. +
  2652. + dfd = glnx_dirfd_canonicalize (dfd);
  2653. +
  2654. + g_autofree char *path = g_strdup (tmpl);
  2655. + for (int count = 0; count < 100; count++)
  2656. + {
  2657. + glnx_gen_temp_name (path);
  2658. +
  2659. + /* Ideally we could use openat(O_DIRECTORY | O_CREAT | O_EXCL) here
  2660. + * to create and open the directory atomically, but that’s not supported by
  2661. + * current kernel versions: http://www.openwall.com/lists/oss-security/2014/11/26/14
  2662. + * (Tested on kernel 4.10.10-200.fc25.x86_64). For the moment, accept a
  2663. + * TOCTTOU race here. */
  2664. + if (mkdirat (dfd, path, mode) == -1)
  2665. + {
  2666. + if (errno == EEXIST)
  2667. + continue;
  2668. +
  2669. + /* Any other error will apply also to other names we might
  2670. + * try, and there are 2^32 or so of them, so give up now.
  2671. + */
  2672. + return glnx_throw_errno_prefix (error, "mkdirat");
  2673. + }
  2674. +
  2675. + /* And open it */
  2676. + glnx_autofd int ret_dfd = -1;
  2677. + if (!glnx_opendirat (dfd, path, FALSE, &ret_dfd, error))
  2678. + {
  2679. + /* If we fail to open, let's try to clean up */
  2680. + (void)unlinkat (dfd, path, AT_REMOVEDIR);
  2681. + return FALSE;
  2682. + }
  2683. +
  2684. + /* Return the initialized directory struct */
  2685. + out_tmpdir->initialized = TRUE;
  2686. + out_tmpdir->src_dfd = dfd; /* referenced; see above docs */
  2687. + out_tmpdir->fd = glnx_steal_fd (&ret_dfd);
  2688. + out_tmpdir->path = g_steal_pointer (&path);
  2689. + return TRUE;
  2690. + }
  2691. +
  2692. + /* Failure */
  2693. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  2694. + "glnx_mkdtempat ran out of combinations to try");
  2695. + return FALSE;
  2696. +}
  2697. +
  2698. +/**
  2699. + * glnx_mkdtemp:
  2700. + * @tmpl: (type filename): Source template directory name, last 6 characters will be replaced
  2701. + * @mode: permissions to create the temporary directory with
  2702. + * @out_tmpdir: (out caller-allocates): Return location for tmpdir data
  2703. + * @error: Return location for a #GError, or %NULL
  2704. + *
  2705. + * Similar to glnx_mkdtempat(), but will use g_get_tmp_dir() as the parent
  2706. + * directory to @tmpl.
  2707. + *
  2708. + * Returns: %TRUE on success, %FALSE otherwise
  2709. + * Since: UNRELEASED
  2710. + */
  2711. +gboolean
  2712. +glnx_mkdtemp (const gchar *tmpl,
  2713. + int mode,
  2714. + GLnxTmpDir *out_tmpdir,
  2715. + GError **error)
  2716. +{
  2717. + g_autofree char *path = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
  2718. + return glnx_mkdtempat (AT_FDCWD, path, mode,
  2719. + out_tmpdir, error);
  2720. +}
  2721. +
  2722. +static gboolean
  2723. +_glnx_tmpdir_free (GLnxTmpDir *tmpd,
  2724. + gboolean delete_dir,
  2725. + GCancellable *cancellable,
  2726. + GError **error)
  2727. +{
  2728. + /* Support being passed NULL so we work nicely in a GPtrArray */
  2729. + if (!(tmpd && tmpd->initialized))
  2730. + return TRUE;
  2731. + g_assert_cmpint (tmpd->fd, !=, -1);
  2732. + glnx_close_fd (&tmpd->fd);
  2733. + g_assert (tmpd->path);
  2734. + g_assert_cmpint (tmpd->src_dfd, !=, -1);
  2735. + g_autofree char *path = tmpd->path; /* Take ownership */
  2736. + tmpd->initialized = FALSE;
  2737. + if (delete_dir)
  2738. + {
  2739. + if (!glnx_shutil_rm_rf_at (tmpd->src_dfd, path, cancellable, error))
  2740. + return FALSE;
  2741. + }
  2742. + return TRUE;
  2743. +}
  2744. +
  2745. +/**
  2746. + * glnx_tmpdir_delete:
  2747. + * @tmpf: Temporary dir
  2748. + * @cancellable: Cancellable
  2749. + * @error: Error
  2750. + *
  2751. + * Deallocate a tmpdir, closing the fd and recursively deleting the path. This
  2752. + * is normally called indirectly via glnx_tmpdir_cleanup() by the autocleanup
  2753. + * attribute, but you can also invoke this directly.
  2754. + *
  2755. + * If an error occurs while deleting the filesystem path, @tmpf will still have
  2756. + * been deallocated and should not be reused.
  2757. + *
  2758. + * See also `glnx_tmpdir_unset` to avoid deleting the path.
  2759. + */
  2760. +gboolean
  2761. +glnx_tmpdir_delete (GLnxTmpDir *tmpf, GCancellable *cancellable, GError **error)
  2762. +{
  2763. + return _glnx_tmpdir_free (tmpf, TRUE, cancellable, error);
  2764. +}
  2765. +
  2766. +/**
  2767. + * glnx_tmpdir_unset:
  2768. + * @tmpf: Temporary dir
  2769. + * @cancellable: Cancellable
  2770. + * @error: Error
  2771. + *
  2772. + * Deallocate a tmpdir, but do not delete the filesystem path. See also
  2773. + * `glnx_tmpdir_delete()`.
  2774. + */
  2775. +void
  2776. +glnx_tmpdir_unset (GLnxTmpDir *tmpf)
  2777. +{
  2778. + (void) _glnx_tmpdir_free (tmpf, FALSE, NULL, NULL);
  2779. +}
  2780. diff -Nuar ostree-2018.8.orig/libglnx/glnx-dirfd.h ostree-2018.8/libglnx/glnx-dirfd.h
  2781. --- ostree-2018.8.orig/libglnx/glnx-dirfd.h 1970-01-01 02:00:00.000000000 +0200
  2782. +++ ostree-2018.8/libglnx/glnx-dirfd.h 2018-05-26 00:37:32.006018846 +0300
  2783. @@ -0,0 +1,137 @@
  2784. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  2785. + *
  2786. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  2787. + *
  2788. + * This library is free software; you can redistribute it and/or
  2789. + * modify it under the terms of the GNU Lesser General Public
  2790. + * License as published by the Free Software Foundation; either
  2791. + * version 2 of the License, or (at your option) any later version.
  2792. + *
  2793. + * This library is distributed in the hope that it will be useful,
  2794. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2795. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2796. + * Lesser General Public License for more details.
  2797. + *
  2798. + * You should have received a copy of the GNU Lesser General Public
  2799. + * License along with this library; if not, write to the
  2800. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  2801. + * Boston, MA 02111-1307, USA.
  2802. + */
  2803. +
  2804. +#pragma once
  2805. +
  2806. +#include <glnx-backport-autocleanups.h>
  2807. +#include <glnx-macros.h>
  2808. +#include <glnx-errors.h>
  2809. +#include <limits.h>
  2810. +#include <dirent.h>
  2811. +#include <sys/stat.h>
  2812. +#include <fcntl.h>
  2813. +
  2814. +G_BEGIN_DECLS
  2815. +
  2816. +/**
  2817. + * glnx_dirfd_canonicalize:
  2818. + * @fd: A directory file descriptor
  2819. + *
  2820. + * It's often convenient in programs to use `-1` for "unassigned fd",
  2821. + * and also because gobject-introspection doesn't support `AT_FDCWD`,
  2822. + * libglnx honors `-1` to mean `AT_FDCWD`. This small inline function
  2823. + * canonicalizes `-1 -> AT_FDCWD`.
  2824. + */
  2825. +static inline int
  2826. +glnx_dirfd_canonicalize (int fd)
  2827. +{
  2828. + if (fd == -1)
  2829. + return AT_FDCWD;
  2830. + return fd;
  2831. +}
  2832. +
  2833. +struct GLnxDirFdIterator {
  2834. + gboolean initialized;
  2835. + int fd;
  2836. + gpointer padding_data[4];
  2837. +};
  2838. +
  2839. +typedef struct GLnxDirFdIterator GLnxDirFdIterator;
  2840. +gboolean glnx_dirfd_iterator_init_at (int dfd, const char *path,
  2841. + gboolean follow,
  2842. + GLnxDirFdIterator *dfd_iter, GError **error);
  2843. +gboolean glnx_dirfd_iterator_init_take_fd (int *dfd, GLnxDirFdIterator *dfd_iter, GError **error);
  2844. +gboolean glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter,
  2845. + struct dirent **out_dent,
  2846. + GCancellable *cancellable,
  2847. + GError **error);
  2848. +gboolean glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator *dfd_iter,
  2849. + struct dirent **out_dent,
  2850. + GCancellable *cancellable,
  2851. + GError **error);
  2852. +void glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter);
  2853. +
  2854. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxDirFdIterator, glnx_dirfd_iterator_clear)
  2855. +
  2856. +int glnx_opendirat_with_errno (int dfd,
  2857. + const char *path,
  2858. + gboolean follow);
  2859. +
  2860. +gboolean glnx_opendirat (int dfd,
  2861. + const char *path,
  2862. + gboolean follow,
  2863. + int *out_fd,
  2864. + GError **error);
  2865. +
  2866. +char *glnx_fdrel_abspath (int dfd,
  2867. + const char *path);
  2868. +
  2869. +void glnx_gen_temp_name (gchar *tmpl);
  2870. +
  2871. +/**
  2872. + * glnx_ensure_dir:
  2873. + * @dfd: directory fd
  2874. + * @path: Directory path
  2875. + * @mode: Mode
  2876. + * @error: Return location for a #GError, or %NULL
  2877. + *
  2878. + * Wrapper around mkdirat() which adds #GError support, ensures that
  2879. + * it retries on %EINTR, and also ignores `EEXIST`.
  2880. + *
  2881. + * See also `glnx_shutil_mkdir_p_at()` for recursive handling.
  2882. + *
  2883. + * Returns: %TRUE on success, %FALSE otherwise
  2884. + */
  2885. +static inline gboolean
  2886. +glnx_ensure_dir (int dfd,
  2887. + const char *path,
  2888. + mode_t mode,
  2889. + GError **error)
  2890. +{
  2891. + if (TEMP_FAILURE_RETRY (mkdirat (dfd, path, mode)) != 0)
  2892. + {
  2893. + if (G_UNLIKELY (errno != EEXIST))
  2894. + return glnx_throw_errno_prefix (error, "mkdirat(%s)", path);
  2895. + }
  2896. + return TRUE;
  2897. +}
  2898. +
  2899. +typedef struct {
  2900. + gboolean initialized;
  2901. + int src_dfd;
  2902. + int fd;
  2903. + char *path;
  2904. +} GLnxTmpDir;
  2905. +gboolean glnx_tmpdir_delete (GLnxTmpDir *tmpf, GCancellable *cancellable, GError **error);
  2906. +void glnx_tmpdir_unset (GLnxTmpDir *tmpf);
  2907. +static inline void
  2908. +glnx_tmpdir_cleanup (GLnxTmpDir *tmpf)
  2909. +{
  2910. + (void)glnx_tmpdir_delete (tmpf, NULL, NULL);
  2911. +}
  2912. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxTmpDir, glnx_tmpdir_cleanup)
  2913. +
  2914. +gboolean glnx_mkdtempat (int dfd, const char *tmpl, int mode,
  2915. + GLnxTmpDir *out_tmpdir, GError **error);
  2916. +
  2917. +gboolean glnx_mkdtemp (const char *tmpl, int mode,
  2918. + GLnxTmpDir *out_tmpdir, GError **error);
  2919. +
  2920. +G_END_DECLS
  2921. diff -Nuar ostree-2018.8.orig/libglnx/glnx-errors.c ostree-2018.8/libglnx/glnx-errors.c
  2922. --- ostree-2018.8.orig/libglnx/glnx-errors.c 1970-01-01 02:00:00.000000000 +0200
  2923. +++ ostree-2018.8/libglnx/glnx-errors.c 2018-05-26 00:37:32.006018846 +0300
  2924. @@ -0,0 +1,131 @@
  2925. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  2926. + *
  2927. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  2928. + *
  2929. + * This library is free software; you can redistribute it and/or
  2930. + * modify it under the terms of the GNU Lesser General Public
  2931. + * License as published by the Free Software Foundation; either
  2932. + * version 2 of the License, or (at your option) any later version.
  2933. + *
  2934. + * This library is distributed in the hope that it will be useful,
  2935. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2936. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2937. + * Lesser General Public License for more details.
  2938. + *
  2939. + * You should have received a copy of the GNU Lesser General Public
  2940. + * License along with this library; if not, write to the
  2941. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  2942. + * Boston, MA 02111-1307, USA.
  2943. + */
  2944. +
  2945. +#include "config.h"
  2946. +
  2947. +#include <glnx-backport-autocleanups.h>
  2948. +#include <glnx-errors.h>
  2949. +
  2950. +/* Set @error with G_IO_ERROR/G_IO_ERROR_FAILED.
  2951. + *
  2952. + * This function returns %FALSE so it can be used conveniently in a single
  2953. + * statement:
  2954. + *
  2955. + * ```
  2956. + * if (strcmp (foo, "somevalue") != 0)
  2957. + * return glnx_throw (error, "key must be somevalue, not '%s'", foo);
  2958. + * ```
  2959. + */
  2960. +gboolean
  2961. +glnx_throw (GError **error,
  2962. + const char *fmt,
  2963. + ...)
  2964. +{
  2965. + if (error == NULL)
  2966. + return FALSE;
  2967. +
  2968. + va_list args;
  2969. + va_start (args, fmt);
  2970. + GError *new = g_error_new_valist (G_IO_ERROR, G_IO_ERROR_FAILED, fmt, args);
  2971. + va_end (args);
  2972. + g_propagate_error (error, g_steal_pointer (&new));
  2973. + return FALSE;
  2974. +}
  2975. +
  2976. +void
  2977. +glnx_real_set_prefix_error_va (GError *error,
  2978. + const char *format,
  2979. + va_list args)
  2980. +{
  2981. + if (error == NULL)
  2982. + return;
  2983. +
  2984. + g_autofree char *old_msg = g_steal_pointer (&error->message);
  2985. + g_autoptr(GString) buf = g_string_new ("");
  2986. + g_string_append_vprintf (buf, format, args);
  2987. + g_string_append (buf, ": ");
  2988. + g_string_append (buf, old_msg);
  2989. + error->message = g_string_free (g_steal_pointer (&buf), FALSE);
  2990. +}
  2991. +
  2992. +/* Prepend to @error's message by `$prefix: ` where `$prefix` is computed via
  2993. + * printf @fmt. Returns %FALSE so it can be used conveniently in a single
  2994. + * statement:
  2995. + *
  2996. + * ```
  2997. + * if (!function_that_fails (s, error))
  2998. + * return glnx_throw_prefix (error, "while handling '%s'", s);
  2999. + * ```
  3000. + * */
  3001. +gboolean
  3002. +glnx_prefix_error (GError **error,
  3003. + const char *fmt,
  3004. + ...)
  3005. +{
  3006. + if (error == NULL)
  3007. + return FALSE;
  3008. +
  3009. + va_list args;
  3010. + va_start (args, fmt);
  3011. + glnx_real_set_prefix_error_va (*error, fmt, args);
  3012. + va_end (args);
  3013. + return FALSE;
  3014. +}
  3015. +
  3016. +void
  3017. +glnx_real_set_prefix_error_from_errno_va (GError **error,
  3018. + gint errsv,
  3019. + const char *format,
  3020. + va_list args)
  3021. +{
  3022. + if (!error)
  3023. + return;
  3024. +
  3025. + g_set_error_literal (error,
  3026. + G_IO_ERROR,
  3027. + g_io_error_from_errno (errsv),
  3028. + g_strerror (errsv));
  3029. + glnx_real_set_prefix_error_va (*error, format, args);
  3030. +}
  3031. +
  3032. +/* Set @error using the value of `$prefix: g_strerror (errno)` where `$prefix`
  3033. + * is computed via printf @fmt.
  3034. + *
  3035. + * This function returns %FALSE so it can be used conveniently in a single
  3036. + * statement:
  3037. + *
  3038. + * ```
  3039. + * return glnx_throw_errno_prefix (error, "unlinking %s", pathname);
  3040. + * ```
  3041. + */
  3042. +gboolean
  3043. +glnx_throw_errno_prefix (GError **error,
  3044. + const char *fmt,
  3045. + ...)
  3046. +{
  3047. + int errsv = errno;
  3048. + va_list args;
  3049. + va_start (args, fmt);
  3050. + glnx_real_set_prefix_error_from_errno_va (error, errsv, fmt, args);
  3051. + va_end (args);
  3052. + /* See comment in glnx_throw_errno() about preserving errno */
  3053. + errno = errsv;
  3054. + return FALSE;
  3055. +}
  3056. diff -Nuar ostree-2018.8.orig/libglnx/glnx-errors.h ostree-2018.8/libglnx/glnx-errors.h
  3057. --- ostree-2018.8.orig/libglnx/glnx-errors.h 1970-01-01 02:00:00.000000000 +0200
  3058. +++ ostree-2018.8/libglnx/glnx-errors.h 2018-05-26 00:37:32.006018846 +0300
  3059. @@ -0,0 +1,134 @@
  3060. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3061. + *
  3062. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  3063. + *
  3064. + * This library is free software; you can redistribute it and/or
  3065. + * modify it under the terms of the GNU Lesser General Public
  3066. + * License as published by the Free Software Foundation; either
  3067. + * version 2 of the License, or (at your option) any later version.
  3068. + *
  3069. + * This library is distributed in the hope that it will be useful,
  3070. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3071. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3072. + * Lesser General Public License for more details.
  3073. + *
  3074. + * You should have received a copy of the GNU Lesser General Public
  3075. + * License along with this library; if not, write to the
  3076. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3077. + * Boston, MA 02111-1307, USA.
  3078. + */
  3079. +
  3080. +#pragma once
  3081. +
  3082. +#include <glnx-backport-autocleanups.h>
  3083. +#include <errno.h>
  3084. +
  3085. +G_BEGIN_DECLS
  3086. +
  3087. +gboolean glnx_throw (GError **error, const char *fmt, ...) G_GNUC_PRINTF (2,3);
  3088. +
  3089. +/* Like `glnx_throw ()`, but returns %NULL. */
  3090. +#define glnx_null_throw(error, args...) \
  3091. + ({glnx_throw (error, args); NULL;})
  3092. +
  3093. +/* Implementation detail of glnx_throw_prefix() */
  3094. +void glnx_real_set_prefix_error_va (GError *error,
  3095. + const char *format,
  3096. + va_list args) G_GNUC_PRINTF (2,0);
  3097. +
  3098. +gboolean glnx_prefix_error (GError **error, const char *fmt, ...) G_GNUC_PRINTF (2,3);
  3099. +
  3100. +/* Like `glnx_prefix_error ()`, but returns %NULL. */
  3101. +#define glnx_prefix_error_null(error, args...) \
  3102. + ({glnx_prefix_error (error, args); NULL;})
  3103. +
  3104. +/**
  3105. + * GLNX_AUTO_PREFIX_ERROR:
  3106. + *
  3107. + * An autocleanup-based macro to automatically call `g_prefix_error()` (also with a colon+space `: `)
  3108. + * when it goes out of scope. This is useful when one wants error strings built up by the callee
  3109. + * function, not all callers.
  3110. + *
  3111. + * ```
  3112. + * gboolean start_http_request (..., GError **error)
  3113. + * {
  3114. + * GLNX_AUTO_PREFIX_ERROR ("HTTP request", error)
  3115. + *
  3116. + * if (!libhttp_request_start (..., error))
  3117. + * return FALSE;
  3118. + * ...
  3119. + * return TRUE;
  3120. + * ```
  3121. + */
  3122. +typedef struct {
  3123. + const char *prefix;
  3124. + GError **error;
  3125. +} GLnxAutoErrorPrefix;
  3126. +static inline void
  3127. +glnx_cleanup_auto_prefix_error (GLnxAutoErrorPrefix *prefix)
  3128. +{
  3129. + if (prefix->error && *(prefix->error))
  3130. + g_prefix_error (prefix->error, "%s: ", prefix->prefix);
  3131. +}
  3132. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxAutoErrorPrefix, glnx_cleanup_auto_prefix_error)
  3133. +#define GLNX_AUTO_PREFIX_ERROR(text, error) \
  3134. + G_GNUC_UNUSED g_auto(GLnxAutoErrorPrefix) _GLNX_MAKE_ANONYMOUS(_glnxautoprefixerror_) = { text, error }
  3135. +
  3136. +/* Set @error using the value of `g_strerror (errno)`.
  3137. + *
  3138. + * This function returns %FALSE so it can be used conveniently in a single
  3139. + * statement:
  3140. + *
  3141. + * ```
  3142. + * if (unlinkat (fd, somepathname) < 0)
  3143. + * return glnx_throw_errno (error);
  3144. + * ```
  3145. + */
  3146. +static inline gboolean
  3147. +glnx_throw_errno (GError **error)
  3148. +{
  3149. + /* Save the value of errno, in case one of the
  3150. + * intermediate function calls happens to set it.
  3151. + */
  3152. + int errsv = errno;
  3153. + g_set_error_literal (error, G_IO_ERROR,
  3154. + g_io_error_from_errno (errsv),
  3155. + g_strerror (errsv));
  3156. + /* We also restore the value of errno, since that's
  3157. + * what was done in a long-ago libgsystem commit
  3158. + * https://git.gnome.org/browse/libgsystem/commit/?id=ed106741f7a0596dc8b960b31fdae671d31d666d
  3159. + * but I certainly can't remember now why I did that.
  3160. + */
  3161. + errno = errsv;
  3162. + return FALSE;
  3163. +}
  3164. +
  3165. +/* Like glnx_throw_errno(), but yields a NULL pointer. */
  3166. +#define glnx_null_throw_errno(error) \
  3167. + ({glnx_throw_errno (error); NULL;})
  3168. +
  3169. +/* Implementation detail of glnx_throw_errno_prefix() */
  3170. +void glnx_real_set_prefix_error_from_errno_va (GError **error,
  3171. + gint errsv,
  3172. + const char *format,
  3173. + va_list args) G_GNUC_PRINTF (3,0);
  3174. +
  3175. +gboolean glnx_throw_errno_prefix (GError **error, const char *fmt, ...) G_GNUC_PRINTF (2,3);
  3176. +
  3177. +/* Like glnx_throw_errno_prefix(), but yields a NULL pointer. */
  3178. +#define glnx_null_throw_errno_prefix(error, args...) \
  3179. + ({glnx_throw_errno_prefix (error, args); NULL;})
  3180. +
  3181. +/* BEGIN LEGACY APIS */
  3182. +
  3183. +#define glnx_set_error_from_errno(error) \
  3184. + do { \
  3185. + glnx_throw_errno (error); \
  3186. + } while (0);
  3187. +
  3188. +#define glnx_set_prefix_error_from_errno(error, format, args...) \
  3189. + do { \
  3190. + glnx_throw_errno_prefix (error, format, args); \
  3191. + } while (0);
  3192. +
  3193. +G_END_DECLS
  3194. diff -Nuar ostree-2018.8.orig/libglnx/glnx-fdio.c ostree-2018.8/libglnx/glnx-fdio.c
  3195. --- ostree-2018.8.orig/libglnx/glnx-fdio.c 1970-01-01 02:00:00.000000000 +0200
  3196. +++ ostree-2018.8/libglnx/glnx-fdio.c 2018-05-26 00:37:32.007018846 +0300
  3197. @@ -0,0 +1,1106 @@
  3198. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3199. + *
  3200. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  3201. + *
  3202. + * Portions derived from systemd:
  3203. + * Copyright 2010 Lennart Poettering
  3204. + *
  3205. + * This library is free software; you can redistribute it and/or
  3206. + * modify it under the terms of the GNU Lesser General Public
  3207. + * License as published by the Free Software Foundation; either
  3208. + * version 2 of the License, or (at your option) any later version.
  3209. + *
  3210. + * This library is distributed in the hope that it will be useful,
  3211. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3212. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3213. + * Lesser General Public License for more details.
  3214. + *
  3215. + * You should have received a copy of the GNU Lesser General Public
  3216. + * License along with this library; if not, write to the
  3217. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3218. + * Boston, MA 02111-1307, USA.
  3219. + */
  3220. +
  3221. +#include "config.h"
  3222. +
  3223. +#include <string.h>
  3224. +#include <stdio.h>
  3225. +#include <stdlib.h>
  3226. +#include <stdint.h>
  3227. +#include <stdbool.h>
  3228. +#include <sys/ioctl.h>
  3229. +#include <sys/sendfile.h>
  3230. +#include <errno.h>
  3231. +
  3232. +#include <glnx-fdio.h>
  3233. +#include <glnx-dirfd.h>
  3234. +#include <glnx-errors.h>
  3235. +#include <glnx-xattrs.h>
  3236. +#include <glnx-backport-autoptr.h>
  3237. +#include <glnx-local-alloc.h>
  3238. +#include <glnx-missing.h>
  3239. +
  3240. +/* The standardized version of BTRFS_IOC_CLONE */
  3241. +#ifndef FICLONE
  3242. +#define FICLONE _IOW(0x94, 9, int)
  3243. +#endif
  3244. +
  3245. +/* Returns the number of chars needed to format variables of the
  3246. + * specified type as a decimal string. Adds in extra space for a
  3247. + * negative '-' prefix (hence works correctly on signed
  3248. + * types). Includes space for the trailing NUL. */
  3249. +#define DECIMAL_STR_MAX(type) \
  3250. + (2+(sizeof(type) <= 1 ? 3 : \
  3251. + sizeof(type) <= 2 ? 5 : \
  3252. + sizeof(type) <= 4 ? 10 : \
  3253. + sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
  3254. +
  3255. +gboolean
  3256. +glnx_stdio_file_flush (FILE *f, GError **error)
  3257. +{
  3258. + if (fflush (f) != 0)
  3259. + return glnx_throw_errno_prefix (error, "fflush");
  3260. + if (ferror (f) != 0)
  3261. + return glnx_throw_errno_prefix (error, "ferror");
  3262. + return TRUE;
  3263. +}
  3264. +
  3265. +/* An implementation of renameat2(..., RENAME_NOREPLACE)
  3266. + * with fallback to a non-atomic version.
  3267. + */
  3268. +int
  3269. +glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
  3270. + int newdirfd, const char *newpath)
  3271. +{
  3272. +#ifndef ENABLE_WRPSEUDO_COMPAT
  3273. + if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE) < 0)
  3274. + {
  3275. + if (G_IN_SET(errno, EINVAL, ENOSYS))
  3276. + {
  3277. + /* Fall through */
  3278. + }
  3279. + else
  3280. + {
  3281. + return -1;
  3282. + }
  3283. + }
  3284. + else
  3285. + return TRUE;
  3286. +#endif
  3287. +
  3288. + if (linkat (olddirfd, oldpath, newdirfd, newpath, 0) < 0)
  3289. + return -1;
  3290. +
  3291. + if (unlinkat (olddirfd, oldpath, 0) < 0)
  3292. + return -1;
  3293. +
  3294. + return 0;
  3295. +}
  3296. +
  3297. +static gboolean
  3298. +rename_file_noreplace_at (int olddirfd, const char *oldpath,
  3299. + int newdirfd, const char *newpath,
  3300. + gboolean ignore_eexist,
  3301. + GError **error)
  3302. +{
  3303. + if (glnx_renameat2_noreplace (olddirfd, oldpath,
  3304. + newdirfd, newpath) < 0)
  3305. + {
  3306. + if (errno == EEXIST && ignore_eexist)
  3307. + {
  3308. + (void) unlinkat (olddirfd, oldpath, 0);
  3309. + return TRUE;
  3310. + }
  3311. + else
  3312. + return glnx_throw_errno_prefix (error, "renameat");
  3313. + }
  3314. + return TRUE;
  3315. +}
  3316. +
  3317. +/* An implementation of renameat2(..., RENAME_EXCHANGE)
  3318. + * with fallback to a non-atomic version.
  3319. + */
  3320. +int
  3321. +glnx_renameat2_exchange (int olddirfd, const char *oldpath,
  3322. + int newdirfd, const char *newpath)
  3323. +{
  3324. +#ifndef ENABLE_WRPSEUDO_COMPAT
  3325. + if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_EXCHANGE) == 0)
  3326. + return 0;
  3327. + else
  3328. + {
  3329. + if (G_IN_SET(errno, ENOSYS, EINVAL))
  3330. + {
  3331. + /* Fall through */
  3332. + }
  3333. + else
  3334. + {
  3335. + return -1;
  3336. + }
  3337. + }
  3338. +#endif
  3339. +
  3340. + /* Fallback */
  3341. + { char *old_tmp_name_buf = glnx_strjoina (oldpath, ".XXXXXX");
  3342. + /* This obviously isn't race-free, but doing better gets tricky, since if
  3343. + * we're here the kernel isn't likely to support RENAME_NOREPLACE either.
  3344. + * Anyways, upgrade the kernel. Failing that, avoid use of this function in
  3345. + * shared subdirectories like /tmp.
  3346. + */
  3347. + glnx_gen_temp_name (old_tmp_name_buf);
  3348. + const char *old_tmp_name = old_tmp_name_buf;
  3349. +
  3350. + /* Move old out of the way */
  3351. + if (renameat (olddirfd, oldpath, olddirfd, old_tmp_name) < 0)
  3352. + return -1;
  3353. + /* Now move new into its place */
  3354. + if (renameat (newdirfd, newpath, olddirfd, oldpath) < 0)
  3355. + return -1;
  3356. + /* And finally old(tmp) into new */
  3357. + if (renameat (olddirfd, old_tmp_name, newdirfd, newpath) < 0)
  3358. + return -1;
  3359. + }
  3360. + return 0;
  3361. +}
  3362. +
  3363. +/* Deallocate a tmpfile, closing the fd and deleting the path, if any. This is
  3364. + * normally called by default by the autocleanup attribute, but you can also
  3365. + * invoke this directly.
  3366. + */
  3367. +void
  3368. +glnx_tmpfile_clear (GLnxTmpfile *tmpf)
  3369. +{
  3370. + /* Support being passed NULL so we work nicely in a GPtrArray */
  3371. + if (!tmpf)
  3372. + return;
  3373. + if (!tmpf->initialized)
  3374. + return;
  3375. + glnx_close_fd (&tmpf->fd);
  3376. + /* If ->path is set, we're likely aborting due to an error. Clean it up */
  3377. + if (tmpf->path)
  3378. + {
  3379. + (void) unlinkat (tmpf->src_dfd, tmpf->path, 0);
  3380. + g_free (tmpf->path);
  3381. + }
  3382. + tmpf->initialized = FALSE;
  3383. +}
  3384. +
  3385. +static gboolean
  3386. +open_tmpfile_core (int dfd, const char *subpath,
  3387. + int flags,
  3388. + GLnxTmpfile *out_tmpf,
  3389. + GError **error)
  3390. +{
  3391. + /* Picked this to match mkstemp() */
  3392. + const guint mode = 0600;
  3393. +
  3394. + dfd = glnx_dirfd_canonicalize (dfd);
  3395. +
  3396. + /* Creates a temporary file, that shall be renamed to "target"
  3397. + * later. If possible, this uses O_TMPFILE – in which case
  3398. + * "ret_path" will be returned as NULL. If not possible a the
  3399. + * tempoary path name used is returned in "ret_path". Use
  3400. + * link_tmpfile() below to rename the result after writing the file
  3401. + * in full. */
  3402. +#if defined(O_TMPFILE) && !defined(DISABLE_OTMPFILE) && !defined(ENABLE_WRPSEUDO_COMPAT)
  3403. + {
  3404. + glnx_autofd int fd = openat (dfd, subpath, O_TMPFILE|flags, mode);
  3405. + if (fd == -1 && !(G_IN_SET(errno, ENOSYS, EISDIR, EOPNOTSUPP)))
  3406. + return glnx_throw_errno_prefix (error, "open(O_TMPFILE)");
  3407. + if (fd != -1)
  3408. + {
  3409. + /* Workaround for https://sourceware.org/bugzilla/show_bug.cgi?id=17523
  3410. + * See also https://github.com/ostreedev/ostree/issues/991
  3411. + */
  3412. + if (fchmod (fd, mode) < 0)
  3413. + return glnx_throw_errno_prefix (error, "fchmod");
  3414. + out_tmpf->initialized = TRUE;
  3415. + out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
  3416. + out_tmpf->fd = glnx_steal_fd (&fd);
  3417. + out_tmpf->path = NULL;
  3418. + return TRUE;
  3419. + }
  3420. + }
  3421. + /* Fallthrough */
  3422. +#endif
  3423. +
  3424. + const guint count_max = 100;
  3425. + { g_autofree char *tmp = g_strconcat (subpath, "/tmp.XXXXXX", NULL);
  3426. +
  3427. + for (int count = 0; count < count_max; count++)
  3428. + {
  3429. + glnx_gen_temp_name (tmp);
  3430. +
  3431. + glnx_autofd int fd = openat (dfd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, mode);
  3432. + if (fd < 0)
  3433. + {
  3434. + if (errno == EEXIST)
  3435. + continue;
  3436. + else
  3437. + return glnx_throw_errno_prefix (error, "Creating temp file");
  3438. + }
  3439. + else
  3440. + {
  3441. + out_tmpf->initialized = TRUE;
  3442. + out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
  3443. + out_tmpf->fd = glnx_steal_fd (&fd);
  3444. + out_tmpf->path = g_steal_pointer (&tmp);
  3445. + return TRUE;
  3446. + }
  3447. + }
  3448. + }
  3449. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  3450. + "Exhausted %u attempts to create temporary file", count_max);
  3451. + return FALSE;
  3452. +}
  3453. +
  3454. +/* Allocate a temporary file, using Linux O_TMPFILE if available. The file mode
  3455. + * will be 0600.
  3456. + *
  3457. + * The result will be stored in @out_tmpf, which is caller allocated
  3458. + * so you can store it on the stack in common scenarios.
  3459. + *
  3460. + * The directory fd @dfd must live at least as long as the output @out_tmpf.
  3461. + */
  3462. +gboolean
  3463. +glnx_open_tmpfile_linkable_at (int dfd,
  3464. + const char *subpath,
  3465. + int flags,
  3466. + GLnxTmpfile *out_tmpf,
  3467. + GError **error)
  3468. +{
  3469. + /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE;
  3470. + * it's used for glnx_open_anonymous_tmpfile().
  3471. + */
  3472. + g_return_val_if_fail ((flags & O_EXCL) == 0, FALSE);
  3473. +
  3474. + return open_tmpfile_core (dfd, subpath, flags, out_tmpf, error);
  3475. +}
  3476. +
  3477. +/* A variant of `glnx_open_tmpfile_linkable_at()` which doesn't support linking.
  3478. + * Useful for true temporary storage. The fd will be allocated in /var/tmp to
  3479. + * ensure maximum storage space.
  3480. + */
  3481. +gboolean
  3482. +glnx_open_anonymous_tmpfile (int flags,
  3483. + GLnxTmpfile *out_tmpf,
  3484. + GError **error)
  3485. +{
  3486. + /* Add in O_EXCL */
  3487. + if (!open_tmpfile_core (AT_FDCWD, "/var/tmp", flags | O_EXCL, out_tmpf, error))
  3488. + return FALSE;
  3489. + if (out_tmpf->path)
  3490. + {
  3491. + (void) unlinkat (out_tmpf->src_dfd, out_tmpf->path, 0);
  3492. + g_clear_pointer (&out_tmpf->path, g_free);
  3493. + }
  3494. + out_tmpf->anonymous = TRUE;
  3495. + out_tmpf->src_dfd = -1;
  3496. + return TRUE;
  3497. +}
  3498. +
  3499. +/* Use this after calling glnx_open_tmpfile_linkable_at() to give
  3500. + * the file its final name (link into place).
  3501. + */
  3502. +gboolean
  3503. +glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
  3504. + GLnxLinkTmpfileReplaceMode mode,
  3505. + int target_dfd,
  3506. + const char *target,
  3507. + GError **error)
  3508. +{
  3509. + const gboolean replace = (mode == GLNX_LINK_TMPFILE_REPLACE);
  3510. + const gboolean ignore_eexist = (mode == GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST);
  3511. +
  3512. + g_return_val_if_fail (!tmpf->anonymous, FALSE);
  3513. + g_return_val_if_fail (tmpf->fd >= 0, FALSE);
  3514. + g_return_val_if_fail (tmpf->src_dfd == AT_FDCWD || tmpf->src_dfd >= 0, FALSE);
  3515. +
  3516. + /* Unlike the original systemd code, this function also supports
  3517. + * replacing existing files.
  3518. + */
  3519. +
  3520. + /* We have `tmpfile_path` for old systems without O_TMPFILE. */
  3521. + if (tmpf->path)
  3522. + {
  3523. + if (replace)
  3524. + {
  3525. + /* We have a regular tempfile, we're overwriting - this is a
  3526. + * simple renameat().
  3527. + */
  3528. + if (renameat (tmpf->src_dfd, tmpf->path, target_dfd, target) < 0)
  3529. + return glnx_throw_errno_prefix (error, "renameat");
  3530. + }
  3531. + else
  3532. + {
  3533. + /* We need to use renameat2(..., NOREPLACE) or emulate it */
  3534. + if (!rename_file_noreplace_at (tmpf->src_dfd, tmpf->path, target_dfd, target,
  3535. + ignore_eexist,
  3536. + error))
  3537. + return FALSE;
  3538. + }
  3539. + /* Now, clear the pointer so we don't try to unlink it */
  3540. + g_clear_pointer (&tmpf->path, g_free);
  3541. + }
  3542. + else
  3543. + {
  3544. + /* This case we have O_TMPFILE, so our reference to it is via /proc/self/fd */
  3545. + char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(tmpf->fd) + 1];
  3546. +
  3547. + sprintf (proc_fd_path, "/proc/self/fd/%i", tmpf->fd);
  3548. +
  3549. + if (replace)
  3550. + {
  3551. + /* In this case, we had our temp file atomically hidden, but now
  3552. + * we need to make it visible in the FS so we can do a rename.
  3553. + * Ideally, linkat() would gain AT_REPLACE or so.
  3554. + */
  3555. + /* TODO - avoid double alloca, we can just alloca a copy of
  3556. + * the pathname plus space for tmp.XXXXX */
  3557. + char *dnbuf = strdupa (target);
  3558. + const char *dn = dirname (dnbuf);
  3559. + char *tmpname_buf = glnx_strjoina (dn, "/tmp.XXXXXX");
  3560. +
  3561. + const guint count_max = 100;
  3562. + guint count;
  3563. + for (count = 0; count < count_max; count++)
  3564. + {
  3565. + glnx_gen_temp_name (tmpname_buf);
  3566. +
  3567. + if (linkat (AT_FDCWD, proc_fd_path, target_dfd, tmpname_buf, AT_SYMLINK_FOLLOW) < 0)
  3568. + {
  3569. + if (errno == EEXIST)
  3570. + continue;
  3571. + else
  3572. + return glnx_throw_errno_prefix (error, "linkat");
  3573. + }
  3574. + else
  3575. + break;
  3576. + }
  3577. + if (count == count_max)
  3578. + {
  3579. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  3580. + "Exhausted %u attempts to create temporary file", count);
  3581. + return FALSE;
  3582. + }
  3583. + if (!glnx_renameat (target_dfd, tmpname_buf, target_dfd, target, error))
  3584. + {
  3585. + /* This is currently the only case where we need to have
  3586. + * a cleanup unlinkat() still with O_TMPFILE.
  3587. + */
  3588. + (void) unlinkat (target_dfd, tmpname_buf, 0);
  3589. + return FALSE;
  3590. + }
  3591. + }
  3592. + else
  3593. + {
  3594. + if (linkat (AT_FDCWD, proc_fd_path, target_dfd, target, AT_SYMLINK_FOLLOW) < 0)
  3595. + {
  3596. + if (errno == EEXIST && mode == GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST)
  3597. + ;
  3598. + else
  3599. + return glnx_throw_errno_prefix (error, "linkat");
  3600. + }
  3601. + }
  3602. +
  3603. + }
  3604. + return TRUE;
  3605. +}
  3606. +
  3607. +/**
  3608. + * glnx_openat_rdonly:
  3609. + * @dfd: File descriptor for origin directory
  3610. + * @path: Pathname, relative to @dfd
  3611. + * @follow: Whether or not to follow symbolic links in the final component
  3612. + * @out_fd: (out): File descriptor
  3613. + * @error: Error
  3614. + *
  3615. + * Use openat() to open a file, with flags `O_RDONLY | O_CLOEXEC | O_NOCTTY`.
  3616. + * Like the other libglnx wrappers, will use `TEMP_FAILURE_RETRY` and
  3617. + * also includes @path in @error in case of failure.
  3618. + */
  3619. +gboolean
  3620. +glnx_openat_rdonly (int dfd,
  3621. + const char *path,
  3622. + gboolean follow,
  3623. + int *out_fd,
  3624. + GError **error)
  3625. +{
  3626. + int flags = O_RDONLY | O_CLOEXEC | O_NOCTTY;
  3627. + if (!follow)
  3628. + flags |= O_NOFOLLOW;
  3629. + int fd = TEMP_FAILURE_RETRY (openat (dfd, path, flags));
  3630. + if (fd == -1)
  3631. + return glnx_throw_errno_prefix (error, "openat(%s)", path);
  3632. + *out_fd = fd;
  3633. + return TRUE;
  3634. +}
  3635. +
  3636. +static guint8*
  3637. +glnx_fd_readall_malloc (int fd,
  3638. + gsize *out_len,
  3639. + gboolean nul_terminate,
  3640. + GCancellable *cancellable,
  3641. + GError **error)
  3642. +{
  3643. + const guint maxreadlen = 4096;
  3644. +
  3645. + struct stat stbuf;
  3646. + if (!glnx_fstat (fd, &stbuf, error))
  3647. + return FALSE;
  3648. +
  3649. + gsize buf_allocated;
  3650. + if (S_ISREG (stbuf.st_mode) && stbuf.st_size > 0)
  3651. + buf_allocated = stbuf.st_size;
  3652. + else
  3653. + buf_allocated = 16;
  3654. +
  3655. + g_autofree guint8* buf = g_malloc (buf_allocated);
  3656. +
  3657. + gsize buf_size = 0;
  3658. + while (TRUE)
  3659. + {
  3660. + gsize readlen = MIN (buf_allocated - buf_size, maxreadlen);
  3661. +
  3662. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  3663. + return FALSE;
  3664. +
  3665. + gssize bytes_read;
  3666. + do
  3667. + bytes_read = read (fd, buf + buf_size, readlen);
  3668. + while (G_UNLIKELY (bytes_read == -1 && errno == EINTR));
  3669. + if (G_UNLIKELY (bytes_read == -1))
  3670. + return glnx_null_throw_errno (error);
  3671. + if (bytes_read == 0)
  3672. + break;
  3673. +
  3674. + buf_size += bytes_read;
  3675. + if (buf_allocated - buf_size < maxreadlen)
  3676. + buf = g_realloc (buf, buf_allocated *= 2);
  3677. + }
  3678. +
  3679. + if (nul_terminate)
  3680. + {
  3681. + if (buf_allocated - buf_size == 0)
  3682. + buf = g_realloc (buf, buf_allocated + 1);
  3683. + buf[buf_size] = '\0';
  3684. + }
  3685. +
  3686. + *out_len = buf_size;
  3687. + return g_steal_pointer (&buf);
  3688. +}
  3689. +
  3690. +/**
  3691. + * glnx_fd_readall_bytes:
  3692. + * @fd: A file descriptor
  3693. + * @cancellable: Cancellable:
  3694. + * @error: Error
  3695. + *
  3696. + * Read all data from file descriptor @fd into a #GBytes. It's
  3697. + * recommended to only use this for small files.
  3698. + *
  3699. + * Returns: (transfer full): A newly allocated #GBytes
  3700. + */
  3701. +GBytes *
  3702. +glnx_fd_readall_bytes (int fd,
  3703. + GCancellable *cancellable,
  3704. + GError **error)
  3705. +{
  3706. + gsize len;
  3707. + guint8 *buf = glnx_fd_readall_malloc (fd, &len, FALSE, cancellable, error);
  3708. + if (!buf)
  3709. + return NULL;
  3710. + return g_bytes_new_take (buf, len);
  3711. +}
  3712. +
  3713. +/**
  3714. + * glnx_fd_readall_utf8:
  3715. + * @fd: A file descriptor
  3716. + * @out_len: (out): Returned length
  3717. + * @cancellable: Cancellable:
  3718. + * @error: Error
  3719. + *
  3720. + * Read all data from file descriptor @fd, validating
  3721. + * the result as UTF-8.
  3722. + *
  3723. + * Returns: (transfer full): A string validated as UTF-8, or %NULL on error.
  3724. + */
  3725. +char *
  3726. +glnx_fd_readall_utf8 (int fd,
  3727. + gsize *out_len,
  3728. + GCancellable *cancellable,
  3729. + GError **error)
  3730. +{
  3731. + gsize len;
  3732. + g_autofree guint8 *buf = glnx_fd_readall_malloc (fd, &len, TRUE, cancellable, error);
  3733. + if (!buf)
  3734. + return FALSE;
  3735. +
  3736. + if (!g_utf8_validate ((char*)buf, len, NULL))
  3737. + {
  3738. + g_set_error (error,
  3739. + G_IO_ERROR,
  3740. + G_IO_ERROR_INVALID_DATA,
  3741. + "Invalid UTF-8");
  3742. + return FALSE;
  3743. + }
  3744. +
  3745. + if (out_len)
  3746. + *out_len = len;
  3747. + return (char*)g_steal_pointer (&buf);
  3748. +}
  3749. +
  3750. +/**
  3751. + * glnx_file_get_contents_utf8_at:
  3752. + * @dfd: Directory file descriptor
  3753. + * @subpath: Path relative to @dfd
  3754. + * @out_len: (out) (allow-none): Optional length
  3755. + * @cancellable: Cancellable
  3756. + * @error: Error
  3757. + *
  3758. + * Read the entire contents of the file referred
  3759. + * to by @dfd and @subpath, validate the result as UTF-8.
  3760. + * The length is optionally stored in @out_len.
  3761. + *
  3762. + * Returns: (transfer full): UTF-8 validated text, or %NULL on error
  3763. + */
  3764. +char *
  3765. +glnx_file_get_contents_utf8_at (int dfd,
  3766. + const char *subpath,
  3767. + gsize *out_len,
  3768. + GCancellable *cancellable,
  3769. + GError **error)
  3770. +{
  3771. + dfd = glnx_dirfd_canonicalize (dfd);
  3772. +
  3773. + glnx_autofd int fd = -1;
  3774. + if (!glnx_openat_rdonly (dfd, subpath, TRUE, &fd, error))
  3775. + return NULL;
  3776. +
  3777. + gsize len;
  3778. + g_autofree char *buf = glnx_fd_readall_utf8 (fd, &len, cancellable, error);
  3779. + if (G_UNLIKELY(!buf))
  3780. + return FALSE;
  3781. +
  3782. + if (out_len)
  3783. + *out_len = len;
  3784. + return g_steal_pointer (&buf);
  3785. +}
  3786. +
  3787. +/**
  3788. + * glnx_readlinkat_malloc:
  3789. + * @dfd: Directory file descriptor
  3790. + * @subpath: Subpath
  3791. + * @cancellable: Cancellable
  3792. + * @error: Error
  3793. + *
  3794. + * Read the value of a symlink into a dynamically
  3795. + * allocated buffer.
  3796. + */
  3797. +char *
  3798. +glnx_readlinkat_malloc (int dfd,
  3799. + const char *subpath,
  3800. + GCancellable *cancellable,
  3801. + GError **error)
  3802. +{
  3803. + dfd = glnx_dirfd_canonicalize (dfd);
  3804. +
  3805. + size_t l = 100;
  3806. + for (;;)
  3807. + {
  3808. + g_autofree char *c = g_malloc (l);
  3809. + ssize_t n = TEMP_FAILURE_RETRY (readlinkat (dfd, subpath, c, l-1));
  3810. + if (n < 0)
  3811. + return glnx_null_throw_errno_prefix (error, "readlinkat");
  3812. +
  3813. + if ((size_t) n < l-1)
  3814. + {
  3815. + c[n] = 0;
  3816. + return g_steal_pointer (&c);
  3817. + }
  3818. +
  3819. + l *= 2;
  3820. + }
  3821. +
  3822. + g_assert_not_reached ();
  3823. +}
  3824. +
  3825. +static gboolean
  3826. +copy_symlink_at (int src_dfd,
  3827. + const char *src_subpath,
  3828. + const struct stat *src_stbuf,
  3829. + int dest_dfd,
  3830. + const char *dest_subpath,
  3831. + GLnxFileCopyFlags copyflags,
  3832. + GCancellable *cancellable,
  3833. + GError **error)
  3834. +{
  3835. + g_autofree char *buf = glnx_readlinkat_malloc (src_dfd, src_subpath, cancellable, error);
  3836. + if (!buf)
  3837. + return FALSE;
  3838. +
  3839. + if (TEMP_FAILURE_RETRY (symlinkat (buf, dest_dfd, dest_subpath)) != 0)
  3840. + return glnx_throw_errno_prefix (error, "symlinkat");
  3841. +
  3842. + if (!(copyflags & GLNX_FILE_COPY_NOXATTRS))
  3843. + {
  3844. + g_autoptr(GVariant) xattrs = NULL;
  3845. +
  3846. + if (!glnx_dfd_name_get_all_xattrs (src_dfd, src_subpath, &xattrs,
  3847. + cancellable, error))
  3848. + return FALSE;
  3849. +
  3850. + if (!glnx_dfd_name_set_all_xattrs (dest_dfd, dest_subpath, xattrs,
  3851. + cancellable, error))
  3852. + return FALSE;
  3853. + }
  3854. +
  3855. + if (TEMP_FAILURE_RETRY (fchownat (dest_dfd, dest_subpath,
  3856. + src_stbuf->st_uid, src_stbuf->st_gid,
  3857. + AT_SYMLINK_NOFOLLOW)) != 0)
  3858. + return glnx_throw_errno_prefix (error, "fchownat");
  3859. +
  3860. + return TRUE;
  3861. +}
  3862. +
  3863. +#define COPY_BUFFER_SIZE (16*1024)
  3864. +
  3865. +/* Most of the code below is from systemd, but has been reindented to GNU style,
  3866. + * and changed to use POSIX error conventions (return -1, set errno) to more
  3867. + * conveniently fit in with the rest of libglnx.
  3868. + */
  3869. +
  3870. +/* Like write(), but loop until @nbytes are written, or an error
  3871. + * occurs.
  3872. + *
  3873. + * On error, -1 is returned an @errno is set. NOTE: This is an
  3874. + * API change from previous versions of this function.
  3875. + */
  3876. +int
  3877. +glnx_loop_write(int fd, const void *buf, size_t nbytes)
  3878. +{
  3879. + g_return_val_if_fail (fd >= 0, -1);
  3880. + g_return_val_if_fail (buf, -1);
  3881. +
  3882. + errno = 0;
  3883. +
  3884. + const uint8_t *p = buf;
  3885. + while (nbytes > 0)
  3886. + {
  3887. + ssize_t k = write(fd, p, nbytes);
  3888. + if (k < 0)
  3889. + {
  3890. + if (errno == EINTR)
  3891. + continue;
  3892. +
  3893. + return -1;
  3894. + }
  3895. +
  3896. + if (k == 0) /* Can't really happen */
  3897. + {
  3898. + errno = EIO;
  3899. + return -1;
  3900. + }
  3901. +
  3902. + p += k;
  3903. + nbytes -= k;
  3904. + }
  3905. +
  3906. + return 0;
  3907. +}
  3908. +
  3909. +/* Read from @fdf until EOF, writing to @fdt. If max_bytes is -1, a full-file
  3910. + * clone will be attempted. Otherwise Linux copy_file_range(), sendfile()
  3911. + * syscall will be attempted. If none of those work, this function will do a
  3912. + * plain read()/write() loop.
  3913. + *
  3914. + * The file descriptor @fdf must refer to a regular file.
  3915. + *
  3916. + * If provided, @max_bytes specifies the maximum number of bytes to read from @fdf.
  3917. + * On error, this function returns `-1` and @errno will be set.
  3918. + */
  3919. +int
  3920. +glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes)
  3921. +{
  3922. + /* Last updates from systemd as of commit 6bda23dd6aaba50cf8e3e6024248cf736cc443ca */
  3923. + static int have_cfr = -1; /* -1 means unknown */
  3924. + bool try_cfr = have_cfr != 0;
  3925. + static int have_sendfile = -1; /* -1 means unknown */
  3926. + bool try_sendfile = have_sendfile != 0;
  3927. +
  3928. + g_return_val_if_fail (fdf >= 0, -1);
  3929. + g_return_val_if_fail (fdt >= 0, -1);
  3930. + g_return_val_if_fail (max_bytes >= -1, -1);
  3931. +
  3932. + /* If we've requested to copy the whole range, try a full-file clone first.
  3933. + */
  3934. + if (max_bytes == (off_t) -1)
  3935. + {
  3936. + if (ioctl (fdt, FICLONE, fdf) == 0)
  3937. + return 0;
  3938. + /* Fall through */
  3939. + struct stat stbuf;
  3940. +
  3941. + /* Gather the size so we can provide the whole thing at once to
  3942. + * copy_file_range() or sendfile().
  3943. + */
  3944. + if (fstat (fdf, &stbuf) < 0)
  3945. + return -1;
  3946. + max_bytes = stbuf.st_size;
  3947. + }
  3948. +
  3949. + while (TRUE)
  3950. + {
  3951. + ssize_t n;
  3952. +
  3953. + /* First, try copy_file_range(). Note this is an inlined version of
  3954. + * try_copy_file_range() from systemd upstream, which works better since
  3955. + * we use POSIX errno style.
  3956. + */
  3957. + if (try_cfr)
  3958. + {
  3959. + n = copy_file_range (fdf, NULL, fdt, NULL, max_bytes, 0u);
  3960. + if (n < 0)
  3961. + {
  3962. + if (errno == ENOSYS)
  3963. + {
  3964. + /* No cfr in kernel, mark as permanently unavailable
  3965. + * and fall through to sendfile().
  3966. + */
  3967. + have_cfr = 0;
  3968. + try_cfr = false;
  3969. + }
  3970. + else if (errno == EXDEV)
  3971. + /* We won't try cfr again for this run, but let's be
  3972. + * conservative and not mark it as available/unavailable until
  3973. + * we know for sure.
  3974. + */
  3975. + try_cfr = false;
  3976. + else
  3977. + return -1;
  3978. + }
  3979. + else
  3980. + {
  3981. + /* cfr worked, mark it as available */
  3982. + if (have_cfr == -1)
  3983. + have_cfr = 1;
  3984. +
  3985. + if (n == 0) /* EOF */
  3986. + break;
  3987. + else
  3988. + /* Success! */
  3989. + goto next;
  3990. + }
  3991. + }
  3992. +
  3993. + /* Next try sendfile(); this version is also changed from systemd upstream
  3994. + * to match the same logic we have for copy_file_range().
  3995. + */
  3996. + if (try_sendfile)
  3997. + {
  3998. + n = sendfile (fdt, fdf, NULL, max_bytes);
  3999. + if (n < 0)
  4000. + {
  4001. + if (G_IN_SET (errno, EINVAL, ENOSYS))
  4002. + {
  4003. + /* No sendfile(), or it doesn't work on regular files.
  4004. + * Mark it as permanently unavailable, and fall through
  4005. + * to plain read()/write().
  4006. + */
  4007. + have_sendfile = 0;
  4008. + try_sendfile = false;
  4009. + }
  4010. + else
  4011. + return -1;
  4012. + }
  4013. + else
  4014. + {
  4015. + /* sendfile() worked, mark it as available */
  4016. + if (have_sendfile == -1)
  4017. + have_sendfile = 1;
  4018. +
  4019. + if (n == 0) /* EOF */
  4020. + break;
  4021. + else if (n > 0)
  4022. + /* Succcess! */
  4023. + goto next;
  4024. + }
  4025. + }
  4026. +
  4027. + /* As a fallback just copy bits by hand */
  4028. + { size_t m = COPY_BUFFER_SIZE;
  4029. + if (max_bytes != (off_t) -1)
  4030. + {
  4031. + if ((off_t) m > max_bytes)
  4032. + m = (size_t) max_bytes;
  4033. + }
  4034. + char buf[m];
  4035. +
  4036. + n = TEMP_FAILURE_RETRY (read (fdf, buf, m));
  4037. + if (n < 0)
  4038. + return -1;
  4039. + if (n == 0) /* EOF */
  4040. + break;
  4041. +
  4042. + if (glnx_loop_write (fdt, buf, (size_t) n) < 0)
  4043. + return -1;
  4044. + }
  4045. +
  4046. + next:
  4047. + if (max_bytes != (off_t) -1)
  4048. + {
  4049. + g_assert_cmpint (max_bytes, >=, n);
  4050. + max_bytes -= n;
  4051. + if (max_bytes == 0)
  4052. + break;
  4053. + }
  4054. + }
  4055. +
  4056. + return 0;
  4057. +}
  4058. +
  4059. +/**
  4060. + * glnx_file_copy_at:
  4061. + * @src_dfd: Source directory fd
  4062. + * @src_subpath: Subpath relative to @src_dfd
  4063. + * @src_stbuf: (allow-none): Optional stat buffer for source; if a stat() has already been done
  4064. + * @dest_dfd: Target directory fd
  4065. + * @dest_subpath: Destination name
  4066. + * @copyflags: Flags
  4067. + * @cancellable: cancellable
  4068. + * @error: Error
  4069. + *
  4070. + * Perform a full copy of the regular file or symbolic link from @src_subpath to
  4071. + * @dest_subpath; if @src_subpath is anything other than a regular file or
  4072. + * symbolic link, an error will be returned.
  4073. + *
  4074. + * If the source is a regular file and the destination exists as a symbolic
  4075. + * link, the symbolic link will not be followed; rather the link itself will be
  4076. + * replaced. Related to this: for regular files, when `GLNX_FILE_COPY_OVERWRITE`
  4077. + * is specified, this function always uses `O_TMPFILE` (if available) and does a
  4078. + * rename-into-place rather than `open(O_TRUNC)`.
  4079. + */
  4080. +gboolean
  4081. +glnx_file_copy_at (int src_dfd,
  4082. + const char *src_subpath,
  4083. + struct stat *src_stbuf,
  4084. + int dest_dfd,
  4085. + const char *dest_subpath,
  4086. + GLnxFileCopyFlags copyflags,
  4087. + GCancellable *cancellable,
  4088. + GError **error)
  4089. +{
  4090. + /* Canonicalize dfds */
  4091. + src_dfd = glnx_dirfd_canonicalize (src_dfd);
  4092. + dest_dfd = glnx_dirfd_canonicalize (dest_dfd);
  4093. +
  4094. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  4095. + return FALSE;
  4096. +
  4097. + /* Automatically do stat() if no stat buffer was supplied */
  4098. + struct stat local_stbuf;
  4099. + if (!src_stbuf)
  4100. + {
  4101. + if (!glnx_fstatat (src_dfd, src_subpath, &local_stbuf, AT_SYMLINK_NOFOLLOW, error))
  4102. + return FALSE;
  4103. + src_stbuf = &local_stbuf;
  4104. + }
  4105. +
  4106. + /* For symlinks, defer entirely to copy_symlink_at() */
  4107. + if (S_ISLNK (src_stbuf->st_mode))
  4108. + {
  4109. + return copy_symlink_at (src_dfd, src_subpath, src_stbuf,
  4110. + dest_dfd, dest_subpath,
  4111. + copyflags,
  4112. + cancellable, error);
  4113. + }
  4114. + else if (!S_ISREG (src_stbuf->st_mode))
  4115. + {
  4116. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  4117. + "Cannot copy non-regular/non-symlink file: %s", src_subpath);
  4118. + return FALSE;
  4119. + }
  4120. +
  4121. + /* Regular file path below here */
  4122. +
  4123. + glnx_autofd int src_fd = -1;
  4124. + if (!glnx_openat_rdonly (src_dfd, src_subpath, FALSE, &src_fd, error))
  4125. + return FALSE;
  4126. +
  4127. + /* Open a tmpfile for dest. Particularly for AT_FDCWD calls, we really want to
  4128. + * open in the target directory, otherwise we may not be able to link.
  4129. + */
  4130. + g_auto(GLnxTmpfile) tmp_dest = { 0, };
  4131. + { char *dnbuf = strdupa (dest_subpath);
  4132. + const char *dn = dirname (dnbuf);
  4133. + if (!glnx_open_tmpfile_linkable_at (dest_dfd, dn, O_WRONLY | O_CLOEXEC,
  4134. + &tmp_dest, error))
  4135. + return FALSE;
  4136. + }
  4137. +
  4138. + if (glnx_regfile_copy_bytes (src_fd, tmp_dest.fd, (off_t) -1) < 0)
  4139. + return glnx_throw_errno_prefix (error, "regfile copy");
  4140. +
  4141. + if (fchown (tmp_dest.fd, src_stbuf->st_uid, src_stbuf->st_gid) != 0)
  4142. + return glnx_throw_errno_prefix (error, "fchown");
  4143. +
  4144. + if (!(copyflags & GLNX_FILE_COPY_NOXATTRS))
  4145. + {
  4146. + g_autoptr(GVariant) xattrs = NULL;
  4147. +
  4148. + if (!glnx_fd_get_all_xattrs (src_fd, &xattrs,
  4149. + cancellable, error))
  4150. + return FALSE;
  4151. +
  4152. + if (!glnx_fd_set_all_xattrs (tmp_dest.fd, xattrs,
  4153. + cancellable, error))
  4154. + return FALSE;
  4155. + }
  4156. +
  4157. + /* Always chmod after setting xattrs, in case the file has mode 0400 or less,
  4158. + * like /etc/shadow. Linux currently allows write() on non-writable open files
  4159. + * but not fsetxattr().
  4160. + */
  4161. + if (fchmod (tmp_dest.fd, src_stbuf->st_mode & 07777) != 0)
  4162. + return glnx_throw_errno_prefix (error, "fchmod");
  4163. +
  4164. + struct timespec ts[2];
  4165. + ts[0] = src_stbuf->st_atim;
  4166. + ts[1] = src_stbuf->st_mtim;
  4167. + (void) futimens (tmp_dest.fd, ts);
  4168. +
  4169. + if (copyflags & GLNX_FILE_COPY_DATASYNC)
  4170. + {
  4171. + if (fdatasync (tmp_dest.fd) < 0)
  4172. + return glnx_throw_errno_prefix (error, "fdatasync");
  4173. + }
  4174. +
  4175. + const GLnxLinkTmpfileReplaceMode replacemode =
  4176. + (copyflags & GLNX_FILE_COPY_OVERWRITE) ?
  4177. + GLNX_LINK_TMPFILE_REPLACE :
  4178. + GLNX_LINK_TMPFILE_NOREPLACE;
  4179. +
  4180. + if (!glnx_link_tmpfile_at (&tmp_dest, replacemode, dest_dfd, dest_subpath, error))
  4181. + return FALSE;
  4182. +
  4183. + return TRUE;
  4184. +}
  4185. +
  4186. +/**
  4187. + * glnx_file_replace_contents_at:
  4188. + * @dfd: Directory fd
  4189. + * @subpath: Subpath
  4190. + * @buf: (array len=len) (element-type guint8): File contents
  4191. + * @len: Length (if `-1`, assume @buf is `NUL` terminated)
  4192. + * @flags: Flags
  4193. + * @cancellable: Cancellable
  4194. + * @error: Error
  4195. + *
  4196. + * Create a new file, atomically replacing the contents of @subpath
  4197. + * (relative to @dfd) with @buf. By default, if the file already
  4198. + * existed, fdatasync() will be used before rename() to ensure stable
  4199. + * contents. This and other behavior can be controlled via @flags.
  4200. + *
  4201. + * Note that no metadata from the existing file is preserved, such as
  4202. + * uid/gid or extended attributes. The default mode will be `0666`,
  4203. + * modified by umask.
  4204. + */
  4205. +gboolean
  4206. +glnx_file_replace_contents_at (int dfd,
  4207. + const char *subpath,
  4208. + const guint8 *buf,
  4209. + gsize len,
  4210. + GLnxFileReplaceFlags flags,
  4211. + GCancellable *cancellable,
  4212. + GError **error)
  4213. +{
  4214. + return glnx_file_replace_contents_with_perms_at (dfd, subpath, buf, len,
  4215. + (mode_t) -1, (uid_t) -1, (gid_t) -1,
  4216. + flags, cancellable, error);
  4217. +}
  4218. +
  4219. +/**
  4220. + * glnx_file_replace_contents_with_perms_at:
  4221. + * @dfd: Directory fd
  4222. + * @subpath: Subpath
  4223. + * @buf: (array len=len) (element-type guint8): File contents
  4224. + * @len: Length (if `-1`, assume @buf is `NUL` terminated)
  4225. + * @mode: File mode; if `-1`, use `0666 - umask`
  4226. + * @flags: Flags
  4227. + * @cancellable: Cancellable
  4228. + * @error: Error
  4229. + *
  4230. + * Like glnx_file_replace_contents_at(), but also supports
  4231. + * setting mode, and uid/gid.
  4232. + */
  4233. +gboolean
  4234. +glnx_file_replace_contents_with_perms_at (int dfd,
  4235. + const char *subpath,
  4236. + const guint8 *buf,
  4237. + gsize len,
  4238. + mode_t mode,
  4239. + uid_t uid,
  4240. + gid_t gid,
  4241. + GLnxFileReplaceFlags flags,
  4242. + GCancellable *cancellable,
  4243. + GError **error)
  4244. +{
  4245. + char *dnbuf = strdupa (subpath);
  4246. + const char *dn = dirname (dnbuf);
  4247. +
  4248. + dfd = glnx_dirfd_canonicalize (dfd);
  4249. +
  4250. + /* With O_TMPFILE we can't use umask, and we can't sanely query the
  4251. + * umask...let's assume something relatively standard.
  4252. + */
  4253. + if (mode == (mode_t) -1)
  4254. + mode = 0644;
  4255. +
  4256. + g_auto(GLnxTmpfile) tmpf = { 0, };
  4257. + if (!glnx_open_tmpfile_linkable_at (dfd, dn, O_WRONLY | O_CLOEXEC,
  4258. + &tmpf, error))
  4259. + return FALSE;
  4260. +
  4261. + if (len == -1)
  4262. + len = strlen ((char*)buf);
  4263. +
  4264. + if (!glnx_try_fallocate (tmpf.fd, 0, len, error))
  4265. + return FALSE;
  4266. +
  4267. + if (glnx_loop_write (tmpf.fd, buf, len) < 0)
  4268. + return glnx_throw_errno_prefix (error, "write");
  4269. +
  4270. + if (!(flags & GLNX_FILE_REPLACE_NODATASYNC))
  4271. + {
  4272. + struct stat stbuf;
  4273. + gboolean do_sync;
  4274. +
  4275. + if (!glnx_fstatat_allow_noent (dfd, subpath, &stbuf, AT_SYMLINK_NOFOLLOW, error))
  4276. + return FALSE;
  4277. + if (errno == ENOENT)
  4278. + do_sync = (flags & GLNX_FILE_REPLACE_DATASYNC_NEW) > 0;
  4279. + else
  4280. + do_sync = TRUE;
  4281. +
  4282. + if (do_sync)
  4283. + {
  4284. + if (fdatasync (tmpf.fd) != 0)
  4285. + return glnx_throw_errno_prefix (error, "fdatasync");
  4286. + }
  4287. + }
  4288. +
  4289. + if (uid != (uid_t) -1)
  4290. + {
  4291. + if (fchown (tmpf.fd, uid, gid) != 0)
  4292. + return glnx_throw_errno_prefix (error, "fchown");
  4293. + }
  4294. +
  4295. + if (fchmod (tmpf.fd, mode) != 0)
  4296. + return glnx_throw_errno_prefix (error, "fchmod");
  4297. +
  4298. + if (!glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_REPLACE,
  4299. + dfd, subpath, error))
  4300. + return FALSE;
  4301. +
  4302. + return TRUE;
  4303. +}
  4304. diff -Nuar ostree-2018.8.orig/libglnx/glnx-fdio.h ostree-2018.8/libglnx/glnx-fdio.h
  4305. --- ostree-2018.8.orig/libglnx/glnx-fdio.h 1970-01-01 02:00:00.000000000 +0200
  4306. +++ ostree-2018.8/libglnx/glnx-fdio.h 2018-05-26 00:37:32.007018846 +0300
  4307. @@ -0,0 +1,369 @@
  4308. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4309. + *
  4310. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  4311. + *
  4312. + * This library is free software; you can redistribute it and/or
  4313. + * modify it under the terms of the GNU Lesser General Public
  4314. + * License as published by the Free Software Foundation; either
  4315. + * version 2 of the License, or (at your option) any later version.
  4316. + *
  4317. + * This library is distributed in the hope that it will be useful,
  4318. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4319. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4320. + * Lesser General Public License for more details.
  4321. + *
  4322. + * You should have received a copy of the GNU Lesser General Public
  4323. + * License along with this library; if not, write to the
  4324. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4325. + * Boston, MA 02111-1307, USA.
  4326. + */
  4327. +
  4328. +#pragma once
  4329. +
  4330. +#include <glnx-backport-autocleanups.h>
  4331. +#include <gio/gfiledescriptorbased.h>
  4332. +#include <limits.h>
  4333. +#include <dirent.h>
  4334. +#include <sys/stat.h>
  4335. +#include <fcntl.h>
  4336. +#include <string.h>
  4337. +#include <stdio.h>
  4338. +#include <sys/xattr.h>
  4339. +// For dirname(), and previously basename()
  4340. +#include <libgen.h>
  4341. +
  4342. +#include <glnx-macros.h>
  4343. +#include <glnx-errors.h>
  4344. +
  4345. +G_BEGIN_DECLS
  4346. +
  4347. +/* Irritatingly, g_basename() which is what we want
  4348. + * is deprecated.
  4349. + */
  4350. +static inline
  4351. +const char *glnx_basename (const char *path)
  4352. +{
  4353. + gchar *base = strrchr (path, G_DIR_SEPARATOR);
  4354. +
  4355. + if (base)
  4356. + return base + 1;
  4357. +
  4358. + return path;
  4359. +}
  4360. +
  4361. +/* Utilities for standard FILE* */
  4362. +static inline void
  4363. +glnx_stdio_file_cleanup (void *filep)
  4364. +{
  4365. + FILE *f = filep;
  4366. + if (f)
  4367. + fclose (f);
  4368. +}
  4369. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(FILE, glnx_stdio_file_cleanup)
  4370. +
  4371. +/**
  4372. + * glnx_stdio_file_flush:
  4373. + * Call fflush() and check ferror().
  4374. + */
  4375. +gboolean
  4376. +glnx_stdio_file_flush (FILE *f, GError **error);
  4377. +
  4378. +typedef struct {
  4379. + gboolean initialized;
  4380. + gboolean anonymous;
  4381. + int src_dfd;
  4382. + int fd;
  4383. + char *path;
  4384. +} GLnxTmpfile;
  4385. +void glnx_tmpfile_clear (GLnxTmpfile *tmpf);
  4386. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxTmpfile, glnx_tmpfile_clear)
  4387. +
  4388. +gboolean
  4389. +glnx_open_anonymous_tmpfile (int flags,
  4390. + GLnxTmpfile *out_tmpf,
  4391. + GError **error);
  4392. +
  4393. +gboolean
  4394. +glnx_open_tmpfile_linkable_at (int dfd,
  4395. + const char *subpath,
  4396. + int flags,
  4397. + GLnxTmpfile *out_tmpf,
  4398. + GError **error);
  4399. +
  4400. +typedef enum {
  4401. + GLNX_LINK_TMPFILE_REPLACE,
  4402. + GLNX_LINK_TMPFILE_NOREPLACE,
  4403. + GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST
  4404. +} GLnxLinkTmpfileReplaceMode;
  4405. +
  4406. +gboolean
  4407. +glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
  4408. + GLnxLinkTmpfileReplaceMode flags,
  4409. + int target_dfd,
  4410. + const char *target,
  4411. + GError **error);
  4412. +
  4413. +gboolean
  4414. +glnx_openat_rdonly (int dfd,
  4415. + const char *path,
  4416. + gboolean follow,
  4417. + int *out_fd,
  4418. + GError **error);
  4419. +
  4420. +GBytes *
  4421. +glnx_fd_readall_bytes (int fd,
  4422. + GCancellable *cancellable,
  4423. + GError **error);
  4424. +
  4425. +char *
  4426. +glnx_fd_readall_utf8 (int fd,
  4427. + gsize *out_len,
  4428. + GCancellable *cancellable,
  4429. + GError **error);
  4430. +
  4431. +char *
  4432. +glnx_file_get_contents_utf8_at (int dfd,
  4433. + const char *subpath,
  4434. + gsize *out_len,
  4435. + GCancellable *cancellable,
  4436. + GError **error);
  4437. +
  4438. +/**
  4439. + * GLnxFileReplaceFlags:
  4440. + * @GLNX_FILE_REPLACE_DATASYNC_NEW: Call fdatasync() even if the file did not exist
  4441. + * @GLNX_FILE_REPLACE_NODATASYNC: Never call fdatasync()
  4442. + *
  4443. + * Flags controlling file replacement.
  4444. + */
  4445. +typedef enum {
  4446. + GLNX_FILE_REPLACE_DATASYNC_NEW = (1 << 0),
  4447. + GLNX_FILE_REPLACE_NODATASYNC = (1 << 1),
  4448. +} GLnxFileReplaceFlags;
  4449. +
  4450. +gboolean
  4451. +glnx_file_replace_contents_at (int dfd,
  4452. + const char *subpath,
  4453. + const guint8 *buf,
  4454. + gsize len,
  4455. + GLnxFileReplaceFlags flags,
  4456. + GCancellable *cancellable,
  4457. + GError **error);
  4458. +
  4459. +gboolean
  4460. +glnx_file_replace_contents_with_perms_at (int dfd,
  4461. + const char *subpath,
  4462. + const guint8 *buf,
  4463. + gsize len,
  4464. + mode_t mode,
  4465. + uid_t uid,
  4466. + gid_t gid,
  4467. + GLnxFileReplaceFlags flags,
  4468. + GCancellable *cancellable,
  4469. + GError **error);
  4470. +
  4471. +char *
  4472. +glnx_readlinkat_malloc (int dfd,
  4473. + const char *subpath,
  4474. + GCancellable *cancellable,
  4475. + GError **error);
  4476. +
  4477. +int
  4478. +glnx_loop_write (int fd, const void *buf, size_t nbytes);
  4479. +
  4480. +int
  4481. +glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes);
  4482. +
  4483. +typedef enum {
  4484. + GLNX_FILE_COPY_OVERWRITE = (1 << 0),
  4485. + GLNX_FILE_COPY_NOXATTRS = (1 << 1),
  4486. + GLNX_FILE_COPY_DATASYNC = (1 << 2)
  4487. +} GLnxFileCopyFlags;
  4488. +
  4489. +gboolean
  4490. +glnx_file_copy_at (int src_dfd,
  4491. + const char *src_subpath,
  4492. + struct stat *src_stbuf,
  4493. + int dest_dfd,
  4494. + const char *dest_subpath,
  4495. + GLnxFileCopyFlags copyflags,
  4496. + GCancellable *cancellable,
  4497. + GError **error);
  4498. +
  4499. +int glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
  4500. + int newdirfd, const char *newpath);
  4501. +int glnx_renameat2_exchange (int olddirfd, const char *oldpath,
  4502. + int newdirfd, const char *newpath);
  4503. +
  4504. +/**
  4505. + * glnx_try_fallocate:
  4506. + * @fd: File descriptor
  4507. + * @size: Size
  4508. + * @error: Error
  4509. + *
  4510. + * Wrapper for Linux fallocate(). Explicitly ignores a @size of zero.
  4511. + * Also, will silently do nothing if the underlying filesystem doesn't
  4512. + * support it. Use this instead of posix_fallocate(), since the glibc fallback
  4513. + * is bad: https://sourceware.org/bugzilla/show_bug.cgi?id=18515
  4514. + */
  4515. +static inline gboolean
  4516. +glnx_try_fallocate (int fd,
  4517. + off_t offset,
  4518. + off_t size,
  4519. + GError **error)
  4520. +{
  4521. + /* This is just nicer than throwing an error */
  4522. + if (size == 0)
  4523. + return TRUE;
  4524. +
  4525. + if (fallocate (fd, 0, offset, size) < 0)
  4526. + {
  4527. + if (G_IN_SET(errno, ENOSYS, EOPNOTSUPP))
  4528. + ; /* Ignore */
  4529. + else
  4530. + return glnx_throw_errno_prefix (error, "fallocate");
  4531. + }
  4532. +
  4533. + return TRUE;
  4534. +}
  4535. +
  4536. +/**
  4537. + * glnx_fstat:
  4538. + * @fd: FD to stat
  4539. + * @buf: (out caller-allocates): Return location for stat details
  4540. + * @error: Return location for a #GError, or %NULL
  4541. + *
  4542. + * Wrapper around fstat() which adds #GError support and ensures that it retries
  4543. + * on %EINTR.
  4544. + *
  4545. + * Returns: %TRUE on success, %FALSE otherwise
  4546. + * Since: UNRELEASED
  4547. + */
  4548. +static inline gboolean
  4549. +glnx_fstat (int fd,
  4550. + struct stat *buf,
  4551. + GError **error)
  4552. +{
  4553. + if (TEMP_FAILURE_RETRY (fstat (fd, buf)) != 0)
  4554. + return glnx_throw_errno_prefix (error, "fstat");
  4555. + return TRUE;
  4556. +}
  4557. +
  4558. +/**
  4559. + * glnx_fchmod:
  4560. + * @fd: FD
  4561. + * @mode: Mode
  4562. + * @error: Return location for a #GError, or %NULL
  4563. + *
  4564. + * Wrapper around fchmod() which adds #GError support and ensures that it
  4565. + * retries on %EINTR.
  4566. + *
  4567. + * Returns: %TRUE on success, %FALSE otherwise
  4568. + * Since: UNRELEASED
  4569. + */
  4570. +static inline gboolean
  4571. +glnx_fchmod (int fd,
  4572. + mode_t mode,
  4573. + GError **error)
  4574. +{
  4575. + if (TEMP_FAILURE_RETRY (fchmod (fd, mode)) != 0)
  4576. + return glnx_throw_errno_prefix (error, "fchmod");
  4577. + return TRUE;
  4578. +}
  4579. +
  4580. +/**
  4581. + * glnx_fstatat:
  4582. + * @dfd: Directory FD to stat beneath
  4583. + * @path: Path to stat beneath @dfd
  4584. + * @buf: (out caller-allocates): Return location for stat details
  4585. + * @flags: Flags to pass to fstatat()
  4586. + * @error: Return location for a #GError, or %NULL
  4587. + *
  4588. + * Wrapper around fstatat() which adds #GError support and ensures that it
  4589. + * retries on %EINTR.
  4590. + *
  4591. + * Returns: %TRUE on success, %FALSE otherwise
  4592. + * Since: UNRELEASED
  4593. + */
  4594. +static inline gboolean
  4595. +glnx_fstatat (int dfd,
  4596. + const gchar *path,
  4597. + struct stat *buf,
  4598. + int flags,
  4599. + GError **error)
  4600. +{
  4601. + if (TEMP_FAILURE_RETRY (fstatat (dfd, path, buf, flags)) != 0)
  4602. + return glnx_throw_errno_prefix (error, "fstatat(%s)", path);
  4603. + return TRUE;
  4604. +}
  4605. +
  4606. +/**
  4607. + * glnx_fstatat_allow_noent:
  4608. + * @dfd: Directory FD to stat beneath
  4609. + * @path: Path to stat beneath @dfd
  4610. + * @buf: (out caller-allocates) (allow-none): Return location for stat details
  4611. + * @flags: Flags to pass to fstatat()
  4612. + * @error: Return location for a #GError, or %NULL
  4613. + *
  4614. + * Like glnx_fstatat(), but handles `ENOENT` in a non-error way. Instead,
  4615. + * on success `errno` will be zero, otherwise it will be preserved. Hence
  4616. + * you can test `if (errno == 0)` to conditionalize on the file existing,
  4617. + * or `if (errno == ENOENT)` for non-existence.
  4618. + *
  4619. + * Returns: %TRUE on success, %FALSE otherwise (errno is preserved)
  4620. + * Since: UNRELEASED
  4621. + */
  4622. +static inline gboolean
  4623. +glnx_fstatat_allow_noent (int dfd,
  4624. + const char *path,
  4625. + struct stat *out_buf,
  4626. + int flags,
  4627. + GError **error)
  4628. +{
  4629. + G_GNUC_UNUSED struct stat unused_stbuf;
  4630. + if (TEMP_FAILURE_RETRY (fstatat (dfd, path, out_buf ? out_buf : &unused_stbuf, flags)) != 0)
  4631. + {
  4632. + if (errno != ENOENT)
  4633. + return glnx_throw_errno_prefix (error, "fstatat(%s)", path);
  4634. + /* Note we preserve errno as ENOENT */
  4635. + }
  4636. + else
  4637. + errno = 0;
  4638. + return TRUE;
  4639. +}
  4640. +
  4641. +/**
  4642. + * glnx_renameat:
  4643. + *
  4644. + * Wrapper around renameat() which adds #GError support and ensures that it
  4645. + * retries on %EINTR.
  4646. + */
  4647. +static inline gboolean
  4648. +glnx_renameat (int src_dfd,
  4649. + const gchar *src_path,
  4650. + int dest_dfd,
  4651. + const gchar *dest_path,
  4652. + GError **error)
  4653. +{
  4654. + if (TEMP_FAILURE_RETRY (renameat (src_dfd, src_path, dest_dfd, dest_path)) != 0)
  4655. + return glnx_throw_errno_prefix (error, "renameat(%s, %s)", src_path, dest_path);
  4656. + return TRUE;
  4657. +}
  4658. +
  4659. +/**
  4660. + * glnx_unlinkat:
  4661. + *
  4662. + * Wrapper around unlinkat() which adds #GError support and ensures that it
  4663. + * retries on %EINTR.
  4664. + */
  4665. +static inline gboolean
  4666. +glnx_unlinkat (int dfd,
  4667. + const gchar *path,
  4668. + int flags,
  4669. + GError **error)
  4670. +{
  4671. + if (TEMP_FAILURE_RETRY (unlinkat (dfd, path, flags)) != 0)
  4672. + return glnx_throw_errno_prefix (error, "unlinkat(%s)", path);
  4673. + return TRUE;
  4674. +}
  4675. +
  4676. +G_END_DECLS
  4677. diff -Nuar ostree-2018.8.orig/libglnx/glnx-local-alloc.c ostree-2018.8/libglnx/glnx-local-alloc.c
  4678. --- ostree-2018.8.orig/libglnx/glnx-local-alloc.c 1970-01-01 02:00:00.000000000 +0200
  4679. +++ ostree-2018.8/libglnx/glnx-local-alloc.c 2018-05-26 00:37:32.007018846 +0300
  4680. @@ -0,0 +1,72 @@
  4681. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4682. + *
  4683. + * Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>
  4684. + *
  4685. + * This library is free software; you can redistribute it and/or
  4686. + * modify it under the terms of the GNU Lesser General Public
  4687. + * License as published by the Free Software Foundation; either
  4688. + * version 2 of the License, or (at your option) any later version.
  4689. + *
  4690. + * This library is distributed in the hope that it will be useful,
  4691. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4692. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4693. + * Lesser General Public License for more details.
  4694. + *
  4695. + * You should have received a copy of the GNU Lesser General Public
  4696. + * License along with this library; if not, write to the
  4697. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4698. + * Boston, MA 02111-1307, USA.
  4699. + */
  4700. +
  4701. +#include "config.h"
  4702. +
  4703. +#include "glnx-local-alloc.h"
  4704. +
  4705. +/**
  4706. + * SECTION:glnxlocalalloc
  4707. + * @title: GLnx local allocation
  4708. + * @short_description: Release local variables automatically when they go out of scope
  4709. + *
  4710. + * These macros leverage the GCC extension __attribute__ ((cleanup))
  4711. + * to allow calling a cleanup function such as g_free() when a
  4712. + * variable goes out of scope. See <ulink
  4713. + * url="http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html">
  4714. + * for more information on the attribute.
  4715. + *
  4716. + * The provided macros make it easy to use the cleanup attribute for
  4717. + * types that come with GLib. The primary two are #glnx_free and
  4718. + * #glnx_unref_object, which correspond to g_free() and
  4719. + * g_object_unref(), respectively.
  4720. + *
  4721. + * The rationale behind this is that particularly when handling error
  4722. + * paths, it can be very tricky to ensure the right variables are
  4723. + * freed. With this, one simply applies glnx_unref_object to a
  4724. + * locally-allocated #GFile for example, and it will be automatically
  4725. + * unreferenced when it goes out of scope.
  4726. + *
  4727. + * Note - you should only use these macros for <emphasis>stack
  4728. + * allocated</emphasis> variables. They don't provide garbage
  4729. + * collection or let you avoid freeing things. They're simply a
  4730. + * compiler assisted deterministic mechanism for calling a cleanup
  4731. + * function when a stack frame ends.
  4732. + *
  4733. + * <example id="gs-lfree"><title>Calling g_free automatically</title>
  4734. + * <programlisting>
  4735. + *
  4736. + * GFile *
  4737. + * create_file (GError **error)
  4738. + * {
  4739. + * glnx_free char *random_id = NULL;
  4740. + *
  4741. + * if (!prepare_file (error))
  4742. + * return NULL;
  4743. + *
  4744. + * random_id = alloc_random_id ();
  4745. + *
  4746. + * return create_file_real (error);
  4747. + * // Note that random_id is freed here automatically
  4748. + * }
  4749. + * </programlisting>
  4750. + * </example>
  4751. + *
  4752. + */
  4753. diff -Nuar ostree-2018.8.orig/libglnx/glnx-local-alloc.h ostree-2018.8/libglnx/glnx-local-alloc.h
  4754. --- ostree-2018.8.orig/libglnx/glnx-local-alloc.h 1970-01-01 02:00:00.000000000 +0200
  4755. +++ ostree-2018.8/libglnx/glnx-local-alloc.h 2018-05-26 00:37:32.007018846 +0300
  4756. @@ -0,0 +1,91 @@
  4757. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4758. + *
  4759. + * Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>.
  4760. + *
  4761. + * This library is free software; you can redistribute it and/or
  4762. + * modify it under the terms of the GNU Lesser General Public
  4763. + * License as published by the Free Software Foundation; either
  4764. + * version 2 of the License, or (at your option) any later version.
  4765. + *
  4766. + * This library is distributed in the hope that it will be useful,
  4767. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4768. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4769. + * Lesser General Public License for more details.
  4770. + *
  4771. + * You should have received a copy of the GNU Lesser General Public
  4772. + * License along with this library; if not, write to the
  4773. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4774. + * Boston, MA 02111-1307, USA.
  4775. + */
  4776. +
  4777. +#pragma once
  4778. +
  4779. +#include <gio/gio.h>
  4780. +#include <errno.h>
  4781. +
  4782. +G_BEGIN_DECLS
  4783. +
  4784. +/**
  4785. + * glnx_unref_object:
  4786. + *
  4787. + * Call g_object_unref() on a variable location when it goes out of
  4788. + * scope. Note that unlike g_object_unref(), the variable may be
  4789. + * %NULL.
  4790. + */
  4791. +#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
  4792. +static inline void
  4793. +glnx_local_obj_unref (void *v)
  4794. +{
  4795. + GObject *o = *(GObject **)v;
  4796. + if (o)
  4797. + g_object_unref (o);
  4798. +}
  4799. +#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
  4800. +
  4801. +static inline int
  4802. +glnx_steal_fd (int *fdp)
  4803. +{
  4804. + int fd = *fdp;
  4805. + *fdp = -1;
  4806. + return fd;
  4807. +}
  4808. +
  4809. +/**
  4810. + * glnx_close_fd:
  4811. + * @fdp: Pointer to fd
  4812. + *
  4813. + * Effectively `close (glnx_steal_fd (&fd))`. Also
  4814. + * asserts that `close()` did not raise `EBADF` - encountering
  4815. + * that error is usually a critical bug in the program.
  4816. + */
  4817. +static inline void
  4818. +glnx_close_fd (int *fdp)
  4819. +{
  4820. + int errsv;
  4821. +
  4822. + g_assert (fdp);
  4823. +
  4824. + int fd = glnx_steal_fd (fdp);
  4825. + if (fd >= 0)
  4826. + {
  4827. + errsv = errno;
  4828. + if (close (fd) < 0)
  4829. + g_assert (errno != EBADF);
  4830. + errno = errsv;
  4831. + }
  4832. +}
  4833. +
  4834. +/**
  4835. + * glnx_fd_close:
  4836. + *
  4837. + * Deprecated in favor of `glnx_autofd`.
  4838. + */
  4839. +#define glnx_fd_close __attribute__((cleanup(glnx_close_fd)))
  4840. +/**
  4841. + * glnx_autofd:
  4842. + *
  4843. + * Call close() on a variable location when it goes out of scope.
  4844. + */
  4845. +#define glnx_autofd __attribute__((cleanup(glnx_close_fd)))
  4846. +
  4847. +G_END_DECLS
  4848. diff -Nuar ostree-2018.8.orig/libglnx/glnx-lockfile.c ostree-2018.8/libglnx/glnx-lockfile.c
  4849. --- ostree-2018.8.orig/libglnx/glnx-lockfile.c 1970-01-01 02:00:00.000000000 +0200
  4850. +++ ostree-2018.8/libglnx/glnx-lockfile.c 2018-05-26 00:37:32.007018846 +0300
  4851. @@ -0,0 +1,179 @@
  4852. +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  4853. +
  4854. +/***
  4855. + This file is part of systemd.
  4856. + Now copied into libglnx:
  4857. + - Use GError
  4858. +
  4859. + Copyright 2010 Lennart Poettering
  4860. + Copyright 2015 Colin Walters <walters@verbum.org>
  4861. +
  4862. + systemd is free software; you can redistribute it and/or modify it
  4863. + under the terms of the GNU Lesser General Public License as published by
  4864. + the Free Software Foundation; either version 2.1 of the License, or
  4865. + (at your option) any later version.
  4866. +
  4867. + systemd is distributed in the hope that it will be useful, but
  4868. + WITHOUT ANY WARRANTY; without even the implied warranty of
  4869. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4870. + Lesser General Public License for more details.
  4871. +
  4872. + You should have received a copy of the GNU Lesser General Public License
  4873. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  4874. +***/
  4875. +
  4876. +#include "config.h"
  4877. +
  4878. +#include <stdlib.h>
  4879. +#include <stdbool.h>
  4880. +#include <errno.h>
  4881. +#include <string.h>
  4882. +#include <stdio.h>
  4883. +#include <limits.h>
  4884. +#include <unistd.h>
  4885. +#include <sys/types.h>
  4886. +#include <sys/file.h>
  4887. +#include <sys/stat.h>
  4888. +#include <fcntl.h>
  4889. +
  4890. +#include "glnx-lockfile.h"
  4891. +#include "glnx-errors.h"
  4892. +#include "glnx-fdio.h"
  4893. +#include "glnx-backport-autocleanups.h"
  4894. +#include "glnx-local-alloc.h"
  4895. +
  4896. +#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
  4897. +
  4898. +/**
  4899. + * glnx_make_lock_file:
  4900. + * @dfd: Directory file descriptor (if not `AT_FDCWD`, must have lifetime `>=` @out_lock)
  4901. + * @p: Path
  4902. + * @operation: one of `LOCK_SH`, `LOCK_EX`, `LOCK_UN`, as passed to flock()
  4903. + * @out_lock: (out) (caller allocates): Return location for lock
  4904. + * @error: Error
  4905. + *
  4906. + * Block until a lock file named @p (relative to @dfd) can be created,
  4907. + * using the flags in @operation, returning the lock data in the
  4908. + * caller-allocated location @out_lock.
  4909. + *
  4910. + * This API wraps new-style process locking if available, otherwise
  4911. + * falls back to BSD locks.
  4912. + */
  4913. +gboolean
  4914. +glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_lock, GError **error) {
  4915. + glnx_autofd int fd = -1;
  4916. + g_autofree char *t = NULL;
  4917. + int r;
  4918. +
  4919. + /*
  4920. + * We use UNPOSIX locks if they are available. They have nice
  4921. + * semantics, and are mostly compatible with NFS. However,
  4922. + * they are only available on new kernels. When we detect we
  4923. + * are running on an older kernel, then we fall back to good
  4924. + * old BSD locks. They also have nice semantics, but are
  4925. + * slightly problematic on NFS, where they are upgraded to
  4926. + * POSIX locks, even though locally they are orthogonal to
  4927. + * POSIX locks.
  4928. + */
  4929. +
  4930. + t = g_strdup(p);
  4931. +
  4932. + for (;;) {
  4933. +#ifdef F_OFD_SETLK
  4934. + struct flock fl = {
  4935. + .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
  4936. + .l_whence = SEEK_SET,
  4937. + };
  4938. +#endif
  4939. + struct stat st;
  4940. +
  4941. + fd = openat(dfd, p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
  4942. + if (fd < 0)
  4943. + return glnx_throw_errno(error);
  4944. +
  4945. + /* Unfortunately, new locks are not in RHEL 7.1 glibc */
  4946. +#ifdef F_OFD_SETLK
  4947. + r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
  4948. +#else
  4949. + r = -1;
  4950. + errno = EINVAL;
  4951. +#endif
  4952. + if (r < 0) {
  4953. +
  4954. + /* If the kernel is too old, use good old BSD locks */
  4955. + if (errno == EINVAL)
  4956. + r = flock(fd, operation);
  4957. +
  4958. + if (r < 0)
  4959. + return glnx_throw_errno_prefix (error, "flock");
  4960. + }
  4961. +
  4962. + /* If we acquired the lock, let's check if the file
  4963. + * still exists in the file system. If not, then the
  4964. + * previous exclusive owner removed it and then closed
  4965. + * it. In such a case our acquired lock is worthless,
  4966. + * hence try again. */
  4967. +
  4968. + if (!glnx_fstat (fd, &st, error))
  4969. + return FALSE;
  4970. + if (st.st_nlink > 0)
  4971. + break;
  4972. +
  4973. + glnx_close_fd (&fd);
  4974. + }
  4975. +
  4976. + /* Note that if this is not AT_FDCWD, the caller takes responsibility
  4977. + * for the fd's lifetime being >= that of the lock.
  4978. + */
  4979. + out_lock->initialized = TRUE;
  4980. + out_lock->dfd = dfd;
  4981. + out_lock->path = g_steal_pointer (&t);
  4982. + out_lock->fd = glnx_steal_fd (&fd);
  4983. + out_lock->operation = operation;
  4984. + return TRUE;
  4985. +}
  4986. +
  4987. +void glnx_release_lock_file(GLnxLockFile *f) {
  4988. + int r;
  4989. +
  4990. + if (!(f && f->initialized))
  4991. + return;
  4992. +
  4993. + if (f->path) {
  4994. +
  4995. + /* If we are the exclusive owner we can safely delete
  4996. + * the lock file itself. If we are not the exclusive
  4997. + * owner, we can try becoming it. */
  4998. +
  4999. + if (f->fd >= 0 &&
  5000. + (f->operation & ~LOCK_NB) == LOCK_SH) {
  5001. +#ifdef F_OFD_SETLK
  5002. + static const struct flock fl = {
  5003. + .l_type = F_WRLCK,
  5004. + .l_whence = SEEK_SET,
  5005. + };
  5006. +
  5007. + r = fcntl(f->fd, F_OFD_SETLK, &fl);
  5008. +#else
  5009. + r = -1;
  5010. + errno = EINVAL;
  5011. +#endif
  5012. + if (r < 0 && errno == EINVAL)
  5013. + r = flock(f->fd, LOCK_EX|LOCK_NB);
  5014. +
  5015. + if (r >= 0)
  5016. + f->operation = LOCK_EX|LOCK_NB;
  5017. + }
  5018. +
  5019. + if ((f->operation & ~LOCK_NB) == LOCK_EX) {
  5020. + (void) unlinkat(f->dfd, f->path, 0);
  5021. + }
  5022. +
  5023. + g_free(f->path);
  5024. + f->path = NULL;
  5025. + }
  5026. +
  5027. + glnx_close_fd (&f->fd);
  5028. + f->operation = 0;
  5029. + f->initialized = FALSE;
  5030. +}
  5031. diff -Nuar ostree-2018.8.orig/libglnx/glnx-lockfile.h ostree-2018.8/libglnx/glnx-lockfile.h
  5032. --- ostree-2018.8.orig/libglnx/glnx-lockfile.h 1970-01-01 02:00:00.000000000 +0200
  5033. +++ ostree-2018.8/libglnx/glnx-lockfile.h 2018-05-26 00:37:32.007018846 +0300
  5034. @@ -0,0 +1,40 @@
  5035. +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  5036. +
  5037. +#pragma once
  5038. +
  5039. +/***
  5040. + This file is part of systemd.
  5041. +
  5042. + Copyright 2011 Lennart Poettering
  5043. + Copyright 2015 Colin Walters <walters@verbum.org>
  5044. +
  5045. + systemd is free software; you can redistribute it and/or modify it
  5046. + under the terms of the GNU Lesser General Public License as published by
  5047. + the Free Software Foundation; either version 2.1 of the License, or
  5048. + (at your option) any later version.
  5049. +
  5050. + systemd is distributed in the hope that it will be useful, but
  5051. + WITHOUT ANY WARRANTY; without even the implied warranty of
  5052. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5053. + Lesser General Public License for more details.
  5054. +
  5055. + You should have received a copy of the GNU Lesser General Public License
  5056. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  5057. +***/
  5058. +
  5059. +#include "config.h"
  5060. +
  5061. +#include "glnx-backport-autoptr.h"
  5062. +
  5063. +typedef struct GLnxLockFile {
  5064. + gboolean initialized;
  5065. + int dfd;
  5066. + char *path;
  5067. + int fd;
  5068. + int operation;
  5069. +} GLnxLockFile;
  5070. +
  5071. +gboolean glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *ret, GError **error);
  5072. +void glnx_release_lock_file(GLnxLockFile *f);
  5073. +
  5074. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxLockFile, glnx_release_lock_file)
  5075. diff -Nuar ostree-2018.8.orig/libglnx/glnx-macros.h ostree-2018.8/libglnx/glnx-macros.h
  5076. --- ostree-2018.8.orig/libglnx/glnx-macros.h 1970-01-01 02:00:00.000000000 +0200
  5077. +++ ostree-2018.8/libglnx/glnx-macros.h 2018-05-26 00:37:32.007018846 +0300
  5078. @@ -0,0 +1,189 @@
  5079. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5080. + *
  5081. + * Copyright (C) 2017 Colin Walters <walters@verbum.org>
  5082. + * With original source from systemd:
  5083. + * Copyright 2010 Lennart Poettering
  5084. + *
  5085. + * This library is free software; you can redistribute it and/or
  5086. + * modify it under the terms of the GNU Lesser General Public
  5087. + * License as published by the Free Software Foundation; either
  5088. + * version 2 of the License, or (at your option) any later version.
  5089. + *
  5090. + * This library is distributed in the hope that it will be useful,
  5091. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5092. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5093. + * Lesser General Public License for more details.
  5094. + *
  5095. + * You should have received a copy of the GNU Lesser General Public
  5096. + * License along with this library; if not, write to the
  5097. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5098. + * Boston, MA 02111-1307, USA.
  5099. + */
  5100. +
  5101. +#pragma once
  5102. +
  5103. +#include <stdlib.h>
  5104. +#include <string.h>
  5105. +#include <gio/gio.h>
  5106. +
  5107. +G_BEGIN_DECLS
  5108. +
  5109. +/* All of these are for C only. */
  5110. +#ifndef __GI_SCANNER__
  5111. +
  5112. +/* Taken from https://github.com/systemd/systemd/src/basic/string-util.h
  5113. + * at revision v228-666-gcf6c8c4
  5114. + */
  5115. +#define glnx_strjoina(a, ...) \
  5116. + ({ \
  5117. + const char *_appendees_[] = { a, __VA_ARGS__ }; \
  5118. + char *_d_, *_p_; \
  5119. + size_t _len_ = 0; \
  5120. + unsigned _i_; \
  5121. + for (_i_ = 0; _i_ < G_N_ELEMENTS(_appendees_) && _appendees_[_i_]; _i_++) \
  5122. + _len_ += strlen(_appendees_[_i_]); \
  5123. + _p_ = _d_ = alloca(_len_ + 1); \
  5124. + for (_i_ = 0; _i_ < G_N_ELEMENTS(_appendees_) && _appendees_[_i_]; _i_++) \
  5125. + _p_ = stpcpy(_p_, _appendees_[_i_]); \
  5126. + *_p_ = 0; \
  5127. + _d_; \
  5128. + })
  5129. +
  5130. +#ifndef G_IN_SET
  5131. +
  5132. +/* Infrastructure for `G_IN_SET`; this code is copied from
  5133. + * systemd's macro.h - please treat that version as canonical
  5134. + * and submit patches first to systemd.
  5135. + */
  5136. +#define _G_INSET_CASE_F(X) case X:
  5137. +#define _G_INSET_CASE_F_1(CASE, X) _G_INSET_CASE_F(X)
  5138. +#define _G_INSET_CASE_F_2(CASE, X, ...) CASE(X) _G_INSET_CASE_F_1(CASE, __VA_ARGS__)
  5139. +#define _G_INSET_CASE_F_3(CASE, X, ...) CASE(X) _G_INSET_CASE_F_2(CASE, __VA_ARGS__)
  5140. +#define _G_INSET_CASE_F_4(CASE, X, ...) CASE(X) _G_INSET_CASE_F_3(CASE, __VA_ARGS__)
  5141. +#define _G_INSET_CASE_F_5(CASE, X, ...) CASE(X) _G_INSET_CASE_F_4(CASE, __VA_ARGS__)
  5142. +#define _G_INSET_CASE_F_6(CASE, X, ...) CASE(X) _G_INSET_CASE_F_5(CASE, __VA_ARGS__)
  5143. +#define _G_INSET_CASE_F_7(CASE, X, ...) CASE(X) _G_INSET_CASE_F_6(CASE, __VA_ARGS__)
  5144. +#define _G_INSET_CASE_F_8(CASE, X, ...) CASE(X) _G_INSET_CASE_F_7(CASE, __VA_ARGS__)
  5145. +#define _G_INSET_CASE_F_9(CASE, X, ...) CASE(X) _G_INSET_CASE_F_8(CASE, __VA_ARGS__)
  5146. +#define _G_INSET_CASE_F_10(CASE, X, ...) CASE(X) _G_INSET_CASE_F_9(CASE, __VA_ARGS__)
  5147. +#define _G_INSET_CASE_F_11(CASE, X, ...) CASE(X) _G_INSET_CASE_F_10(CASE, __VA_ARGS__)
  5148. +#define _G_INSET_CASE_F_12(CASE, X, ...) CASE(X) _G_INSET_CASE_F_11(CASE, __VA_ARGS__)
  5149. +#define _G_INSET_CASE_F_13(CASE, X, ...) CASE(X) _G_INSET_CASE_F_12(CASE, __VA_ARGS__)
  5150. +#define _G_INSET_CASE_F_14(CASE, X, ...) CASE(X) _G_INSET_CASE_F_13(CASE, __VA_ARGS__)
  5151. +#define _G_INSET_CASE_F_15(CASE, X, ...) CASE(X) _G_INSET_CASE_F_14(CASE, __VA_ARGS__)
  5152. +#define _G_INSET_CASE_F_16(CASE, X, ...) CASE(X) _G_INSET_CASE_F_15(CASE, __VA_ARGS__)
  5153. +#define _G_INSET_CASE_F_17(CASE, X, ...) CASE(X) _G_INSET_CASE_F_16(CASE, __VA_ARGS__)
  5154. +#define _G_INSET_CASE_F_18(CASE, X, ...) CASE(X) _G_INSET_CASE_F_17(CASE, __VA_ARGS__)
  5155. +#define _G_INSET_CASE_F_19(CASE, X, ...) CASE(X) _G_INSET_CASE_F_18(CASE, __VA_ARGS__)
  5156. +#define _G_INSET_CASE_F_20(CASE, X, ...) CASE(X) _G_INSET_CASE_F_19(CASE, __VA_ARGS__)
  5157. +
  5158. +#define _G_INSET_GET_CASE_F(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,NAME,...) NAME
  5159. +#define _G_INSET_FOR_EACH_MAKE_CASE(...) \
  5160. + _G_INSET_GET_CASE_F(__VA_ARGS__,_G_INSET_CASE_F_20,_G_INSET_CASE_F_19,_G_INSET_CASE_F_18,_G_INSET_CASE_F_17,_G_INSET_CASE_F_16,_G_INSET_CASE_F_15,_G_INSET_CASE_F_14,_G_INSET_CASE_F_13,_G_INSET_CASE_F_12,_G_INSET_CASE_F_11, \
  5161. + _G_INSET_CASE_F_10,_G_INSET_CASE_F_9,_G_INSET_CASE_F_8,_G_INSET_CASE_F_7,_G_INSET_CASE_F_6,_G_INSET_CASE_F_5,_G_INSET_CASE_F_4,_G_INSET_CASE_F_3,_G_INSET_CASE_F_2,_G_INSET_CASE_F_1) \
  5162. + (_G_INSET_CASE_F,__VA_ARGS__)
  5163. +
  5164. +/* Note: claiming the name here even though it isn't upstream yet
  5165. + * https://bugzilla.gnome.org/show_bug.cgi?id=783751
  5166. + */
  5167. +/**
  5168. + * G_IN_SET:
  5169. + * @x: Integer (or smaller) sized value
  5170. + * @...: Elements to compare
  5171. + *
  5172. + * It's quite common to test whether or not `char` values or Unix @errno (among) others
  5173. + * are members of a small set. Normally one has to choose to either use `if (x == val || x == otherval ...)`
  5174. + * or a `switch` statement. This macro is useful to reduce duplication in the first case,
  5175. + * where one can write simply `if (G_IN_SET (x, val, otherval))`, and avoid the verbosity
  5176. + * that the `switch` statement requires.
  5177. + */
  5178. +#define G_IN_SET(x, ...) \
  5179. + ({ \
  5180. + gboolean _g_inset_found = FALSE; \
  5181. + /* If the build breaks in the line below, you need to extend the case macros */ \
  5182. + static G_GNUC_UNUSED char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){__VA_ARGS__})/sizeof(int)]; \
  5183. + switch(x) { \
  5184. + _G_INSET_FOR_EACH_MAKE_CASE(__VA_ARGS__) \
  5185. + _g_inset_found = TRUE; \
  5186. + break; \
  5187. + default: \
  5188. + break; \
  5189. + } \
  5190. + _g_inset_found; \
  5191. + })
  5192. +
  5193. +#endif /* ifndef G_IN_SET */
  5194. +
  5195. +#define _GLNX_CONCAT(a, b) a##b
  5196. +#define _GLNX_CONCAT_INDIRECT(a, b) _GLNX_CONCAT(a, b)
  5197. +#define _GLNX_MAKE_ANONYMOUS(a) _GLNX_CONCAT_INDIRECT(a, __COUNTER__)
  5198. +
  5199. +#define _GLNX_HASH_TABLE_FOREACH_IMPL_KV(guard, ht, it, kt, k, vt, v) \
  5200. + gboolean guard = TRUE; \
  5201. + G_STATIC_ASSERT (sizeof (kt) == sizeof (void*)); \
  5202. + G_STATIC_ASSERT (sizeof (vt) == sizeof (void*)); \
  5203. + for (GHashTableIter it; \
  5204. + guard && ({ g_hash_table_iter_init (&it, ht), TRUE; }); \
  5205. + guard = FALSE) \
  5206. + for (kt k; guard; guard = FALSE) \
  5207. + for (vt v; g_hash_table_iter_next (&it, (gpointer)&k, (gpointer)&v);)
  5208. +
  5209. +
  5210. +/* Cleaner method to iterate over a GHashTable. I.e. rather than
  5211. + *
  5212. + * gpointer k, v;
  5213. + * GHashTableIter it;
  5214. + * g_hash_table_iter_init (&it, table);
  5215. + * while (g_hash_table_iter_next (&it, &k, &v))
  5216. + * {
  5217. + * const char *str = k;
  5218. + * GPtrArray *arr = v;
  5219. + * ...
  5220. + * }
  5221. + *
  5222. + * you can simply do
  5223. + *
  5224. + * GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, str, GPtrArray*, arr)
  5225. + * {
  5226. + * ...
  5227. + * }
  5228. + *
  5229. + * All variables are scoped within the loop. You may use the `it` variable as
  5230. + * usual, e.g. to remove an element using g_hash_table_iter_remove(&it). There
  5231. + * are shorter variants for the more common cases where you do not need access
  5232. + * to the iterator or to keys/values:
  5233. + *
  5234. + * GLNX_HASH_TABLE_FOREACH (table, const char*, str) { ... }
  5235. + * GLNX_HASH_TABLE_FOREACH_V (table, MyData*, data) { ... }
  5236. + * GLNX_HASH_TABLE_FOREACH_KV (table, const char*, str, MyData*, data) { ... }
  5237. + *
  5238. + */
  5239. +#define GLNX_HASH_TABLE_FOREACH_IT(ht, it, kt, k, vt, v) \
  5240. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  5241. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, it, kt, k, vt, v)
  5242. +
  5243. +/* Variant of GLNX_HASH_TABLE_FOREACH without having to specify an iterator. An
  5244. + * anonymous iterator will be created. */
  5245. +#define GLNX_HASH_TABLE_FOREACH_KV(ht, kt, k, vt, v) \
  5246. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  5247. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  5248. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), kt, k, vt, v)
  5249. +
  5250. +/* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking keys. */
  5251. +#define GLNX_HASH_TABLE_FOREACH_V(ht, vt, v) \
  5252. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  5253. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  5254. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), \
  5255. + gpointer, _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_v_), \
  5256. + vt, v)
  5257. +
  5258. +/* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking vals. */
  5259. +#define GLNX_HASH_TABLE_FOREACH(ht, kt, k) \
  5260. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  5261. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  5262. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), kt, k, \
  5263. + gpointer, _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_v_))
  5264. +
  5265. +#endif /* GI_SCANNER */
  5266. +
  5267. +G_END_DECLS
  5268. diff -Nuar ostree-2018.8.orig/libglnx/glnx-missing.h ostree-2018.8/libglnx/glnx-missing.h
  5269. --- ostree-2018.8.orig/libglnx/glnx-missing.h 1970-01-01 02:00:00.000000000 +0200
  5270. +++ ostree-2018.8/libglnx/glnx-missing.h 2018-05-26 00:37:32.007018846 +0300
  5271. @@ -0,0 +1,95 @@
  5272. +#pragma once
  5273. +
  5274. +/***
  5275. + This file was originally part of systemd.
  5276. +
  5277. + Copyright 2010 Lennart Poettering
  5278. +
  5279. + systemd is free software; you can redistribute it and/or modify it
  5280. + under the terms of the GNU Lesser General Public License as published by
  5281. + the Free Software Foundation; either version 2.1 of the License, or
  5282. + (at your option) any later version.
  5283. +
  5284. + systemd is distributed in the hope that it will be useful, but
  5285. + WITHOUT ANY WARRANTY; without even the implied warranty of
  5286. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5287. + Lesser General Public License for more details.
  5288. +
  5289. + You should have received a copy of the GNU Lesser General Public License
  5290. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  5291. +***/
  5292. +
  5293. +/* Missing glibc definitions to access certain kernel APIs.
  5294. + This file is last updated from systemd git:
  5295. +
  5296. + commit 71e5200f94b22589922704aa4abdf95d4fe2e528
  5297. + Author: Daniel Mack <daniel@zonque.org>
  5298. + AuthorDate: Tue Oct 18 17:57:10 2016 +0200
  5299. + Commit: Lennart Poettering <lennart@poettering.net>
  5300. + CommitDate: Fri Sep 22 15:24:54 2017 +0200
  5301. +
  5302. + Add abstraction model for BPF programs
  5303. +*/
  5304. +
  5305. +#include <errno.h>
  5306. +#include <fcntl.h>
  5307. +#include <stdlib.h>
  5308. +#include <sys/resource.h>
  5309. +#include <sys/syscall.h>
  5310. +#include <uchar.h>
  5311. +#include <unistd.h>
  5312. +
  5313. +/* The precise definition of __O_TMPFILE is arch specific; use the
  5314. + * values defined by the kernel (note: some are hexa, some are octal,
  5315. + * duplicated as-is from the kernel definitions):
  5316. + * - alpha, parisc, sparc: each has a specific value;
  5317. + * - others: they use the "generic" value.
  5318. + */
  5319. +
  5320. +#ifndef __O_TMPFILE
  5321. +#if defined(__alpha__)
  5322. +#define __O_TMPFILE 0100000000
  5323. +#elif defined(__parisc__) || defined(__hppa__)
  5324. +#define __O_TMPFILE 0400000000
  5325. +#elif defined(__sparc__) || defined(__sparc64__)
  5326. +#define __O_TMPFILE 0x2000000
  5327. +#else
  5328. +#define __O_TMPFILE 020000000
  5329. +#endif
  5330. +#endif
  5331. +
  5332. +/* a horrid kludge trying to make sure that this will fail on old kernels */
  5333. +#ifndef O_TMPFILE
  5334. +#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
  5335. +#endif
  5336. +
  5337. +#ifndef RENAME_NOREPLACE
  5338. +#define RENAME_NOREPLACE (1 << 0)
  5339. +#endif
  5340. +#ifndef RENAME_EXCHANGE
  5341. +#define RENAME_EXCHANGE (1 << 1)
  5342. +#endif
  5343. +
  5344. +#ifndef F_LINUX_SPECIFIC_BASE
  5345. +#define F_LINUX_SPECIFIC_BASE 1024
  5346. +#endif
  5347. +
  5348. +#ifndef F_ADD_SEALS
  5349. +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
  5350. +#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
  5351. +
  5352. +#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
  5353. +#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
  5354. +#define F_SEAL_GROW 0x0004 /* prevent file from growing */
  5355. +#define F_SEAL_WRITE 0x0008 /* prevent writes */
  5356. +#endif
  5357. +
  5358. +#ifndef MFD_ALLOW_SEALING
  5359. +#define MFD_ALLOW_SEALING 0x0002U
  5360. +#endif
  5361. +
  5362. +#ifndef MFD_CLOEXEC
  5363. +#define MFD_CLOEXEC 0x0001U
  5364. +#endif
  5365. +
  5366. +#include "glnx-missing-syscall.h"
  5367. diff -Nuar ostree-2018.8.orig/libglnx/glnx-missing-syscall.h ostree-2018.8/libglnx/glnx-missing-syscall.h
  5368. --- ostree-2018.8.orig/libglnx/glnx-missing-syscall.h 1970-01-01 02:00:00.000000000 +0200
  5369. +++ ostree-2018.8/libglnx/glnx-missing-syscall.h 2018-05-26 00:37:32.007018846 +0300
  5370. @@ -0,0 +1,156 @@
  5371. +/***
  5372. + This file was originally part of systemd.
  5373. +
  5374. + Copyright 2010 Lennart Poettering
  5375. + Copyright 2016 Zbigniew Jędrzejewski-Szmek
  5376. +
  5377. + systemd is free software; you can redistribute it and/or modify it
  5378. + under the terms of the GNU Lesser General Public License as published by
  5379. + the Free Software Foundation; either version 2.1 of the License, or
  5380. + (at your option) any later version.
  5381. +
  5382. + systemd is distributed in the hope that it will be useful, but
  5383. + WITHOUT ANY WARRANTY; without even the implied warranty of
  5384. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5385. + Lesser General Public License for more details.
  5386. +
  5387. + You should have received a copy of the GNU Lesser General Public License
  5388. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  5389. +***/
  5390. +
  5391. +/* Missing glibc definitions to access certain kernel APIs.
  5392. + This file is last updated from systemd git:
  5393. +
  5394. + commit 71e5200f94b22589922704aa4abdf95d4fe2e528
  5395. + Author: Daniel Mack <daniel@zonque.org>
  5396. + AuthorDate: Tue Oct 18 17:57:10 2016 +0200
  5397. + Commit: Lennart Poettering <lennart@poettering.net>
  5398. + CommitDate: Fri Sep 22 15:24:54 2017 +0200
  5399. +
  5400. + Add abstraction model for BPF programs
  5401. +*/
  5402. +
  5403. +#include "config.h"
  5404. +
  5405. +#if !HAVE_DECL_RENAMEAT2
  5406. +# ifndef __NR_renameat2
  5407. +# if defined __x86_64__
  5408. +# define __NR_renameat2 316
  5409. +# elif defined __arm__
  5410. +# define __NR_renameat2 382
  5411. +# elif defined __aarch64__
  5412. +# define __NR_renameat2 276
  5413. +# elif defined _MIPS_SIM
  5414. +# if _MIPS_SIM == _MIPS_SIM_ABI32
  5415. +# define __NR_renameat2 4351
  5416. +# endif
  5417. +# if _MIPS_SIM == _MIPS_SIM_NABI32
  5418. +# define __NR_renameat2 6315
  5419. +# endif
  5420. +# if _MIPS_SIM == _MIPS_SIM_ABI64
  5421. +# define __NR_renameat2 5311
  5422. +# endif
  5423. +# elif defined __i386__
  5424. +# define __NR_renameat2 353
  5425. +# elif defined __powerpc64__
  5426. +# define __NR_renameat2 357
  5427. +# elif defined __s390__ || defined __s390x__
  5428. +# define __NR_renameat2 347
  5429. +# elif defined __arc__
  5430. +# define __NR_renameat2 276
  5431. +# else
  5432. +# warning "__NR_renameat2 unknown for your architecture"
  5433. +# endif
  5434. +# endif
  5435. +
  5436. +static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
  5437. +# ifdef __NR_renameat2
  5438. + return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
  5439. +# else
  5440. + errno = ENOSYS;
  5441. + return -1;
  5442. +# endif
  5443. +}
  5444. +#endif
  5445. +
  5446. +#if !HAVE_DECL_MEMFD_CREATE
  5447. +# ifndef __NR_memfd_create
  5448. +# if defined __x86_64__
  5449. +# define __NR_memfd_create 319
  5450. +# elif defined __arm__
  5451. +# define __NR_memfd_create 385
  5452. +# elif defined __aarch64__
  5453. +# define __NR_memfd_create 279
  5454. +# elif defined __s390__
  5455. +# define __NR_memfd_create 350
  5456. +# elif defined _MIPS_SIM
  5457. +# if _MIPS_SIM == _MIPS_SIM_ABI32
  5458. +# define __NR_memfd_create 4354
  5459. +# endif
  5460. +# if _MIPS_SIM == _MIPS_SIM_NABI32
  5461. +# define __NR_memfd_create 6318
  5462. +# endif
  5463. +# if _MIPS_SIM == _MIPS_SIM_ABI64
  5464. +# define __NR_memfd_create 5314
  5465. +# endif
  5466. +# elif defined __i386__
  5467. +# define __NR_memfd_create 356
  5468. +# elif defined __arc__
  5469. +# define __NR_memfd_create 279
  5470. +# else
  5471. +# warning "__NR_memfd_create unknown for your architecture"
  5472. +# endif
  5473. +# endif
  5474. +
  5475. +static inline int memfd_create(const char *name, unsigned int flags) {
  5476. +# ifdef __NR_memfd_create
  5477. + return syscall(__NR_memfd_create, name, flags);
  5478. +# else
  5479. + errno = ENOSYS;
  5480. + return -1;
  5481. +# endif
  5482. +}
  5483. +#endif
  5484. +
  5485. +/* Copied from systemd git:
  5486. + commit 6bda23dd6aaba50cf8e3e6024248cf736cc443ca
  5487. + Author: Yu Watanabe <watanabe.yu+github@gmail.com>
  5488. + AuthorDate: Thu Jul 27 20:22:54 2017 +0900
  5489. + Commit: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
  5490. + CommitDate: Thu Jul 27 07:22:54 2017 -0400
  5491. +*/
  5492. +#if !HAVE_DECL_COPY_FILE_RANGE
  5493. +# ifndef __NR_copy_file_range
  5494. +# if defined(__x86_64__)
  5495. +# define __NR_copy_file_range 326
  5496. +# elif defined(__i386__)
  5497. +# define __NR_copy_file_range 377
  5498. +# elif defined __s390__
  5499. +# define __NR_copy_file_range 375
  5500. +# elif defined __arm__
  5501. +# define __NR_copy_file_range 391
  5502. +# elif defined __aarch64__
  5503. +# define __NR_copy_file_range 285
  5504. +# elif defined __powerpc__
  5505. +# define __NR_copy_file_range 379
  5506. +# elif defined __arc__
  5507. +# define __NR_copy_file_range 285
  5508. +# else
  5509. +# warning "__NR_copy_file_range not defined for your architecture"
  5510. +# endif
  5511. +# endif
  5512. +
  5513. +static inline ssize_t missing_copy_file_range(int fd_in, loff_t *off_in,
  5514. + int fd_out, loff_t *off_out,
  5515. + size_t len,
  5516. + unsigned int flags) {
  5517. +# ifdef __NR_copy_file_range
  5518. + return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
  5519. +# else
  5520. + errno = ENOSYS;
  5521. + return -1;
  5522. +# endif
  5523. +}
  5524. +
  5525. +# define copy_file_range missing_copy_file_range
  5526. +#endif
  5527. diff -Nuar ostree-2018.8.orig/libglnx/glnx-shutil.c ostree-2018.8/libglnx/glnx-shutil.c
  5528. --- ostree-2018.8.orig/libglnx/glnx-shutil.c 1970-01-01 02:00:00.000000000 +0200
  5529. +++ ostree-2018.8/libglnx/glnx-shutil.c 2018-05-26 00:37:32.007018846 +0300
  5530. @@ -0,0 +1,260 @@
  5531. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5532. + *
  5533. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  5534. + *
  5535. + * This library is free software; you can redistribute it and/or
  5536. + * modify it under the terms of the GNU Lesser General Public
  5537. + * License as published by the Free Software Foundation; either
  5538. + * version 2 of the License, or (at your option) any later version.
  5539. + *
  5540. + * This library is distributed in the hope that it will be useful,
  5541. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5542. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5543. + * Lesser General Public License for more details.
  5544. + *
  5545. + * You should have received a copy of the GNU Lesser General Public
  5546. + * License along with this library; if not, write to the
  5547. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5548. + * Boston, MA 02111-1307, USA.
  5549. + */
  5550. +
  5551. +#include "config.h"
  5552. +
  5553. +#include <string.h>
  5554. +
  5555. +#include <glnx-shutil.h>
  5556. +#include <glnx-errors.h>
  5557. +#include <glnx-local-alloc.h>
  5558. +
  5559. +static gboolean
  5560. +glnx_shutil_rm_rf_children (GLnxDirFdIterator *dfd_iter,
  5561. + GCancellable *cancellable,
  5562. + GError **error)
  5563. +{
  5564. + struct dirent *dent;
  5565. +
  5566. + while (TRUE)
  5567. + {
  5568. + if (!glnx_dirfd_iterator_next_dent_ensure_dtype (dfd_iter, &dent, cancellable, error))
  5569. + return FALSE;
  5570. + if (dent == NULL)
  5571. + break;
  5572. +
  5573. + if (dent->d_type == DT_DIR)
  5574. + {
  5575. + g_auto(GLnxDirFdIterator) child_dfd_iter = { 0, };
  5576. +
  5577. + if (!glnx_dirfd_iterator_init_at (dfd_iter->fd, dent->d_name, FALSE,
  5578. + &child_dfd_iter, error))
  5579. + return FALSE;
  5580. +
  5581. + if (!glnx_shutil_rm_rf_children (&child_dfd_iter, cancellable, error))
  5582. + return FALSE;
  5583. +
  5584. + if (unlinkat (dfd_iter->fd, dent->d_name, AT_REMOVEDIR) == -1)
  5585. + return glnx_throw_errno_prefix (error, "unlinkat");
  5586. + }
  5587. + else
  5588. + {
  5589. + if (unlinkat (dfd_iter->fd, dent->d_name, 0) == -1)
  5590. + {
  5591. + if (errno != ENOENT)
  5592. + return glnx_throw_errno_prefix (error, "unlinkat");
  5593. + }
  5594. + }
  5595. + }
  5596. +
  5597. + return TRUE;
  5598. +}
  5599. +
  5600. +/**
  5601. + * glnx_shutil_rm_rf_at:
  5602. + * @dfd: A directory file descriptor, or `AT_FDCWD` or `-1` for current
  5603. + * @path: Path
  5604. + * @cancellable: Cancellable
  5605. + * @error: Error
  5606. + *
  5607. + * Recursively delete the filename referenced by the combination of
  5608. + * the directory fd @dfd and @path; it may be a file or directory. No
  5609. + * error is thrown if @path does not exist.
  5610. + */
  5611. +gboolean
  5612. +glnx_shutil_rm_rf_at (int dfd,
  5613. + const char *path,
  5614. + GCancellable *cancellable,
  5615. + GError **error)
  5616. +{
  5617. + dfd = glnx_dirfd_canonicalize (dfd);
  5618. +
  5619. +
  5620. + /* With O_NOFOLLOW first */
  5621. + glnx_autofd int target_dfd =
  5622. + openat (dfd, path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
  5623. +
  5624. + if (target_dfd == -1)
  5625. + {
  5626. + int errsv = errno;
  5627. + if (errsv == ENOENT)
  5628. + {
  5629. + ;
  5630. + }
  5631. + else if (errsv == ENOTDIR || errsv == ELOOP)
  5632. + {
  5633. + if (unlinkat (dfd, path, 0) != 0)
  5634. + return glnx_throw_errno_prefix (error, "unlinkat");
  5635. + }
  5636. + else
  5637. + return glnx_throw_errno_prefix (error, "open(%s)", path);
  5638. + }
  5639. + else
  5640. + {
  5641. + g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
  5642. + if (!glnx_dirfd_iterator_init_take_fd (&target_dfd, &dfd_iter, error))
  5643. + return FALSE;
  5644. +
  5645. + if (!glnx_shutil_rm_rf_children (&dfd_iter, cancellable, error))
  5646. + return FALSE;
  5647. +
  5648. + if (unlinkat (dfd, path, AT_REMOVEDIR) == -1)
  5649. + {
  5650. + if (errno != ENOENT)
  5651. + return glnx_throw_errno_prefix (error, "unlinkat");
  5652. + }
  5653. + }
  5654. +
  5655. + return TRUE;
  5656. +}
  5657. +
  5658. +static gboolean
  5659. +mkdir_p_at_internal (int dfd,
  5660. + char *path,
  5661. + int mode,
  5662. + GCancellable *cancellable,
  5663. + GError **error)
  5664. +{
  5665. + gboolean did_recurse = FALSE;
  5666. +
  5667. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  5668. + return FALSE;
  5669. +
  5670. + again:
  5671. + if (mkdirat (dfd, path, mode) == -1)
  5672. + {
  5673. + if (errno == ENOENT)
  5674. + {
  5675. + char *lastslash;
  5676. +
  5677. + g_assert (!did_recurse);
  5678. +
  5679. + lastslash = strrchr (path, '/');
  5680. + if (lastslash == NULL)
  5681. + {
  5682. + /* This can happen if @dfd was deleted between being opened and
  5683. + * passed to mkdir_p_at_internal(). */
  5684. + return glnx_throw_errno_prefix (error, "mkdir(%s)", path);
  5685. + }
  5686. +
  5687. + /* Note we can mutate the buffer as we dup'd it */
  5688. + *lastslash = '\0';
  5689. +
  5690. + if (!glnx_shutil_mkdir_p_at (dfd, path, mode,
  5691. + cancellable, error))
  5692. + return FALSE;
  5693. +
  5694. + /* Now restore it for another mkdir attempt */
  5695. + *lastslash = '/';
  5696. +
  5697. + did_recurse = TRUE;
  5698. + goto again;
  5699. + }
  5700. + else if (errno == EEXIST)
  5701. + {
  5702. + /* Fall through; it may not have been a directory,
  5703. + * but we'll find that out on the next call up.
  5704. + */
  5705. + }
  5706. + else
  5707. + return glnx_throw_errno_prefix (error, "mkdir(%s)", path);
  5708. + }
  5709. +
  5710. + return TRUE;
  5711. +}
  5712. +
  5713. +/**
  5714. + * glnx_shutil_mkdir_p_at:
  5715. + * @dfd: Directory fd
  5716. + * @path: Directory path to be created
  5717. + * @mode: Mode for newly created directories
  5718. + * @cancellable: Cancellable
  5719. + * @error: Error
  5720. + *
  5721. + * Similar to g_mkdir_with_parents(), except operates relative to the
  5722. + * directory fd @dfd.
  5723. + *
  5724. + * See also glnx_ensure_dir() for a non-recursive version.
  5725. + *
  5726. + * This will return %G_IO_ERROR_NOT_FOUND if @dfd has been deleted since being
  5727. + * opened. It may return other errors from mkdirat() in other situations.
  5728. + */
  5729. +gboolean
  5730. +glnx_shutil_mkdir_p_at (int dfd,
  5731. + const char *path,
  5732. + int mode,
  5733. + GCancellable *cancellable,
  5734. + GError **error)
  5735. +{
  5736. + struct stat stbuf;
  5737. + char *buf;
  5738. +
  5739. + /* Fast path stat to see whether it already exists */
  5740. + if (fstatat (dfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
  5741. + {
  5742. + /* Note early return */
  5743. + if (S_ISDIR (stbuf.st_mode))
  5744. + return TRUE;
  5745. + }
  5746. +
  5747. + buf = strdupa (path);
  5748. +
  5749. + if (!mkdir_p_at_internal (dfd, buf, mode, cancellable, error))
  5750. + return FALSE;
  5751. +
  5752. + return TRUE;
  5753. +}
  5754. +
  5755. +/**
  5756. + * glnx_shutil_mkdir_p_at_open:
  5757. + * @dfd: Directory fd
  5758. + * @path: Directory path to be created
  5759. + * @mode: Mode for newly created directories
  5760. + * @out_dfd: (out caller-allocates): Return location for an FD to @dfd/@path,
  5761. + * or `-1` on error
  5762. + * @cancellable: (nullable): Cancellable, or %NULL
  5763. + * @error: Return location for a #GError, or %NULL
  5764. + *
  5765. + * Similar to glnx_shutil_mkdir_p_at(), except it opens the resulting directory
  5766. + * and returns a directory FD to it. Currently, this is not guaranteed to be
  5767. + * race-free.
  5768. + *
  5769. + * Returns: %TRUE on success, %FALSE otherwise
  5770. + * Since: UNRELEASED
  5771. + */
  5772. +gboolean
  5773. +glnx_shutil_mkdir_p_at_open (int dfd,
  5774. + const char *path,
  5775. + int mode,
  5776. + int *out_dfd,
  5777. + GCancellable *cancellable,
  5778. + GError **error)
  5779. +{
  5780. + /* FIXME: It’s not possible to eliminate the race here until
  5781. + * openat(O_DIRECTORY | O_CREAT) works (and returns a directory rather than a
  5782. + * file). It appears to be not supported in current kernels. (Tested with
  5783. + * 4.10.10-200.fc25.x86_64.) */
  5784. + *out_dfd = -1;
  5785. +
  5786. + if (!glnx_shutil_mkdir_p_at (dfd, path, mode, cancellable, error))
  5787. + return FALSE;
  5788. +
  5789. + return glnx_opendirat (dfd, path, TRUE, out_dfd, error);
  5790. +}
  5791. diff -Nuar ostree-2018.8.orig/libglnx/glnx-shutil.h ostree-2018.8/libglnx/glnx-shutil.h
  5792. --- ostree-2018.8.orig/libglnx/glnx-shutil.h 1970-01-01 02:00:00.000000000 +0200
  5793. +++ ostree-2018.8/libglnx/glnx-shutil.h 2018-05-26 00:37:32.008018846 +0300
  5794. @@ -0,0 +1,48 @@
  5795. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5796. + *
  5797. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  5798. + *
  5799. + * This library is free software; you can redistribute it and/or
  5800. + * modify it under the terms of the GNU Lesser General Public
  5801. + * License as published by the Free Software Foundation; either
  5802. + * version 2 of the License, or (at your option) any later version.
  5803. + *
  5804. + * This library is distributed in the hope that it will be useful,
  5805. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5806. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5807. + * Lesser General Public License for more details.
  5808. + *
  5809. + * You should have received a copy of the GNU Lesser General Public
  5810. + * License along with this library; if not, write to the
  5811. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5812. + * Boston, MA 02111-1307, USA.
  5813. + */
  5814. +
  5815. +#pragma once
  5816. +
  5817. +#include <glnx-dirfd.h>
  5818. +
  5819. +G_BEGIN_DECLS
  5820. +
  5821. +gboolean
  5822. +glnx_shutil_rm_rf_at (int dfd,
  5823. + const char *path,
  5824. + GCancellable *cancellable,
  5825. + GError **error);
  5826. +
  5827. +gboolean
  5828. +glnx_shutil_mkdir_p_at (int dfd,
  5829. + const char *path,
  5830. + int mode,
  5831. + GCancellable *cancellable,
  5832. + GError **error);
  5833. +
  5834. +gboolean
  5835. +glnx_shutil_mkdir_p_at_open (int dfd,
  5836. + const char *path,
  5837. + int mode,
  5838. + int *out_dfd,
  5839. + GCancellable *cancellable,
  5840. + GError **error);
  5841. +
  5842. +G_END_DECLS
  5843. diff -Nuar ostree-2018.8.orig/libglnx/glnx-xattrs.c ostree-2018.8/libglnx/glnx-xattrs.c
  5844. --- ostree-2018.8.orig/libglnx/glnx-xattrs.c 1970-01-01 02:00:00.000000000 +0200
  5845. +++ ostree-2018.8/libglnx/glnx-xattrs.c 2018-05-26 00:37:32.008018846 +0300
  5846. @@ -0,0 +1,444 @@
  5847. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5848. + *
  5849. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  5850. + *
  5851. + * This library is free software; you can redistribute it and/or
  5852. + * modify it under the terms of the GNU Lesser General Public
  5853. + * License as published by the Free Software Foundation; either
  5854. + * version 2 of the License, or (at your option) any later version.
  5855. + *
  5856. + * This library is distributed in the hope that it will be useful,
  5857. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5858. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5859. + * Lesser General Public License for more details.
  5860. + *
  5861. + * You should have received a copy of the GNU Lesser General Public
  5862. + * License along with this library; if not, write to the
  5863. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5864. + * Boston, MA 02111-1307, USA.
  5865. + */
  5866. +
  5867. +#include "config.h"
  5868. +
  5869. +#include <string.h>
  5870. +#include <stdio.h>
  5871. +
  5872. +#include <glnx-macros.h>
  5873. +#include <glnx-xattrs.h>
  5874. +#include <glnx-errors.h>
  5875. +#include <glnx-local-alloc.h>
  5876. +
  5877. +static GVariant *
  5878. +variant_new_ay_bytes (GBytes *bytes)
  5879. +{
  5880. + gsize size;
  5881. + gconstpointer data;
  5882. + data = g_bytes_get_data (bytes, &size);
  5883. + g_bytes_ref (bytes);
  5884. + return g_variant_new_from_data (G_VARIANT_TYPE ("ay"), data, size,
  5885. + TRUE, (GDestroyNotify)g_bytes_unref, bytes);
  5886. +}
  5887. +
  5888. +static char *
  5889. +canonicalize_xattrs (char *xattr_string,
  5890. + size_t len)
  5891. +{
  5892. + char *p;
  5893. + GSList *xattrs = NULL;
  5894. + GSList *iter;
  5895. + GString *result;
  5896. +
  5897. + result = g_string_new (0);
  5898. +
  5899. + p = xattr_string;
  5900. + while (p < xattr_string+len)
  5901. + {
  5902. + xattrs = g_slist_prepend (xattrs, p);
  5903. + p += strlen (p) + 1;
  5904. + }
  5905. +
  5906. + xattrs = g_slist_sort (xattrs, (GCompareFunc) strcmp);
  5907. + for (iter = xattrs; iter; iter = iter->next) {
  5908. + g_string_append (result, iter->data);
  5909. + g_string_append_c (result, '\0');
  5910. + }
  5911. +
  5912. + g_slist_free (xattrs);
  5913. + return g_string_free (result, FALSE);
  5914. +}
  5915. +
  5916. +static gboolean
  5917. +read_xattr_name_array (const char *path,
  5918. + int fd,
  5919. + const char *xattrs,
  5920. + size_t len,
  5921. + GVariantBuilder *builder,
  5922. + GError **error)
  5923. +{
  5924. + gboolean ret = FALSE;
  5925. + const char *p;
  5926. + int r;
  5927. + const char *funcstr;
  5928. +
  5929. + g_assert (path != NULL || fd != -1);
  5930. +
  5931. + funcstr = fd != -1 ? "fgetxattr" : "lgetxattr";
  5932. +
  5933. + for (p = xattrs; p < xattrs+len; p = p + strlen (p) + 1)
  5934. + {
  5935. + ssize_t bytes_read;
  5936. + g_autofree char *buf = NULL;
  5937. + g_autoptr(GBytes) bytes = NULL;
  5938. +
  5939. + again:
  5940. + if (fd != -1)
  5941. + bytes_read = fgetxattr (fd, p, NULL, 0);
  5942. + else
  5943. + bytes_read = lgetxattr (path, p, NULL, 0);
  5944. + if (bytes_read < 0)
  5945. + {
  5946. + if (errno == ENODATA)
  5947. + continue;
  5948. +
  5949. + glnx_set_prefix_error_from_errno (error, "%s", funcstr);
  5950. + goto out;
  5951. + }
  5952. + if (bytes_read == 0)
  5953. + continue;
  5954. +
  5955. + buf = g_malloc (bytes_read);
  5956. + if (fd != -1)
  5957. + r = fgetxattr (fd, p, buf, bytes_read);
  5958. + else
  5959. + r = lgetxattr (path, p, buf, bytes_read);
  5960. + if (r < 0)
  5961. + {
  5962. + if (errno == ERANGE)
  5963. + {
  5964. + g_free (g_steal_pointer (&buf));
  5965. + goto again;
  5966. + }
  5967. + else if (errno == ENODATA)
  5968. + continue;
  5969. +
  5970. + glnx_set_prefix_error_from_errno (error, "%s", funcstr);
  5971. + goto out;
  5972. + }
  5973. +
  5974. + bytes = g_bytes_new_take (g_steal_pointer (&buf), bytes_read);
  5975. + g_variant_builder_add (builder, "(@ay@ay)",
  5976. + g_variant_new_bytestring (p),
  5977. + variant_new_ay_bytes (bytes));
  5978. + }
  5979. +
  5980. + ret = TRUE;
  5981. + out:
  5982. + return ret;
  5983. +}
  5984. +
  5985. +static gboolean
  5986. +get_xattrs_impl (const char *path,
  5987. + int fd,
  5988. + GVariant **out_xattrs,
  5989. + GCancellable *cancellable,
  5990. + GError **error)
  5991. +{
  5992. + gboolean ret = FALSE;
  5993. + ssize_t bytes_read, real_size;
  5994. + g_autofree char *xattr_names = NULL;
  5995. + g_autofree char *xattr_names_canonical = NULL;
  5996. + GVariantBuilder builder;
  5997. + gboolean builder_initialized = FALSE;
  5998. + g_autoptr(GVariant) ret_xattrs = NULL;
  5999. +
  6000. + g_assert (path != NULL || fd != -1);
  6001. +
  6002. + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ayay)"));
  6003. + builder_initialized = TRUE;
  6004. +
  6005. + again:
  6006. + if (path)
  6007. + bytes_read = llistxattr (path, NULL, 0);
  6008. + else
  6009. + bytes_read = flistxattr (fd, NULL, 0);
  6010. +
  6011. + if (bytes_read < 0)
  6012. + {
  6013. + if (errno != ENOTSUP)
  6014. + {
  6015. + glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
  6016. + goto out;
  6017. + }
  6018. + }
  6019. + else if (bytes_read > 0)
  6020. + {
  6021. + xattr_names = g_malloc (bytes_read);
  6022. + if (path)
  6023. + real_size = llistxattr (path, xattr_names, bytes_read);
  6024. + else
  6025. + real_size = flistxattr (fd, xattr_names, bytes_read);
  6026. + if (real_size < 0)
  6027. + {
  6028. + if (errno == ERANGE)
  6029. + {
  6030. + g_free (xattr_names);
  6031. + goto again;
  6032. + }
  6033. + glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
  6034. + goto out;
  6035. + }
  6036. + else if (real_size > 0)
  6037. + {
  6038. + xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
  6039. +
  6040. + if (!read_xattr_name_array (path, fd, xattr_names_canonical, real_size, &builder, error))
  6041. + goto out;
  6042. + }
  6043. + }
  6044. +
  6045. + ret_xattrs = g_variant_builder_end (&builder);
  6046. + builder_initialized = FALSE;
  6047. + g_variant_ref_sink (ret_xattrs);
  6048. +
  6049. + ret = TRUE;
  6050. + if (out_xattrs)
  6051. + *out_xattrs = g_steal_pointer (&ret_xattrs);
  6052. + out:
  6053. + if (!builder_initialized)
  6054. + g_variant_builder_clear (&builder);
  6055. + return ret;
  6056. +}
  6057. +
  6058. +/**
  6059. + * glnx_fd_get_all_xattrs:
  6060. + * @fd: a file descriptor
  6061. + * @out_xattrs: (out): A new #GVariant containing the extended attributes
  6062. + * @cancellable: Cancellable
  6063. + * @error: Error
  6064. + *
  6065. + * Read all extended attributes from @fd in a canonical sorted order, and
  6066. + * set @out_xattrs with the result.
  6067. + *
  6068. + * If the filesystem does not support extended attributes, @out_xattrs
  6069. + * will have 0 elements, and this function will return successfully.
  6070. + */
  6071. +gboolean
  6072. +glnx_fd_get_all_xattrs (int fd,
  6073. + GVariant **out_xattrs,
  6074. + GCancellable *cancellable,
  6075. + GError **error)
  6076. +{
  6077. + return get_xattrs_impl (NULL, fd, out_xattrs,
  6078. + cancellable, error);
  6079. +}
  6080. +
  6081. +/**
  6082. + * glnx_dfd_name_get_all_xattrs:
  6083. + * @dfd: Parent directory file descriptor
  6084. + * @name: File name
  6085. + * @out_xattrs: (out): Extended attribute set
  6086. + * @cancellable: Cancellable
  6087. + * @error: Error
  6088. + *
  6089. + * Load all extended attributes for the file named @name residing in
  6090. + * directory @dfd.
  6091. + */
  6092. +gboolean
  6093. +glnx_dfd_name_get_all_xattrs (int dfd,
  6094. + const char *name,
  6095. + GVariant **out_xattrs,
  6096. + GCancellable *cancellable,
  6097. + GError **error)
  6098. +{
  6099. + if (G_IN_SET(dfd, AT_FDCWD, -1))
  6100. + {
  6101. + return get_xattrs_impl (name, -1, out_xattrs, cancellable, error);
  6102. + }
  6103. + else
  6104. + {
  6105. + char buf[PATH_MAX];
  6106. + /* A workaround for the lack of lgetxattrat(), thanks to Florian Weimer:
  6107. + * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
  6108. + */
  6109. + snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
  6110. + return get_xattrs_impl (buf, -1, out_xattrs, cancellable, error);
  6111. + }
  6112. +}
  6113. +
  6114. +static gboolean
  6115. +set_all_xattrs_for_path (const char *path,
  6116. + GVariant *xattrs,
  6117. + GCancellable *cancellable,
  6118. + GError **error)
  6119. +{
  6120. + const guint n = g_variant_n_children (xattrs);
  6121. + for (guint i = 0; i < n; i++)
  6122. + {
  6123. + const guint8* name;
  6124. + g_autoptr(GVariant) value = NULL;
  6125. + g_variant_get_child (xattrs, i, "(^&ay@ay)",
  6126. + &name, &value);
  6127. +
  6128. + gsize value_len;
  6129. + const guint8* value_data = g_variant_get_fixed_array (value, &value_len, 1);
  6130. +
  6131. + if (lsetxattr (path, (char*)name, (char*)value_data, value_len, 0) < 0)
  6132. + return glnx_throw_errno_prefix (error, "lsetxattr");
  6133. + }
  6134. +
  6135. + return TRUE;
  6136. +}
  6137. +
  6138. +/**
  6139. + * glnx_dfd_name_set_all_xattrs:
  6140. + * @dfd: Parent directory file descriptor
  6141. + * @name: File name
  6142. + * @xattrs: Extended attribute set
  6143. + * @cancellable: Cancellable
  6144. + * @error: Error
  6145. + *
  6146. + * Set all extended attributes for the file named @name residing in
  6147. + * directory @dfd.
  6148. + */
  6149. +gboolean
  6150. +glnx_dfd_name_set_all_xattrs (int dfd,
  6151. + const char *name,
  6152. + GVariant *xattrs,
  6153. + GCancellable *cancellable,
  6154. + GError **error)
  6155. +{
  6156. + if (G_IN_SET(dfd, AT_FDCWD, -1))
  6157. + {
  6158. + return set_all_xattrs_for_path (name, xattrs, cancellable, error);
  6159. + }
  6160. + else
  6161. + {
  6162. + char buf[PATH_MAX];
  6163. + /* A workaround for the lack of lsetxattrat(), thanks to Florian Weimer:
  6164. + * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
  6165. + */
  6166. + snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
  6167. + return set_all_xattrs_for_path (buf, xattrs, cancellable, error);
  6168. + }
  6169. +}
  6170. +
  6171. +/**
  6172. + * glnx_fd_set_all_xattrs:
  6173. + * @fd: File descriptor
  6174. + * @xattrs: Extended attributes
  6175. + * @cancellable: Cancellable
  6176. + * @error: Error
  6177. + *
  6178. + * For each attribute in @xattrs, set its value on the file or
  6179. + * directory referred to by @fd. This function does not remove any
  6180. + * attributes not in @xattrs.
  6181. + */
  6182. +gboolean
  6183. +glnx_fd_set_all_xattrs (int fd,
  6184. + GVariant *xattrs,
  6185. + GCancellable *cancellable,
  6186. + GError **error)
  6187. +{
  6188. + const guint n = g_variant_n_children (xattrs);
  6189. + for (guint i = 0; i < n; i++)
  6190. + {
  6191. + const guint8* name;
  6192. + g_autoptr(GVariant) value = NULL;
  6193. + g_variant_get_child (xattrs, i, "(^&ay@ay)",
  6194. + &name, &value);
  6195. +
  6196. + gsize value_len;
  6197. + const guint8* value_data = g_variant_get_fixed_array (value, &value_len, 1);
  6198. +
  6199. + if (TEMP_FAILURE_RETRY (fsetxattr (fd, (char*)name, (char*)value_data, value_len, 0)) < 0)
  6200. + return glnx_throw_errno_prefix (error, "fsetxattr");
  6201. + }
  6202. +
  6203. + return TRUE;
  6204. +}
  6205. +
  6206. +/**
  6207. + * glnx_lgetxattrat:
  6208. + * @dfd: Directory file descriptor
  6209. + * @subpath: Subpath
  6210. + * @attribute: Extended attribute to retrieve
  6211. + * @error: Error
  6212. + *
  6213. + * Retrieve an extended attribute value, relative to a directory file
  6214. + * descriptor.
  6215. + */
  6216. +GBytes *
  6217. +glnx_lgetxattrat (int dfd,
  6218. + const char *subpath,
  6219. + const char *attribute,
  6220. + GError **error)
  6221. +{
  6222. + char pathbuf[PATH_MAX];
  6223. + snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
  6224. +
  6225. + ssize_t bytes_read, real_size;
  6226. + if (TEMP_FAILURE_RETRY (bytes_read = lgetxattr (pathbuf, attribute, NULL, 0)) < 0)
  6227. + return glnx_null_throw_errno_prefix (error, "lgetxattr");
  6228. +
  6229. + g_autofree guint8 *buf = g_malloc (bytes_read);
  6230. + if (TEMP_FAILURE_RETRY (real_size = lgetxattr (pathbuf, attribute, buf, bytes_read)) < 0)
  6231. + return glnx_null_throw_errno_prefix (error, "lgetxattr");
  6232. +
  6233. + return g_bytes_new_take (g_steal_pointer (&buf), real_size);
  6234. +}
  6235. +
  6236. +/**
  6237. + * glnx_fgetxattr_bytes:
  6238. + * @fd: Directory file descriptor
  6239. + * @attribute: Extended attribute to retrieve
  6240. + * @error: Error
  6241. + *
  6242. + * Returns: (transfer full): An extended attribute value, or %NULL on error
  6243. + */
  6244. +GBytes *
  6245. +glnx_fgetxattr_bytes (int fd,
  6246. + const char *attribute,
  6247. + GError **error)
  6248. +{
  6249. + ssize_t bytes_read, real_size;
  6250. +
  6251. + if (TEMP_FAILURE_RETRY (bytes_read = fgetxattr (fd, attribute, NULL, 0)) < 0)
  6252. + return glnx_null_throw_errno_prefix (error, "fgetxattr");
  6253. +
  6254. + g_autofree guint8 *buf = g_malloc (bytes_read);
  6255. + if (TEMP_FAILURE_RETRY (real_size = fgetxattr (fd, attribute, buf, bytes_read)) < 0)
  6256. + return glnx_null_throw_errno_prefix (error, "fgetxattr");
  6257. +
  6258. + return g_bytes_new_take (g_steal_pointer (&buf), real_size);
  6259. +}
  6260. +
  6261. +/**
  6262. + * glnx_lsetxattrat:
  6263. + * @dfd: Directory file descriptor
  6264. + * @subpath: Path
  6265. + * @attribute: An attribute name
  6266. + * @value: (array length=len) (element-type guint8): Attribute value
  6267. + * @len: Length of @value
  6268. + * @flags: Flags, containing either XATTR_CREATE or XATTR_REPLACE
  6269. + * @error: Error
  6270. + *
  6271. + * Set an extended attribute, relative to a directory file descriptor.
  6272. + */
  6273. +gboolean
  6274. +glnx_lsetxattrat (int dfd,
  6275. + const char *subpath,
  6276. + const char *attribute,
  6277. + const guint8 *value,
  6278. + gsize len,
  6279. + int flags,
  6280. + GError **error)
  6281. +{
  6282. + char pathbuf[PATH_MAX];
  6283. + snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
  6284. +
  6285. + if (TEMP_FAILURE_RETRY (lsetxattr (subpath, attribute, value, len, flags)) < 0)
  6286. + return glnx_throw_errno_prefix (error, "lsetxattr");
  6287. +
  6288. + return TRUE;
  6289. +}
  6290. +
  6291. diff -Nuar ostree-2018.8.orig/libglnx/glnx-xattrs.h ostree-2018.8/libglnx/glnx-xattrs.h
  6292. --- ostree-2018.8.orig/libglnx/glnx-xattrs.h 1970-01-01 02:00:00.000000000 +0200
  6293. +++ ostree-2018.8/libglnx/glnx-xattrs.h 2018-05-26 00:37:32.008018846 +0300
  6294. @@ -0,0 +1,78 @@
  6295. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  6296. + *
  6297. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  6298. + *
  6299. + * This library is free software; you can redistribute it and/or
  6300. + * modify it under the terms of the GNU Lesser General Public
  6301. + * License as published by the Free Software Foundation; either
  6302. + * version 2 of the License, or (at your option) any later version.
  6303. + *
  6304. + * This library is distributed in the hope that it will be useful,
  6305. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6306. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6307. + * Lesser General Public License for more details.
  6308. + *
  6309. + * You should have received a copy of the GNU Lesser General Public
  6310. + * License along with this library; if not, write to the
  6311. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6312. + * Boston, MA 02111-1307, USA.
  6313. + */
  6314. +
  6315. +#pragma once
  6316. +
  6317. +#include <glnx-backport-autocleanups.h>
  6318. +#include <limits.h>
  6319. +#include <dirent.h>
  6320. +#include <sys/stat.h>
  6321. +#include <fcntl.h>
  6322. +#include <sys/xattr.h>
  6323. +
  6324. +G_BEGIN_DECLS
  6325. +
  6326. +gboolean
  6327. +glnx_dfd_name_get_all_xattrs (int dfd,
  6328. + const char *name,
  6329. + GVariant **out_xattrs,
  6330. + GCancellable *cancellable,
  6331. + GError **error);
  6332. +
  6333. +gboolean
  6334. +glnx_fd_get_all_xattrs (int fd,
  6335. + GVariant **out_xattrs,
  6336. + GCancellable *cancellable,
  6337. + GError **error);
  6338. +
  6339. +gboolean
  6340. +glnx_dfd_name_set_all_xattrs (int dfd,
  6341. + const char *name,
  6342. + GVariant *xattrs,
  6343. + GCancellable *cancellable,
  6344. + GError **error);
  6345. +
  6346. +gboolean
  6347. +glnx_fd_set_all_xattrs (int fd,
  6348. + GVariant *xattrs,
  6349. + GCancellable *cancellable,
  6350. + GError **error);
  6351. +
  6352. +GBytes *
  6353. +glnx_lgetxattrat (int dfd,
  6354. + const char *subpath,
  6355. + const char *attribute,
  6356. + GError **error);
  6357. +
  6358. +GBytes *
  6359. +glnx_fgetxattr_bytes (int fd,
  6360. + const char *attribute,
  6361. + GError **error);
  6362. +
  6363. +gboolean
  6364. +glnx_lsetxattrat (int dfd,
  6365. + const char *subpath,
  6366. + const char *attribute,
  6367. + const guint8 *value,
  6368. + gsize len,
  6369. + int flags,
  6370. + GError **error);
  6371. +
  6372. +G_END_DECLS
  6373. diff -Nuar ostree-2018.8.orig/libglnx/libglnx.doap ostree-2018.8/libglnx/libglnx.doap
  6374. --- ostree-2018.8.orig/libglnx/libglnx.doap 1970-01-01 02:00:00.000000000 +0200
  6375. +++ ostree-2018.8/libglnx/libglnx.doap 2018-05-26 00:37:32.008018846 +0300
  6376. @@ -0,0 +1,31 @@
  6377. +<?xml version="1.0" encoding="UTF-8"?>
  6378. +<Project xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  6379. + xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  6380. + xmlns:foaf="http://xmlns.com/foaf/0.1/"
  6381. + xmlns:gnome="http://api.gnome.org/doap-extensions#"
  6382. + xmlns="http://usefulinc.com/ns/doap#">
  6383. +
  6384. + <name>libglnx</name>
  6385. + <shortname>libglnx</shortname>
  6386. +
  6387. + <shortdesc xml:lang="en">"Copylib" for system service modules using GLib with Linux</shortdesc>
  6388. +
  6389. + <description xml:lang="en">This module is intended for use by
  6390. + infrastructure code using GLib that is also Linux specific, such as
  6391. + ostree, NetworkManager, and others.
  6392. + </description>
  6393. +
  6394. + <license rdf:resource="http://usefulinc.com/doap/licenses/lgpl" />
  6395. + <mailing-list rdf:resource="mailto:desktop-devel-list@gnome.org" />
  6396. +
  6397. + <programming-language>C</programming-language>
  6398. +
  6399. + <maintainer>
  6400. + <foaf:Person>
  6401. + <foaf:name>Colin Walters</foaf:name>
  6402. + <foaf:mbox rdf:resource="mailto:walters@verbum.org"/>
  6403. + <gnome:userid>walters</gnome:userid>
  6404. + </foaf:Person>
  6405. + </maintainer>
  6406. +
  6407. +</Project>
  6408. diff -Nuar ostree-2018.8.orig/libglnx/libglnx.h ostree-2018.8/libglnx/libglnx.h
  6409. --- ostree-2018.8.orig/libglnx/libglnx.h 1970-01-01 02:00:00.000000000 +0200
  6410. +++ ostree-2018.8/libglnx/libglnx.h 2018-05-26 00:37:32.008018846 +0300
  6411. @@ -0,0 +1,40 @@
  6412. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  6413. + *
  6414. + * Copyright (C) 2012,2013,2015 Colin Walters <walters@verbum.org>.
  6415. + *
  6416. + * This library is free software; you can redistribute it and/or
  6417. + * modify it under the terms of the GNU Lesser General Public
  6418. + * License as published by the Free Software Foundation; either
  6419. + * version 2 of the License, or (at your option) any later version.
  6420. + *
  6421. + * This library is distributed in the hope that it will be useful,
  6422. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6423. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6424. + * Lesser General Public License for more details.
  6425. + *
  6426. + * You should have received a copy of the GNU Lesser General Public
  6427. + * License along with this library; if not, write to the
  6428. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6429. + * Boston, MA 02111-1307, USA.
  6430. + */
  6431. +
  6432. +#pragma once
  6433. +
  6434. +#include <gio/gio.h>
  6435. +
  6436. +G_BEGIN_DECLS
  6437. +
  6438. +#include <glnx-macros.h>
  6439. +#include <glnx-missing.h>
  6440. +#include <glnx-local-alloc.h>
  6441. +#include <glnx-backport-autocleanups.h>
  6442. +#include <glnx-backports.h>
  6443. +#include <glnx-lockfile.h>
  6444. +#include <glnx-errors.h>
  6445. +#include <glnx-dirfd.h>
  6446. +#include <glnx-shutil.h>
  6447. +#include <glnx-xattrs.h>
  6448. +#include <glnx-console.h>
  6449. +#include <glnx-fdio.h>
  6450. +
  6451. +G_END_DECLS
  6452. diff -Nuar ostree-2018.8.orig/libglnx/libglnx.m4 ostree-2018.8/libglnx/libglnx.m4
  6453. --- ostree-2018.8.orig/libglnx/libglnx.m4 1970-01-01 02:00:00.000000000 +0200
  6454. +++ ostree-2018.8/libglnx/libglnx.m4 2018-05-26 00:37:32.008018846 +0300
  6455. @@ -0,0 +1,33 @@
  6456. +AC_DEFUN([LIBGLNX_CONFIGURE],
  6457. +[
  6458. +AC_CHECK_DECLS([
  6459. + renameat2,
  6460. + memfd_create,
  6461. + copy_file_range],
  6462. + [], [], [[
  6463. +#include <sys/types.h>
  6464. +#include <unistd.h>
  6465. +#include <sys/mount.h>
  6466. +#include <fcntl.h>
  6467. +#include <sched.h>
  6468. +#include <linux/loop.h>
  6469. +#include <linux/random.h>
  6470. +#include <sys/mman.h>
  6471. +]])
  6472. +
  6473. +AC_ARG_ENABLE(otmpfile,
  6474. + [AS_HELP_STRING([--disable-otmpfile],
  6475. + [Disable use of O_TMPFILE [default=no]])],,
  6476. + [enable_otmpfile=yes])
  6477. +AS_IF([test $enable_otmpfile = yes], [], [
  6478. + AC_DEFINE([DISABLE_OTMPFILE], 1, [Define if we should avoid using O_TMPFILE])])
  6479. +
  6480. +AC_ARG_ENABLE(wrpseudo-compat,
  6481. + [AS_HELP_STRING([--enable-wrpseudo-compat],
  6482. + [Disable use syscall() and filesystem calls to for compatibility with wrpseudo [default=no]])],,
  6483. + [enable_wrpseudo_compat=no])
  6484. +AS_IF([test $enable_wrpseudo_compat = no], [], [
  6485. + AC_DEFINE([ENABLE_WRPSEUDO_COMPAT], 1, [Define if we should be compatible with wrpseudo])])
  6486. +
  6487. +dnl end LIBGLNX_CONFIGURE
  6488. +])
  6489. diff -Nuar ostree-2018.8.orig/libglnx/Makefile-libglnx.am ostree-2018.8/libglnx/Makefile-libglnx.am
  6490. --- ostree-2018.8.orig/libglnx/Makefile-libglnx.am 1970-01-01 02:00:00.000000000 +0200
  6491. +++ ostree-2018.8/libglnx/Makefile-libglnx.am 2018-05-26 00:37:32.006018846 +0300
  6492. @@ -0,0 +1,78 @@
  6493. +# Copyright (C) 2015 Colin Walters <walters@verbum.org>
  6494. +#
  6495. +# This library is free software; you can redistribute it and/or
  6496. +# modify it under the terms of the GNU Lesser General Public
  6497. +# License as published by the Free Software Foundation; either
  6498. +# version 2 of the License, or (at your option) any later version.
  6499. +#
  6500. +# This library is distributed in the hope that it will be useful,
  6501. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  6502. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6503. +# Lesser General Public License for more details.
  6504. +#
  6505. +# You should have received a copy of the GNU Lesser General Public
  6506. +# License along with this library; if not, write to the
  6507. +# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6508. +# Boston, MA 02111-1307, USA.
  6509. +
  6510. +EXTRA_DIST += \
  6511. + $(libglnx_srcpath)/README.md \
  6512. + $(libglnx_srcpath)/COPYING \
  6513. + $(libglnx_srcpath)/libglnx.m4 \
  6514. + $(NULL)
  6515. +
  6516. +libglnx_la_SOURCES = \
  6517. + $(libglnx_srcpath)/glnx-macros.h \
  6518. + $(libglnx_srcpath)/glnx-backport-autocleanups.h \
  6519. + $(libglnx_srcpath)/glnx-backport-autoptr.h \
  6520. + $(libglnx_srcpath)/glnx-backports.h \
  6521. + $(libglnx_srcpath)/glnx-backports.c \
  6522. + $(libglnx_srcpath)/glnx-local-alloc.h \
  6523. + $(libglnx_srcpath)/glnx-local-alloc.c \
  6524. + $(libglnx_srcpath)/glnx-errors.h \
  6525. + $(libglnx_srcpath)/glnx-errors.c \
  6526. + $(libglnx_srcpath)/glnx-console.h \
  6527. + $(libglnx_srcpath)/glnx-console.c \
  6528. + $(libglnx_srcpath)/glnx-dirfd.h \
  6529. + $(libglnx_srcpath)/glnx-dirfd.c \
  6530. + $(libglnx_srcpath)/glnx-fdio.h \
  6531. + $(libglnx_srcpath)/glnx-fdio.c \
  6532. + $(libglnx_srcpath)/glnx-lockfile.h \
  6533. + $(libglnx_srcpath)/glnx-lockfile.c \
  6534. + $(libglnx_srcpath)/glnx-missing-syscall.h \
  6535. + $(libglnx_srcpath)/glnx-missing.h \
  6536. + $(libglnx_srcpath)/glnx-xattrs.h \
  6537. + $(libglnx_srcpath)/glnx-xattrs.c \
  6538. + $(libglnx_srcpath)/glnx-shutil.h \
  6539. + $(libglnx_srcpath)/glnx-shutil.c \
  6540. + $(libglnx_srcpath)/libglnx.h \
  6541. + $(libglnx_srcpath)/tests/libglnx-testlib.h \
  6542. + $(NULL)
  6543. +
  6544. +libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6545. +libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic
  6546. +libglnx_la_LIBADD = $(libglnx_libs)
  6547. +
  6548. +libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors test-libglnx-macros test-libglnx-shutil
  6549. +TESTS += $(libglnx_tests)
  6550. +
  6551. +check_PROGRAMS += $(libglnx_tests)
  6552. +test_libglnx_xattrs_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-xattrs.c
  6553. +test_libglnx_xattrs_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6554. +test_libglnx_xattrs_LDADD = $(libglnx_libs) libglnx.la
  6555. +
  6556. +test_libglnx_fdio_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-fdio.c
  6557. +test_libglnx_fdio_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6558. +test_libglnx_fdio_LDADD = $(libglnx_libs) libglnx.la
  6559. +
  6560. +test_libglnx_errors_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-errors.c
  6561. +test_libglnx_errors_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6562. +test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la
  6563. +
  6564. +test_libglnx_macros_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-macros.c
  6565. +test_libglnx_macros_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6566. +test_libglnx_macros_LDADD = $(libglnx_libs) libglnx.la
  6567. +
  6568. +test_libglnx_shutil_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-shutil.c
  6569. +test_libglnx_shutil_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  6570. +test_libglnx_shutil_LDADD = $(libglnx_libs) libglnx.la
  6571. diff -Nuar ostree-2018.8.orig/libglnx/README.md ostree-2018.8/libglnx/README.md
  6572. --- ostree-2018.8.orig/libglnx/README.md 1970-01-01 02:00:00.000000000 +0200
  6573. +++ ostree-2018.8/libglnx/README.md 2018-05-26 00:37:32.006018846 +0300
  6574. @@ -0,0 +1,52 @@
  6575. +libglnx is the successor to libgsystem: https://git.gnome.org/browse/libgsystem
  6576. +
  6577. +It is for modules which depend on both GLib and Linux, intended to be
  6578. +used as a git submodule.
  6579. +
  6580. +Features:
  6581. +
  6582. + - File APIs which use `openat()` like APIs, but also take a `GCancellable`
  6583. + to support dynamic cancellation
  6584. + - APIs also have a `GError` parameter
  6585. + - High level "shutil", somewhat inspired by Python's
  6586. + - A "console" API for tty output
  6587. + - A backport of the GLib cleanup macros for projects which can't yet take
  6588. + a dependency on 2.40.
  6589. +
  6590. +Why?
  6591. +----
  6592. +
  6593. +There are multiple projects which have a hard dependency on Linux and
  6594. +GLib, such as NetworkManager, ostree, flatpak, etc. It makes sense
  6595. +for them to be able to share Linux-specific APIs.
  6596. +
  6597. +This module also contains some code taken from systemd, which has very
  6598. +high quality LGPLv2+ shared library code, but most of the internal
  6599. +shared library is private, and not namespaced.
  6600. +
  6601. +One could also compare this project to gnulib; the salient differences
  6602. +there are that at least some of this module is eventually destined for
  6603. +inclusion in GLib.
  6604. +
  6605. +Porting from libgsystem
  6606. +-----------------------
  6607. +
  6608. +For all of the filesystem access code, libglnx exposes only
  6609. +fd-relative API, not `GFile*`. It does use `GCancellable` where
  6610. +applicable.
  6611. +
  6612. +For local allocation macros, you should start using the `g_auto`
  6613. +macros from GLib. A backport is included in libglnx. There are a few
  6614. +APIs not defined in GLib yet, such as `glnx_autofd`.
  6615. +
  6616. +`gs_transfer_out_value` is replaced by `g_steal_pointer`.
  6617. +
  6618. +Contributing
  6619. +------------
  6620. +
  6621. +Currently there is not a Bugzilla product - one may be created
  6622. +in the future. You can submit PRs against the Github mirror:
  6623. +
  6624. +https://github.com/GNOME/libglnx/pulls
  6625. +
  6626. +Or alternatively, email one of the maintainers directly.
  6627. diff -Nuar ostree-2018.8.orig/libglnx/tests/libglnx-testlib.h ostree-2018.8/libglnx/tests/libglnx-testlib.h
  6628. --- ostree-2018.8.orig/libglnx/tests/libglnx-testlib.h 1970-01-01 02:00:00.000000000 +0200
  6629. +++ ostree-2018.8/libglnx/tests/libglnx-testlib.h 2018-05-26 00:37:32.008018846 +0300
  6630. @@ -0,0 +1,34 @@
  6631. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  6632. + *
  6633. + * Copyright (C) 2017 Red Hat, Inc.
  6634. + *
  6635. + * This library is free software; you can redistribute it and/or
  6636. + * modify it under the terms of the GNU Lesser General Public
  6637. + * License as published by the Free Software Foundation; either
  6638. + * version 2 of the License, or (at your option) any later version.
  6639. + *
  6640. + * This library is distributed in the hope that it will be useful,
  6641. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6642. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6643. + * Lesser General Public License for more details.
  6644. + *
  6645. + * You should have received a copy of the GNU Lesser General Public
  6646. + * License along with this library; if not, write to the
  6647. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6648. + * Boston, MA 02111-1307, USA.
  6649. + */
  6650. +
  6651. +#pragma once
  6652. +
  6653. +typedef GError _GLnxTestAutoError;
  6654. +static inline void
  6655. +_glnx_test_auto_error_cleanup (_GLnxTestAutoError *autoerror)
  6656. +{
  6657. + g_assert_no_error (autoerror);
  6658. + /* We could add a clear call here, but no point...we'll have aborted */
  6659. +}
  6660. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(_GLnxTestAutoError, _glnx_test_auto_error_cleanup);
  6661. +
  6662. +#define _GLNX_TEST_DECLARE_ERROR(local_error, error) \
  6663. + g_autoptr(_GLnxTestAutoError) local_error = NULL; \
  6664. + GError **error = &local_error
  6665. diff -Nuar ostree-2018.8.orig/libglnx/tests/test-libglnx-errors.c ostree-2018.8/libglnx/tests/test-libglnx-errors.c
  6666. --- ostree-2018.8.orig/libglnx/tests/test-libglnx-errors.c 1970-01-01 02:00:00.000000000 +0200
  6667. +++ ostree-2018.8/libglnx/tests/test-libglnx-errors.c 2018-05-26 00:37:32.008018846 +0300
  6668. @@ -0,0 +1,183 @@
  6669. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  6670. + *
  6671. + * Copyright (C) 2017 Red Hat, Inc.
  6672. + *
  6673. + * This library is free software; you can redistribute it and/or
  6674. + * modify it under the terms of the GNU Lesser General Public
  6675. + * License as published by the Free Software Foundation; either
  6676. + * version 2 of the License, or (at your option) any later version.
  6677. + *
  6678. + * This library is distributed in the hope that it will be useful,
  6679. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6680. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6681. + * Lesser General Public License for more details.
  6682. + *
  6683. + * You should have received a copy of the GNU Lesser General Public
  6684. + * License along with this library; if not, write to the
  6685. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6686. + * Boston, MA 02111-1307, USA.
  6687. + */
  6688. +
  6689. +#include "config.h"
  6690. +#include "libglnx.h"
  6691. +#include <glib.h>
  6692. +#include <stdlib.h>
  6693. +#include <gio/gio.h>
  6694. +#include <string.h>
  6695. +
  6696. +static void
  6697. +test_error_throw (void)
  6698. +{
  6699. + g_autoptr(GError) error = NULL;
  6700. +
  6701. + g_assert (!glnx_throw (&error, "foo: %s %d", "hello", 42));
  6702. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  6703. + g_assert_cmpstr (error->message, ==, "foo: hello 42");
  6704. + g_clear_error (&error);
  6705. +
  6706. + gpointer dummy = glnx_null_throw (&error, "literal foo");
  6707. + g_assert (dummy == NULL);
  6708. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  6709. + g_assert_cmpstr (error->message, ==, "literal foo");
  6710. + g_clear_error (&error);
  6711. +
  6712. + gpointer dummy2 = glnx_null_throw (&error, "foo: %s %d", "hola", 24);
  6713. + g_assert (dummy2 == NULL);
  6714. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  6715. + g_assert_cmpstr (error->message, ==, "foo: hola 24");
  6716. + g_clear_error (&error);
  6717. +}
  6718. +
  6719. +static void
  6720. +test_error_errno (void)
  6721. +{
  6722. + g_autoptr(GError) error = NULL;
  6723. + const char noent_path[] = "/enoent-this-should-not-exist";
  6724. + int fd;
  6725. +
  6726. + fd = open (noent_path, O_RDONLY);
  6727. + if (fd < 0)
  6728. + {
  6729. + g_assert (!glnx_throw_errno (&error));
  6730. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6731. + g_assert (!glnx_prefix_error (&error, "myprefix"));
  6732. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6733. + g_assert (g_str_has_prefix (error->message, "myprefix: "));
  6734. + g_clear_error (&error);
  6735. + }
  6736. + else
  6737. + g_assert_cmpint (fd, ==, -1);
  6738. +
  6739. + fd = open (noent_path, O_RDONLY);
  6740. + if (fd < 0)
  6741. + {
  6742. + gpointer dummy = glnx_null_throw_errno (&error);
  6743. + g_assert (dummy == NULL);
  6744. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6745. + dummy = glnx_prefix_error_null (&error, "myprefix");
  6746. + g_assert (dummy == NULL);
  6747. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6748. + g_assert (g_str_has_prefix (error->message, "myprefix: "));
  6749. + g_clear_error (&error);
  6750. + }
  6751. + else
  6752. + g_assert_cmpint (fd, ==, -1);
  6753. +
  6754. + fd = open (noent_path, O_RDONLY);
  6755. + if (fd < 0)
  6756. + {
  6757. + g_autofree char *expected_prefix = g_strdup_printf ("Failed to open %s", noent_path);
  6758. + g_assert (!glnx_throw_errno_prefix (&error, "Failed to open %s", noent_path));
  6759. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6760. + g_assert (g_str_has_prefix (error->message, expected_prefix));
  6761. + g_clear_error (&error);
  6762. + /* And test the legacy wrapper */
  6763. + glnx_set_prefix_error_from_errno (&error, "Failed to open %s", noent_path);
  6764. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6765. + g_assert (g_str_has_prefix (error->message, expected_prefix));
  6766. + g_clear_error (&error);
  6767. + }
  6768. + else
  6769. + g_assert_cmpint (fd, ==, -1);
  6770. +
  6771. + fd = open (noent_path, O_RDONLY);
  6772. + if (fd < 0)
  6773. + {
  6774. + gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open file");
  6775. + g_assert (dummy == NULL);
  6776. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6777. + g_assert (g_str_has_prefix (error->message, "Failed to open file"));
  6778. + g_clear_error (&error);
  6779. + }
  6780. + else
  6781. + g_assert_cmpint (fd, ==, -1);
  6782. +
  6783. + fd = open (noent_path, O_RDONLY);
  6784. + if (fd < 0)
  6785. + {
  6786. + gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open %s", noent_path);
  6787. + g_assert (dummy == NULL);
  6788. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  6789. + g_assert (g_str_has_prefix (error->message, glnx_strjoina ("Failed to open ", noent_path)));
  6790. + g_clear_error (&error);
  6791. + }
  6792. + else
  6793. + g_assert_cmpint (fd, ==, -1);
  6794. +}
  6795. +
  6796. +static void
  6797. +test_error_auto_nothrow (GError **error)
  6798. +{
  6799. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  6800. + /* Side effect to avoid otherwise empty function */
  6801. + g_assert_no_error (*error);
  6802. +}
  6803. +
  6804. +static void
  6805. +test_error_auto_throw (GError **error)
  6806. +{
  6807. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  6808. + (void) glnx_throw (error, "oops");
  6809. +}
  6810. +
  6811. +static void
  6812. +test_error_auto_throw_recurse (GError **error)
  6813. +{
  6814. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  6815. +
  6816. + if (TRUE)
  6817. + {
  6818. + GLNX_AUTO_PREFIX_ERROR("bar", error);
  6819. + (void) glnx_throw (error, "oops");
  6820. + }
  6821. +}
  6822. +
  6823. +static void
  6824. +test_error_auto (void)
  6825. +{
  6826. + g_autoptr(GError) error = NULL;
  6827. + test_error_auto_nothrow (&error);
  6828. + g_assert_no_error (error);
  6829. + test_error_auto_throw (&error);
  6830. + g_assert_nonnull (error);
  6831. + g_assert_cmpstr (error->message, ==, "foo: oops");
  6832. + g_clear_error (&error);
  6833. + test_error_auto_throw_recurse (&error);
  6834. + g_assert_nonnull (error);
  6835. + g_assert_cmpstr (error->message, ==, "foo: bar: oops");
  6836. +}
  6837. +
  6838. +int main (int argc, char **argv)
  6839. +{
  6840. + int ret;
  6841. +
  6842. + g_test_init (&argc, &argv, NULL);
  6843. +
  6844. + g_test_add_func ("/error-throw", test_error_throw);
  6845. + g_test_add_func ("/error-errno", test_error_errno);
  6846. + g_test_add_func ("/error-auto", test_error_auto);
  6847. +
  6848. + ret = g_test_run();
  6849. +
  6850. + return ret;
  6851. +}
  6852. diff -Nuar ostree-2018.8.orig/libglnx/tests/test-libglnx-fdio.c ostree-2018.8/libglnx/tests/test-libglnx-fdio.c
  6853. --- ostree-2018.8.orig/libglnx/tests/test-libglnx-fdio.c 1970-01-01 02:00:00.000000000 +0200
  6854. +++ ostree-2018.8/libglnx/tests/test-libglnx-fdio.c 2018-05-26 00:37:32.008018846 +0300
  6855. @@ -0,0 +1,254 @@
  6856. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  6857. + *
  6858. + * Copyright (C) 2017 Red Hat, Inc.
  6859. + *
  6860. + * This library is free software; you can redistribute it and/or
  6861. + * modify it under the terms of the GNU Lesser General Public
  6862. + * License as published by the Free Software Foundation; either
  6863. + * version 2 of the License, or (at your option) any later version.
  6864. + *
  6865. + * This library is distributed in the hope that it will be useful,
  6866. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6867. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  6868. + * Lesser General Public License for more details.
  6869. + *
  6870. + * You should have received a copy of the GNU Lesser General Public
  6871. + * License along with this library; if not, write to the
  6872. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  6873. + * Boston, MA 02111-1307, USA.
  6874. + */
  6875. +
  6876. +#include "config.h"
  6877. +#include "libglnx.h"
  6878. +#include <glib.h>
  6879. +#include <stdlib.h>
  6880. +#include <gio/gio.h>
  6881. +#include <err.h>
  6882. +#include <string.h>
  6883. +
  6884. +#include "libglnx-testlib.h"
  6885. +
  6886. +static gboolean
  6887. +renameat_test_setup (int *out_srcfd, int *out_destfd,
  6888. + GError **error)
  6889. +{
  6890. + glnx_autofd int srcfd = -1;
  6891. + glnx_autofd int destfd = -1;
  6892. +
  6893. + (void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL);
  6894. + if (mkdir ("srcdir", 0755) < 0)
  6895. + err (1, "mkdir");
  6896. + if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error))
  6897. + return FALSE;
  6898. + (void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL);
  6899. + if (mkdir ("destdir", 0755) < 0)
  6900. + err (1, "mkdir");
  6901. + if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error))
  6902. + return FALSE;
  6903. +
  6904. + if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"),
  6905. + GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
  6906. + return FALSE;
  6907. + if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"),
  6908. + GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
  6909. + return FALSE;
  6910. +
  6911. + *out_srcfd = srcfd; srcfd = -1;
  6912. + *out_destfd = destfd; destfd = -1;
  6913. + return TRUE;
  6914. +}
  6915. +
  6916. +static void
  6917. +test_renameat2_noreplace (void)
  6918. +{
  6919. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  6920. + glnx_autofd int srcfd = -1;
  6921. + glnx_autofd int destfd = -1;
  6922. + struct stat stbuf;
  6923. +
  6924. + if (!renameat_test_setup (&srcfd, &destfd, error))
  6925. + return;
  6926. +
  6927. + if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0)
  6928. + g_assert_not_reached ();
  6929. + else
  6930. + {
  6931. + g_assert_cmpint (errno, ==, EEXIST);
  6932. + }
  6933. +
  6934. + if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0)
  6935. + return (void)glnx_throw_errno_prefix (error, "renameat");
  6936. + if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  6937. + return;
  6938. +
  6939. + if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
  6940. + g_assert_not_reached ();
  6941. + else
  6942. + g_assert_cmpint (errno, ==, ENOENT);
  6943. +}
  6944. +
  6945. +static void
  6946. +test_renameat2_exchange (void)
  6947. +{
  6948. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  6949. +
  6950. + glnx_autofd int srcfd = -1;
  6951. + glnx_autofd int destfd = -1;
  6952. + if (!renameat_test_setup (&srcfd, &destfd, error))
  6953. + return;
  6954. +
  6955. + if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0)
  6956. + return (void)glnx_throw_errno_prefix (error, "renameat");
  6957. +
  6958. + /* Ensure the dir fds are the same */
  6959. + struct stat stbuf;
  6960. + if (!glnx_fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  6961. + return;
  6962. + if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  6963. + return;
  6964. + /* But the dirs should be swapped */
  6965. + if (!glnx_fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  6966. + return;
  6967. + if (!glnx_fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  6968. + return;
  6969. +}
  6970. +
  6971. +static void
  6972. +test_tmpfile (void)
  6973. +{
  6974. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  6975. +
  6976. + g_auto(GLnxTmpfile) tmpf = { 0, };
  6977. + if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error))
  6978. + return;
  6979. + if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0)
  6980. + return (void)glnx_throw_errno_prefix (error, "write");
  6981. + if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error))
  6982. + return;
  6983. +}
  6984. +
  6985. +static void
  6986. +test_stdio_file (void)
  6987. +{
  6988. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  6989. + g_auto(GLnxTmpfile) tmpf = { 0, };
  6990. + g_autoptr(FILE) f = NULL;
  6991. +
  6992. + if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error))
  6993. + return;
  6994. + f = fdopen (tmpf.fd, "w");
  6995. + tmpf.fd = -1; /* Ownership was transferred via fdopen() */
  6996. + if (!f)
  6997. + return (void)glnx_throw_errno_prefix (error, "fdopen");
  6998. + if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello"))
  6999. + return (void)glnx_throw_errno_prefix (error, "fwrite");
  7000. + if (!glnx_stdio_file_flush (f, error))
  7001. + return;
  7002. +}
  7003. +
  7004. +static void
  7005. +test_fstatat (void)
  7006. +{
  7007. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  7008. + struct stat stbuf = { 0, };
  7009. +
  7010. + if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", &stbuf, 0, error))
  7011. + return;
  7012. + g_assert_cmpint (errno, ==, 0);
  7013. + g_assert_no_error (local_error);
  7014. + g_assert (S_ISDIR (stbuf.st_mode));
  7015. + if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", &stbuf, 0, error))
  7016. + return;
  7017. + g_assert_cmpint (errno, ==, ENOENT);
  7018. + g_assert_no_error (local_error);
  7019. +
  7020. + /* test NULL parameter for stat */
  7021. + if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", NULL, 0, error))
  7022. + return;
  7023. + g_assert_cmpint (errno, ==, 0);
  7024. + g_assert_no_error (local_error);
  7025. + if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", NULL, 0, error))
  7026. + return;
  7027. + g_assert_cmpint (errno, ==, ENOENT);
  7028. + g_assert_no_error (local_error);
  7029. +}
  7030. +
  7031. +static void
  7032. +test_filecopy (void)
  7033. +{
  7034. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  7035. + const char foo[] = "foo";
  7036. + struct stat stbuf;
  7037. +
  7038. + if (!glnx_ensure_dir (AT_FDCWD, "subdir", 0755, error))
  7039. + return;
  7040. +
  7041. + if (!glnx_file_replace_contents_at (AT_FDCWD, foo, (guint8*)foo, sizeof (foo),
  7042. + GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
  7043. + return;
  7044. +
  7045. + /* Copy it into both the same dir and a subdir */
  7046. + if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
  7047. + GLNX_FILE_COPY_NOXATTRS, NULL, error))
  7048. + return;
  7049. + if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "subdir/bar",
  7050. + GLNX_FILE_COPY_NOXATTRS, NULL, error))
  7051. + return;
  7052. + if (!glnx_fstatat (AT_FDCWD, "subdir/bar", &stbuf, 0, error))
  7053. + return;
  7054. +
  7055. + if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
  7056. + GLNX_FILE_COPY_NOXATTRS, NULL, error))
  7057. + g_assert_not_reached ();
  7058. + g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  7059. + g_clear_error (&local_error);
  7060. +
  7061. + if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
  7062. + GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
  7063. + NULL, error))
  7064. + return;
  7065. +
  7066. + if (symlinkat ("nosuchtarget", AT_FDCWD, "link") < 0)
  7067. + return (void) glnx_throw_errno_prefix (error, "symlinkat");
  7068. +
  7069. + /* Shouldn't be able to overwrite a symlink without GLNX_FILE_COPY_OVERWRITE */
  7070. + if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
  7071. + GLNX_FILE_COPY_NOXATTRS,
  7072. + NULL, error))
  7073. + g_assert_not_reached ();
  7074. + g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  7075. + g_clear_error (&local_error);
  7076. +
  7077. + /* Test overwriting symlink */
  7078. + if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
  7079. + GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
  7080. + NULL, error))
  7081. + return;
  7082. +
  7083. + if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchtarget", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  7084. + return;
  7085. + g_assert_cmpint (errno, ==, ENOENT);
  7086. + g_assert_no_error (local_error);
  7087. +
  7088. + if (!glnx_fstatat (AT_FDCWD, "link", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  7089. + return;
  7090. + g_assert (S_ISREG (stbuf.st_mode));
  7091. +}
  7092. +
  7093. +int main (int argc, char **argv)
  7094. +{
  7095. + int ret;
  7096. +
  7097. + g_test_init (&argc, &argv, NULL);
  7098. +
  7099. + g_test_add_func ("/tmpfile", test_tmpfile);
  7100. + g_test_add_func ("/stdio-file", test_stdio_file);
  7101. + g_test_add_func ("/filecopy", test_filecopy);
  7102. + g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace);
  7103. + g_test_add_func ("/renameat2-exchange", test_renameat2_exchange);
  7104. + g_test_add_func ("/fstat", test_fstatat);
  7105. +
  7106. + ret = g_test_run();
  7107. +
  7108. + return ret;
  7109. +}
  7110. diff -Nuar ostree-2018.8.orig/libglnx/tests/test-libglnx-macros.c ostree-2018.8/libglnx/tests/test-libglnx-macros.c
  7111. --- ostree-2018.8.orig/libglnx/tests/test-libglnx-macros.c 1970-01-01 02:00:00.000000000 +0200
  7112. +++ ostree-2018.8/libglnx/tests/test-libglnx-macros.c 2018-05-26 00:37:32.008018846 +0300
  7113. @@ -0,0 +1,109 @@
  7114. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  7115. + *
  7116. + * Copyright (C) 2017 Red Hat, Inc.
  7117. + *
  7118. + * This library is free software; you can redistribute it and/or
  7119. + * modify it under the terms of the GNU Lesser General Public
  7120. + * License as published by the Free Software Foundation; either
  7121. + * version 2 of the License, or (at your option) any later version.
  7122. + *
  7123. + * This library is distributed in the hope that it will be useful,
  7124. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7125. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7126. + * Lesser General Public License for more details.
  7127. + *
  7128. + * You should have received a copy of the GNU Lesser General Public
  7129. + * License along with this library; if not, write to the
  7130. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  7131. + * Boston, MA 02111-1307, USA.
  7132. + */
  7133. +
  7134. +#include "config.h"
  7135. +#include "libglnx.h"
  7136. +#include <glib.h>
  7137. +#include <stdlib.h>
  7138. +#include <gio/gio.h>
  7139. +#include <string.h>
  7140. +
  7141. +static void
  7142. +test_inset (void)
  7143. +{
  7144. + g_assert (G_IN_SET (7, 7));
  7145. + g_assert (G_IN_SET (7, 42, 7));
  7146. + g_assert (G_IN_SET (7, 7,42,3,9));
  7147. + g_assert (G_IN_SET (42, 7,42,3,9));
  7148. + g_assert (G_IN_SET (3, 7,42,3,9));
  7149. + g_assert (G_IN_SET (9, 7,42,3,9));
  7150. + g_assert (!G_IN_SET (8, 7,42,3,9));
  7151. + g_assert (!G_IN_SET (-1, 7,42,3,9));
  7152. + g_assert (G_IN_SET ('x', 'a', 'x', 'c'));
  7153. + g_assert (!G_IN_SET ('y', 'a', 'x', 'c'));
  7154. +}
  7155. +
  7156. +static void
  7157. +test_hash_table_foreach (void)
  7158. +{
  7159. + /* use var names all different from the macro metavars to ensure proper
  7160. + * substitution */
  7161. + g_autoptr(GHashTable) table = g_hash_table_new (g_str_hash, g_str_equal);
  7162. + const char *keys[] = {"key1", "key2"};
  7163. + const char *vals[] = {"val1", "val2"};
  7164. + g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
  7165. + g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
  7166. +
  7167. + guint i = 0;
  7168. + GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
  7169. + {
  7170. + g_assert_cmpstr (key, ==, keys[i]);
  7171. + g_assert_cmpstr (val, ==, vals[i]);
  7172. + i++;
  7173. + }
  7174. + g_assert_cmpuint (i, ==, 2);
  7175. +
  7176. + i = 0;
  7177. + GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
  7178. + {
  7179. + g_hash_table_iter_remove (&it);
  7180. + break;
  7181. + }
  7182. + g_assert_cmpuint (g_hash_table_size (table), ==, 1);
  7183. +
  7184. + g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
  7185. + g_assert_cmpuint (g_hash_table_size (table), ==, 1);
  7186. +
  7187. + g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
  7188. + g_assert_cmpuint (g_hash_table_size (table), ==, 2);
  7189. +
  7190. + i = 0;
  7191. + GLNX_HASH_TABLE_FOREACH_KV (table, const char*, key, const char*, val)
  7192. + {
  7193. + g_assert_cmpstr (key, ==, keys[i]);
  7194. + g_assert_cmpstr (val, ==, vals[i]);
  7195. + i++;
  7196. + }
  7197. + g_assert_cmpuint (i, ==, 2);
  7198. +
  7199. + i = 0;
  7200. + GLNX_HASH_TABLE_FOREACH (table, const char*, key)
  7201. + {
  7202. + g_assert_cmpstr (key, ==, keys[i]);
  7203. + i++;
  7204. + }
  7205. + g_assert_cmpuint (i, ==, 2);
  7206. +
  7207. + i = 0;
  7208. + GLNX_HASH_TABLE_FOREACH_V (table, const char*, val)
  7209. + {
  7210. + g_assert_cmpstr (val, ==, vals[i]);
  7211. + i++;
  7212. + }
  7213. + g_assert_cmpuint (i, ==, 2);
  7214. +}
  7215. +
  7216. +int main (int argc, char **argv)
  7217. +{
  7218. + g_test_init (&argc, &argv, NULL);
  7219. + g_test_add_func ("/inset", test_inset);
  7220. + g_test_add_func ("/hash_table_foreach", test_hash_table_foreach);
  7221. + return g_test_run();
  7222. +}
  7223. diff -Nuar ostree-2018.8.orig/libglnx/tests/test-libglnx-shutil.c ostree-2018.8/libglnx/tests/test-libglnx-shutil.c
  7224. --- ostree-2018.8.orig/libglnx/tests/test-libglnx-shutil.c 1970-01-01 02:00:00.000000000 +0200
  7225. +++ ostree-2018.8/libglnx/tests/test-libglnx-shutil.c 2018-05-26 00:37:32.008018846 +0300
  7226. @@ -0,0 +1,63 @@
  7227. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  7228. + *
  7229. + * Copyright © 2017 Endless Mobile, Inc.
  7230. + *
  7231. + * This library is free software; you can redistribute it and/or
  7232. + * modify it under the terms of the GNU Lesser General Public
  7233. + * License as published by the Free Software Foundation; either
  7234. + * version 2 of the License, or (at your option) any later version.
  7235. + *
  7236. + * This library is distributed in the hope that it will be useful,
  7237. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7238. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7239. + * Lesser General Public License for more details.
  7240. + *
  7241. + * You should have received a copy of the GNU Lesser General Public
  7242. + * License along with this library; if not, write to the
  7243. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  7244. + * Boston, MA 02111-1307, USA.
  7245. + */
  7246. +
  7247. +#include "config.h"
  7248. +#include "libglnx.h"
  7249. +#include <glib.h>
  7250. +#include <stdlib.h>
  7251. +#include <gio/gio.h>
  7252. +#include <err.h>
  7253. +#include <string.h>
  7254. +
  7255. +#include "libglnx-testlib.h"
  7256. +
  7257. +static void
  7258. +test_mkdir_p_enoent (void)
  7259. +{
  7260. + _GLNX_TEST_DECLARE_ERROR(local_error, error);
  7261. + glnx_autofd int dfd = -1;
  7262. +
  7263. + if (!glnx_ensure_dir (AT_FDCWD, "test", 0755, error))
  7264. + return;
  7265. + if (!glnx_opendirat (AT_FDCWD, "test", FALSE, &dfd, error))
  7266. + return;
  7267. + if (rmdir ("test") < 0)
  7268. + return (void) glnx_throw_errno_prefix (error, "rmdir(%s)", "test");
  7269. +
  7270. + /* This should fail with ENOENT. */
  7271. + glnx_shutil_mkdir_p_at (dfd, "blah/baz", 0755, NULL, error);
  7272. + g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  7273. + g_clear_error (&local_error);
  7274. +}
  7275. +
  7276. +int
  7277. +main (int argc,
  7278. + char **argv)
  7279. +{
  7280. + int ret;
  7281. +
  7282. + g_test_init (&argc, &argv, NULL);
  7283. +
  7284. + g_test_add_func ("/mkdir-p/enoent", test_mkdir_p_enoent);
  7285. +
  7286. + ret = g_test_run();
  7287. +
  7288. + return ret;
  7289. +}
  7290. diff -Nuar ostree-2018.8.orig/libglnx/tests/test-libglnx-xattrs.c ostree-2018.8/libglnx/tests/test-libglnx-xattrs.c
  7291. --- ostree-2018.8.orig/libglnx/tests/test-libglnx-xattrs.c 1970-01-01 02:00:00.000000000 +0200
  7292. +++ ostree-2018.8/libglnx/tests/test-libglnx-xattrs.c 2018-05-26 00:37:32.008018846 +0300
  7293. @@ -0,0 +1,283 @@
  7294. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  7295. + *
  7296. + * Copyright (C) 2017 Red Hat, Inc.
  7297. + *
  7298. + * This library is free software; you can redistribute it and/or
  7299. + * modify it under the terms of the GNU Lesser General Public
  7300. + * License as published by the Free Software Foundation; either
  7301. + * version 2 of the License, or (at your option) any later version.
  7302. + *
  7303. + * This library is distributed in the hope that it will be useful,
  7304. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7305. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7306. + * Lesser General Public License for more details.
  7307. + *
  7308. + * You should have received a copy of the GNU Lesser General Public
  7309. + * License along with this library; if not, write to the
  7310. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  7311. + * Boston, MA 02111-1307, USA.
  7312. + */
  7313. +
  7314. +#include "config.h"
  7315. +#include "libglnx.h"
  7316. +#include <glib.h>
  7317. +#include <stdlib.h>
  7318. +#include <gio/gio.h>
  7319. +#include <string.h>
  7320. +
  7321. +#define XATTR_THREAD_RUN_TIME_USECS (5 * G_USEC_PER_SEC)
  7322. +
  7323. +struct XattrWorker {
  7324. + int dfd;
  7325. + gboolean is_writer;
  7326. + guint n_attrs_read;
  7327. +};
  7328. +
  7329. +typedef enum {
  7330. + WRITE_RUN_MUTATE,
  7331. + WRITE_RUN_CREATE,
  7332. +} WriteType;
  7333. +
  7334. +static gboolean
  7335. +set_random_xattr_value (int fd, const char *name, GError **error)
  7336. +{
  7337. + const guint8 randxattrbyte = g_random_int_range (0, 256);
  7338. + const guint32 randxattrvalue_len = (g_random_int () % 256) + 1; /* Picked to be not too small or large */
  7339. + g_autofree char *randxattrvalue = g_malloc (randxattrvalue_len);
  7340. +
  7341. + memset (randxattrvalue, randxattrbyte, randxattrvalue_len);
  7342. +
  7343. + if (fsetxattr (fd, name, randxattrvalue, randxattrvalue_len, 0) < 0)
  7344. + {
  7345. + glnx_set_error_from_errno (error);
  7346. + return FALSE;
  7347. + }
  7348. +
  7349. + return TRUE;
  7350. +}
  7351. +
  7352. +static gboolean
  7353. +add_random_xattrs (int fd, GError **error)
  7354. +{
  7355. + const guint nattrs = MIN (2, g_random_int () % 16);
  7356. +
  7357. + for (guint i = 0; i < nattrs; i++)
  7358. + {
  7359. + guint32 randxattrname_v = g_random_int ();
  7360. + g_autofree char *randxattrname = g_strdup_printf ("user.test%u", randxattrname_v);
  7361. +
  7362. + if (!set_random_xattr_value (fd, randxattrname, error))
  7363. + return FALSE;
  7364. + }
  7365. +
  7366. + return TRUE;
  7367. +}
  7368. +
  7369. +static gboolean
  7370. +do_write_run (GLnxDirFdIterator *dfd_iter, GError **error)
  7371. +{
  7372. + WriteType wtype = g_random_int () % 2;
  7373. +
  7374. + if (wtype == WRITE_RUN_CREATE)
  7375. + {
  7376. + guint32 randname_v = g_random_int ();
  7377. + g_autofree char *randname = g_strdup_printf ("file%u", randname_v);
  7378. + glnx_autofd int fd = -1;
  7379. +
  7380. + again:
  7381. + fd = openat (dfd_iter->fd, randname, O_CREAT | O_EXCL, 0644);
  7382. + if (fd < 0)
  7383. + {
  7384. + if (errno == EEXIST)
  7385. + {
  7386. + g_printerr ("Congratulations! I suggest purchasing a lottery ticket today!\n");
  7387. + goto again;
  7388. + }
  7389. + else
  7390. + {
  7391. + glnx_set_error_from_errno (error);
  7392. + return FALSE;
  7393. + }
  7394. + }
  7395. +
  7396. + if (!add_random_xattrs (fd, error))
  7397. + return FALSE;
  7398. + }
  7399. + else if (wtype == WRITE_RUN_MUTATE)
  7400. + {
  7401. + while (TRUE)
  7402. + {
  7403. + struct dirent *dent;
  7404. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
  7405. + return FALSE;
  7406. + if (!dent)
  7407. + break;
  7408. +
  7409. + glnx_autofd int fd = -1;
  7410. + if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
  7411. + return FALSE;
  7412. +
  7413. + g_autoptr(GVariant) current_xattrs = NULL;
  7414. + if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
  7415. + return FALSE;
  7416. +
  7417. + for (int i = 0; i < g_variant_n_children (current_xattrs); i++)
  7418. + {
  7419. + const char *name, *value;
  7420. + g_variant_get_child (current_xattrs, i, "(^&ay^&ay)", &name, &value);
  7421. +
  7422. + /* We don't want to potentially test/change xattrs like security.selinux
  7423. + * that were injected by the system.
  7424. + */
  7425. + if (!g_str_has_prefix (name, "user.test"))
  7426. + continue;
  7427. +
  7428. + if (!set_random_xattr_value (fd, name, error))
  7429. + return FALSE;
  7430. + }
  7431. + }
  7432. + }
  7433. + else
  7434. + g_assert_not_reached ();
  7435. +
  7436. + return TRUE;
  7437. +}
  7438. +
  7439. +static gboolean
  7440. +do_read_run (GLnxDirFdIterator *dfd_iter,
  7441. + guint *out_n_read,
  7442. + GError **error)
  7443. +{
  7444. + guint nattrs = 0;
  7445. + while (TRUE)
  7446. + {
  7447. + struct dirent *dent;
  7448. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
  7449. + return FALSE;
  7450. + if (!dent)
  7451. + break;
  7452. +
  7453. + glnx_autofd int fd = -1;
  7454. + if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
  7455. + return FALSE;
  7456. +
  7457. + g_autoptr(GVariant) current_xattrs = NULL;
  7458. + if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
  7459. + return FALSE;
  7460. +
  7461. + /* We don't actually care about the values, just use the variable
  7462. + * to avoid compiler warnings.
  7463. + */
  7464. + nattrs += g_variant_n_children (current_xattrs);
  7465. + }
  7466. +
  7467. + *out_n_read = nattrs;
  7468. + return TRUE;
  7469. +}
  7470. +
  7471. +static gpointer
  7472. +xattr_thread (gpointer data)
  7473. +{
  7474. + g_autoptr(GError) local_error = NULL;
  7475. + GError **error = &local_error;
  7476. + struct XattrWorker *worker = data;
  7477. + guint64 end_time = g_get_monotonic_time () + XATTR_THREAD_RUN_TIME_USECS;
  7478. + guint n_read = 0;
  7479. +
  7480. + while (g_get_monotonic_time () < end_time)
  7481. + {
  7482. + g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
  7483. +
  7484. + if (!glnx_dirfd_iterator_init_at (worker->dfd, ".", TRUE, &dfd_iter, error))
  7485. + goto out;
  7486. +
  7487. + if (worker->is_writer)
  7488. + {
  7489. + if (!do_write_run (&dfd_iter, error))
  7490. + goto out;
  7491. + }
  7492. + else
  7493. + {
  7494. + if (!do_read_run (&dfd_iter, &n_read, error))
  7495. + goto out;
  7496. + }
  7497. + }
  7498. +
  7499. + out:
  7500. + g_assert_no_error (local_error);
  7501. +
  7502. + return GINT_TO_POINTER (n_read);
  7503. +}
  7504. +
  7505. +static void
  7506. +test_xattr_races (void)
  7507. +{
  7508. + /* If for some reason we're built in a VM which only has one vcpu, let's still
  7509. + * at least make the test do something.
  7510. + */
  7511. + /* FIXME - this deadlocks for me on 4.9.4-201.fc25.x86_64, whether
  7512. + * using overlayfs or xfs as source/dest.
  7513. + */
  7514. + const guint nprocs = MAX (4, g_get_num_processors ());
  7515. + struct XattrWorker wdata[nprocs];
  7516. + GThread *threads[nprocs];
  7517. + g_autoptr(GError) local_error = NULL;
  7518. + GError **error = &local_error;
  7519. + g_auto(GLnxTmpDir) tmpdir = { 0, };
  7520. + g_autofree char *tmpdir_path = g_strdup_printf ("%s/libglnx-xattrs-XXXXXX",
  7521. + getenv ("TMPDIR") ?: "/var/tmp");
  7522. + guint nread = 0;
  7523. +
  7524. + if (!glnx_mkdtempat (AT_FDCWD, tmpdir_path, 0700,
  7525. + &tmpdir, error))
  7526. + goto out;
  7527. +
  7528. + /* Support people building/testing on tmpfs https://github.com/flatpak/flatpak/issues/686 */
  7529. + if (fsetxattr (tmpdir.fd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
  7530. + {
  7531. + if (errno == EOPNOTSUPP)
  7532. + {
  7533. + g_test_skip ("no xattr support");
  7534. + return;
  7535. + }
  7536. + else
  7537. + {
  7538. + glnx_set_error_from_errno (error);
  7539. + goto out;
  7540. + }
  7541. + }
  7542. +
  7543. + for (guint i = 0; i < nprocs; i++)
  7544. + {
  7545. + struct XattrWorker *worker = &wdata[i];
  7546. + worker->dfd = tmpdir.fd;
  7547. + worker->is_writer = i % 2 == 0;
  7548. + threads[i] = g_thread_new (NULL, xattr_thread, worker);
  7549. + }
  7550. +
  7551. + for (guint i = 0; i < nprocs; i++)
  7552. + {
  7553. + if (wdata[i].is_writer)
  7554. + (void) g_thread_join (threads[i]);
  7555. + else
  7556. + nread += GPOINTER_TO_UINT (g_thread_join (threads[i]));
  7557. + }
  7558. +
  7559. + g_print ("Read %u xattrs race free!\n", nread);
  7560. +
  7561. + out:
  7562. + g_assert_no_error (local_error);
  7563. +}
  7564. +
  7565. +int main (int argc, char **argv)
  7566. +{
  7567. + int ret;
  7568. +
  7569. + g_test_init (&argc, &argv, NULL);
  7570. +
  7571. + g_test_add_func ("/xattr-races", test_xattr_races);
  7572. +
  7573. + ret = g_test_run();
  7574. +
  7575. + return ret;
  7576. +}