libglnx_submodule.patch 226 KB

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