libglnx_submodule.patch 209 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154
  1. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/COPYING flatpak-builder-0.10.6/libglnx/COPYING
  2. --- flatpak-builder-0.10.6.orig/libglnx/COPYING 1970-01-01 02:00:00.000000000 +0200
  3. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/.git flatpak-builder-0.10.6/libglnx/.git
  508. --- flatpak-builder-0.10.6.orig/libglnx/.git 1970-01-01 02:00:00.000000000 +0200
  509. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/.gitignore flatpak-builder-0.10.6/libglnx/.gitignore
  513. --- flatpak-builder-0.10.6.orig/libglnx/.gitignore 1970-01-01 02:00:00.000000000 +0200
  514. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/glnx-backport-autocleanups.h flatpak-builder-0.10.6/libglnx/glnx-backport-autocleanups.h
  533. --- flatpak-builder-0.10.6.orig/libglnx/glnx-backport-autocleanups.h 1970-01-01 02:00:00.000000000 +0200
  534. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/glnx-backport-autoptr.h flatpak-builder-0.10.6/libglnx/glnx-backport-autoptr.h
  661. --- flatpak-builder-0.10.6.orig/libglnx/glnx-backport-autoptr.h 1970-01-01 02:00:00.000000000 +0200
  662. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/glnx-backports.c flatpak-builder-0.10.6/libglnx/glnx-backports.c
  798. --- flatpak-builder-0.10.6.orig/libglnx/glnx-backports.c 1970-01-01 02:00:00.000000000 +0200
  799. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/glnx-backports.h flatpak-builder-0.10.6/libglnx/glnx-backports.h
  863. --- flatpak-builder-0.10.6.orig/libglnx/glnx-backports.h 1970-01-01 02:00:00.000000000 +0200
  864. +++ flatpak-builder-0.10.6/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.6.orig/libglnx/glnx-console.c flatpak-builder-0.10.6/libglnx/glnx-console.c
  913. --- flatpak-builder-0.10.6.orig/libglnx/glnx-console.c 1970-01-01 02:00:00.000000000 +0200
  914. +++ flatpak-builder-0.10.6/libglnx/glnx-console.c 2018-02-11 12:03:43.448373307 +0300
  915. @@ -0,0 +1,309 @@
  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. +static char *current_text = NULL;
  948. +static gint current_percent = -1;
  949. +static gboolean locked;
  950. +
  951. +static gboolean
  952. +stdout_is_tty (void)
  953. +{
  954. + static gsize initialized = 0;
  955. + static gboolean stdout_is_tty_v;
  956. +
  957. + if (g_once_init_enter (&initialized))
  958. + {
  959. + stdout_is_tty_v = isatty (1);
  960. + g_once_init_leave (&initialized, 1);
  961. + }
  962. +
  963. + return stdout_is_tty_v;
  964. +}
  965. +
  966. +static volatile guint cached_columns = 0;
  967. +static volatile guint cached_lines = 0;
  968. +
  969. +static int
  970. +fd_columns (int fd)
  971. +{
  972. + struct winsize ws = {};
  973. +
  974. + if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
  975. + return -errno;
  976. +
  977. + if (ws.ws_col <= 0)
  978. + return -EIO;
  979. +
  980. + return ws.ws_col;
  981. +}
  982. +
  983. +/**
  984. + * glnx_console_columns:
  985. + *
  986. + * Returns: The number of columns for terminal output
  987. + */
  988. +guint
  989. +glnx_console_columns (void)
  990. +{
  991. + if (G_UNLIKELY (cached_columns == 0))
  992. + {
  993. + int c;
  994. +
  995. + c = fd_columns (STDOUT_FILENO);
  996. +
  997. + if (c <= 0)
  998. + c = 80;
  999. +
  1000. + if (c > 256)
  1001. + c = 256;
  1002. +
  1003. + cached_columns = c;
  1004. + }
  1005. +
  1006. + return cached_columns;
  1007. +}
  1008. +
  1009. +static int
  1010. +fd_lines (int fd)
  1011. +{
  1012. + struct winsize ws = {};
  1013. +
  1014. + if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
  1015. + return -errno;
  1016. +
  1017. + if (ws.ws_row <= 0)
  1018. + return -EIO;
  1019. +
  1020. + return ws.ws_row;
  1021. +}
  1022. +
  1023. +/**
  1024. + * glnx_console_lines:
  1025. + *
  1026. + * Returns: The number of lines for terminal output
  1027. + */
  1028. +guint
  1029. +glnx_console_lines (void)
  1030. +{
  1031. + if (G_UNLIKELY (cached_lines == 0))
  1032. + {
  1033. + int l;
  1034. +
  1035. + l = fd_lines (STDOUT_FILENO);
  1036. +
  1037. + if (l <= 0)
  1038. + l = 24;
  1039. +
  1040. + cached_lines = l;
  1041. + }
  1042. +
  1043. + return cached_lines;
  1044. +}
  1045. +
  1046. +static void
  1047. +on_sigwinch (int signum)
  1048. +{
  1049. + cached_columns = 0;
  1050. + cached_lines = 0;
  1051. +}
  1052. +
  1053. +void
  1054. +glnx_console_lock (GLnxConsoleRef *console)
  1055. +{
  1056. + static gsize sigwinch_initialized = 0;
  1057. +
  1058. + g_return_if_fail (!locked);
  1059. + g_return_if_fail (!console->locked);
  1060. +
  1061. + console->is_tty = stdout_is_tty ();
  1062. +
  1063. + locked = console->locked = TRUE;
  1064. +
  1065. + current_percent = 0;
  1066. +
  1067. + if (console->is_tty)
  1068. + {
  1069. + if (g_once_init_enter (&sigwinch_initialized))
  1070. + {
  1071. + signal (SIGWINCH, on_sigwinch);
  1072. + g_once_init_leave (&sigwinch_initialized, 1);
  1073. + }
  1074. +
  1075. + { static const char initbuf[] = { '\n', 0x1B, 0x37 };
  1076. + (void) fwrite (initbuf, 1, sizeof (initbuf), stdout);
  1077. + }
  1078. + }
  1079. +}
  1080. +
  1081. +static void
  1082. +printpad (const char *padbuf,
  1083. + guint padbuf_len,
  1084. + guint n)
  1085. +{
  1086. + const guint d = n / padbuf_len;
  1087. + const guint r = n % padbuf_len;
  1088. + guint i;
  1089. +
  1090. + for (i = 0; i < d; i++)
  1091. + fwrite (padbuf, 1, padbuf_len, stdout);
  1092. + fwrite (padbuf, 1, r, stdout);
  1093. +}
  1094. +
  1095. +static void
  1096. +text_percent_internal (const char *text,
  1097. + int percentage)
  1098. +{
  1099. + static const char equals[] = "====================";
  1100. + const guint n_equals = sizeof (equals) - 1;
  1101. + static const char spaces[] = " ";
  1102. + const guint n_spaces = sizeof (spaces) - 1;
  1103. + const guint ncolumns = glnx_console_columns ();
  1104. + const guint bar_min = 10;
  1105. +
  1106. + if (text && !*text)
  1107. + text = NULL;
  1108. +
  1109. + const guint input_textlen = text ? strlen (text) : 0;
  1110. +
  1111. + if (percentage == current_percent
  1112. + && g_strcmp0 (text, current_text) == 0)
  1113. + return;
  1114. +
  1115. + if (!stdout_is_tty ())
  1116. + {
  1117. + if (text)
  1118. + fprintf (stdout, "%s", text);
  1119. + if (percentage != -1)
  1120. + {
  1121. + if (text)
  1122. + fputc (' ', stdout);
  1123. + fprintf (stdout, "%u%%", percentage);
  1124. + }
  1125. + fputc ('\n', stdout);
  1126. + fflush (stdout);
  1127. + return;
  1128. + }
  1129. +
  1130. + if (ncolumns < bar_min)
  1131. + return; /* TODO: spinner */
  1132. +
  1133. + /* Restore cursor */
  1134. + { const char beginbuf[2] = { 0x1B, 0x38 };
  1135. + (void) fwrite (beginbuf, 1, sizeof (beginbuf), stdout);
  1136. + }
  1137. +
  1138. + if (percentage == -1)
  1139. + {
  1140. + if (text != NULL)
  1141. + fwrite (text, 1, input_textlen, stdout);
  1142. +
  1143. + /* Overwrite remaining space, if any */
  1144. + if (ncolumns > input_textlen)
  1145. + printpad (spaces, n_spaces, ncolumns - input_textlen);
  1146. + }
  1147. + else
  1148. + {
  1149. + const guint textlen = MIN (input_textlen, ncolumns - bar_min);
  1150. + const guint barlen = ncolumns - (textlen + 1);;
  1151. +
  1152. + if (textlen > 0)
  1153. + {
  1154. + fwrite (text, 1, textlen, stdout);
  1155. + fputc (' ', stdout);
  1156. + }
  1157. +
  1158. + {
  1159. + const guint nbraces = 2;
  1160. + const guint textpercent_len = 5;
  1161. + const guint bar_internal_len = barlen - nbraces - textpercent_len;
  1162. + const guint eqlen = bar_internal_len * (percentage / 100.0);
  1163. + const guint spacelen = bar_internal_len - eqlen;
  1164. +
  1165. + fputc ('[', stdout);
  1166. + printpad (equals, n_equals, eqlen);
  1167. + printpad (spaces, n_spaces, spacelen);
  1168. + fputc (']', stdout);
  1169. + fprintf (stdout, " %3d%%", percentage);
  1170. + }
  1171. + }
  1172. +
  1173. + fflush (stdout);
  1174. +}
  1175. +
  1176. +/**
  1177. + * glnx_console_progress_text_percent:
  1178. + * @text: Show this text before the progress bar
  1179. + * @percentage: An integer in the range of 0 to 100
  1180. + *
  1181. + * On a tty, print to the console @text followed by an ASCII art
  1182. + * progress bar whose percentage is @percentage. If stdout is not a
  1183. + * tty, a more basic line by line change will be printed.
  1184. + *
  1185. + * You must have called glnx_console_lock() before invoking this
  1186. + * function.
  1187. + *
  1188. + */
  1189. +void
  1190. +glnx_console_progress_text_percent (const char *text,
  1191. + guint percentage)
  1192. +{
  1193. + g_return_if_fail (percentage <= 100);
  1194. +
  1195. + text_percent_internal (text, percentage);
  1196. +}
  1197. +
  1198. +void
  1199. +glnx_console_text (const char *text)
  1200. +{
  1201. + text_percent_internal (text, -1);
  1202. +}
  1203. +
  1204. +/**
  1205. + * glnx_console_unlock:
  1206. + *
  1207. + * Print a newline, and reset all cached console progress state.
  1208. + *
  1209. + * This function does nothing if stdout is not a tty.
  1210. + */
  1211. +void
  1212. +glnx_console_unlock (GLnxConsoleRef *console)
  1213. +{
  1214. + g_return_if_fail (locked);
  1215. + g_return_if_fail (console->locked);
  1216. +
  1217. + current_percent = -1;
  1218. + g_clear_pointer (&current_text, g_free);
  1219. +
  1220. + if (console->is_tty)
  1221. + fputc ('\n', stdout);
  1222. +
  1223. + locked = console->locked = FALSE;
  1224. +}
  1225. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-console.h flatpak-builder-0.10.6/libglnx/glnx-console.h
  1226. --- flatpak-builder-0.10.6.orig/libglnx/glnx-console.h 1970-01-01 02:00:00.000000000 +0200
  1227. +++ flatpak-builder-0.10.6/libglnx/glnx-console.h 2018-02-11 12:03:43.448373307 +0300
  1228. @@ -0,0 +1,55 @@
  1229. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1230. + *
  1231. + * Copyright (C) 2013,2014,2015 Colin Walters <walters@verbum.org>
  1232. + *
  1233. + * This program is free software: you can redistribute it and/or modify
  1234. + * it under the terms of the GNU Lesser General Public License as published
  1235. + * by the Free Software Foundation; either version 2 of the licence or (at
  1236. + * your option) any later version.
  1237. + *
  1238. + * This library is distributed in the hope that it will be useful,
  1239. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1240. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1241. + * Lesser General Public License for more details.
  1242. + *
  1243. + * You should have received a copy of the GNU Lesser General
  1244. + * Public License along with this library; if not, write to the
  1245. + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  1246. + * Boston, MA 02111-1307, USA.
  1247. + */
  1248. +
  1249. +#pragma once
  1250. +
  1251. +#include <glnx-backport-autocleanups.h>
  1252. +
  1253. +G_BEGIN_DECLS
  1254. +
  1255. +struct GLnxConsoleRef {
  1256. + gboolean locked;
  1257. + gboolean is_tty;
  1258. +};
  1259. +
  1260. +typedef struct GLnxConsoleRef GLnxConsoleRef;
  1261. +
  1262. +void glnx_console_lock (GLnxConsoleRef *ref);
  1263. +
  1264. +void glnx_console_text (const char *text);
  1265. +
  1266. +void glnx_console_progress_text_percent (const char *text,
  1267. + guint percentage);
  1268. +
  1269. +void glnx_console_unlock (GLnxConsoleRef *ref);
  1270. +
  1271. +guint glnx_console_lines (void);
  1272. +
  1273. +guint glnx_console_columns (void);
  1274. +
  1275. +static inline void
  1276. +glnx_console_ref_cleanup (GLnxConsoleRef *p)
  1277. +{
  1278. + if (p->locked)
  1279. + glnx_console_unlock (p);
  1280. +}
  1281. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxConsoleRef, glnx_console_ref_cleanup)
  1282. +
  1283. +G_END_DECLS
  1284. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-dirfd.c flatpak-builder-0.10.6/libglnx/glnx-dirfd.c
  1285. --- flatpak-builder-0.10.6.orig/libglnx/glnx-dirfd.c 1970-01-01 02:00:00.000000000 +0200
  1286. +++ flatpak-builder-0.10.6/libglnx/glnx-dirfd.c 2018-02-11 12:03:43.448373307 +0300
  1287. @@ -0,0 +1,390 @@
  1288. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1289. + *
  1290. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  1291. + *
  1292. + * This library is free software; you can redistribute it and/or
  1293. + * modify it under the terms of the GNU Lesser General Public
  1294. + * License as published by the Free Software Foundation; either
  1295. + * version 2 of the License, or (at your option) any later version.
  1296. + *
  1297. + * This library is distributed in the hope that it will be useful,
  1298. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1299. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1300. + * Lesser General Public License for more details.
  1301. + *
  1302. + * You should have received a copy of the GNU Lesser General Public
  1303. + * License along with this library; if not, write to the
  1304. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1305. + * Boston, MA 02111-1307, USA.
  1306. + */
  1307. +
  1308. +#include "config.h"
  1309. +
  1310. +#include <string.h>
  1311. +
  1312. +#include <glnx-dirfd.h>
  1313. +#include <glnx-errors.h>
  1314. +#include <glnx-local-alloc.h>
  1315. +
  1316. +/**
  1317. + * glnx_opendirat_with_errno:
  1318. + * @dfd: File descriptor for origin directory
  1319. + * @name: Pathname, relative to @dfd
  1320. + * @follow: Whether or not to follow symbolic links
  1321. + *
  1322. + * Use openat() to open a directory, using a standard set of flags.
  1323. + * This function sets errno.
  1324. + */
  1325. +int
  1326. +glnx_opendirat_with_errno (int dfd,
  1327. + const char *path,
  1328. + gboolean follow)
  1329. +{
  1330. + int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY;
  1331. + if (!follow)
  1332. + flags |= O_NOFOLLOW;
  1333. +
  1334. + dfd = glnx_dirfd_canonicalize (dfd);
  1335. +
  1336. + return openat (dfd, path, flags);
  1337. +}
  1338. +
  1339. +/**
  1340. + * glnx_opendirat:
  1341. + * @dfd: File descriptor for origin directory
  1342. + * @path: Pathname, relative to @dfd
  1343. + * @follow: Whether or not to follow symbolic links
  1344. + * @error: Error
  1345. + *
  1346. + * Use openat() to open a directory, using a standard set of flags.
  1347. + */
  1348. +gboolean
  1349. +glnx_opendirat (int dfd,
  1350. + const char *path,
  1351. + gboolean follow,
  1352. + int *out_fd,
  1353. + GError **error)
  1354. +{
  1355. + int ret = glnx_opendirat_with_errno (dfd, path, follow);
  1356. + if (ret == -1)
  1357. + return glnx_throw_errno_prefix (error, "opendir(%s)", path);
  1358. + *out_fd = ret;
  1359. + return TRUE;
  1360. +}
  1361. +
  1362. +struct GLnxRealDirfdIterator
  1363. +{
  1364. + gboolean initialized;
  1365. + int fd;
  1366. + DIR *d;
  1367. +};
  1368. +typedef struct GLnxRealDirfdIterator GLnxRealDirfdIterator;
  1369. +
  1370. +/**
  1371. + * glnx_dirfd_iterator_init_at:
  1372. + * @dfd: File descriptor, may be AT_FDCWD or -1
  1373. + * @path: Path, may be relative to @dfd
  1374. + * @follow: If %TRUE and the last component of @path is a symlink, follow it
  1375. + * @out_dfd_iter: (out caller-allocates): A directory iterator, will be initialized
  1376. + * @error: Error
  1377. + *
  1378. + * Initialize @out_dfd_iter from @dfd and @path.
  1379. + */
  1380. +gboolean
  1381. +glnx_dirfd_iterator_init_at (int dfd,
  1382. + const char *path,
  1383. + gboolean follow,
  1384. + GLnxDirFdIterator *out_dfd_iter,
  1385. + GError **error)
  1386. +{
  1387. + glnx_fd_close int fd = -1;
  1388. + if (!glnx_opendirat (dfd, path, follow, &fd, error))
  1389. + return FALSE;
  1390. +
  1391. + if (!glnx_dirfd_iterator_init_take_fd (&fd, out_dfd_iter, error))
  1392. + return FALSE;
  1393. +
  1394. + return TRUE;
  1395. +}
  1396. +
  1397. +/**
  1398. + * glnx_dirfd_iterator_init_take_fd:
  1399. + * @dfd: File descriptor - ownership is taken, and the value is set to -1
  1400. + * @dfd_iter: A directory iterator
  1401. + * @error: Error
  1402. + *
  1403. + * Steal ownership of @dfd, using it to initialize @dfd_iter for
  1404. + * iteration.
  1405. + */
  1406. +gboolean
  1407. +glnx_dirfd_iterator_init_take_fd (int *dfd,
  1408. + GLnxDirFdIterator *dfd_iter,
  1409. + GError **error)
  1410. +{
  1411. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  1412. + DIR *d = fdopendir (*dfd);
  1413. + if (!d)
  1414. + return glnx_throw_errno_prefix (error, "fdopendir");
  1415. +
  1416. + real_dfd_iter->fd = glnx_steal_fd (dfd);
  1417. + real_dfd_iter->d = d;
  1418. + real_dfd_iter->initialized = TRUE;
  1419. +
  1420. + return TRUE;
  1421. +}
  1422. +
  1423. +/**
  1424. + * glnx_dirfd_iterator_next_dent:
  1425. + * @dfd_iter: A directory iterator
  1426. + * @out_dent: (out) (transfer none): Pointer to dirent; do not free
  1427. + * @cancellable: Cancellable
  1428. + * @error: Error
  1429. + *
  1430. + * Read the next value from @dfd_iter, causing @out_dent to be
  1431. + * updated. If end of stream is reached, @out_dent will be set
  1432. + * to %NULL, and %TRUE will be returned.
  1433. + */
  1434. +gboolean
  1435. +glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter,
  1436. + struct dirent **out_dent,
  1437. + GCancellable *cancellable,
  1438. + GError **error)
  1439. +{
  1440. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  1441. +
  1442. + g_return_val_if_fail (out_dent, FALSE);
  1443. + g_return_val_if_fail (dfd_iter->initialized, FALSE);
  1444. +
  1445. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  1446. + return FALSE;
  1447. +
  1448. + do
  1449. + {
  1450. + errno = 0;
  1451. + *out_dent = readdir (real_dfd_iter->d);
  1452. + if (*out_dent == NULL && errno != 0)
  1453. + return glnx_throw_errno_prefix (error, "readdir");
  1454. + } while (*out_dent &&
  1455. + (strcmp ((*out_dent)->d_name, ".") == 0 ||
  1456. + strcmp ((*out_dent)->d_name, "..") == 0));
  1457. +
  1458. + return TRUE;
  1459. +}
  1460. +
  1461. +/**
  1462. + * glnx_dirfd_iterator_next_dent_ensure_dtype:
  1463. + * @dfd_iter: A directory iterator
  1464. + * @out_dent: (out) (transfer none): Pointer to dirent; do not free
  1465. + * @cancellable: Cancellable
  1466. + * @error: Error
  1467. + *
  1468. + * A variant of @glnx_dirfd_iterator_next_dent, which will ensure the
  1469. + * `dent->d_type` member is filled in by calling `fstatat`
  1470. + * automatically if the underlying filesystem type sets `DT_UNKNOWN`.
  1471. + */
  1472. +gboolean
  1473. +glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator *dfd_iter,
  1474. + struct dirent **out_dent,
  1475. + GCancellable *cancellable,
  1476. + GError **error)
  1477. +{
  1478. + struct dirent *ret_dent;
  1479. +
  1480. + g_return_val_if_fail (out_dent, FALSE);
  1481. +
  1482. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, out_dent, cancellable, error))
  1483. + return FALSE;
  1484. +
  1485. + ret_dent = *out_dent;
  1486. +
  1487. + if (ret_dent)
  1488. + {
  1489. +
  1490. + if (ret_dent->d_type == DT_UNKNOWN)
  1491. + {
  1492. + struct stat stbuf;
  1493. + if (TEMP_FAILURE_RETRY (fstatat (dfd_iter->fd, ret_dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW)) != 0)
  1494. + return glnx_throw_errno (error);
  1495. + ret_dent->d_type = IFTODT (stbuf.st_mode);
  1496. + }
  1497. + }
  1498. +
  1499. + return TRUE;
  1500. +}
  1501. +
  1502. +/**
  1503. + * glnx_dirfd_iterator_clear:
  1504. + * @dfd_iter: Iterator, will be de-initialized
  1505. + *
  1506. + * Unset @dfd_iter, freeing any resources. If @dfd_iter is not
  1507. + * initialized, do nothing.
  1508. + */
  1509. +void
  1510. +glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter)
  1511. +{
  1512. + GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
  1513. + /* fd is owned by dfd_iter */
  1514. + if (!real_dfd_iter->initialized)
  1515. + return;
  1516. + (void) closedir (real_dfd_iter->d);
  1517. + real_dfd_iter->initialized = FALSE;
  1518. +}
  1519. +
  1520. +/**
  1521. + * glnx_fdrel_abspath:
  1522. + * @dfd: Directory fd
  1523. + * @path: Path
  1524. + *
  1525. + * Turn a fd-relative pair into something that can be used for legacy
  1526. + * APIs expecting absolute paths.
  1527. + *
  1528. + * This is Linux specific, and only valid inside this process (unless
  1529. + * you set up the child process to have the exact same fd number, but
  1530. + * don't try that).
  1531. + */
  1532. +char *
  1533. +glnx_fdrel_abspath (int dfd,
  1534. + const char *path)
  1535. +{
  1536. + dfd = glnx_dirfd_canonicalize (dfd);
  1537. + if (dfd == AT_FDCWD)
  1538. + return g_strdup (path);
  1539. + return g_strdup_printf ("/proc/self/fd/%d/%s", dfd, path);
  1540. +}
  1541. +
  1542. +/**
  1543. + * glnx_gen_temp_name:
  1544. + * @tmpl: (type filename): template directory name, the last 6 characters will be replaced
  1545. + *
  1546. + * Replace the last 6 characters of @tmpl with random ASCII. You must
  1547. + * use this in combination with a mechanism to ensure race-free file
  1548. + * creation such as `O_EXCL`.
  1549. + */
  1550. +void
  1551. +glnx_gen_temp_name (gchar *tmpl)
  1552. +{
  1553. + size_t len;
  1554. + char *XXXXXX;
  1555. + int i;
  1556. + static const char letters[] =
  1557. + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  1558. + static const int NLETTERS = sizeof (letters) - 1;
  1559. +
  1560. + g_return_if_fail (tmpl != NULL);
  1561. + len = strlen (tmpl);
  1562. + g_return_if_fail (len >= 6);
  1563. +
  1564. + XXXXXX = tmpl + (len - 6);
  1565. +
  1566. + for (i = 0; i < 6; i++)
  1567. + XXXXXX[i] = letters[g_random_int_range(0, NLETTERS)];
  1568. +}
  1569. +
  1570. +/**
  1571. + * glnx_mkdtempat:
  1572. + * @dfd: Directory fd
  1573. + * @tmpl: (type filename): template directory name, last 6 characters will be replaced
  1574. + * @mode: permissions to create the temporary directory with
  1575. + * @error: Error
  1576. + *
  1577. + * Similar to g_mkdtemp_full, but using openat.
  1578. + */
  1579. +gboolean
  1580. +glnx_mkdtempat (int dfd,
  1581. + gchar *tmpl,
  1582. + int mode,
  1583. + GError **error)
  1584. +{
  1585. + int count;
  1586. +
  1587. + g_return_val_if_fail (tmpl != NULL, -1);
  1588. +
  1589. + for (count = 0; count < 100; count++)
  1590. + {
  1591. + glnx_gen_temp_name (tmpl);
  1592. +
  1593. + if (mkdirat (dfd, tmpl, mode) == -1)
  1594. + {
  1595. + if (errno == EEXIST)
  1596. + continue;
  1597. +
  1598. + /* Any other error will apply also to other names we might
  1599. + * try, and there are 2^32 or so of them, so give up now.
  1600. + */
  1601. + return glnx_throw_errno_prefix (error, "mkdirat");
  1602. + }
  1603. +
  1604. + return TRUE;
  1605. + }
  1606. +
  1607. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  1608. + "mkstempat ran out of combinations to try.");
  1609. + return FALSE;
  1610. +}
  1611. +
  1612. +/**
  1613. + * glnx_mkdtempat_open:
  1614. + * @dfd: Directory FD
  1615. + * @tmpl: (type filename): template directory name, last 6 characters will be replaced
  1616. + * @mode: permissions to create the temporary directory with
  1617. + * @out_dfd: (out caller-allocates): Return location for an FD for the new
  1618. + * temporary directory, or `-1` on error
  1619. + * @error: Return location for a #GError, or %NULL
  1620. + *
  1621. + * Similar to glnx_mkdtempat(), except it will open the resulting temporary
  1622. + * directory and return a directory FD to it.
  1623. + *
  1624. + * Returns: %TRUE on success, %FALSE otherwise
  1625. + * Since: UNRELEASED
  1626. + */
  1627. +gboolean
  1628. +glnx_mkdtempat_open (int dfd,
  1629. + gchar *tmpl,
  1630. + int mode,
  1631. + int *out_dfd,
  1632. + GError **error)
  1633. +{
  1634. + /* FIXME: Ideally we could use openat(O_DIRECTORY | O_CREAT | O_EXCL) here
  1635. + * to create and open the directory atomically, but that’s not supported by
  1636. + * current kernel versions: http://www.openwall.com/lists/oss-security/2014/11/26/14
  1637. + * (Tested on kernel 4.10.10-200.fc25.x86_64). For the moment, accept a
  1638. + * TOCTTOU race here. */
  1639. + *out_dfd = -1;
  1640. +
  1641. + if (!glnx_mkdtempat (dfd, tmpl, mode, error))
  1642. + return FALSE;
  1643. +
  1644. + return glnx_opendirat (dfd, tmpl, FALSE, out_dfd, error);
  1645. +}
  1646. +
  1647. +/**
  1648. + * glnx_mkdtempat_open_in_system:
  1649. + * @tmpl: (type filename): template directory name, last 6 characters will be replaced
  1650. + * @mode: permissions to create the temporary directory with
  1651. + * @out_dfd: (out caller-allocates): Return location for an FD for the new
  1652. + * temporary directory, or `-1` on error
  1653. + * @error: Return location for a #GError, or %NULL
  1654. + *
  1655. + * Similar to glnx_mkdtempat_open(), except it will use the system temporary
  1656. + * directory (from g_get_tmp_dir()) as the parent directory to @tmpl.
  1657. + *
  1658. + * Returns: %TRUE on success, %FALSE otherwise
  1659. + * Since: UNRELEASED
  1660. + */
  1661. +gboolean
  1662. +glnx_mkdtempat_open_in_system (gchar *tmpl,
  1663. + int mode,
  1664. + int *out_dfd,
  1665. + GError **error)
  1666. +{
  1667. + glnx_fd_close int tmp_dfd = -1;
  1668. +
  1669. + *out_dfd = -1;
  1670. +
  1671. + if (!glnx_opendirat (-1, g_get_tmp_dir (), TRUE, &tmp_dfd, error))
  1672. + return FALSE;
  1673. +
  1674. + return glnx_mkdtempat_open (tmp_dfd, tmpl, mode, out_dfd, error);
  1675. +}
  1676. +
  1677. +
  1678. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-dirfd.h flatpak-builder-0.10.6/libglnx/glnx-dirfd.h
  1679. --- flatpak-builder-0.10.6.orig/libglnx/glnx-dirfd.h 1970-01-01 02:00:00.000000000 +0200
  1680. +++ flatpak-builder-0.10.6/libglnx/glnx-dirfd.h 2018-02-11 12:03:43.448373307 +0300
  1681. @@ -0,0 +1,132 @@
  1682. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1683. + *
  1684. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  1685. + *
  1686. + * This library is free software; you can redistribute it and/or
  1687. + * modify it under the terms of the GNU Lesser General Public
  1688. + * License as published by the Free Software Foundation; either
  1689. + * version 2 of the License, or (at your option) any later version.
  1690. + *
  1691. + * This library is distributed in the hope that it will be useful,
  1692. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1693. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1694. + * Lesser General Public License for more details.
  1695. + *
  1696. + * You should have received a copy of the GNU Lesser General Public
  1697. + * License along with this library; if not, write to the
  1698. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1699. + * Boston, MA 02111-1307, USA.
  1700. + */
  1701. +
  1702. +#pragma once
  1703. +
  1704. +#include <glnx-backport-autocleanups.h>
  1705. +#include <glnx-macros.h>
  1706. +#include <glnx-errors.h>
  1707. +#include <limits.h>
  1708. +#include <dirent.h>
  1709. +#include <sys/stat.h>
  1710. +#include <fcntl.h>
  1711. +
  1712. +G_BEGIN_DECLS
  1713. +
  1714. +/**
  1715. + * glnx_dirfd_canonicalize:
  1716. + * @fd: A directory file descriptor
  1717. + *
  1718. + * It's often convenient in programs to use `-1` for "unassigned fd",
  1719. + * and also because gobject-introspection doesn't support `AT_FDCWD`,
  1720. + * libglnx honors `-1` to mean `AT_FDCWD`. This small inline function
  1721. + * canonicalizes `-1 -> AT_FDCWD`.
  1722. + */
  1723. +static inline int
  1724. +glnx_dirfd_canonicalize (int fd)
  1725. +{
  1726. + if (fd == -1)
  1727. + return AT_FDCWD;
  1728. + return fd;
  1729. +}
  1730. +
  1731. +struct GLnxDirFdIterator {
  1732. + gboolean initialized;
  1733. + int fd;
  1734. + gpointer padding_data[4];
  1735. +};
  1736. +
  1737. +typedef struct GLnxDirFdIterator GLnxDirFdIterator;
  1738. +gboolean glnx_dirfd_iterator_init_at (int dfd, const char *path,
  1739. + gboolean follow,
  1740. + GLnxDirFdIterator *dfd_iter, GError **error);
  1741. +gboolean glnx_dirfd_iterator_init_take_fd (int *dfd, GLnxDirFdIterator *dfd_iter, GError **error);
  1742. +gboolean glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter,
  1743. + struct dirent **out_dent,
  1744. + GCancellable *cancellable,
  1745. + GError **error);
  1746. +gboolean glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator *dfd_iter,
  1747. + struct dirent **out_dent,
  1748. + GCancellable *cancellable,
  1749. + GError **error);
  1750. +void glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter);
  1751. +
  1752. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxDirFdIterator, glnx_dirfd_iterator_clear)
  1753. +
  1754. +int glnx_opendirat_with_errno (int dfd,
  1755. + const char *path,
  1756. + gboolean follow);
  1757. +
  1758. +gboolean glnx_opendirat (int dfd,
  1759. + const char *path,
  1760. + gboolean follow,
  1761. + int *out_fd,
  1762. + GError **error);
  1763. +
  1764. +char *glnx_fdrel_abspath (int dfd,
  1765. + const char *path);
  1766. +
  1767. +void glnx_gen_temp_name (gchar *tmpl);
  1768. +
  1769. +/**
  1770. + * glnx_ensure_dir:
  1771. + * @dfd: directory fd
  1772. + * @path: Directory path
  1773. + * @mode: Mode
  1774. + * @error: Return location for a #GError, or %NULL
  1775. + *
  1776. + * Wrapper around mkdirat() which ignores adds #GError support, ensures that
  1777. + * it retries on %EINTR, and also ignores `EEXIST`.
  1778. + *
  1779. + * See also `glnx_shutil_mkdir_p_at()` for recursive handling.
  1780. + *
  1781. + * Returns: %TRUE on success, %FALSE otherwise
  1782. + */
  1783. +static inline gboolean
  1784. +glnx_ensure_dir (int dfd,
  1785. + const char *path,
  1786. + mode_t mode,
  1787. + GError **error)
  1788. +{
  1789. + if (TEMP_FAILURE_RETRY (mkdirat (dfd, path, mode)) != 0)
  1790. + {
  1791. + if (G_UNLIKELY (errno != EEXIST))
  1792. + return glnx_throw_errno_prefix (error, "mkdirat(%s)", path);
  1793. + }
  1794. + return TRUE;
  1795. +}
  1796. +
  1797. +gboolean glnx_mkdtempat (int dfd,
  1798. + gchar *tmpl,
  1799. + int mode,
  1800. + GError **error);
  1801. +
  1802. +gboolean glnx_mkdtempat_open (int dfd,
  1803. + gchar *tmpl,
  1804. + int mode,
  1805. + int *out_dfd,
  1806. + GError **error);
  1807. +
  1808. +gboolean glnx_mkdtempat_open_in_system (gchar *tmpl,
  1809. + int mode,
  1810. + int *out_dfd,
  1811. + GError **error);
  1812. +
  1813. +G_END_DECLS
  1814. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-errors.c flatpak-builder-0.10.6/libglnx/glnx-errors.c
  1815. --- flatpak-builder-0.10.6.orig/libglnx/glnx-errors.c 1970-01-01 02:00:00.000000000 +0200
  1816. +++ flatpak-builder-0.10.6/libglnx/glnx-errors.c 2018-02-11 12:03:43.448373307 +0300
  1817. @@ -0,0 +1,56 @@
  1818. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1819. + *
  1820. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  1821. + *
  1822. + * This library is free software; you can redistribute it and/or
  1823. + * modify it under the terms of the GNU Lesser General Public
  1824. + * License as published by the Free Software Foundation; either
  1825. + * version 2 of the License, or (at your option) any later version.
  1826. + *
  1827. + * This library is distributed in the hope that it will be useful,
  1828. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1829. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1830. + * Lesser General Public License for more details.
  1831. + *
  1832. + * You should have received a copy of the GNU Lesser General Public
  1833. + * License along with this library; if not, write to the
  1834. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1835. + * Boston, MA 02111-1307, USA.
  1836. + */
  1837. +
  1838. +#include "config.h"
  1839. +
  1840. +#include <glnx-backport-autocleanups.h>
  1841. +#include <glnx-errors.h>
  1842. +
  1843. +void
  1844. +glnx_real_set_prefix_error_va (GError *error,
  1845. + const char *format,
  1846. + va_list args)
  1847. +{
  1848. + if (error == NULL)
  1849. + return;
  1850. +
  1851. + g_autofree char *old_msg = g_steal_pointer (&error->message);
  1852. + g_autoptr(GString) buf = g_string_new ("");
  1853. + g_string_append_vprintf (buf, format, args);
  1854. + g_string_append (buf, ": ");
  1855. + g_string_append (buf, old_msg);
  1856. + error->message = g_string_free (g_steal_pointer (&buf), FALSE);
  1857. +}
  1858. +
  1859. +void
  1860. +glnx_real_set_prefix_error_from_errno_va (GError **error,
  1861. + gint errsv,
  1862. + const char *format,
  1863. + va_list args)
  1864. +{
  1865. + if (!error)
  1866. + return;
  1867. +
  1868. + g_set_error_literal (error,
  1869. + G_IO_ERROR,
  1870. + g_io_error_from_errno (errsv),
  1871. + g_strerror (errsv));
  1872. + glnx_real_set_prefix_error_va (*error, format, args);
  1873. +}
  1874. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-errors.h flatpak-builder-0.10.6/libglnx/glnx-errors.h
  1875. --- flatpak-builder-0.10.6.orig/libglnx/glnx-errors.h 1970-01-01 02:00:00.000000000 +0200
  1876. +++ flatpak-builder-0.10.6/libglnx/glnx-errors.h 2018-02-11 12:03:43.448373307 +0300
  1877. @@ -0,0 +1,197 @@
  1878. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  1879. + *
  1880. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  1881. + *
  1882. + * This library is free software; you can redistribute it and/or
  1883. + * modify it under the terms of the GNU Lesser General Public
  1884. + * License as published by the Free Software Foundation; either
  1885. + * version 2 of the License, or (at your option) any later version.
  1886. + *
  1887. + * This library is distributed in the hope that it will be useful,
  1888. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1889. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1890. + * Lesser General Public License for more details.
  1891. + *
  1892. + * You should have received a copy of the GNU Lesser General Public
  1893. + * License along with this library; if not, write to the
  1894. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1895. + * Boston, MA 02111-1307, USA.
  1896. + */
  1897. +
  1898. +#pragma once
  1899. +
  1900. +#include <glnx-backport-autocleanups.h>
  1901. +#include <errno.h>
  1902. +
  1903. +G_BEGIN_DECLS
  1904. +
  1905. +/* Set @error with G_IO_ERROR/G_IO_ERROR_FAILED.
  1906. + *
  1907. + * This function returns %FALSE so it can be used conveniently in a single
  1908. + * statement:
  1909. + *
  1910. + * ```
  1911. + * if (strcmp (foo, "somevalue") != 0)
  1912. + * return glnx_throw (error, "key must be somevalue, not '%s'", foo);
  1913. + * ```
  1914. + */
  1915. +static inline gboolean G_GNUC_PRINTF (2,3)
  1916. +glnx_throw (GError **error, const char *fmt, ...)
  1917. +{
  1918. + if (error == NULL)
  1919. + return FALSE;
  1920. +
  1921. + va_list args;
  1922. + va_start (args, fmt);
  1923. + GError *new = g_error_new_valist (G_IO_ERROR, G_IO_ERROR_FAILED, fmt, args);
  1924. + va_end (args);
  1925. + g_propagate_error (error, g_steal_pointer (&new));
  1926. + return FALSE;
  1927. +}
  1928. +
  1929. +/* Like `glnx_throw ()`, but returns %NULL. */
  1930. +#define glnx_null_throw(error, args...) \
  1931. + ({glnx_throw (error, args); NULL;})
  1932. +
  1933. +/* Implementation detail of glnx_throw_prefix() */
  1934. +void glnx_real_set_prefix_error_va (GError *error,
  1935. + const char *format,
  1936. + va_list args) G_GNUC_PRINTF (2,0);
  1937. +
  1938. +/* Prepend to @error's message by `$prefix: ` where `$prefix` is computed via
  1939. + * printf @fmt. Returns %FALSE so it can be used conveniently in a single
  1940. + * statement:
  1941. + *
  1942. + * ```
  1943. + * if (!function_that_fails (s, error))
  1944. + * return glnx_throw_prefix (error, "while handling '%s'", s);
  1945. + * ```
  1946. + * */
  1947. +static inline gboolean G_GNUC_PRINTF (2,3)
  1948. +glnx_prefix_error (GError **error, const char *fmt, ...)
  1949. +{
  1950. + if (error == NULL)
  1951. + return FALSE;
  1952. +
  1953. + va_list args;
  1954. + va_start (args, fmt);
  1955. + glnx_real_set_prefix_error_va (*error, fmt, args);
  1956. + va_end (args);
  1957. + return FALSE;
  1958. +}
  1959. +
  1960. +/* Like `glnx_prefix_error ()`, but returns %NULL. */
  1961. +#define glnx_prefix_error_null(error, args...) \
  1962. + ({glnx_prefix_error (error, args); NULL;})
  1963. +
  1964. +/**
  1965. + * GLNX_AUTO_PREFIX_ERROR:
  1966. + *
  1967. + * An autocleanup-based macro to automatically call `g_prefix_error()` (also with a colon+space `: `)
  1968. + * when it goes out of scope. This is useful when one wants error strings built up by the callee
  1969. + * function, not all callers.
  1970. + *
  1971. + * ```
  1972. + * gboolean start_http_request (..., GError **error)
  1973. + * {
  1974. + * GLNX_AUTO_PREFIX_ERROR ("HTTP request", error)
  1975. + *
  1976. + * if (!libhttp_request_start (..., error))
  1977. + * return FALSE;
  1978. + * ...
  1979. + * return TRUE;
  1980. + * ```
  1981. + */
  1982. +typedef struct {
  1983. + const char *prefix;
  1984. + GError **error;
  1985. +} GLnxAutoErrorPrefix;
  1986. +static inline void
  1987. +glnx_cleanup_auto_prefix_error (GLnxAutoErrorPrefix *prefix)
  1988. +{
  1989. + if (prefix->error && *(prefix->error))
  1990. + g_prefix_error (prefix->error, "%s: ", prefix->prefix);
  1991. +}
  1992. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxAutoErrorPrefix, glnx_cleanup_auto_prefix_error)
  1993. +#define GLNX_AUTO_PREFIX_ERROR(text, error) \
  1994. + G_GNUC_UNUSED g_auto(GLnxAutoErrorPrefix) _GLNX_MAKE_ANONYMOUS(_glnxautoprefixerror_) = { text, error }
  1995. +
  1996. +/* Set @error using the value of `g_strerror (errno)`.
  1997. + *
  1998. + * This function returns %FALSE so it can be used conveniently in a single
  1999. + * statement:
  2000. + *
  2001. + * ```
  2002. + * if (unlinkat (fd, somepathname) < 0)
  2003. + * return glnx_throw_errno (error);
  2004. + * ```
  2005. + */
  2006. +static inline gboolean
  2007. +glnx_throw_errno (GError **error)
  2008. +{
  2009. + /* Save the value of errno, in case one of the
  2010. + * intermediate function calls happens to set it.
  2011. + */
  2012. + int errsv = errno;
  2013. + g_set_error_literal (error, G_IO_ERROR,
  2014. + g_io_error_from_errno (errsv),
  2015. + g_strerror (errsv));
  2016. + /* We also restore the value of errno, since that's
  2017. + * what was done in a long-ago libgsystem commit
  2018. + * https://git.gnome.org/browse/libgsystem/commit/?id=ed106741f7a0596dc8b960b31fdae671d31d666d
  2019. + * but I certainly can't remember now why I did that.
  2020. + */
  2021. + errno = errsv;
  2022. + return FALSE;
  2023. +}
  2024. +
  2025. +/* Like glnx_throw_errno(), but yields a NULL pointer. */
  2026. +#define glnx_null_throw_errno(error) \
  2027. + ({glnx_throw_errno (error); NULL;})
  2028. +
  2029. +/* Implementation detail of glnx_throw_errno_prefix() */
  2030. +void glnx_real_set_prefix_error_from_errno_va (GError **error,
  2031. + gint errsv,
  2032. + const char *format,
  2033. + va_list args) G_GNUC_PRINTF (3,0);
  2034. +
  2035. +/* Set @error using the value of `$prefix: g_strerror (errno)` where `$prefix`
  2036. + * is computed via printf @fmt.
  2037. + *
  2038. + * This function returns %FALSE so it can be used conveniently in a single
  2039. + * statement:
  2040. + *
  2041. + * ```
  2042. + * return glnx_throw_errno_prefix (error, "unlinking %s", pathname);
  2043. + * ```
  2044. + */
  2045. +static inline gboolean G_GNUC_PRINTF (2,3)
  2046. +glnx_throw_errno_prefix (GError **error, const char *fmt, ...)
  2047. +{
  2048. + int errsv = errno;
  2049. + va_list args;
  2050. + va_start (args, fmt);
  2051. + glnx_real_set_prefix_error_from_errno_va (error, errsv, fmt, args);
  2052. + va_end (args);
  2053. + /* See comment above about preserving errno */
  2054. + errno = errsv;
  2055. + return FALSE;
  2056. +}
  2057. +
  2058. +/* Like glnx_throw_errno_prefix(), but yields a NULL pointer. */
  2059. +#define glnx_null_throw_errno_prefix(error, args...) \
  2060. + ({glnx_throw_errno_prefix (error, args); NULL;})
  2061. +
  2062. +/* BEGIN LEGACY APIS */
  2063. +
  2064. +#define glnx_set_error_from_errno(error) \
  2065. + do { \
  2066. + glnx_throw_errno (error); \
  2067. + } while (0);
  2068. +
  2069. +#define glnx_set_prefix_error_from_errno(error, format, args...) \
  2070. + do { \
  2071. + glnx_throw_errno_prefix (error, format, args); \
  2072. + } while (0);
  2073. +
  2074. +G_END_DECLS
  2075. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-fdio.c flatpak-builder-0.10.6/libglnx/glnx-fdio.c
  2076. --- flatpak-builder-0.10.6.orig/libglnx/glnx-fdio.c 1970-01-01 02:00:00.000000000 +0200
  2077. +++ flatpak-builder-0.10.6/libglnx/glnx-fdio.c 2018-02-11 12:03:43.449373307 +0300
  2078. @@ -0,0 +1,1111 @@
  2079. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  2080. + *
  2081. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  2082. + *
  2083. + * Portions derived from systemd:
  2084. + * Copyright 2010 Lennart Poettering
  2085. + *
  2086. + * This library is free software; you can redistribute it and/or
  2087. + * modify it under the terms of the GNU Lesser General Public
  2088. + * License as published by the Free Software Foundation; either
  2089. + * version 2 of the License, or (at your option) any later version.
  2090. + *
  2091. + * This library is distributed in the hope that it will be useful,
  2092. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2093. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2094. + * Lesser General Public License for more details.
  2095. + *
  2096. + * You should have received a copy of the GNU Lesser General Public
  2097. + * License along with this library; if not, write to the
  2098. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  2099. + * Boston, MA 02111-1307, USA.
  2100. + */
  2101. +
  2102. +#include "config.h"
  2103. +
  2104. +#include <string.h>
  2105. +#include <stdio.h>
  2106. +#include <stdlib.h>
  2107. +#include <stdint.h>
  2108. +#include <stdbool.h>
  2109. +#include <sys/ioctl.h>
  2110. +#include <sys/sendfile.h>
  2111. +#include <errno.h>
  2112. +
  2113. +#include <glnx-fdio.h>
  2114. +#include <glnx-dirfd.h>
  2115. +#include <glnx-errors.h>
  2116. +#include <glnx-xattrs.h>
  2117. +#include <glnx-backport-autoptr.h>
  2118. +#include <glnx-local-alloc.h>
  2119. +#include <glnx-missing.h>
  2120. +
  2121. +/* The standardized version of BTRFS_IOC_CLONE */
  2122. +#ifndef FICLONE
  2123. +#define FICLONE _IOW(0x94, 9, int)
  2124. +#endif
  2125. +
  2126. +/* Returns the number of chars needed to format variables of the
  2127. + * specified type as a decimal string. Adds in extra space for a
  2128. + * negative '-' prefix (hence works correctly on signed
  2129. + * types). Includes space for the trailing NUL. */
  2130. +#define DECIMAL_STR_MAX(type) \
  2131. + (2+(sizeof(type) <= 1 ? 3 : \
  2132. + sizeof(type) <= 2 ? 5 : \
  2133. + sizeof(type) <= 4 ? 10 : \
  2134. + sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
  2135. +
  2136. +gboolean
  2137. +glnx_stdio_file_flush (FILE *f, GError **error)
  2138. +{
  2139. + if (fflush (f) != 0)
  2140. + return glnx_throw_errno_prefix (error, "fflush");
  2141. + if (ferror (f) != 0)
  2142. + return glnx_throw_errno_prefix (error, "ferror");
  2143. + return TRUE;
  2144. +}
  2145. +
  2146. +/* An implementation of renameat2(..., RENAME_NOREPLACE)
  2147. + * with fallback to a non-atomic version.
  2148. + */
  2149. +int
  2150. +glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
  2151. + int newdirfd, const char *newpath)
  2152. +{
  2153. +#ifndef ENABLE_WRPSEUDO_COMPAT
  2154. + if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE) < 0)
  2155. + {
  2156. + if (G_IN_SET(errno, EINVAL, ENOSYS))
  2157. + {
  2158. + /* Fall through */
  2159. + }
  2160. + else
  2161. + {
  2162. + return -1;
  2163. + }
  2164. + }
  2165. + else
  2166. + return TRUE;
  2167. +#endif
  2168. +
  2169. + if (linkat (olddirfd, oldpath, newdirfd, newpath, 0) < 0)
  2170. + return -1;
  2171. +
  2172. + if (unlinkat (olddirfd, oldpath, 0) < 0)
  2173. + return -1;
  2174. +
  2175. + return 0;
  2176. +}
  2177. +
  2178. +static gboolean
  2179. +rename_file_noreplace_at (int olddirfd, const char *oldpath,
  2180. + int newdirfd, const char *newpath,
  2181. + gboolean ignore_eexist,
  2182. + GError **error)
  2183. +{
  2184. + if (glnx_renameat2_noreplace (olddirfd, oldpath,
  2185. + newdirfd, newpath) < 0)
  2186. + {
  2187. + if (errno == EEXIST && ignore_eexist)
  2188. + {
  2189. + (void) unlinkat (olddirfd, oldpath, 0);
  2190. + return TRUE;
  2191. + }
  2192. + else
  2193. + return glnx_throw_errno (error);
  2194. + }
  2195. + return TRUE;
  2196. +}
  2197. +
  2198. +/* An implementation of renameat2(..., RENAME_EXCHANGE)
  2199. + * with fallback to a non-atomic version.
  2200. + */
  2201. +int
  2202. +glnx_renameat2_exchange (int olddirfd, const char *oldpath,
  2203. + int newdirfd, const char *newpath)
  2204. +{
  2205. +#ifndef ENABLE_WRPSEUDO_COMPAT
  2206. + if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_EXCHANGE) == 0)
  2207. + return 0;
  2208. + else
  2209. + {
  2210. + if (G_IN_SET(errno, ENOSYS, EINVAL))
  2211. + {
  2212. + /* Fall through */
  2213. + }
  2214. + else
  2215. + {
  2216. + return -1;
  2217. + }
  2218. + }
  2219. +#endif
  2220. +
  2221. + /* Fallback */
  2222. + { const char *old_tmp_name = glnx_strjoina (oldpath, ".XXXXXX");
  2223. +
  2224. + /* Move old out of the way */
  2225. + if (renameat (olddirfd, oldpath, olddirfd, old_tmp_name) < 0)
  2226. + return -1;
  2227. + /* Now move new into its place */
  2228. + if (renameat (newdirfd, newpath, olddirfd, oldpath) < 0)
  2229. + return -1;
  2230. + /* And finally old(tmp) into new */
  2231. + if (renameat (olddirfd, old_tmp_name, newdirfd, newpath) < 0)
  2232. + return -1;
  2233. + }
  2234. + return 0;
  2235. +}
  2236. +
  2237. +/* Deallocate a tmpfile, closing the fd and deleting the path, if any. This is
  2238. + * normally called by default by the autocleanup attribute, but you can also
  2239. + * invoke this directly.
  2240. + */
  2241. +void
  2242. +glnx_tmpfile_clear (GLnxTmpfile *tmpf)
  2243. +{
  2244. + /* Support being passed NULL so we work nicely in a GPtrArray */
  2245. + if (!tmpf)
  2246. + return;
  2247. + if (!tmpf->initialized)
  2248. + return;
  2249. + if (tmpf->fd == -1)
  2250. + return;
  2251. + (void) close (tmpf->fd);
  2252. + /* If ->path is set, we're likely aborting due to an error. Clean it up */
  2253. + if (tmpf->path)
  2254. + {
  2255. + (void) unlinkat (tmpf->src_dfd, tmpf->path, 0);
  2256. + g_free (tmpf->path);
  2257. + }
  2258. + tmpf->initialized = FALSE;
  2259. +}
  2260. +
  2261. +/* Allocate a temporary file, using Linux O_TMPFILE if available. The file mode
  2262. + * will be 0600.
  2263. + *
  2264. + * The result will be stored in @out_tmpf, which is caller allocated
  2265. + * so you can store it on the stack in common scenarios.
  2266. + *
  2267. + * The directory fd @dfd must live at least as long as the output @out_tmpf.
  2268. + */
  2269. +gboolean
  2270. +glnx_open_tmpfile_linkable_at (int dfd,
  2271. + const char *subpath,
  2272. + int flags,
  2273. + GLnxTmpfile *out_tmpf,
  2274. + GError **error)
  2275. +{
  2276. + const guint mode = 0600;
  2277. + glnx_fd_close int fd = -1;
  2278. + int count;
  2279. +
  2280. + dfd = glnx_dirfd_canonicalize (dfd);
  2281. +
  2282. + /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
  2283. + g_return_val_if_fail ((flags & O_EXCL) == 0, FALSE);
  2284. +
  2285. + /* Creates a temporary file, that shall be renamed to "target"
  2286. + * later. If possible, this uses O_TMPFILE – in which case
  2287. + * "ret_path" will be returned as NULL. If not possible a the
  2288. + * tempoary path name used is returned in "ret_path". Use
  2289. + * link_tmpfile() below to rename the result after writing the file
  2290. + * in full. */
  2291. +#if defined(O_TMPFILE) && !defined(DISABLE_OTMPFILE) && !defined(ENABLE_WRPSEUDO_COMPAT)
  2292. + fd = openat (dfd, subpath, O_TMPFILE|flags, mode);
  2293. + if (fd == -1 && !(G_IN_SET(errno, ENOSYS, EISDIR, EOPNOTSUPP)))
  2294. + return glnx_throw_errno_prefix (error, "open(O_TMPFILE)");
  2295. + if (fd != -1)
  2296. + {
  2297. + /* Workaround for https://sourceware.org/bugzilla/show_bug.cgi?id=17523
  2298. + * See also https://github.com/ostreedev/ostree/issues/991
  2299. + */
  2300. + if (fchmod (fd, mode) < 0)
  2301. + return glnx_throw_errno_prefix (error, "fchmod");
  2302. + out_tmpf->initialized = TRUE;
  2303. + out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
  2304. + out_tmpf->fd = glnx_steal_fd (&fd);
  2305. + out_tmpf->path = NULL;
  2306. + return TRUE;
  2307. + }
  2308. + /* Fallthrough */
  2309. +#endif
  2310. +
  2311. + { g_autofree char *tmp = g_strconcat (subpath, "/tmp.XXXXXX", NULL);
  2312. + const guint count_max = 100;
  2313. +
  2314. + for (count = 0; count < count_max; count++)
  2315. + {
  2316. + glnx_gen_temp_name (tmp);
  2317. +
  2318. + fd = openat (dfd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, mode);
  2319. + if (fd < 0)
  2320. + {
  2321. + if (errno == EEXIST)
  2322. + continue;
  2323. + else
  2324. + return glnx_throw_errno_prefix (error, "Creating temp file");
  2325. + }
  2326. + else
  2327. + {
  2328. + out_tmpf->initialized = TRUE;
  2329. + out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
  2330. + out_tmpf->fd = glnx_steal_fd (&fd);
  2331. + out_tmpf->path = g_steal_pointer (&tmp);
  2332. + return TRUE;
  2333. + }
  2334. + }
  2335. + }
  2336. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  2337. + "Exhausted %u attempts to create temporary file", count);
  2338. + return FALSE;
  2339. +}
  2340. +
  2341. +/* A variant of `glnx_open_tmpfile_linkable_at()` which doesn't support linking.
  2342. + * Useful for true temporary storage. The fd will be allocated in /var/tmp to
  2343. + * ensure maximum storage space.
  2344. + */
  2345. +gboolean
  2346. +glnx_open_anonymous_tmpfile (int flags,
  2347. + GLnxTmpfile *out_tmpf,
  2348. + GError **error)
  2349. +{
  2350. + if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, "/var/tmp", flags, out_tmpf, error))
  2351. + return FALSE;
  2352. + if (out_tmpf->path)
  2353. + {
  2354. + (void) unlinkat (out_tmpf->src_dfd, out_tmpf->path, 0);
  2355. + g_clear_pointer (&out_tmpf->path, g_free);
  2356. + }
  2357. + out_tmpf->anonymous = TRUE;
  2358. + out_tmpf->src_dfd = -1;
  2359. + return TRUE;
  2360. +}
  2361. +
  2362. +/* Use this after calling glnx_open_tmpfile_linkable_at() to give
  2363. + * the file its final name (link into place).
  2364. + */
  2365. +gboolean
  2366. +glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
  2367. + GLnxLinkTmpfileReplaceMode mode,
  2368. + int target_dfd,
  2369. + const char *target,
  2370. + GError **error)
  2371. +{
  2372. + const gboolean replace = (mode == GLNX_LINK_TMPFILE_REPLACE);
  2373. + const gboolean ignore_eexist = (mode == GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST);
  2374. +
  2375. + g_return_val_if_fail (!tmpf->anonymous, FALSE);
  2376. + g_return_val_if_fail (tmpf->fd >= 0, FALSE);
  2377. + g_return_val_if_fail (tmpf->src_dfd == AT_FDCWD || tmpf->src_dfd >= 0, FALSE);
  2378. +
  2379. + /* Unlike the original systemd code, this function also supports
  2380. + * replacing existing files.
  2381. + */
  2382. +
  2383. + /* We have `tmpfile_path` for old systems without O_TMPFILE. */
  2384. + if (tmpf->path)
  2385. + {
  2386. + if (replace)
  2387. + {
  2388. + /* We have a regular tempfile, we're overwriting - this is a
  2389. + * simple renameat().
  2390. + */
  2391. + if (renameat (tmpf->src_dfd, tmpf->path, target_dfd, target) < 0)
  2392. + return glnx_throw_errno_prefix (error, "renameat");
  2393. + }
  2394. + else
  2395. + {
  2396. + /* We need to use renameat2(..., NOREPLACE) or emulate it */
  2397. + if (!rename_file_noreplace_at (tmpf->src_dfd, tmpf->path, target_dfd, target,
  2398. + ignore_eexist,
  2399. + error))
  2400. + return FALSE;
  2401. + }
  2402. + /* Now, clear the pointer so we don't try to unlink it */
  2403. + g_clear_pointer (&tmpf->path, g_free);
  2404. + }
  2405. + else
  2406. + {
  2407. + /* This case we have O_TMPFILE, so our reference to it is via /proc/self/fd */
  2408. + char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(tmpf->fd) + 1];
  2409. +
  2410. + sprintf (proc_fd_path, "/proc/self/fd/%i", tmpf->fd);
  2411. +
  2412. + if (replace)
  2413. + {
  2414. + /* In this case, we had our temp file atomically hidden, but now
  2415. + * we need to make it visible in the FS so we can do a rename.
  2416. + * Ideally, linkat() would gain AT_REPLACE or so.
  2417. + */
  2418. + /* TODO - avoid double alloca, we can just alloca a copy of
  2419. + * the pathname plus space for tmp.XXXXX */
  2420. + char *dnbuf = strdupa (target);
  2421. + const char *dn = dirname (dnbuf);
  2422. + char *tmpname_buf = glnx_strjoina (dn, "/tmp.XXXXXX");
  2423. + guint count;
  2424. + const guint count_max = 100;
  2425. +
  2426. + for (count = 0; count < count_max; count++)
  2427. + {
  2428. + glnx_gen_temp_name (tmpname_buf);
  2429. +
  2430. + if (linkat (AT_FDCWD, proc_fd_path, target_dfd, tmpname_buf, AT_SYMLINK_FOLLOW) < 0)
  2431. + {
  2432. + if (errno == EEXIST)
  2433. + continue;
  2434. + else
  2435. + return glnx_throw_errno_prefix (error, "linkat");
  2436. + }
  2437. + else
  2438. + break;
  2439. + }
  2440. + if (count == count_max)
  2441. + {
  2442. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  2443. + "Exhausted %u attempts to create temporary file", count);
  2444. + return FALSE;
  2445. + }
  2446. + if (!glnx_renameat (target_dfd, tmpname_buf, target_dfd, target, error))
  2447. + {
  2448. + /* This is currently the only case where we need to have
  2449. + * a cleanup unlinkat() still with O_TMPFILE.
  2450. + */
  2451. + (void) unlinkat (target_dfd, tmpname_buf, 0);
  2452. + return FALSE;
  2453. + }
  2454. + }
  2455. + else
  2456. + {
  2457. + if (linkat (AT_FDCWD, proc_fd_path, target_dfd, target, AT_SYMLINK_FOLLOW) < 0)
  2458. + {
  2459. + if (errno == EEXIST && mode == GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST)
  2460. + ;
  2461. + else
  2462. + return glnx_throw_errno_prefix (error, "linkat");
  2463. + }
  2464. + }
  2465. +
  2466. + }
  2467. + return TRUE;
  2468. +}
  2469. +
  2470. +/**
  2471. + * glnx_openat_rdonly:
  2472. + * @dfd: File descriptor for origin directory
  2473. + * @path: Pathname, relative to @dfd
  2474. + * @follow: Whether or not to follow symbolic links in the final component
  2475. + * @out_fd: (out): File descriptor
  2476. + * @error: Error
  2477. + *
  2478. + * Use openat() to open a file, with flags `O_RDONLY | O_CLOEXEC | O_NOCTTY`.
  2479. + * Like the other libglnx wrappers, will use `TEMP_FAILURE_RETRY` and
  2480. + * also includes @path in @error in case of failure.
  2481. + */
  2482. +gboolean
  2483. +glnx_openat_rdonly (int dfd,
  2484. + const char *path,
  2485. + gboolean follow,
  2486. + int *out_fd,
  2487. + GError **error)
  2488. +{
  2489. + int flags = O_RDONLY | O_CLOEXEC | O_NOCTTY;
  2490. + if (!follow)
  2491. + flags |= O_NOFOLLOW;
  2492. + int fd = TEMP_FAILURE_RETRY (openat (dfd, path, flags));
  2493. + if (fd == -1)
  2494. + return glnx_throw_errno_prefix (error, "openat(%s)", path);
  2495. + *out_fd = fd;
  2496. + return TRUE;
  2497. +}
  2498. +
  2499. +static guint8*
  2500. +glnx_fd_readall_malloc (int fd,
  2501. + gsize *out_len,
  2502. + gboolean nul_terminate,
  2503. + GCancellable *cancellable,
  2504. + GError **error)
  2505. +{
  2506. + const guint maxreadlen = 4096;
  2507. +
  2508. + struct stat stbuf;
  2509. + if (TEMP_FAILURE_RETRY (fstat (fd, &stbuf)) < 0)
  2510. + return glnx_null_throw_errno (error);
  2511. +
  2512. + gsize buf_allocated;
  2513. + if (S_ISREG (stbuf.st_mode) && stbuf.st_size > 0)
  2514. + buf_allocated = stbuf.st_size;
  2515. + else
  2516. + buf_allocated = 16;
  2517. +
  2518. + g_autofree guint8* buf = g_malloc (buf_allocated);
  2519. +
  2520. + gsize buf_size = 0;
  2521. + while (TRUE)
  2522. + {
  2523. + gsize readlen = MIN (buf_allocated - buf_size, maxreadlen);
  2524. +
  2525. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  2526. + return FALSE;
  2527. +
  2528. + gssize bytes_read;
  2529. + do
  2530. + bytes_read = read (fd, buf + buf_size, readlen);
  2531. + while (G_UNLIKELY (bytes_read == -1 && errno == EINTR));
  2532. + if (G_UNLIKELY (bytes_read == -1))
  2533. + return glnx_null_throw_errno (error);
  2534. + if (bytes_read == 0)
  2535. + break;
  2536. +
  2537. + buf_size += bytes_read;
  2538. + if (buf_allocated - buf_size < maxreadlen)
  2539. + buf = g_realloc (buf, buf_allocated *= 2);
  2540. + }
  2541. +
  2542. + if (nul_terminate)
  2543. + {
  2544. + if (buf_allocated - buf_size == 0)
  2545. + buf = g_realloc (buf, buf_allocated + 1);
  2546. + buf[buf_size] = '\0';
  2547. + }
  2548. +
  2549. + *out_len = buf_size;
  2550. + return g_steal_pointer (&buf);
  2551. +}
  2552. +
  2553. +/**
  2554. + * glnx_fd_readall_bytes:
  2555. + * @fd: A file descriptor
  2556. + * @cancellable: Cancellable:
  2557. + * @error: Error
  2558. + *
  2559. + * Read all data from file descriptor @fd into a #GBytes. It's
  2560. + * recommended to only use this for small files.
  2561. + *
  2562. + * Returns: (transfer full): A newly allocated #GBytes
  2563. + */
  2564. +GBytes *
  2565. +glnx_fd_readall_bytes (int fd,
  2566. + GCancellable *cancellable,
  2567. + GError **error)
  2568. +{
  2569. + gsize len;
  2570. + guint8 *buf = glnx_fd_readall_malloc (fd, &len, FALSE, cancellable, error);
  2571. + if (!buf)
  2572. + return NULL;
  2573. + return g_bytes_new_take (buf, len);
  2574. +}
  2575. +
  2576. +/**
  2577. + * glnx_fd_readall_utf8:
  2578. + * @fd: A file descriptor
  2579. + * @out_len: (out): Returned length
  2580. + * @cancellable: Cancellable:
  2581. + * @error: Error
  2582. + *
  2583. + * Read all data from file descriptor @fd, validating
  2584. + * the result as UTF-8.
  2585. + *
  2586. + * Returns: (transfer full): A string validated as UTF-8, or %NULL on error.
  2587. + */
  2588. +char *
  2589. +glnx_fd_readall_utf8 (int fd,
  2590. + gsize *out_len,
  2591. + GCancellable *cancellable,
  2592. + GError **error)
  2593. +{
  2594. + gsize len;
  2595. + g_autofree guint8 *buf = glnx_fd_readall_malloc (fd, &len, TRUE, cancellable, error);
  2596. + if (!buf)
  2597. + return FALSE;
  2598. +
  2599. + if (!g_utf8_validate ((char*)buf, len, NULL))
  2600. + {
  2601. + g_set_error (error,
  2602. + G_IO_ERROR,
  2603. + G_IO_ERROR_INVALID_DATA,
  2604. + "Invalid UTF-8");
  2605. + return FALSE;
  2606. + }
  2607. +
  2608. + if (out_len)
  2609. + *out_len = len;
  2610. + return (char*)g_steal_pointer (&buf);
  2611. +}
  2612. +
  2613. +/**
  2614. + * glnx_file_get_contents_utf8_at:
  2615. + * @dfd: Directory file descriptor
  2616. + * @subpath: Path relative to @dfd
  2617. + * @out_len: (out) (allow-none): Optional length
  2618. + * @cancellable: Cancellable
  2619. + * @error: Error
  2620. + *
  2621. + * Read the entire contents of the file referred
  2622. + * to by @dfd and @subpath, validate the result as UTF-8.
  2623. + * The length is optionally stored in @out_len.
  2624. + *
  2625. + * Returns: (transfer full): UTF-8 validated text, or %NULL on error
  2626. + */
  2627. +char *
  2628. +glnx_file_get_contents_utf8_at (int dfd,
  2629. + const char *subpath,
  2630. + gsize *out_len,
  2631. + GCancellable *cancellable,
  2632. + GError **error)
  2633. +{
  2634. + dfd = glnx_dirfd_canonicalize (dfd);
  2635. +
  2636. + glnx_fd_close int fd = -1;
  2637. + if (!glnx_openat_rdonly (dfd, subpath, TRUE, &fd, error))
  2638. + return NULL;
  2639. +
  2640. + gsize len;
  2641. + g_autofree char *buf = glnx_fd_readall_utf8 (fd, &len, cancellable, error);
  2642. + if (G_UNLIKELY(!buf))
  2643. + return FALSE;
  2644. +
  2645. + if (out_len)
  2646. + *out_len = len;
  2647. + return g_steal_pointer (&buf);
  2648. +}
  2649. +
  2650. +/**
  2651. + * glnx_readlinkat_malloc:
  2652. + * @dfd: Directory file descriptor
  2653. + * @subpath: Subpath
  2654. + * @cancellable: Cancellable
  2655. + * @error: Error
  2656. + *
  2657. + * Read the value of a symlink into a dynamically
  2658. + * allocated buffer.
  2659. + */
  2660. +char *
  2661. +glnx_readlinkat_malloc (int dfd,
  2662. + const char *subpath,
  2663. + GCancellable *cancellable,
  2664. + GError **error)
  2665. +{
  2666. + size_t l = 100;
  2667. +
  2668. + dfd = glnx_dirfd_canonicalize (dfd);
  2669. +
  2670. + for (;;)
  2671. + {
  2672. + g_autofree char *c = NULL;
  2673. + ssize_t n;
  2674. +
  2675. + c = g_malloc (l);
  2676. + n = TEMP_FAILURE_RETRY (readlinkat (dfd, subpath, c, l-1));
  2677. + if (n < 0)
  2678. + return glnx_null_throw_errno (error);
  2679. +
  2680. + if ((size_t) n < l-1)
  2681. + {
  2682. + c[n] = 0;
  2683. + return g_steal_pointer (&c);
  2684. + }
  2685. +
  2686. + l *= 2;
  2687. + }
  2688. +
  2689. + g_assert_not_reached ();
  2690. +}
  2691. +
  2692. +static gboolean
  2693. +copy_symlink_at (int src_dfd,
  2694. + const char *src_subpath,
  2695. + const struct stat *src_stbuf,
  2696. + int dest_dfd,
  2697. + const char *dest_subpath,
  2698. + GLnxFileCopyFlags copyflags,
  2699. + GCancellable *cancellable,
  2700. + GError **error)
  2701. +{
  2702. + g_autofree char *buf = glnx_readlinkat_malloc (src_dfd, src_subpath, cancellable, error);
  2703. + if (!buf)
  2704. + return FALSE;
  2705. +
  2706. + if (TEMP_FAILURE_RETRY (symlinkat (buf, dest_dfd, dest_subpath)) != 0)
  2707. + return glnx_throw_errno_prefix (error, "symlinkat");
  2708. +
  2709. + if (!(copyflags & GLNX_FILE_COPY_NOXATTRS))
  2710. + {
  2711. + g_autoptr(GVariant) xattrs = NULL;
  2712. +
  2713. + if (!glnx_dfd_name_get_all_xattrs (src_dfd, src_subpath, &xattrs,
  2714. + cancellable, error))
  2715. + return FALSE;
  2716. +
  2717. + if (!glnx_dfd_name_set_all_xattrs (dest_dfd, dest_subpath, xattrs,
  2718. + cancellable, error))
  2719. + return FALSE;
  2720. + }
  2721. +
  2722. + if (TEMP_FAILURE_RETRY (fchownat (dest_dfd, dest_subpath,
  2723. + src_stbuf->st_uid, src_stbuf->st_gid,
  2724. + AT_SYMLINK_NOFOLLOW)) != 0)
  2725. + return glnx_throw_errno (error);
  2726. +
  2727. + return TRUE;
  2728. +}
  2729. +
  2730. +#define COPY_BUFFER_SIZE (16*1024)
  2731. +
  2732. +/* Most of the code below is from systemd, but has been reindented to GNU style,
  2733. + * and changed to use POSIX error conventions (return -1, set errno) to more
  2734. + * conveniently fit in with the rest of libglnx.
  2735. + */
  2736. +
  2737. +/* Like write(), but loop until @nbytes are written, or an error
  2738. + * occurs.
  2739. + *
  2740. + * On error, -1 is returned an @errno is set. NOTE: This is an
  2741. + * API change from previous versions of this function.
  2742. + */
  2743. +int
  2744. +glnx_loop_write(int fd, const void *buf, size_t nbytes)
  2745. +{
  2746. + const uint8_t *p = buf;
  2747. +
  2748. + g_return_val_if_fail(fd >= 0, -1);
  2749. + g_return_val_if_fail(buf, -1);
  2750. +
  2751. + errno = 0;
  2752. +
  2753. + while (nbytes > 0)
  2754. + {
  2755. + ssize_t k;
  2756. +
  2757. + k = write(fd, p, nbytes);
  2758. + if (k < 0)
  2759. + {
  2760. + if (errno == EINTR)
  2761. + continue;
  2762. +
  2763. + return -1;
  2764. + }
  2765. +
  2766. + if (k == 0) /* Can't really happen */
  2767. + {
  2768. + errno = EIO;
  2769. + return -1;
  2770. + }
  2771. +
  2772. + p += k;
  2773. + nbytes -= k;
  2774. + }
  2775. +
  2776. + return 0;
  2777. +}
  2778. +
  2779. +/* Read from @fdf until EOF, writing to @fdt. If max_bytes is -1, a full-file
  2780. + * clone will be attempted. Otherwise Linux copy_file_range(), sendfile()
  2781. + * syscall will be attempted. If none of those work, this function will do a
  2782. + * plain read()/write() loop.
  2783. + *
  2784. + * The file descriptor @fdf must refer to a regular file.
  2785. + *
  2786. + * If provided, @max_bytes specifies the maximum number of bytes to read from @fdf.
  2787. + * On error, this function returns `-1` and @errno will be set.
  2788. + */
  2789. +int
  2790. +glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes)
  2791. +{
  2792. + /* Last updates from systemd as of commit 6bda23dd6aaba50cf8e3e6024248cf736cc443ca */
  2793. + static int have_cfr = -1; /* -1 means unknown */
  2794. + bool try_cfr = have_cfr != 0;
  2795. + static int have_sendfile = -1; /* -1 means unknown */
  2796. + bool try_sendfile = have_sendfile != 0;
  2797. +
  2798. + g_return_val_if_fail (fdf >= 0, -1);
  2799. + g_return_val_if_fail (fdt >= 0, -1);
  2800. + g_return_val_if_fail (max_bytes >= -1, -1);
  2801. +
  2802. + /* If we've requested to copy the whole range, try a full-file clone first.
  2803. + */
  2804. + if (max_bytes == (off_t) -1)
  2805. + {
  2806. + if (ioctl (fdt, FICLONE, fdf) == 0)
  2807. + return 0;
  2808. + /* Fall through */
  2809. + struct stat stbuf;
  2810. +
  2811. + /* Gather the size so we can provide the whole thing at once to
  2812. + * copy_file_range() or sendfile().
  2813. + */
  2814. + if (fstat (fdf, &stbuf) < 0)
  2815. + return -1;
  2816. + max_bytes = stbuf.st_size;
  2817. + }
  2818. +
  2819. + while (TRUE)
  2820. + {
  2821. + ssize_t n;
  2822. +
  2823. + /* First, try copy_file_range(). Note this is an inlined version of
  2824. + * try_copy_file_range() from systemd upstream, which works better since
  2825. + * we use POSIX errno style.
  2826. + */
  2827. + if (try_cfr)
  2828. + {
  2829. + n = copy_file_range (fdf, NULL, fdt, NULL, max_bytes, 0u);
  2830. + if (n < 0)
  2831. + {
  2832. + if (errno == ENOSYS)
  2833. + {
  2834. + /* No cfr in kernel, mark as permanently unavailable
  2835. + * and fall through to sendfile().
  2836. + */
  2837. + have_cfr = 0;
  2838. + try_cfr = false;
  2839. + }
  2840. + else if (errno == EXDEV)
  2841. + /* We won't try cfr again for this run, but let's be
  2842. + * conservative and not mark it as available/unavailable until
  2843. + * we know for sure.
  2844. + */
  2845. + try_cfr = false;
  2846. + else
  2847. + return -1;
  2848. + }
  2849. + else
  2850. + {
  2851. + /* cfr worked, mark it as available */
  2852. + if (have_cfr == -1)
  2853. + have_cfr = 1;
  2854. +
  2855. + if (n == 0) /* EOF */
  2856. + break;
  2857. + else
  2858. + /* Success! */
  2859. + goto next;
  2860. + }
  2861. + }
  2862. +
  2863. + /* Next try sendfile(); this version is also changed from systemd upstream
  2864. + * to match the same logic we have for copy_file_range().
  2865. + */
  2866. + if (try_sendfile)
  2867. + {
  2868. + n = sendfile (fdt, fdf, NULL, max_bytes);
  2869. + if (n < 0)
  2870. + {
  2871. + if (G_IN_SET (errno, EINVAL, ENOSYS))
  2872. + {
  2873. + /* No sendfile(), or it doesn't work on regular files.
  2874. + * Mark it as permanently unavailable, and fall through
  2875. + * to plain read()/write().
  2876. + */
  2877. + have_sendfile = 0;
  2878. + try_sendfile = false;
  2879. + }
  2880. + else
  2881. + return -1;
  2882. + }
  2883. + else
  2884. + {
  2885. + /* sendfile() worked, mark it as available */
  2886. + if (have_sendfile == -1)
  2887. + have_sendfile = 1;
  2888. +
  2889. + if (n == 0) /* EOF */
  2890. + break;
  2891. + else if (n > 0)
  2892. + /* Succcess! */
  2893. + goto next;
  2894. + }
  2895. + }
  2896. +
  2897. + /* As a fallback just copy bits by hand */
  2898. + { size_t m = COPY_BUFFER_SIZE;
  2899. + if (max_bytes != (off_t) -1)
  2900. + {
  2901. + if ((off_t) m > max_bytes)
  2902. + m = (size_t) max_bytes;
  2903. + }
  2904. + char buf[m];
  2905. +
  2906. + n = TEMP_FAILURE_RETRY (read (fdf, buf, m));
  2907. + if (n < 0)
  2908. + return -1;
  2909. + if (n == 0) /* EOF */
  2910. + break;
  2911. +
  2912. + if (glnx_loop_write (fdt, buf, (size_t) n) < 0)
  2913. + return -1;
  2914. + }
  2915. +
  2916. + next:
  2917. + if (max_bytes != (off_t) -1)
  2918. + {
  2919. + g_assert_cmpint (max_bytes, >=, n);
  2920. + max_bytes -= n;
  2921. + if (max_bytes == 0)
  2922. + break;
  2923. + }
  2924. + }
  2925. +
  2926. + return 0;
  2927. +}
  2928. +
  2929. +/**
  2930. + * glnx_file_copy_at:
  2931. + * @src_dfd: Source directory fd
  2932. + * @src_subpath: Subpath relative to @src_dfd
  2933. + * @dest_dfd: Target directory fd
  2934. + * @dest_subpath: Destination name
  2935. + * @copyflags: Flags
  2936. + * @cancellable: cancellable
  2937. + * @error: Error
  2938. + *
  2939. + * Perform a full copy of the regular file or
  2940. + * symbolic link from @src_subpath to @dest_subpath.
  2941. + *
  2942. + * If @src_subpath is anything other than a regular
  2943. + * file or symbolic link, an error will be returned.
  2944. + */
  2945. +gboolean
  2946. +glnx_file_copy_at (int src_dfd,
  2947. + const char *src_subpath,
  2948. + struct stat *src_stbuf,
  2949. + int dest_dfd,
  2950. + const char *dest_subpath,
  2951. + GLnxFileCopyFlags copyflags,
  2952. + GCancellable *cancellable,
  2953. + GError **error)
  2954. +{
  2955. + gboolean ret = FALSE;
  2956. + int r;
  2957. + int dest_open_flags;
  2958. + struct timespec ts[2];
  2959. + glnx_fd_close int src_fd = -1;
  2960. + glnx_fd_close int dest_fd = -1;
  2961. + struct stat local_stbuf;
  2962. +
  2963. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  2964. + goto out;
  2965. +
  2966. + src_dfd = glnx_dirfd_canonicalize (src_dfd);
  2967. + dest_dfd = glnx_dirfd_canonicalize (dest_dfd);
  2968. +
  2969. + /* Automatically do stat() if no stat buffer was supplied */
  2970. + if (!src_stbuf)
  2971. + {
  2972. + if (fstatat (src_dfd, src_subpath, &local_stbuf, AT_SYMLINK_NOFOLLOW) != 0)
  2973. + {
  2974. + glnx_set_error_from_errno (error);
  2975. + goto out;
  2976. + }
  2977. + src_stbuf = &local_stbuf;
  2978. + }
  2979. +
  2980. + if (S_ISLNK (src_stbuf->st_mode))
  2981. + {
  2982. + return copy_symlink_at (src_dfd, src_subpath, src_stbuf,
  2983. + dest_dfd, dest_subpath,
  2984. + copyflags,
  2985. + cancellable, error);
  2986. + }
  2987. + else if (!S_ISREG (src_stbuf->st_mode))
  2988. + {
  2989. + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  2990. + "Cannot copy non-regular/non-symlink file: %s", src_subpath);
  2991. + goto out;
  2992. + }
  2993. +
  2994. + if (!glnx_openat_rdonly (src_dfd, src_subpath, FALSE, &src_fd, error))
  2995. + goto out;
  2996. +
  2997. + dest_open_flags = O_WRONLY | O_CREAT | O_CLOEXEC | O_NOCTTY;
  2998. + if (!(copyflags & GLNX_FILE_COPY_OVERWRITE))
  2999. + dest_open_flags |= O_EXCL;
  3000. + else
  3001. + dest_open_flags |= O_TRUNC;
  3002. +
  3003. + dest_fd = TEMP_FAILURE_RETRY (openat (dest_dfd, dest_subpath, dest_open_flags, src_stbuf->st_mode));
  3004. + if (dest_fd == -1)
  3005. + {
  3006. + glnx_set_error_from_errno (error);
  3007. + goto out;
  3008. + }
  3009. +
  3010. + r = glnx_regfile_copy_bytes (src_fd, dest_fd, (off_t) -1);
  3011. + if (r < 0)
  3012. + {
  3013. + glnx_set_error_from_errno (error);
  3014. + goto out;
  3015. + }
  3016. +
  3017. + if (fchown (dest_fd, src_stbuf->st_uid, src_stbuf->st_gid) != 0)
  3018. + {
  3019. + glnx_set_error_from_errno (error);
  3020. + goto out;
  3021. + }
  3022. +
  3023. + if (fchmod (dest_fd, src_stbuf->st_mode & 07777) != 0)
  3024. + {
  3025. + glnx_set_error_from_errno (error);
  3026. + goto out;
  3027. + }
  3028. +
  3029. + ts[0] = src_stbuf->st_atim;
  3030. + ts[1] = src_stbuf->st_mtim;
  3031. + (void) futimens (dest_fd, ts);
  3032. +
  3033. + if (!(copyflags & GLNX_FILE_COPY_NOXATTRS))
  3034. + {
  3035. + g_autoptr(GVariant) xattrs = NULL;
  3036. +
  3037. + if (!glnx_fd_get_all_xattrs (src_fd, &xattrs,
  3038. + cancellable, error))
  3039. + goto out;
  3040. +
  3041. + if (!glnx_fd_set_all_xattrs (dest_fd, xattrs,
  3042. + cancellable, error))
  3043. + goto out;
  3044. + }
  3045. +
  3046. + if (copyflags & GLNX_FILE_COPY_DATASYNC)
  3047. + {
  3048. + if (fdatasync (dest_fd) < 0)
  3049. + {
  3050. + glnx_set_error_from_errno (error);
  3051. + goto out;
  3052. + }
  3053. + }
  3054. +
  3055. + r = close (dest_fd);
  3056. + dest_fd = -1;
  3057. + if (r < 0)
  3058. + {
  3059. + glnx_set_error_from_errno (error);
  3060. + goto out;
  3061. + }
  3062. +
  3063. + ret = TRUE;
  3064. + out:
  3065. + if (!ret)
  3066. + (void) unlinkat (dest_dfd, dest_subpath, 0);
  3067. + return ret;
  3068. +}
  3069. +
  3070. +/**
  3071. + * glnx_file_replace_contents_at:
  3072. + * @dfd: Directory fd
  3073. + * @subpath: Subpath
  3074. + * @buf: (array len=len) (element-type guint8): File contents
  3075. + * @len: Length (if `-1`, assume @buf is `NUL` terminated)
  3076. + * @flags: Flags
  3077. + * @cancellable: Cancellable
  3078. + * @error: Error
  3079. + *
  3080. + * Create a new file, atomically replacing the contents of @subpath
  3081. + * (relative to @dfd) with @buf. By default, if the file already
  3082. + * existed, fdatasync() will be used before rename() to ensure stable
  3083. + * contents. This and other behavior can be controlled via @flags.
  3084. + *
  3085. + * Note that no metadata from the existing file is preserved, such as
  3086. + * uid/gid or extended attributes. The default mode will be `0666`,
  3087. + * modified by umask.
  3088. + */
  3089. +gboolean
  3090. +glnx_file_replace_contents_at (int dfd,
  3091. + const char *subpath,
  3092. + const guint8 *buf,
  3093. + gsize len,
  3094. + GLnxFileReplaceFlags flags,
  3095. + GCancellable *cancellable,
  3096. + GError **error)
  3097. +{
  3098. + return glnx_file_replace_contents_with_perms_at (dfd, subpath, buf, len,
  3099. + (mode_t) -1, (uid_t) -1, (gid_t) -1,
  3100. + flags, cancellable, error);
  3101. +}
  3102. +
  3103. +/**
  3104. + * glnx_file_replace_contents_with_perms_at:
  3105. + * @dfd: Directory fd
  3106. + * @subpath: Subpath
  3107. + * @buf: (array len=len) (element-type guint8): File contents
  3108. + * @len: Length (if `-1`, assume @buf is `NUL` terminated)
  3109. + * @mode: File mode; if `-1`, use `0666 - umask`
  3110. + * @flags: Flags
  3111. + * @cancellable: Cancellable
  3112. + * @error: Error
  3113. + *
  3114. + * Like glnx_file_replace_contents_at(), but also supports
  3115. + * setting mode, and uid/gid.
  3116. + */
  3117. +gboolean
  3118. +glnx_file_replace_contents_with_perms_at (int dfd,
  3119. + const char *subpath,
  3120. + const guint8 *buf,
  3121. + gsize len,
  3122. + mode_t mode,
  3123. + uid_t uid,
  3124. + gid_t gid,
  3125. + GLnxFileReplaceFlags flags,
  3126. + GCancellable *cancellable,
  3127. + GError **error)
  3128. +{
  3129. + char *dnbuf = strdupa (subpath);
  3130. + const char *dn = dirname (dnbuf);
  3131. +
  3132. + dfd = glnx_dirfd_canonicalize (dfd);
  3133. +
  3134. + /* With O_TMPFILE we can't use umask, and we can't sanely query the
  3135. + * umask...let's assume something relatively standard.
  3136. + */
  3137. + if (mode == (mode_t) -1)
  3138. + mode = 0644;
  3139. +
  3140. + g_auto(GLnxTmpfile) tmpf = { 0, };
  3141. + if (!glnx_open_tmpfile_linkable_at (dfd, dn, O_WRONLY | O_CLOEXEC,
  3142. + &tmpf, error))
  3143. + return FALSE;
  3144. +
  3145. + if (len == -1)
  3146. + len = strlen ((char*)buf);
  3147. +
  3148. + if (!glnx_try_fallocate (tmpf.fd, 0, len, error))
  3149. + return FALSE;
  3150. +
  3151. + if (glnx_loop_write (tmpf.fd, buf, len) < 0)
  3152. + return glnx_throw_errno (error);
  3153. +
  3154. + if (!(flags & GLNX_FILE_REPLACE_NODATASYNC))
  3155. + {
  3156. + struct stat stbuf;
  3157. + gboolean do_sync;
  3158. +
  3159. + if (fstatat (dfd, subpath, &stbuf, AT_SYMLINK_NOFOLLOW) != 0)
  3160. + {
  3161. + if (errno != ENOENT)
  3162. + return glnx_throw_errno (error);
  3163. + do_sync = (flags & GLNX_FILE_REPLACE_DATASYNC_NEW) > 0;
  3164. + }
  3165. + else
  3166. + do_sync = TRUE;
  3167. +
  3168. + if (do_sync)
  3169. + {
  3170. + if (fdatasync (tmpf.fd) != 0)
  3171. + return glnx_throw_errno_prefix (error, "fdatasync");
  3172. + }
  3173. + }
  3174. +
  3175. + if (uid != (uid_t) -1)
  3176. + {
  3177. + if (fchown (tmpf.fd, uid, gid) != 0)
  3178. + return glnx_throw_errno (error);
  3179. + }
  3180. +
  3181. + if (fchmod (tmpf.fd, mode) != 0)
  3182. + return glnx_throw_errno (error);
  3183. +
  3184. + if (!glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_REPLACE,
  3185. + dfd, subpath, error))
  3186. + return FALSE;
  3187. +
  3188. + return TRUE;
  3189. +}
  3190. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-fdio.h flatpak-builder-0.10.6/libglnx/glnx-fdio.h
  3191. --- flatpak-builder-0.10.6.orig/libglnx/glnx-fdio.h 1970-01-01 02:00:00.000000000 +0200
  3192. +++ flatpak-builder-0.10.6/libglnx/glnx-fdio.h 2018-02-11 12:03:43.449373307 +0300
  3193. @@ -0,0 +1,333 @@
  3194. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3195. + *
  3196. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  3197. + *
  3198. + * This library is free software; you can redistribute it and/or
  3199. + * modify it under the terms of the GNU Lesser General Public
  3200. + * License as published by the Free Software Foundation; either
  3201. + * version 2 of the License, or (at your option) any later version.
  3202. + *
  3203. + * This library is distributed in the hope that it will be useful,
  3204. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3205. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3206. + * Lesser General Public License for more details.
  3207. + *
  3208. + * You should have received a copy of the GNU Lesser General Public
  3209. + * License along with this library; if not, write to the
  3210. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3211. + * Boston, MA 02111-1307, USA.
  3212. + */
  3213. +
  3214. +#pragma once
  3215. +
  3216. +#include <glnx-backport-autocleanups.h>
  3217. +#include <gio/gfiledescriptorbased.h>
  3218. +#include <limits.h>
  3219. +#include <dirent.h>
  3220. +#include <sys/stat.h>
  3221. +#include <fcntl.h>
  3222. +#include <string.h>
  3223. +#include <stdio.h>
  3224. +#include <sys/xattr.h>
  3225. +/* From systemd/src/shared/util.h */
  3226. +/* When we include libgen.h because we need dirname() we immediately
  3227. + * undefine basename() since libgen.h defines it as a macro to the XDG
  3228. + * version which is really broken. */
  3229. +#include <libgen.h>
  3230. +#undef basename
  3231. +
  3232. +#include <glnx-macros.h>
  3233. +#include <glnx-errors.h>
  3234. +
  3235. +G_BEGIN_DECLS
  3236. +
  3237. +/* Irritatingly, g_basename() which is what we want
  3238. + * is deprecated.
  3239. + */
  3240. +static inline
  3241. +const char *glnx_basename (const char *path)
  3242. +{
  3243. + return (basename) (path);
  3244. +}
  3245. +
  3246. +/* Utilities for standard FILE* */
  3247. +static inline void
  3248. +glnx_stdio_file_cleanup (void *filep)
  3249. +{
  3250. + FILE *f = filep;
  3251. + if (f)
  3252. + fclose (f);
  3253. +}
  3254. +G_DEFINE_AUTOPTR_CLEANUP_FUNC(FILE, glnx_stdio_file_cleanup)
  3255. +
  3256. +/**
  3257. + * glnx_stdio_file_flush:
  3258. + * Call fflush() and check ferror().
  3259. + */
  3260. +gboolean
  3261. +glnx_stdio_file_flush (FILE *f, GError **error);
  3262. +
  3263. +typedef struct {
  3264. + gboolean initialized;
  3265. + gboolean anonymous;
  3266. + int src_dfd;
  3267. + int fd;
  3268. + char *path;
  3269. +} GLnxTmpfile;
  3270. +void glnx_tmpfile_clear (GLnxTmpfile *tmpf);
  3271. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxTmpfile, glnx_tmpfile_clear)
  3272. +
  3273. +gboolean
  3274. +glnx_open_anonymous_tmpfile (int flags,
  3275. + GLnxTmpfile *out_tmpf,
  3276. + GError **error);
  3277. +
  3278. +gboolean
  3279. +glnx_open_tmpfile_linkable_at (int dfd,
  3280. + const char *subpath,
  3281. + int flags,
  3282. + GLnxTmpfile *out_tmpf,
  3283. + GError **error);
  3284. +
  3285. +typedef enum {
  3286. + GLNX_LINK_TMPFILE_REPLACE,
  3287. + GLNX_LINK_TMPFILE_NOREPLACE,
  3288. + GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST
  3289. +} GLnxLinkTmpfileReplaceMode;
  3290. +
  3291. +gboolean
  3292. +glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
  3293. + GLnxLinkTmpfileReplaceMode flags,
  3294. + int target_dfd,
  3295. + const char *target,
  3296. + GError **error);
  3297. +
  3298. +gboolean
  3299. +glnx_openat_rdonly (int dfd,
  3300. + const char *path,
  3301. + gboolean follow,
  3302. + int *out_fd,
  3303. + GError **error);
  3304. +
  3305. +GBytes *
  3306. +glnx_fd_readall_bytes (int fd,
  3307. + GCancellable *cancellable,
  3308. + GError **error);
  3309. +
  3310. +char *
  3311. +glnx_fd_readall_utf8 (int fd,
  3312. + gsize *out_len,
  3313. + GCancellable *cancellable,
  3314. + GError **error);
  3315. +
  3316. +char *
  3317. +glnx_file_get_contents_utf8_at (int dfd,
  3318. + const char *subpath,
  3319. + gsize *out_len,
  3320. + GCancellable *cancellable,
  3321. + GError **error);
  3322. +
  3323. +/**
  3324. + * GLnxFileReplaceFlags:
  3325. + * @GLNX_FILE_REPLACE_DATASYNC_NEW: Call fdatasync() even if the file did not exist
  3326. + * @GLNX_FILE_REPLACE_NODATASYNC: Never call fdatasync()
  3327. + *
  3328. + * Flags controlling file replacement.
  3329. + */
  3330. +typedef enum {
  3331. + GLNX_FILE_REPLACE_DATASYNC_NEW = (1 << 0),
  3332. + GLNX_FILE_REPLACE_NODATASYNC = (1 << 1),
  3333. +} GLnxFileReplaceFlags;
  3334. +
  3335. +gboolean
  3336. +glnx_file_replace_contents_at (int dfd,
  3337. + const char *subpath,
  3338. + const guint8 *buf,
  3339. + gsize len,
  3340. + GLnxFileReplaceFlags flags,
  3341. + GCancellable *cancellable,
  3342. + GError **error);
  3343. +
  3344. +gboolean
  3345. +glnx_file_replace_contents_with_perms_at (int dfd,
  3346. + const char *subpath,
  3347. + const guint8 *buf,
  3348. + gsize len,
  3349. + mode_t mode,
  3350. + uid_t uid,
  3351. + gid_t gid,
  3352. + GLnxFileReplaceFlags flags,
  3353. + GCancellable *cancellable,
  3354. + GError **error);
  3355. +
  3356. +char *
  3357. +glnx_readlinkat_malloc (int dfd,
  3358. + const char *subpath,
  3359. + GCancellable *cancellable,
  3360. + GError **error);
  3361. +
  3362. +int
  3363. +glnx_loop_write (int fd, const void *buf, size_t nbytes);
  3364. +
  3365. +int
  3366. +glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes);
  3367. +
  3368. +typedef enum {
  3369. + GLNX_FILE_COPY_OVERWRITE = (1 << 0),
  3370. + GLNX_FILE_COPY_NOXATTRS = (1 << 1),
  3371. + GLNX_FILE_COPY_DATASYNC = (1 << 2)
  3372. +} GLnxFileCopyFlags;
  3373. +
  3374. +gboolean
  3375. +glnx_file_copy_at (int src_dfd,
  3376. + const char *src_subpath,
  3377. + struct stat *src_stbuf,
  3378. + int dest_dfd,
  3379. + const char *dest_subpath,
  3380. + GLnxFileCopyFlags copyflags,
  3381. + GCancellable *cancellable,
  3382. + GError **error);
  3383. +
  3384. +int glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
  3385. + int newdirfd, const char *newpath);
  3386. +int glnx_renameat2_exchange (int olddirfd, const char *oldpath,
  3387. + int newdirfd, const char *newpath);
  3388. +
  3389. +/**
  3390. + * glnx_try_fallocate:
  3391. + * @fd: File descriptor
  3392. + * @size: Size
  3393. + * @error: Error
  3394. + *
  3395. + * Wrapper for Linux fallocate(). Explicitly ignores a @size of zero.
  3396. + * Also, will silently do nothing if the underlying filesystem doesn't
  3397. + * support it. Use this instead of posix_fallocate(), since the glibc fallback
  3398. + * is bad: https://sourceware.org/bugzilla/show_bug.cgi?id=18515
  3399. + */
  3400. +static inline gboolean
  3401. +glnx_try_fallocate (int fd,
  3402. + off_t offset,
  3403. + off_t size,
  3404. + GError **error)
  3405. +{
  3406. + /* This is just nicer than throwing an error */
  3407. + if (size == 0)
  3408. + return TRUE;
  3409. +
  3410. + if (fallocate (fd, 0, offset, size) < 0)
  3411. + {
  3412. + if (G_IN_SET(errno, ENOSYS, EOPNOTSUPP))
  3413. + ; /* Ignore */
  3414. + else
  3415. + return glnx_throw_errno_prefix (error, "fallocate");
  3416. + }
  3417. +
  3418. + return TRUE;
  3419. +}
  3420. +
  3421. +/**
  3422. + * glnx_fstat:
  3423. + * @fd: FD to stat
  3424. + * @buf: (out caller-allocates): Return location for stat details
  3425. + * @error: Return location for a #GError, or %NULL
  3426. + *
  3427. + * Wrapper around fstat() which adds #GError support and ensures that it retries
  3428. + * on %EINTR.
  3429. + *
  3430. + * Returns: %TRUE on success, %FALSE otherwise
  3431. + * Since: UNRELEASED
  3432. + */
  3433. +static inline gboolean
  3434. +glnx_fstat (int fd,
  3435. + struct stat *buf,
  3436. + GError **error)
  3437. +{
  3438. + if (TEMP_FAILURE_RETRY (fstat (fd, buf)) != 0)
  3439. + return glnx_throw_errno_prefix (error, "fstat");
  3440. + return TRUE;
  3441. +}
  3442. +
  3443. +/**
  3444. + * glnx_fchmod:
  3445. + * @fd: FD
  3446. + * @mode: Mode
  3447. + * @error: Return location for a #GError, or %NULL
  3448. + *
  3449. + * Wrapper around fchmod() which adds #GError support and ensures that it
  3450. + * retries on %EINTR.
  3451. + *
  3452. + * Returns: %TRUE on success, %FALSE otherwise
  3453. + * Since: UNRELEASED
  3454. + */
  3455. +static inline gboolean
  3456. +glnx_fchmod (int fd,
  3457. + mode_t mode,
  3458. + GError **error)
  3459. +{
  3460. + if (TEMP_FAILURE_RETRY (fchmod (fd, mode)) != 0)
  3461. + return glnx_throw_errno_prefix (error, "fchmod");
  3462. + return TRUE;
  3463. +}
  3464. +
  3465. +/**
  3466. + * glnx_fstatat:
  3467. + * @dfd: Directory FD to stat beneath
  3468. + * @path: Path to stat beneath @dfd
  3469. + * @buf: (out caller-allocates): Return location for stat details
  3470. + * @flags: Flags to pass to fstatat()
  3471. + * @error: Return location for a #GError, or %NULL
  3472. + *
  3473. + * Wrapper around fstatat() which adds #GError support and ensures that it
  3474. + * retries on %EINTR.
  3475. + *
  3476. + * Returns: %TRUE on success, %FALSE otherwise
  3477. + * Since: UNRELEASED
  3478. + */
  3479. +static inline gboolean
  3480. +glnx_fstatat (int dfd,
  3481. + const gchar *path,
  3482. + struct stat *buf,
  3483. + int flags,
  3484. + GError **error)
  3485. +{
  3486. + if (TEMP_FAILURE_RETRY (fstatat (dfd, path, buf, flags)) != 0)
  3487. + return glnx_throw_errno_prefix (error, "fstatat(%s)", path);
  3488. + return TRUE;
  3489. +}
  3490. +
  3491. +/**
  3492. + * glnx_renameat:
  3493. + *
  3494. + * Wrapper around renameat() which adds #GError support and ensures that it
  3495. + * retries on %EINTR.
  3496. + */
  3497. +static inline gboolean
  3498. +glnx_renameat (int src_dfd,
  3499. + const gchar *src_path,
  3500. + int dest_dfd,
  3501. + const gchar *dest_path,
  3502. + GError **error)
  3503. +{
  3504. + if (TEMP_FAILURE_RETRY (renameat (src_dfd, src_path, dest_dfd, dest_path)) != 0)
  3505. + return glnx_throw_errno_prefix (error, "renameat(%s, %s)", src_path, dest_path);
  3506. + return TRUE;
  3507. +}
  3508. +
  3509. +/**
  3510. + * glnx_unlinkat:
  3511. + *
  3512. + * Wrapper around unlinkat() which adds #GError support and ensures that it
  3513. + * retries on %EINTR.
  3514. + */
  3515. +static inline gboolean
  3516. +glnx_unlinkat (int dfd,
  3517. + const gchar *path,
  3518. + int flags,
  3519. + GError **error)
  3520. +{
  3521. + if (TEMP_FAILURE_RETRY (unlinkat (dfd, path, flags)) != 0)
  3522. + return glnx_throw_errno_prefix (error, "unlinkat(%s)", path);
  3523. + return TRUE;
  3524. +}
  3525. +
  3526. +G_END_DECLS
  3527. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-local-alloc.c flatpak-builder-0.10.6/libglnx/glnx-local-alloc.c
  3528. --- flatpak-builder-0.10.6.orig/libglnx/glnx-local-alloc.c 1970-01-01 02:00:00.000000000 +0200
  3529. +++ flatpak-builder-0.10.6/libglnx/glnx-local-alloc.c 2018-02-11 12:03:43.449373307 +0300
  3530. @@ -0,0 +1,72 @@
  3531. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3532. + *
  3533. + * Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>
  3534. + *
  3535. + * This library is free software; you can redistribute it and/or
  3536. + * modify it under the terms of the GNU Lesser General Public
  3537. + * License as published by the Free Software Foundation; either
  3538. + * version 2 of the License, or (at your option) any later version.
  3539. + *
  3540. + * This library is distributed in the hope that it will be useful,
  3541. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3542. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3543. + * Lesser General Public License for more details.
  3544. + *
  3545. + * You should have received a copy of the GNU Lesser General Public
  3546. + * License along with this library; if not, write to the
  3547. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3548. + * Boston, MA 02111-1307, USA.
  3549. + */
  3550. +
  3551. +#include "config.h"
  3552. +
  3553. +#include "glnx-local-alloc.h"
  3554. +
  3555. +/**
  3556. + * SECTION:glnxlocalalloc
  3557. + * @title: GLnx local allocation
  3558. + * @short_description: Release local variables automatically when they go out of scope
  3559. + *
  3560. + * These macros leverage the GCC extension __attribute__ ((cleanup))
  3561. + * to allow calling a cleanup function such as g_free() when a
  3562. + * variable goes out of scope. See <ulink
  3563. + * url="http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html">
  3564. + * for more information on the attribute.
  3565. + *
  3566. + * The provided macros make it easy to use the cleanup attribute for
  3567. + * types that come with GLib. The primary two are #glnx_free and
  3568. + * #glnx_unref_object, which correspond to g_free() and
  3569. + * g_object_unref(), respectively.
  3570. + *
  3571. + * The rationale behind this is that particularly when handling error
  3572. + * paths, it can be very tricky to ensure the right variables are
  3573. + * freed. With this, one simply applies glnx_unref_object to a
  3574. + * locally-allocated #GFile for example, and it will be automatically
  3575. + * unreferenced when it goes out of scope.
  3576. + *
  3577. + * Note - you should only use these macros for <emphasis>stack
  3578. + * allocated</emphasis> variables. They don't provide garbage
  3579. + * collection or let you avoid freeing things. They're simply a
  3580. + * compiler assisted deterministic mechanism for calling a cleanup
  3581. + * function when a stack frame ends.
  3582. + *
  3583. + * <example id="gs-lfree"><title>Calling g_free automatically</title>
  3584. + * <programlisting>
  3585. + *
  3586. + * GFile *
  3587. + * create_file (GError **error)
  3588. + * {
  3589. + * glnx_free char *random_id = NULL;
  3590. + *
  3591. + * if (!prepare_file (error))
  3592. + * return NULL;
  3593. + *
  3594. + * random_id = alloc_random_id ();
  3595. + *
  3596. + * return create_file_real (error);
  3597. + * // Note that random_id is freed here automatically
  3598. + * }
  3599. + * </programlisting>
  3600. + * </example>
  3601. + *
  3602. + */
  3603. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-local-alloc.h flatpak-builder-0.10.6/libglnx/glnx-local-alloc.h
  3604. --- flatpak-builder-0.10.6.orig/libglnx/glnx-local-alloc.h 1970-01-01 02:00:00.000000000 +0200
  3605. +++ flatpak-builder-0.10.6/libglnx/glnx-local-alloc.h 2018-02-11 12:03:43.449373307 +0300
  3606. @@ -0,0 +1,77 @@
  3607. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3608. + *
  3609. + * Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>.
  3610. + *
  3611. + * This library is free software; you can redistribute it and/or
  3612. + * modify it under the terms of the GNU Lesser General Public
  3613. + * License as published by the Free Software Foundation; either
  3614. + * version 2 of the License, or (at your option) any later version.
  3615. + *
  3616. + * This library is distributed in the hope that it will be useful,
  3617. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3618. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3619. + * Lesser General Public License for more details.
  3620. + *
  3621. + * You should have received a copy of the GNU Lesser General Public
  3622. + * License along with this library; if not, write to the
  3623. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3624. + * Boston, MA 02111-1307, USA.
  3625. + */
  3626. +
  3627. +#pragma once
  3628. +
  3629. +#include <gio/gio.h>
  3630. +#include <errno.h>
  3631. +
  3632. +G_BEGIN_DECLS
  3633. +
  3634. +/**
  3635. + * glnx_unref_object:
  3636. + *
  3637. + * Call g_object_unref() on a variable location when it goes out of
  3638. + * scope. Note that unlike g_object_unref(), the variable may be
  3639. + * %NULL.
  3640. + */
  3641. +#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
  3642. +static inline void
  3643. +glnx_local_obj_unref (void *v)
  3644. +{
  3645. + GObject *o = *(GObject **)v;
  3646. + if (o)
  3647. + g_object_unref (o);
  3648. +}
  3649. +#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
  3650. +
  3651. +static inline void
  3652. +glnx_cleanup_close_fdp (int *fdp)
  3653. +{
  3654. + int fd, errsv;
  3655. +
  3656. + g_assert (fdp);
  3657. +
  3658. + fd = *fdp;
  3659. + if (fd >= 0)
  3660. + {
  3661. + errsv = errno;
  3662. + if (close (fd) < 0)
  3663. + g_assert (errno != EBADF);
  3664. + errno = errsv;
  3665. + }
  3666. +}
  3667. +
  3668. +/**
  3669. + * glnx_fd_close:
  3670. + *
  3671. + * Call close() on a variable location when it goes out of scope.
  3672. + */
  3673. +#define glnx_fd_close __attribute__((cleanup(glnx_cleanup_close_fdp)))
  3674. +
  3675. +static inline int
  3676. +glnx_steal_fd (int *fdp)
  3677. +{
  3678. + int fd = *fdp;
  3679. + *fdp = -1;
  3680. + return fd;
  3681. +}
  3682. +
  3683. +G_END_DECLS
  3684. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-lockfile.c flatpak-builder-0.10.6/libglnx/glnx-lockfile.c
  3685. --- flatpak-builder-0.10.6.orig/libglnx/glnx-lockfile.c 1970-01-01 02:00:00.000000000 +0200
  3686. +++ flatpak-builder-0.10.6/libglnx/glnx-lockfile.c 2018-02-11 12:03:43.449373307 +0300
  3687. @@ -0,0 +1,185 @@
  3688. +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  3689. +
  3690. +/***
  3691. + This file is part of systemd.
  3692. + Now copied into libglnx:
  3693. + - Use GError
  3694. +
  3695. + Copyright 2010 Lennart Poettering
  3696. + Copyright 2015 Colin Walters <walters@verbum.org>
  3697. +
  3698. + systemd is free software; you can redistribute it and/or modify it
  3699. + under the terms of the GNU Lesser General Public License as published by
  3700. + the Free Software Foundation; either version 2.1 of the License, or
  3701. + (at your option) any later version.
  3702. +
  3703. + systemd is distributed in the hope that it will be useful, but
  3704. + WITHOUT ANY WARRANTY; without even the implied warranty of
  3705. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3706. + Lesser General Public License for more details.
  3707. +
  3708. + You should have received a copy of the GNU Lesser General Public License
  3709. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  3710. +***/
  3711. +
  3712. +#include "config.h"
  3713. +
  3714. +#include <stdlib.h>
  3715. +#include <stdbool.h>
  3716. +#include <errno.h>
  3717. +#include <string.h>
  3718. +#include <stdio.h>
  3719. +#include <limits.h>
  3720. +#include <unistd.h>
  3721. +#include <sys/types.h>
  3722. +#include <sys/file.h>
  3723. +#include <sys/stat.h>
  3724. +#include <fcntl.h>
  3725. +
  3726. +#include "glnx-lockfile.h"
  3727. +#include "glnx-errors.h"
  3728. +#include "glnx-fdio.h"
  3729. +#include "glnx-backport-autocleanups.h"
  3730. +#include "glnx-local-alloc.h"
  3731. +
  3732. +#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
  3733. +
  3734. +/**
  3735. + * glnx_make_lock_file:
  3736. + * @dfd: Directory file descriptor (if not `AT_FDCWD`, must have lifetime `>=` @out_lock)
  3737. + * @p: Path
  3738. + * @operation: one of `LOCK_SH`, `LOCK_EX`, `LOCK_UN`, as passed to flock()
  3739. + * @out_lock: (out) (caller allocates): Return location for lock
  3740. + * @error: Error
  3741. + *
  3742. + * Block until a lock file named @p (relative to @dfd) can be created,
  3743. + * using the flags in @operation, returning the lock data in the
  3744. + * caller-allocated location @out_lock.
  3745. + *
  3746. + * This API wraps new-style process locking if available, otherwise
  3747. + * falls back to BSD locks.
  3748. + */
  3749. +gboolean
  3750. +glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_lock, GError **error) {
  3751. + glnx_fd_close int fd = -1;
  3752. + g_autofree char *t = NULL;
  3753. + int r;
  3754. +
  3755. + /*
  3756. + * We use UNPOSIX locks if they are available. They have nice
  3757. + * semantics, and are mostly compatible with NFS. However,
  3758. + * they are only available on new kernels. When we detect we
  3759. + * are running on an older kernel, then we fall back to good
  3760. + * old BSD locks. They also have nice semantics, but are
  3761. + * slightly problematic on NFS, where they are upgraded to
  3762. + * POSIX locks, even though locally they are orthogonal to
  3763. + * POSIX locks.
  3764. + */
  3765. +
  3766. + t = g_strdup(p);
  3767. +
  3768. + for (;;) {
  3769. +#ifdef F_OFD_SETLK
  3770. + struct flock fl = {
  3771. + .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
  3772. + .l_whence = SEEK_SET,
  3773. + };
  3774. +#endif
  3775. + struct stat st;
  3776. +
  3777. + fd = openat(dfd, p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
  3778. + if (fd < 0)
  3779. + return glnx_throw_errno(error);
  3780. +
  3781. + /* Unfortunately, new locks are not in RHEL 7.1 glibc */
  3782. +#ifdef F_OFD_SETLK
  3783. + r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
  3784. +#else
  3785. + r = -1;
  3786. + errno = EINVAL;
  3787. +#endif
  3788. + if (r < 0) {
  3789. +
  3790. + /* If the kernel is too old, use good old BSD locks */
  3791. + if (errno == EINVAL)
  3792. + r = flock(fd, operation);
  3793. +
  3794. + if (r < 0)
  3795. + return glnx_throw_errno(error);
  3796. + }
  3797. +
  3798. + /* If we acquired the lock, let's check if the file
  3799. + * still exists in the file system. If not, then the
  3800. + * previous exclusive owner removed it and then closed
  3801. + * it. In such a case our acquired lock is worthless,
  3802. + * hence try again. */
  3803. +
  3804. + r = fstat(fd, &st);
  3805. + if (r < 0)
  3806. + return glnx_throw_errno(error);
  3807. + if (st.st_nlink > 0)
  3808. + break;
  3809. +
  3810. + (void) close(fd);
  3811. + fd = -1;
  3812. + }
  3813. +
  3814. + /* Note that if this is not AT_FDCWD, the caller takes responsibility
  3815. + * for the fd's lifetime being >= that of the lock.
  3816. + */
  3817. + out_lock->dfd = dfd;
  3818. + out_lock->path = t;
  3819. + out_lock->fd = fd;
  3820. + out_lock->operation = operation;
  3821. +
  3822. + fd = -1;
  3823. + t = NULL;
  3824. +
  3825. + return TRUE;
  3826. +}
  3827. +
  3828. +void glnx_release_lock_file(GLnxLockFile *f) {
  3829. + int r;
  3830. +
  3831. + if (!f)
  3832. + return;
  3833. +
  3834. + if (f->path) {
  3835. +
  3836. + /* If we are the exclusive owner we can safely delete
  3837. + * the lock file itself. If we are not the exclusive
  3838. + * owner, we can try becoming it. */
  3839. +
  3840. + if (f->fd >= 0 &&
  3841. + (f->operation & ~LOCK_NB) == LOCK_SH) {
  3842. +#ifdef F_OFD_SETLK
  3843. + static const struct flock fl = {
  3844. + .l_type = F_WRLCK,
  3845. + .l_whence = SEEK_SET,
  3846. + };
  3847. +
  3848. + r = fcntl(f->fd, F_OFD_SETLK, &fl);
  3849. +#else
  3850. + r = -1;
  3851. + errno = EINVAL;
  3852. +#endif
  3853. + if (r < 0 && errno == EINVAL)
  3854. + r = flock(f->fd, LOCK_EX|LOCK_NB);
  3855. +
  3856. + if (r >= 0)
  3857. + f->operation = LOCK_EX|LOCK_NB;
  3858. + }
  3859. +
  3860. + if ((f->operation & ~LOCK_NB) == LOCK_EX) {
  3861. + (void) unlinkat(f->dfd, f->path, 0);
  3862. + }
  3863. +
  3864. + g_free(f->path);
  3865. + f->path = NULL;
  3866. + }
  3867. +
  3868. + if (f->fd != -1)
  3869. + (void) close (f->fd);
  3870. + f->fd = -1;
  3871. + f->operation = 0;
  3872. +}
  3873. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-lockfile.h flatpak-builder-0.10.6/libglnx/glnx-lockfile.h
  3874. --- flatpak-builder-0.10.6.orig/libglnx/glnx-lockfile.h 1970-01-01 02:00:00.000000000 +0200
  3875. +++ flatpak-builder-0.10.6/libglnx/glnx-lockfile.h 2018-02-11 12:03:43.449373307 +0300
  3876. @@ -0,0 +1,41 @@
  3877. +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  3878. +
  3879. +#pragma once
  3880. +
  3881. +/***
  3882. + This file is part of systemd.
  3883. +
  3884. + Copyright 2011 Lennart Poettering
  3885. + Copyright 2015 Colin Walters <walters@verbum.org>
  3886. +
  3887. + systemd is free software; you can redistribute it and/or modify it
  3888. + under the terms of the GNU Lesser General Public License as published by
  3889. + the Free Software Foundation; either version 2.1 of the License, or
  3890. + (at your option) any later version.
  3891. +
  3892. + systemd is distributed in the hope that it will be useful, but
  3893. + WITHOUT ANY WARRANTY; without even the implied warranty of
  3894. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3895. + Lesser General Public License for more details.
  3896. +
  3897. + You should have received a copy of the GNU Lesser General Public License
  3898. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  3899. +***/
  3900. +
  3901. +#include "config.h"
  3902. +
  3903. +#include "glnx-backport-autoptr.h"
  3904. +
  3905. +typedef struct GLnxLockFile {
  3906. + int dfd;
  3907. + char *path;
  3908. + int fd;
  3909. + int operation;
  3910. +} GLnxLockFile;
  3911. +
  3912. +gboolean glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *ret, GError **error);
  3913. +void glnx_release_lock_file(GLnxLockFile *f);
  3914. +
  3915. +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GLnxLockFile, glnx_release_lock_file)
  3916. +
  3917. +#define GLNX_LOCK_FILE_INIT { .fd = -1, .dfd = AT_FDCWD, .path = NULL }
  3918. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-macros.h flatpak-builder-0.10.6/libglnx/glnx-macros.h
  3919. --- flatpak-builder-0.10.6.orig/libglnx/glnx-macros.h 1970-01-01 02:00:00.000000000 +0200
  3920. +++ flatpak-builder-0.10.6/libglnx/glnx-macros.h 2018-02-11 12:03:43.449373307 +0300
  3921. @@ -0,0 +1,189 @@
  3922. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  3923. + *
  3924. + * Copyright (C) 2017 Colin Walters <walters@verbum.org>
  3925. + * With original source from systemd:
  3926. + * Copyright 2010 Lennart Poettering
  3927. + *
  3928. + * This library is free software; you can redistribute it and/or
  3929. + * modify it under the terms of the GNU Lesser General Public
  3930. + * License as published by the Free Software Foundation; either
  3931. + * version 2 of the License, or (at your option) any later version.
  3932. + *
  3933. + * This library is distributed in the hope that it will be useful,
  3934. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3935. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3936. + * Lesser General Public License for more details.
  3937. + *
  3938. + * You should have received a copy of the GNU Lesser General Public
  3939. + * License along with this library; if not, write to the
  3940. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3941. + * Boston, MA 02111-1307, USA.
  3942. + */
  3943. +
  3944. +#pragma once
  3945. +
  3946. +#include <stdlib.h>
  3947. +#include <string.h>
  3948. +#include <gio/gio.h>
  3949. +
  3950. +G_BEGIN_DECLS
  3951. +
  3952. +/* All of these are for C only. */
  3953. +#ifndef __GI_SCANNER__
  3954. +
  3955. +/* Taken from https://github.com/systemd/systemd/src/basic/string-util.h
  3956. + * at revision v228-666-gcf6c8c4
  3957. + */
  3958. +#define glnx_strjoina(a, ...) \
  3959. + ({ \
  3960. + const char *_appendees_[] = { a, __VA_ARGS__ }; \
  3961. + char *_d_, *_p_; \
  3962. + int _len_ = 0; \
  3963. + unsigned _i_; \
  3964. + for (_i_ = 0; _i_ < G_N_ELEMENTS(_appendees_) && _appendees_[_i_]; _i_++) \
  3965. + _len_ += strlen(_appendees_[_i_]); \
  3966. + _p_ = _d_ = alloca(_len_ + 1); \
  3967. + for (_i_ = 0; _i_ < G_N_ELEMENTS(_appendees_) && _appendees_[_i_]; _i_++) \
  3968. + _p_ = stpcpy(_p_, _appendees_[_i_]); \
  3969. + *_p_ = 0; \
  3970. + _d_; \
  3971. + })
  3972. +
  3973. +#ifndef G_IN_SET
  3974. +
  3975. +/* Infrastructure for `G_IN_SET`; this code is copied from
  3976. + * systemd's macro.h - please treat that version as canonical
  3977. + * and submit patches first to systemd.
  3978. + */
  3979. +#define _G_INSET_CASE_F(X) case X:
  3980. +#define _G_INSET_CASE_F_1(CASE, X) _G_INSET_CASE_F(X)
  3981. +#define _G_INSET_CASE_F_2(CASE, X, ...) CASE(X) _G_INSET_CASE_F_1(CASE, __VA_ARGS__)
  3982. +#define _G_INSET_CASE_F_3(CASE, X, ...) CASE(X) _G_INSET_CASE_F_2(CASE, __VA_ARGS__)
  3983. +#define _G_INSET_CASE_F_4(CASE, X, ...) CASE(X) _G_INSET_CASE_F_3(CASE, __VA_ARGS__)
  3984. +#define _G_INSET_CASE_F_5(CASE, X, ...) CASE(X) _G_INSET_CASE_F_4(CASE, __VA_ARGS__)
  3985. +#define _G_INSET_CASE_F_6(CASE, X, ...) CASE(X) _G_INSET_CASE_F_5(CASE, __VA_ARGS__)
  3986. +#define _G_INSET_CASE_F_7(CASE, X, ...) CASE(X) _G_INSET_CASE_F_6(CASE, __VA_ARGS__)
  3987. +#define _G_INSET_CASE_F_8(CASE, X, ...) CASE(X) _G_INSET_CASE_F_7(CASE, __VA_ARGS__)
  3988. +#define _G_INSET_CASE_F_9(CASE, X, ...) CASE(X) _G_INSET_CASE_F_8(CASE, __VA_ARGS__)
  3989. +#define _G_INSET_CASE_F_10(CASE, X, ...) CASE(X) _G_INSET_CASE_F_9(CASE, __VA_ARGS__)
  3990. +#define _G_INSET_CASE_F_11(CASE, X, ...) CASE(X) _G_INSET_CASE_F_10(CASE, __VA_ARGS__)
  3991. +#define _G_INSET_CASE_F_12(CASE, X, ...) CASE(X) _G_INSET_CASE_F_11(CASE, __VA_ARGS__)
  3992. +#define _G_INSET_CASE_F_13(CASE, X, ...) CASE(X) _G_INSET_CASE_F_12(CASE, __VA_ARGS__)
  3993. +#define _G_INSET_CASE_F_14(CASE, X, ...) CASE(X) _G_INSET_CASE_F_13(CASE, __VA_ARGS__)
  3994. +#define _G_INSET_CASE_F_15(CASE, X, ...) CASE(X) _G_INSET_CASE_F_14(CASE, __VA_ARGS__)
  3995. +#define _G_INSET_CASE_F_16(CASE, X, ...) CASE(X) _G_INSET_CASE_F_15(CASE, __VA_ARGS__)
  3996. +#define _G_INSET_CASE_F_17(CASE, X, ...) CASE(X) _G_INSET_CASE_F_16(CASE, __VA_ARGS__)
  3997. +#define _G_INSET_CASE_F_18(CASE, X, ...) CASE(X) _G_INSET_CASE_F_17(CASE, __VA_ARGS__)
  3998. +#define _G_INSET_CASE_F_19(CASE, X, ...) CASE(X) _G_INSET_CASE_F_18(CASE, __VA_ARGS__)
  3999. +#define _G_INSET_CASE_F_20(CASE, X, ...) CASE(X) _G_INSET_CASE_F_19(CASE, __VA_ARGS__)
  4000. +
  4001. +#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
  4002. +#define _G_INSET_FOR_EACH_MAKE_CASE(...) \
  4003. + _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, \
  4004. + _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) \
  4005. + (_G_INSET_CASE_F,__VA_ARGS__)
  4006. +
  4007. +/* Note: claiming the name here even though it isn't upstream yet
  4008. + * https://bugzilla.gnome.org/show_bug.cgi?id=783751
  4009. + */
  4010. +/**
  4011. + * G_IN_SET:
  4012. + * @x: Integer (or smaller) sized value
  4013. + * @...: Elements to compare
  4014. + *
  4015. + * It's quite common to test whether or not `char` values or Unix @errno (among) others
  4016. + * are members of a small set. Normally one has to choose to either use `if (x == val || x == otherval ...)`
  4017. + * or a `switch` statement. This macro is useful to reduce duplication in the first case,
  4018. + * where one can write simply `if (G_IN_SET (x, val, otherval))`, and avoid the verbosity
  4019. + * that the `switch` statement requires.
  4020. + */
  4021. +#define G_IN_SET(x, ...) \
  4022. + ({ \
  4023. + gboolean _g_inset_found = FALSE; \
  4024. + /* If the build breaks in the line below, you need to extend the case macros */ \
  4025. + static G_GNUC_UNUSED char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){__VA_ARGS__})/sizeof(int)]; \
  4026. + switch(x) { \
  4027. + _G_INSET_FOR_EACH_MAKE_CASE(__VA_ARGS__) \
  4028. + _g_inset_found = TRUE; \
  4029. + break; \
  4030. + default: \
  4031. + break; \
  4032. + } \
  4033. + _g_inset_found; \
  4034. + })
  4035. +
  4036. +#endif /* ifndef G_IN_SET */
  4037. +
  4038. +#define _GLNX_CONCAT(a, b) a##b
  4039. +#define _GLNX_CONCAT_INDIRECT(a, b) _GLNX_CONCAT(a, b)
  4040. +#define _GLNX_MAKE_ANONYMOUS(a) _GLNX_CONCAT_INDIRECT(a, __COUNTER__)
  4041. +
  4042. +#define _GLNX_HASH_TABLE_FOREACH_IMPL_KV(guard, ht, it, kt, k, vt, v) \
  4043. + gboolean guard = TRUE; \
  4044. + G_STATIC_ASSERT (sizeof (kt) == sizeof (void*)); \
  4045. + G_STATIC_ASSERT (sizeof (vt) == sizeof (void*)); \
  4046. + for (GHashTableIter it; \
  4047. + guard && ({ g_hash_table_iter_init (&it, ht), TRUE; }); \
  4048. + guard = FALSE) \
  4049. + for (kt k; guard; guard = FALSE) \
  4050. + for (vt v; g_hash_table_iter_next (&it, (gpointer)&k, (gpointer)&v);)
  4051. +
  4052. +
  4053. +/* Cleaner method to iterate over a GHashTable. I.e. rather than
  4054. + *
  4055. + * gpointer k, v;
  4056. + * GHashTableIter it;
  4057. + * g_hash_table_iter_init (&it, table);
  4058. + * while (g_hash_table_iter_next (&it, &k, &v))
  4059. + * {
  4060. + * const char *str = k;
  4061. + * GPtrArray *arr = v;
  4062. + * ...
  4063. + * }
  4064. + *
  4065. + * you can simply do
  4066. + *
  4067. + * GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, str, GPtrArray*, arr)
  4068. + * {
  4069. + * ...
  4070. + * }
  4071. + *
  4072. + * All variables are scoped within the loop. You may use the `it` variable as
  4073. + * usual, e.g. to remove an element using g_hash_table_iter_remove(&it). There
  4074. + * are shorter variants for the more common cases where you do not need access
  4075. + * to the iterator or to keys/values:
  4076. + *
  4077. + * GLNX_HASH_TABLE_FOREACH (table, const char*, str) { ... }
  4078. + * GLNX_HASH_TABLE_FOREACH_V (table, MyData*, data) { ... }
  4079. + * GLNX_HASH_TABLE_FOREACH_KV (table, const char*, str, MyData*, data) { ... }
  4080. + *
  4081. + */
  4082. +#define GLNX_HASH_TABLE_FOREACH_IT(ht, it, kt, k, vt, v) \
  4083. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  4084. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, it, kt, k, vt, v)
  4085. +
  4086. +/* Variant of GLNX_HASH_TABLE_FOREACH without having to specify an iterator. An
  4087. + * anonymous iterator will be created. */
  4088. +#define GLNX_HASH_TABLE_FOREACH_KV(ht, kt, k, vt, v) \
  4089. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  4090. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  4091. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), kt, k, vt, v)
  4092. +
  4093. +/* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking keys. */
  4094. +#define GLNX_HASH_TABLE_FOREACH_V(ht, vt, v) \
  4095. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  4096. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  4097. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), \
  4098. + gpointer, _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_v_), \
  4099. + vt, v)
  4100. +
  4101. +/* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking vals. */
  4102. +#define GLNX_HASH_TABLE_FOREACH(ht, kt, k) \
  4103. + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \
  4104. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \
  4105. + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), kt, k, \
  4106. + gpointer, _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_v_))
  4107. +
  4108. +#endif /* GI_SCANNER */
  4109. +
  4110. +G_END_DECLS
  4111. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-missing.h flatpak-builder-0.10.6/libglnx/glnx-missing.h
  4112. --- flatpak-builder-0.10.6.orig/libglnx/glnx-missing.h 1970-01-01 02:00:00.000000000 +0200
  4113. +++ flatpak-builder-0.10.6/libglnx/glnx-missing.h 2018-02-11 12:03:43.449373307 +0300
  4114. @@ -0,0 +1,55 @@
  4115. +#pragma once
  4116. +
  4117. +/***
  4118. + This file was originally part of systemd.
  4119. +
  4120. + Copyright 2010 Lennart Poettering
  4121. +
  4122. + systemd is free software; you can redistribute it and/or modify it
  4123. + under the terms of the GNU Lesser General Public License as published by
  4124. + the Free Software Foundation; either version 2.1 of the License, or
  4125. + (at your option) any later version.
  4126. +
  4127. + systemd is distributed in the hope that it will be useful, but
  4128. + WITHOUT ANY WARRANTY; without even the implied warranty of
  4129. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4130. + Lesser General Public License for more details.
  4131. +
  4132. + You should have received a copy of the GNU Lesser General Public License
  4133. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  4134. +***/
  4135. +
  4136. +/* Missing glibc definitions to access certain kernel APIs */
  4137. +
  4138. +#include <errno.h>
  4139. +#include <fcntl.h>
  4140. +#include <stdlib.h>
  4141. +#include <sys/resource.h>
  4142. +#include <sys/syscall.h>
  4143. +#include <uchar.h>
  4144. +#include <unistd.h>
  4145. +
  4146. +#if defined(__i386__) || defined(__x86_64__)
  4147. +
  4148. +/* The precise definition of __O_TMPFILE is arch specific, so let's
  4149. + * just define this on x86 where we know the value. */
  4150. +
  4151. +#ifndef __O_TMPFILE
  4152. +#define __O_TMPFILE 020000000
  4153. +#endif
  4154. +
  4155. +/* a horrid kludge trying to make sure that this will fail on old kernels */
  4156. +#ifndef O_TMPFILE
  4157. +#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
  4158. +#endif
  4159. +
  4160. +#endif
  4161. +
  4162. +#ifndef RENAME_NOREPLACE
  4163. +#define RENAME_NOREPLACE (1 << 0)
  4164. +#endif
  4165. +#ifndef RENAME_EXCHANGE
  4166. +#define RENAME_EXCHANGE (1 << 1)
  4167. +#endif
  4168. +
  4169. +#include "glnx-missing-syscall.h"
  4170. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-missing-syscall.h flatpak-builder-0.10.6/libglnx/glnx-missing-syscall.h
  4171. --- flatpak-builder-0.10.6.orig/libglnx/glnx-missing-syscall.h 1970-01-01 02:00:00.000000000 +0200
  4172. +++ flatpak-builder-0.10.6/libglnx/glnx-missing-syscall.h 2018-02-11 12:03:43.449373307 +0300
  4173. @@ -0,0 +1,95 @@
  4174. +/***
  4175. + This file was originally part of systemd.
  4176. +
  4177. + Copyright 2010 Lennart Poettering
  4178. + Copyright 2016 Zbigniew Jędrzejewski-Szmek
  4179. +
  4180. + systemd is free software; you can redistribute it and/or modify it
  4181. + under the terms of the GNU Lesser General Public License as published by
  4182. + the Free Software Foundation; either version 2.1 of the License, or
  4183. + (at your option) any later version.
  4184. +
  4185. + systemd is distributed in the hope that it will be useful, but
  4186. + WITHOUT ANY WARRANTY; without even the implied warranty of
  4187. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4188. + Lesser General Public License for more details.
  4189. +
  4190. + You should have received a copy of the GNU Lesser General Public License
  4191. + along with systemd; If not, see <http://www.gnu.org/licenses/>.
  4192. +***/
  4193. +
  4194. +/* Missing glibc definitions to access certain kernel APIs */
  4195. +
  4196. +#if !HAVE_DECL_RENAMEAT2
  4197. +# ifndef __NR_renameat2
  4198. +# if defined __x86_64__
  4199. +# define __NR_renameat2 316
  4200. +# elif defined __arm__
  4201. +# define __NR_renameat2 382
  4202. +# elif defined _MIPS_SIM
  4203. +# if _MIPS_SIM == _MIPS_SIM_ABI32
  4204. +# define __NR_renameat2 4351
  4205. +# endif
  4206. +# if _MIPS_SIM == _MIPS_SIM_NABI32
  4207. +# define __NR_renameat2 6315
  4208. +# endif
  4209. +# if _MIPS_SIM == _MIPS_SIM_ABI64
  4210. +# define __NR_renameat2 5311
  4211. +# endif
  4212. +# elif defined __i386__
  4213. +# define __NR_renameat2 353
  4214. +# else
  4215. +# warning "__NR_renameat2 unknown for your architecture"
  4216. +# endif
  4217. +# endif
  4218. +
  4219. +static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
  4220. +# ifdef __NR_renameat2
  4221. + return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
  4222. +# else
  4223. + errno = ENOSYS;
  4224. + return -1;
  4225. +# endif
  4226. +}
  4227. +#endif
  4228. +
  4229. +/* Copied from systemd git:
  4230. + commit 6bda23dd6aaba50cf8e3e6024248cf736cc443ca
  4231. + Author: Yu Watanabe <watanabe.yu+github@gmail.com>
  4232. + AuthorDate: Thu Jul 27 20:22:54 2017 +0900
  4233. + Commit: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
  4234. + CommitDate: Thu Jul 27 07:22:54 2017 -0400
  4235. +*/
  4236. +#if !HAVE_DECL_COPY_FILE_RANGE
  4237. +# ifndef __NR_copy_file_range
  4238. +# if defined(__x86_64__)
  4239. +# define __NR_copy_file_range 326
  4240. +# elif defined(__i386__)
  4241. +# define __NR_copy_file_range 377
  4242. +# elif defined __s390__
  4243. +# define __NR_copy_file_range 375
  4244. +# elif defined __arm__
  4245. +# define __NR_copy_file_range 391
  4246. +# elif defined __aarch64__
  4247. +# define __NR_copy_file_range 285
  4248. +# elif defined __powerpc__
  4249. +# define __NR_copy_file_range 379
  4250. +# elif defined __arc__
  4251. +# define __NR_copy_file_range 285
  4252. +# else
  4253. +# warning "__NR_copy_file_range not defined for your architecture"
  4254. +# endif
  4255. +# endif
  4256. +
  4257. +static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
  4258. + int fd_out, loff_t *off_out,
  4259. + size_t len,
  4260. + unsigned int flags) {
  4261. +# ifdef __NR_copy_file_range
  4262. + return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
  4263. +# else
  4264. + errno = ENOSYS;
  4265. + return -1;
  4266. +# endif
  4267. +}
  4268. +#endif
  4269. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-shutil.c flatpak-builder-0.10.6/libglnx/glnx-shutil.c
  4270. --- flatpak-builder-0.10.6.orig/libglnx/glnx-shutil.c 1970-01-01 02:00:00.000000000 +0200
  4271. +++ flatpak-builder-0.10.6/libglnx/glnx-shutil.c 2018-02-11 12:03:43.449373307 +0300
  4272. @@ -0,0 +1,252 @@
  4273. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4274. + *
  4275. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  4276. + *
  4277. + * This library is free software; you can redistribute it and/or
  4278. + * modify it under the terms of the GNU Lesser General Public
  4279. + * License as published by the Free Software Foundation; either
  4280. + * version 2 of the License, or (at your option) any later version.
  4281. + *
  4282. + * This library is distributed in the hope that it will be useful,
  4283. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4284. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4285. + * Lesser General Public License for more details.
  4286. + *
  4287. + * You should have received a copy of the GNU Lesser General Public
  4288. + * License along with this library; if not, write to the
  4289. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4290. + * Boston, MA 02111-1307, USA.
  4291. + */
  4292. +
  4293. +#include "config.h"
  4294. +
  4295. +#include <string.h>
  4296. +
  4297. +#include <glnx-shutil.h>
  4298. +#include <glnx-errors.h>
  4299. +#include <glnx-local-alloc.h>
  4300. +
  4301. +static gboolean
  4302. +glnx_shutil_rm_rf_children (GLnxDirFdIterator *dfd_iter,
  4303. + GCancellable *cancellable,
  4304. + GError **error)
  4305. +{
  4306. + struct dirent *dent;
  4307. +
  4308. + while (TRUE)
  4309. + {
  4310. + if (!glnx_dirfd_iterator_next_dent_ensure_dtype (dfd_iter, &dent, cancellable, error))
  4311. + return FALSE;
  4312. + if (dent == NULL)
  4313. + break;
  4314. +
  4315. + if (dent->d_type == DT_DIR)
  4316. + {
  4317. + g_auto(GLnxDirFdIterator) child_dfd_iter = { 0, };
  4318. +
  4319. + if (!glnx_dirfd_iterator_init_at (dfd_iter->fd, dent->d_name, FALSE,
  4320. + &child_dfd_iter, error))
  4321. + return FALSE;
  4322. +
  4323. + if (!glnx_shutil_rm_rf_children (&child_dfd_iter, cancellable, error))
  4324. + return FALSE;
  4325. +
  4326. + if (unlinkat (dfd_iter->fd, dent->d_name, AT_REMOVEDIR) == -1)
  4327. + return glnx_throw_errno_prefix (error, "unlinkat");
  4328. + }
  4329. + else
  4330. + {
  4331. + if (unlinkat (dfd_iter->fd, dent->d_name, 0) == -1)
  4332. + {
  4333. + if (errno != ENOENT)
  4334. + return glnx_throw_errno_prefix (error, "unlinkat");
  4335. + }
  4336. + }
  4337. + }
  4338. +
  4339. + return TRUE;
  4340. +}
  4341. +
  4342. +/**
  4343. + * glnx_shutil_rm_rf_at:
  4344. + * @dfd: A directory file descriptor, or `AT_FDCWD` or `-1` for current
  4345. + * @path: Path
  4346. + * @cancellable: Cancellable
  4347. + * @error: Error
  4348. + *
  4349. + * Recursively delete the filename referenced by the combination of
  4350. + * the directory fd @dfd and @path; it may be a file or directory. No
  4351. + * error is thrown if @path does not exist.
  4352. + */
  4353. +gboolean
  4354. +glnx_shutil_rm_rf_at (int dfd,
  4355. + const char *path,
  4356. + GCancellable *cancellable,
  4357. + GError **error)
  4358. +{
  4359. + glnx_fd_close int target_dfd = -1;
  4360. + g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
  4361. +
  4362. + dfd = glnx_dirfd_canonicalize (dfd);
  4363. +
  4364. + /* With O_NOFOLLOW first */
  4365. + target_dfd = openat (dfd, path,
  4366. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
  4367. +
  4368. + if (target_dfd == -1)
  4369. + {
  4370. + int errsv = errno;
  4371. + if (errsv == ENOENT)
  4372. + {
  4373. + ;
  4374. + }
  4375. + else if (errsv == ENOTDIR || errsv == ELOOP)
  4376. + {
  4377. + if (unlinkat (dfd, path, 0) != 0)
  4378. + return glnx_throw_errno_prefix (error, "unlinkat");
  4379. + }
  4380. + else
  4381. + return glnx_throw_errno_prefix (error, "open(%s)", path);
  4382. + }
  4383. + else
  4384. + {
  4385. + if (!glnx_dirfd_iterator_init_take_fd (&target_dfd, &dfd_iter, error))
  4386. + return FALSE;
  4387. +
  4388. + if (!glnx_shutil_rm_rf_children (&dfd_iter, cancellable, error))
  4389. + return FALSE;
  4390. +
  4391. + if (unlinkat (dfd, path, AT_REMOVEDIR) == -1)
  4392. + {
  4393. + if (errno != ENOENT)
  4394. + return glnx_throw_errno_prefix (error, "unlinkat");
  4395. + }
  4396. + }
  4397. +
  4398. + return TRUE;
  4399. +}
  4400. +
  4401. +static gboolean
  4402. +mkdir_p_at_internal (int dfd,
  4403. + char *path,
  4404. + int mode,
  4405. + GCancellable *cancellable,
  4406. + GError **error)
  4407. +{
  4408. + gboolean did_recurse = FALSE;
  4409. +
  4410. + if (g_cancellable_set_error_if_cancelled (cancellable, error))
  4411. + return FALSE;
  4412. +
  4413. + again:
  4414. + if (mkdirat (dfd, path, mode) == -1)
  4415. + {
  4416. + if (errno == ENOENT)
  4417. + {
  4418. + char *lastslash;
  4419. +
  4420. + g_assert (!did_recurse);
  4421. +
  4422. + lastslash = strrchr (path, '/');
  4423. + g_assert (lastslash != NULL);
  4424. + /* Note we can mutate the buffer as we dup'd it */
  4425. + *lastslash = '\0';
  4426. +
  4427. + if (!glnx_shutil_mkdir_p_at (dfd, path, mode,
  4428. + cancellable, error))
  4429. + return FALSE;
  4430. +
  4431. + /* Now restore it for another mkdir attempt */
  4432. + *lastslash = '/';
  4433. +
  4434. + did_recurse = TRUE;
  4435. + goto again;
  4436. + }
  4437. + else if (errno == EEXIST)
  4438. + {
  4439. + /* Fall through; it may not have been a directory,
  4440. + * but we'll find that out on the next call up.
  4441. + */
  4442. + }
  4443. + else
  4444. + return glnx_throw_errno_prefix (error, "mkdir(%s)", path);
  4445. + }
  4446. +
  4447. + return TRUE;
  4448. +}
  4449. +
  4450. +/**
  4451. + * glnx_shutil_mkdir_p_at:
  4452. + * @dfd: Directory fd
  4453. + * @path: Directory path to be created
  4454. + * @mode: Mode for newly created directories
  4455. + * @cancellable: Cancellable
  4456. + * @error: Error
  4457. + *
  4458. + * Similar to g_mkdir_with_parents(), except operates relative to the
  4459. + * directory fd @dfd.
  4460. + *
  4461. + * See also glnx_ensure_dir() for a non-recursive version.
  4462. + */
  4463. +gboolean
  4464. +glnx_shutil_mkdir_p_at (int dfd,
  4465. + const char *path,
  4466. + int mode,
  4467. + GCancellable *cancellable,
  4468. + GError **error)
  4469. +{
  4470. + struct stat stbuf;
  4471. + char *buf;
  4472. +
  4473. + /* Fast path stat to see whether it already exists */
  4474. + if (fstatat (dfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
  4475. + {
  4476. + /* Note early return */
  4477. + if (S_ISDIR (stbuf.st_mode))
  4478. + return TRUE;
  4479. + }
  4480. +
  4481. + buf = strdupa (path);
  4482. +
  4483. + if (!mkdir_p_at_internal (dfd, buf, mode, cancellable, error))
  4484. + return FALSE;
  4485. +
  4486. + return TRUE;
  4487. +}
  4488. +
  4489. +/**
  4490. + * glnx_shutil_mkdir_p_at_open:
  4491. + * @dfd: Directory fd
  4492. + * @path: Directory path to be created
  4493. + * @mode: Mode for newly created directories
  4494. + * @out_dfd: (out caller-allocates): Return location for an FD to @dfd/@path,
  4495. + * or `-1` on error
  4496. + * @cancellable: (nullable): Cancellable, or %NULL
  4497. + * @error: Return location for a #GError, or %NULL
  4498. + *
  4499. + * Similar to glnx_shutil_mkdir_p_at(), except it opens the resulting directory
  4500. + * and returns a directory FD to it. Currently, this is not guaranteed to be
  4501. + * race-free.
  4502. + *
  4503. + * Returns: %TRUE on success, %FALSE otherwise
  4504. + * Since: UNRELEASED
  4505. + */
  4506. +gboolean
  4507. +glnx_shutil_mkdir_p_at_open (int dfd,
  4508. + const char *path,
  4509. + int mode,
  4510. + int *out_dfd,
  4511. + GCancellable *cancellable,
  4512. + GError **error)
  4513. +{
  4514. + /* FIXME: It’s not possible to eliminate the race here until
  4515. + * openat(O_DIRECTORY | O_CREAT) works (and returns a directory rather than a
  4516. + * file). It appears to be not supported in current kernels. (Tested with
  4517. + * 4.10.10-200.fc25.x86_64.) */
  4518. + *out_dfd = -1;
  4519. +
  4520. + if (!glnx_shutil_mkdir_p_at (dfd, path, mode, cancellable, error))
  4521. + return FALSE;
  4522. +
  4523. + return glnx_opendirat (dfd, path, TRUE, out_dfd, error);
  4524. +}
  4525. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-shutil.h flatpak-builder-0.10.6/libglnx/glnx-shutil.h
  4526. --- flatpak-builder-0.10.6.orig/libglnx/glnx-shutil.h 1970-01-01 02:00:00.000000000 +0200
  4527. +++ flatpak-builder-0.10.6/libglnx/glnx-shutil.h 2018-02-11 12:03:43.449373307 +0300
  4528. @@ -0,0 +1,48 @@
  4529. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4530. + *
  4531. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  4532. + *
  4533. + * This library is free software; you can redistribute it and/or
  4534. + * modify it under the terms of the GNU Lesser General Public
  4535. + * License as published by the Free Software Foundation; either
  4536. + * version 2 of the License, or (at your option) any later version.
  4537. + *
  4538. + * This library is distributed in the hope that it will be useful,
  4539. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4540. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4541. + * Lesser General Public License for more details.
  4542. + *
  4543. + * You should have received a copy of the GNU Lesser General Public
  4544. + * License along with this library; if not, write to the
  4545. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4546. + * Boston, MA 02111-1307, USA.
  4547. + */
  4548. +
  4549. +#pragma once
  4550. +
  4551. +#include <glnx-dirfd.h>
  4552. +
  4553. +G_BEGIN_DECLS
  4554. +
  4555. +gboolean
  4556. +glnx_shutil_rm_rf_at (int dfd,
  4557. + const char *path,
  4558. + GCancellable *cancellable,
  4559. + GError **error);
  4560. +
  4561. +gboolean
  4562. +glnx_shutil_mkdir_p_at (int dfd,
  4563. + const char *path,
  4564. + int mode,
  4565. + GCancellable *cancellable,
  4566. + GError **error);
  4567. +
  4568. +gboolean
  4569. +glnx_shutil_mkdir_p_at_open (int dfd,
  4570. + const char *path,
  4571. + int mode,
  4572. + int *out_dfd,
  4573. + GCancellable *cancellable,
  4574. + GError **error);
  4575. +
  4576. +G_END_DECLS
  4577. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-xattrs.c flatpak-builder-0.10.6/libglnx/glnx-xattrs.c
  4578. --- flatpak-builder-0.10.6.orig/libglnx/glnx-xattrs.c 1970-01-01 02:00:00.000000000 +0200
  4579. +++ flatpak-builder-0.10.6/libglnx/glnx-xattrs.c 2018-02-11 12:03:43.449373307 +0300
  4580. @@ -0,0 +1,444 @@
  4581. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  4582. + *
  4583. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  4584. + *
  4585. + * This library is free software; you can redistribute it and/or
  4586. + * modify it under the terms of the GNU Lesser General Public
  4587. + * License as published by the Free Software Foundation; either
  4588. + * version 2 of the License, or (at your option) any later version.
  4589. + *
  4590. + * This library is distributed in the hope that it will be useful,
  4591. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4592. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4593. + * Lesser General Public License for more details.
  4594. + *
  4595. + * You should have received a copy of the GNU Lesser General Public
  4596. + * License along with this library; if not, write to the
  4597. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4598. + * Boston, MA 02111-1307, USA.
  4599. + */
  4600. +
  4601. +#include "config.h"
  4602. +
  4603. +#include <string.h>
  4604. +#include <stdio.h>
  4605. +
  4606. +#include <glnx-macros.h>
  4607. +#include <glnx-xattrs.h>
  4608. +#include <glnx-errors.h>
  4609. +#include <glnx-local-alloc.h>
  4610. +
  4611. +static GVariant *
  4612. +variant_new_ay_bytes (GBytes *bytes)
  4613. +{
  4614. + gsize size;
  4615. + gconstpointer data;
  4616. + data = g_bytes_get_data (bytes, &size);
  4617. + g_bytes_ref (bytes);
  4618. + return g_variant_new_from_data (G_VARIANT_TYPE ("ay"), data, size,
  4619. + TRUE, (GDestroyNotify)g_bytes_unref, bytes);
  4620. +}
  4621. +
  4622. +static char *
  4623. +canonicalize_xattrs (char *xattr_string,
  4624. + size_t len)
  4625. +{
  4626. + char *p;
  4627. + GSList *xattrs = NULL;
  4628. + GSList *iter;
  4629. + GString *result;
  4630. +
  4631. + result = g_string_new (0);
  4632. +
  4633. + p = xattr_string;
  4634. + while (p < xattr_string+len)
  4635. + {
  4636. + xattrs = g_slist_prepend (xattrs, p);
  4637. + p += strlen (p) + 1;
  4638. + }
  4639. +
  4640. + xattrs = g_slist_sort (xattrs, (GCompareFunc) strcmp);
  4641. + for (iter = xattrs; iter; iter = iter->next) {
  4642. + g_string_append (result, iter->data);
  4643. + g_string_append_c (result, '\0');
  4644. + }
  4645. +
  4646. + g_slist_free (xattrs);
  4647. + return g_string_free (result, FALSE);
  4648. +}
  4649. +
  4650. +static gboolean
  4651. +read_xattr_name_array (const char *path,
  4652. + int fd,
  4653. + const char *xattrs,
  4654. + size_t len,
  4655. + GVariantBuilder *builder,
  4656. + GError **error)
  4657. +{
  4658. + gboolean ret = FALSE;
  4659. + const char *p;
  4660. + int r;
  4661. + const char *funcstr;
  4662. +
  4663. + g_assert (path != NULL || fd != -1);
  4664. +
  4665. + funcstr = fd != -1 ? "fgetxattr" : "lgetxattr";
  4666. +
  4667. + for (p = xattrs; p < xattrs+len; p = p + strlen (p) + 1)
  4668. + {
  4669. + ssize_t bytes_read;
  4670. + g_autofree char *buf = NULL;
  4671. + g_autoptr(GBytes) bytes = NULL;
  4672. +
  4673. + again:
  4674. + if (fd != -1)
  4675. + bytes_read = fgetxattr (fd, p, NULL, 0);
  4676. + else
  4677. + bytes_read = lgetxattr (path, p, NULL, 0);
  4678. + if (bytes_read < 0)
  4679. + {
  4680. + if (errno == ENODATA)
  4681. + continue;
  4682. +
  4683. + glnx_set_prefix_error_from_errno (error, "%s", funcstr);
  4684. + goto out;
  4685. + }
  4686. + if (bytes_read == 0)
  4687. + continue;
  4688. +
  4689. + buf = g_malloc (bytes_read);
  4690. + if (fd != -1)
  4691. + r = fgetxattr (fd, p, buf, bytes_read);
  4692. + else
  4693. + r = lgetxattr (path, p, buf, bytes_read);
  4694. + if (r < 0)
  4695. + {
  4696. + if (errno == ERANGE)
  4697. + {
  4698. + g_free (g_steal_pointer (&buf));
  4699. + goto again;
  4700. + }
  4701. + else if (errno == ENODATA)
  4702. + continue;
  4703. +
  4704. + glnx_set_prefix_error_from_errno (error, "%s", funcstr);
  4705. + goto out;
  4706. + }
  4707. +
  4708. + bytes = g_bytes_new_take (g_steal_pointer (&buf), bytes_read);
  4709. + g_variant_builder_add (builder, "(@ay@ay)",
  4710. + g_variant_new_bytestring (p),
  4711. + variant_new_ay_bytes (bytes));
  4712. + }
  4713. +
  4714. + ret = TRUE;
  4715. + out:
  4716. + return ret;
  4717. +}
  4718. +
  4719. +static gboolean
  4720. +get_xattrs_impl (const char *path,
  4721. + int fd,
  4722. + GVariant **out_xattrs,
  4723. + GCancellable *cancellable,
  4724. + GError **error)
  4725. +{
  4726. + gboolean ret = FALSE;
  4727. + ssize_t bytes_read, real_size;
  4728. + g_autofree char *xattr_names = NULL;
  4729. + g_autofree char *xattr_names_canonical = NULL;
  4730. + GVariantBuilder builder;
  4731. + gboolean builder_initialized = FALSE;
  4732. + g_autoptr(GVariant) ret_xattrs = NULL;
  4733. +
  4734. + g_assert (path != NULL || fd != -1);
  4735. +
  4736. + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ayay)"));
  4737. + builder_initialized = TRUE;
  4738. +
  4739. + again:
  4740. + if (path)
  4741. + bytes_read = llistxattr (path, NULL, 0);
  4742. + else
  4743. + bytes_read = flistxattr (fd, NULL, 0);
  4744. +
  4745. + if (bytes_read < 0)
  4746. + {
  4747. + if (errno != ENOTSUP)
  4748. + {
  4749. + glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
  4750. + goto out;
  4751. + }
  4752. + }
  4753. + else if (bytes_read > 0)
  4754. + {
  4755. + xattr_names = g_malloc (bytes_read);
  4756. + if (path)
  4757. + real_size = llistxattr (path, xattr_names, bytes_read);
  4758. + else
  4759. + real_size = flistxattr (fd, xattr_names, bytes_read);
  4760. + if (real_size < 0)
  4761. + {
  4762. + if (errno == ERANGE)
  4763. + {
  4764. + g_free (xattr_names);
  4765. + goto again;
  4766. + }
  4767. + glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
  4768. + goto out;
  4769. + }
  4770. + else if (real_size > 0)
  4771. + {
  4772. + xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
  4773. +
  4774. + if (!read_xattr_name_array (path, fd, xattr_names_canonical, real_size, &builder, error))
  4775. + goto out;
  4776. + }
  4777. + }
  4778. +
  4779. + ret_xattrs = g_variant_builder_end (&builder);
  4780. + builder_initialized = FALSE;
  4781. + g_variant_ref_sink (ret_xattrs);
  4782. +
  4783. + ret = TRUE;
  4784. + if (out_xattrs)
  4785. + *out_xattrs = g_steal_pointer (&ret_xattrs);
  4786. + out:
  4787. + if (!builder_initialized)
  4788. + g_variant_builder_clear (&builder);
  4789. + return ret;
  4790. +}
  4791. +
  4792. +/**
  4793. + * glnx_fd_get_all_xattrs:
  4794. + * @fd: a file descriptor
  4795. + * @out_xattrs: (out): A new #GVariant containing the extended attributes
  4796. + * @cancellable: Cancellable
  4797. + * @error: Error
  4798. + *
  4799. + * Read all extended attributes from @fd in a canonical sorted order, and
  4800. + * set @out_xattrs with the result.
  4801. + *
  4802. + * If the filesystem does not support extended attributes, @out_xattrs
  4803. + * will have 0 elements, and this function will return successfully.
  4804. + */
  4805. +gboolean
  4806. +glnx_fd_get_all_xattrs (int fd,
  4807. + GVariant **out_xattrs,
  4808. + GCancellable *cancellable,
  4809. + GError **error)
  4810. +{
  4811. + return get_xattrs_impl (NULL, fd, out_xattrs,
  4812. + cancellable, error);
  4813. +}
  4814. +
  4815. +/**
  4816. + * glnx_dfd_name_get_all_xattrs:
  4817. + * @dfd: Parent directory file descriptor
  4818. + * @name: File name
  4819. + * @out_xattrs: (out): Extended attribute set
  4820. + * @cancellable: Cancellable
  4821. + * @error: Error
  4822. + *
  4823. + * Load all extended attributes for the file named @name residing in
  4824. + * directory @dfd.
  4825. + */
  4826. +gboolean
  4827. +glnx_dfd_name_get_all_xattrs (int dfd,
  4828. + const char *name,
  4829. + GVariant **out_xattrs,
  4830. + GCancellable *cancellable,
  4831. + GError **error)
  4832. +{
  4833. + if (G_IN_SET(dfd, AT_FDCWD, -1))
  4834. + {
  4835. + return get_xattrs_impl (name, -1, out_xattrs, cancellable, error);
  4836. + }
  4837. + else
  4838. + {
  4839. + char buf[PATH_MAX];
  4840. + /* A workaround for the lack of lgetxattrat(), thanks to Florian Weimer:
  4841. + * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
  4842. + */
  4843. + snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
  4844. + return get_xattrs_impl (buf, -1, out_xattrs, cancellable, error);
  4845. + }
  4846. +}
  4847. +
  4848. +static gboolean
  4849. +set_all_xattrs_for_path (const char *path,
  4850. + GVariant *xattrs,
  4851. + GCancellable *cancellable,
  4852. + GError **error)
  4853. +{
  4854. + const guint n = g_variant_n_children (xattrs);
  4855. + for (guint i = 0; i < n; i++)
  4856. + {
  4857. + const guint8* name;
  4858. + g_autoptr(GVariant) value = NULL;
  4859. + g_variant_get_child (xattrs, i, "(^&ay@ay)",
  4860. + &name, &value);
  4861. +
  4862. + gsize value_len;
  4863. + const guint8* value_data = g_variant_get_fixed_array (value, &value_len, 1);
  4864. +
  4865. + if (lsetxattr (path, (char*)name, (char*)value_data, value_len, 0) < 0)
  4866. + return glnx_throw_errno_prefix (error, "lsetxattr");
  4867. + }
  4868. +
  4869. + return TRUE;
  4870. +}
  4871. +
  4872. +/**
  4873. + * glnx_dfd_name_set_all_xattrs:
  4874. + * @dfd: Parent directory file descriptor
  4875. + * @name: File name
  4876. + * @xattrs: Extended attribute set
  4877. + * @cancellable: Cancellable
  4878. + * @error: Error
  4879. + *
  4880. + * Set all extended attributes for the file named @name residing in
  4881. + * directory @dfd.
  4882. + */
  4883. +gboolean
  4884. +glnx_dfd_name_set_all_xattrs (int dfd,
  4885. + const char *name,
  4886. + GVariant *xattrs,
  4887. + GCancellable *cancellable,
  4888. + GError **error)
  4889. +{
  4890. + if (G_IN_SET(dfd, AT_FDCWD, -1))
  4891. + {
  4892. + return set_all_xattrs_for_path (name, xattrs, cancellable, error);
  4893. + }
  4894. + else
  4895. + {
  4896. + char buf[PATH_MAX];
  4897. + /* A workaround for the lack of lsetxattrat(), thanks to Florian Weimer:
  4898. + * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
  4899. + */
  4900. + snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
  4901. + return set_all_xattrs_for_path (buf, xattrs, cancellable, error);
  4902. + }
  4903. +}
  4904. +
  4905. +/**
  4906. + * glnx_fd_set_all_xattrs:
  4907. + * @fd: File descriptor
  4908. + * @xattrs: Extended attributes
  4909. + * @cancellable: Cancellable
  4910. + * @error: Error
  4911. + *
  4912. + * For each attribute in @xattrs, set its value on the file or
  4913. + * directory referred to by @fd. This function does not remove any
  4914. + * attributes not in @xattrs.
  4915. + */
  4916. +gboolean
  4917. +glnx_fd_set_all_xattrs (int fd,
  4918. + GVariant *xattrs,
  4919. + GCancellable *cancellable,
  4920. + GError **error)
  4921. +{
  4922. + const guint n = g_variant_n_children (xattrs);
  4923. + for (guint i = 0; i < n; i++)
  4924. + {
  4925. + const guint8* name;
  4926. + g_autoptr(GVariant) value = NULL;
  4927. + g_variant_get_child (xattrs, i, "(^&ay@ay)",
  4928. + &name, &value);
  4929. +
  4930. + gsize value_len;
  4931. + const guint8* value_data = g_variant_get_fixed_array (value, &value_len, 1);
  4932. +
  4933. + if (TEMP_FAILURE_RETRY (fsetxattr (fd, (char*)name, (char*)value_data, value_len, 0)) < 0)
  4934. + return glnx_throw_errno_prefix (error, "fsetxattr");
  4935. + }
  4936. +
  4937. + return TRUE;
  4938. +}
  4939. +
  4940. +/**
  4941. + * glnx_lgetxattrat:
  4942. + * @dfd: Directory file descriptor
  4943. + * @subpath: Subpath
  4944. + * @attribute: Extended attribute to retrieve
  4945. + * @error: Error
  4946. + *
  4947. + * Retrieve an extended attribute value, relative to a directory file
  4948. + * descriptor.
  4949. + */
  4950. +GBytes *
  4951. +glnx_lgetxattrat (int dfd,
  4952. + const char *subpath,
  4953. + const char *attribute,
  4954. + GError **error)
  4955. +{
  4956. + char pathbuf[PATH_MAX];
  4957. + snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
  4958. +
  4959. + ssize_t bytes_read, real_size;
  4960. + if (TEMP_FAILURE_RETRY (bytes_read = lgetxattr (pathbuf, attribute, NULL, 0)) < 0)
  4961. + return glnx_null_throw_errno_prefix (error, "lgetxattr");
  4962. +
  4963. + g_autofree guint8 *buf = g_malloc (bytes_read);
  4964. + if (TEMP_FAILURE_RETRY (real_size = lgetxattr (pathbuf, attribute, buf, bytes_read)) < 0)
  4965. + return glnx_null_throw_errno_prefix (error, "lgetxattr");
  4966. +
  4967. + return g_bytes_new_take (g_steal_pointer (&buf), real_size);
  4968. +}
  4969. +
  4970. +/**
  4971. + * glnx_fgetxattr_bytes:
  4972. + * @fd: Directory file descriptor
  4973. + * @attribute: Extended attribute to retrieve
  4974. + * @error: Error
  4975. + *
  4976. + * Returns: (transfer full): An extended attribute value, or %NULL on error
  4977. + */
  4978. +GBytes *
  4979. +glnx_fgetxattr_bytes (int fd,
  4980. + const char *attribute,
  4981. + GError **error)
  4982. +{
  4983. + ssize_t bytes_read, real_size;
  4984. +
  4985. + if (TEMP_FAILURE_RETRY (bytes_read = fgetxattr (fd, attribute, NULL, 0)) < 0)
  4986. + return glnx_null_throw_errno_prefix (error, "fgetxattr");
  4987. +
  4988. + g_autofree guint8 *buf = g_malloc (bytes_read);
  4989. + if (TEMP_FAILURE_RETRY (real_size = fgetxattr (fd, attribute, buf, bytes_read)) < 0)
  4990. + return glnx_null_throw_errno_prefix (error, "fgetxattr");
  4991. +
  4992. + return g_bytes_new_take (g_steal_pointer (&buf), real_size);
  4993. +}
  4994. +
  4995. +/**
  4996. + * glnx_lsetxattrat:
  4997. + * @dfd: Directory file descriptor
  4998. + * @subpath: Path
  4999. + * @attribute: An attribute name
  5000. + * @value: (array length=len) (element-type guint8): Attribute value
  5001. + * @len: Length of @value
  5002. + * @flags: Flags, containing either XATTR_CREATE or XATTR_REPLACE
  5003. + * @error: Error
  5004. + *
  5005. + * Set an extended attribute, relative to a directory file descriptor.
  5006. + */
  5007. +gboolean
  5008. +glnx_lsetxattrat (int dfd,
  5009. + const char *subpath,
  5010. + const char *attribute,
  5011. + const guint8 *value,
  5012. + gsize len,
  5013. + int flags,
  5014. + GError **error)
  5015. +{
  5016. + char pathbuf[PATH_MAX];
  5017. + snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
  5018. +
  5019. + if (TEMP_FAILURE_RETRY (lsetxattr (subpath, attribute, value, len, flags)) < 0)
  5020. + return glnx_throw_errno_prefix (error, "lsetxattr");
  5021. +
  5022. + return TRUE;
  5023. +}
  5024. +
  5025. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/glnx-xattrs.h flatpak-builder-0.10.6/libglnx/glnx-xattrs.h
  5026. --- flatpak-builder-0.10.6.orig/libglnx/glnx-xattrs.h 1970-01-01 02:00:00.000000000 +0200
  5027. +++ flatpak-builder-0.10.6/libglnx/glnx-xattrs.h 2018-02-11 12:03:43.449373307 +0300
  5028. @@ -0,0 +1,78 @@
  5029. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5030. + *
  5031. + * Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
  5032. + *
  5033. + * This library is free software; you can redistribute it and/or
  5034. + * modify it under the terms of the GNU Lesser General Public
  5035. + * License as published by the Free Software Foundation; either
  5036. + * version 2 of the License, or (at your option) any later version.
  5037. + *
  5038. + * This library is distributed in the hope that it will be useful,
  5039. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5040. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5041. + * Lesser General Public License for more details.
  5042. + *
  5043. + * You should have received a copy of the GNU Lesser General Public
  5044. + * License along with this library; if not, write to the
  5045. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5046. + * Boston, MA 02111-1307, USA.
  5047. + */
  5048. +
  5049. +#pragma once
  5050. +
  5051. +#include <glnx-backport-autocleanups.h>
  5052. +#include <limits.h>
  5053. +#include <dirent.h>
  5054. +#include <sys/stat.h>
  5055. +#include <fcntl.h>
  5056. +#include <sys/xattr.h>
  5057. +
  5058. +G_BEGIN_DECLS
  5059. +
  5060. +gboolean
  5061. +glnx_dfd_name_get_all_xattrs (int dfd,
  5062. + const char *name,
  5063. + GVariant **out_xattrs,
  5064. + GCancellable *cancellable,
  5065. + GError **error);
  5066. +
  5067. +gboolean
  5068. +glnx_fd_get_all_xattrs (int fd,
  5069. + GVariant **out_xattrs,
  5070. + GCancellable *cancellable,
  5071. + GError **error);
  5072. +
  5073. +gboolean
  5074. +glnx_dfd_name_set_all_xattrs (int dfd,
  5075. + const char *name,
  5076. + GVariant *xattrs,
  5077. + GCancellable *cancellable,
  5078. + GError **error);
  5079. +
  5080. +gboolean
  5081. +glnx_fd_set_all_xattrs (int fd,
  5082. + GVariant *xattrs,
  5083. + GCancellable *cancellable,
  5084. + GError **error);
  5085. +
  5086. +GBytes *
  5087. +glnx_lgetxattrat (int dfd,
  5088. + const char *subpath,
  5089. + const char *attribute,
  5090. + GError **error);
  5091. +
  5092. +GBytes *
  5093. +glnx_fgetxattr_bytes (int fd,
  5094. + const char *attribute,
  5095. + GError **error);
  5096. +
  5097. +gboolean
  5098. +glnx_lsetxattrat (int dfd,
  5099. + const char *subpath,
  5100. + const char *attribute,
  5101. + const guint8 *value,
  5102. + gsize len,
  5103. + int flags,
  5104. + GError **error);
  5105. +
  5106. +G_END_DECLS
  5107. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/libglnx.doap flatpak-builder-0.10.6/libglnx/libglnx.doap
  5108. --- flatpak-builder-0.10.6.orig/libglnx/libglnx.doap 1970-01-01 02:00:00.000000000 +0200
  5109. +++ flatpak-builder-0.10.6/libglnx/libglnx.doap 2018-02-11 12:03:43.449373307 +0300
  5110. @@ -0,0 +1,31 @@
  5111. +<?xml version="1.0" encoding="UTF-8"?>
  5112. +<Project xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  5113. + xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  5114. + xmlns:foaf="http://xmlns.com/foaf/0.1/"
  5115. + xmlns:gnome="http://api.gnome.org/doap-extensions#"
  5116. + xmlns="http://usefulinc.com/ns/doap#">
  5117. +
  5118. + <name>libglnx</name>
  5119. + <shortname>libglnx</shortname>
  5120. +
  5121. + <shortdesc xml:lang="en">"Copylib" for system service modules using GLib with Linux</shortdesc>
  5122. +
  5123. + <description xml:lang="en">This module is intended for use by
  5124. + infrastructure code using GLib that is also Linux specific, such as
  5125. + ostree, NetworkManager, and others.
  5126. + </description>
  5127. +
  5128. + <license rdf:resource="http://usefulinc.com/doap/licenses/lgpl" />
  5129. + <mailing-list rdf:resource="mailto:desktop-devel-list@gnome.org" />
  5130. +
  5131. + <programming-language>C</programming-language>
  5132. +
  5133. + <maintainer>
  5134. + <foaf:Person>
  5135. + <foaf:name>Colin Walters</foaf:name>
  5136. + <foaf:mbox rdf:resource="mailto:walters@verbum.org"/>
  5137. + <gnome:userid>walters</gnome:userid>
  5138. + </foaf:Person>
  5139. + </maintainer>
  5140. +
  5141. +</Project>
  5142. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/libglnx.h flatpak-builder-0.10.6/libglnx/libglnx.h
  5143. --- flatpak-builder-0.10.6.orig/libglnx/libglnx.h 1970-01-01 02:00:00.000000000 +0200
  5144. +++ flatpak-builder-0.10.6/libglnx/libglnx.h 2018-02-11 12:03:43.449373307 +0300
  5145. @@ -0,0 +1,39 @@
  5146. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5147. + *
  5148. + * Copyright (C) 2012,2013,2015 Colin Walters <walters@verbum.org>.
  5149. + *
  5150. + * This library is free software; you can redistribute it and/or
  5151. + * modify it under the terms of the GNU Lesser General Public
  5152. + * License as published by the Free Software Foundation; either
  5153. + * version 2 of the License, or (at your option) any later version.
  5154. + *
  5155. + * This library is distributed in the hope that it will be useful,
  5156. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5157. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5158. + * Lesser General Public License for more details.
  5159. + *
  5160. + * You should have received a copy of the GNU Lesser General Public
  5161. + * License along with this library; if not, write to the
  5162. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5163. + * Boston, MA 02111-1307, USA.
  5164. + */
  5165. +
  5166. +#pragma once
  5167. +
  5168. +#include <gio/gio.h>
  5169. +
  5170. +G_BEGIN_DECLS
  5171. +
  5172. +#include <glnx-macros.h>
  5173. +#include <glnx-local-alloc.h>
  5174. +#include <glnx-backport-autocleanups.h>
  5175. +#include <glnx-backports.h>
  5176. +#include <glnx-lockfile.h>
  5177. +#include <glnx-errors.h>
  5178. +#include <glnx-dirfd.h>
  5179. +#include <glnx-shutil.h>
  5180. +#include <glnx-xattrs.h>
  5181. +#include <glnx-console.h>
  5182. +#include <glnx-fdio.h>
  5183. +
  5184. +G_END_DECLS
  5185. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/libglnx.m4 flatpak-builder-0.10.6/libglnx/libglnx.m4
  5186. --- flatpak-builder-0.10.6.orig/libglnx/libglnx.m4 1970-01-01 02:00:00.000000000 +0200
  5187. +++ flatpak-builder-0.10.6/libglnx/libglnx.m4 2018-02-11 12:03:43.449373307 +0300
  5188. @@ -0,0 +1,31 @@
  5189. +AC_DEFUN([LIBGLNX_CONFIGURE],
  5190. +[
  5191. +AC_CHECK_DECLS([
  5192. + renameat2,
  5193. + ],
  5194. + [], [], [[
  5195. +#include <sys/types.h>
  5196. +#include <unistd.h>
  5197. +#include <sys/mount.h>
  5198. +#include <fcntl.h>
  5199. +#include <sched.h>
  5200. +#include <linux/loop.h>
  5201. +#include <linux/random.h>
  5202. +]])
  5203. +
  5204. +AC_ARG_ENABLE(otmpfile,
  5205. + [AS_HELP_STRING([--disable-otmpfile],
  5206. + [Disable use of O_TMPFILE [default=no]])],,
  5207. + [enable_otmpfile=yes])
  5208. +AS_IF([test $enable_otmpfile = yes], [], [
  5209. + AC_DEFINE([DISABLE_OTMPFILE], 1, [Define if we should avoid using O_TMPFILE])])
  5210. +
  5211. +AC_ARG_ENABLE(wrpseudo-compat,
  5212. + [AS_HELP_STRING([--enable-wrpseudo-compat],
  5213. + [Disable use syscall() and filesystem calls to for compatibility with wrpseudo [default=no]])],,
  5214. + [enable_wrpseudo_compat=no])
  5215. +AS_IF([test $enable_wrpseudo_compat = no], [], [
  5216. + AC_DEFINE([ENABLE_WRPSEUDO_COMPAT], 1, [Define if we should be compatible with wrpseudo])])
  5217. +
  5218. +dnl end LIBGLNX_CONFIGURE
  5219. +])
  5220. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/Makefile-libglnx.am flatpak-builder-0.10.6/libglnx/Makefile-libglnx.am
  5221. --- flatpak-builder-0.10.6.orig/libglnx/Makefile-libglnx.am 1970-01-01 02:00:00.000000000 +0200
  5222. +++ flatpak-builder-0.10.6/libglnx/Makefile-libglnx.am 2018-02-11 12:03:43.448373307 +0300
  5223. @@ -0,0 +1,73 @@
  5224. +# Copyright (C) 2015 Colin Walters <walters@verbum.org>
  5225. +#
  5226. +# This library is free software; you can redistribute it and/or
  5227. +# modify it under the terms of the GNU Lesser General Public
  5228. +# License as published by the Free Software Foundation; either
  5229. +# version 2 of the License, or (at your option) any later version.
  5230. +#
  5231. +# This library is distributed in the hope that it will be useful,
  5232. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  5233. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5234. +# Lesser General Public License for more details.
  5235. +#
  5236. +# You should have received a copy of the GNU Lesser General Public
  5237. +# License along with this library; if not, write to the
  5238. +# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5239. +# Boston, MA 02111-1307, USA.
  5240. +
  5241. +EXTRA_DIST += \
  5242. + $(libglnx_srcpath)/README.md \
  5243. + $(libglnx_srcpath)/COPYING \
  5244. + $(libglnx_srcpath)/libglnx.m4 \
  5245. + $(NULL)
  5246. +
  5247. +libglnx_la_SOURCES = \
  5248. + $(libglnx_srcpath)/glnx-macros.h \
  5249. + $(libglnx_srcpath)/glnx-backport-autocleanups.h \
  5250. + $(libglnx_srcpath)/glnx-backport-autoptr.h \
  5251. + $(libglnx_srcpath)/glnx-backports.h \
  5252. + $(libglnx_srcpath)/glnx-backports.c \
  5253. + $(libglnx_srcpath)/glnx-local-alloc.h \
  5254. + $(libglnx_srcpath)/glnx-local-alloc.c \
  5255. + $(libglnx_srcpath)/glnx-errors.h \
  5256. + $(libglnx_srcpath)/glnx-errors.c \
  5257. + $(libglnx_srcpath)/glnx-console.h \
  5258. + $(libglnx_srcpath)/glnx-console.c \
  5259. + $(libglnx_srcpath)/glnx-dirfd.h \
  5260. + $(libglnx_srcpath)/glnx-dirfd.c \
  5261. + $(libglnx_srcpath)/glnx-fdio.h \
  5262. + $(libglnx_srcpath)/glnx-fdio.c \
  5263. + $(libglnx_srcpath)/glnx-lockfile.h \
  5264. + $(libglnx_srcpath)/glnx-lockfile.c \
  5265. + $(libglnx_srcpath)/glnx-missing-syscall.h \
  5266. + $(libglnx_srcpath)/glnx-missing.h \
  5267. + $(libglnx_srcpath)/glnx-xattrs.h \
  5268. + $(libglnx_srcpath)/glnx-xattrs.c \
  5269. + $(libglnx_srcpath)/glnx-shutil.h \
  5270. + $(libglnx_srcpath)/glnx-shutil.c \
  5271. + $(libglnx_srcpath)/libglnx.h \
  5272. + $(NULL)
  5273. +
  5274. +libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  5275. +libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic
  5276. +libglnx_la_LIBADD = $(libglnx_libs)
  5277. +
  5278. +libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors test-libglnx-macros
  5279. +TESTS += $(libglnx_tests)
  5280. +
  5281. +check_PROGRAMS += $(libglnx_tests)
  5282. +test_libglnx_xattrs_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-xattrs.c
  5283. +test_libglnx_xattrs_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  5284. +test_libglnx_xattrs_LDADD = $(libglnx_libs) libglnx.la
  5285. +
  5286. +test_libglnx_fdio_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-fdio.c
  5287. +test_libglnx_fdio_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  5288. +test_libglnx_fdio_LDADD = $(libglnx_libs) libglnx.la
  5289. +
  5290. +test_libglnx_errors_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-errors.c
  5291. +test_libglnx_errors_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  5292. +test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la
  5293. +
  5294. +test_libglnx_macros_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-macros.c
  5295. +test_libglnx_macros_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
  5296. +test_libglnx_macros_LDADD = $(libglnx_libs) libglnx.la
  5297. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/README.md flatpak-builder-0.10.6/libglnx/README.md
  5298. --- flatpak-builder-0.10.6.orig/libglnx/README.md 1970-01-01 02:00:00.000000000 +0200
  5299. +++ flatpak-builder-0.10.6/libglnx/README.md 2018-02-11 12:03:43.448373307 +0300
  5300. @@ -0,0 +1,52 @@
  5301. +libglnx is the successor to libgsystem: https://git.gnome.org/browse/libgsystem
  5302. +
  5303. +It is for modules which depend on both GLib and Linux, intended to be
  5304. +used as a git submodule.
  5305. +
  5306. +Features:
  5307. +
  5308. + - File APIs which use `openat()` like APIs, but also take a `GCancellable`
  5309. + to support dynamic cancellation
  5310. + - APIs also have a `GError` parameter
  5311. + - High level "shutil", somewhat inspired by Python's
  5312. + - A "console" API for tty output
  5313. + - A backport of the GLib cleanup macros for projects which can't yet take
  5314. + a dependency on 2.40.
  5315. +
  5316. +Why?
  5317. +----
  5318. +
  5319. +There are multiple projects which have a hard dependency on Linux and
  5320. +GLib, such as NetworkManager, ostree, flatpak, etc. It makes sense
  5321. +for them to be able to share Linux-specific APIs.
  5322. +
  5323. +This module also contains some code taken from systemd, which has very
  5324. +high quality LGPLv2+ shared library code, but most of the internal
  5325. +shared library is private, and not namespaced.
  5326. +
  5327. +One could also compare this project to gnulib; the salient differences
  5328. +there are that at least some of this module is eventually destined for
  5329. +inclusion in GLib.
  5330. +
  5331. +Porting from libgsystem
  5332. +-----------------------
  5333. +
  5334. +For all of the filesystem access code, libglnx exposes only
  5335. +fd-relative API, not `GFile*`. It does use `GCancellable` where
  5336. +applicable.
  5337. +
  5338. +For local allocation macros, you should start using the `g_auto`
  5339. +macros from GLib. A backport is included in libglnx. There are a few
  5340. +APIs not defined in GLib yet, such as `glnx_fd_close`.
  5341. +
  5342. +`gs_transfer_out_value` is replaced by `g_steal_pointer`.
  5343. +
  5344. +Contributing
  5345. +------------
  5346. +
  5347. +Currently there is not a Bugzilla product - one may be created
  5348. +in the future. You can submit PRs against the Github mirror:
  5349. +
  5350. +https://github.com/GNOME/libglnx/pulls
  5351. +
  5352. +Or alternatively, email one of the maintainers directly.
  5353. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-errors.c flatpak-builder-0.10.6/libglnx/tests/test-libglnx-errors.c
  5354. --- flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-errors.c 1970-01-01 02:00:00.000000000 +0200
  5355. +++ flatpak-builder-0.10.6/libglnx/tests/test-libglnx-errors.c 2018-02-11 12:03:43.450373307 +0300
  5356. @@ -0,0 +1,183 @@
  5357. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5358. + *
  5359. + * Copyright (C) 2017 Red Hat, Inc.
  5360. + *
  5361. + * This library is free software; you can redistribute it and/or
  5362. + * modify it under the terms of the GNU Lesser General Public
  5363. + * License as published by the Free Software Foundation; either
  5364. + * version 2 of the License, or (at your option) any later version.
  5365. + *
  5366. + * This library is distributed in the hope that it will be useful,
  5367. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5368. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5369. + * Lesser General Public License for more details.
  5370. + *
  5371. + * You should have received a copy of the GNU Lesser General Public
  5372. + * License along with this library; if not, write to the
  5373. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5374. + * Boston, MA 02111-1307, USA.
  5375. + */
  5376. +
  5377. +#include "config.h"
  5378. +#include "libglnx.h"
  5379. +#include <glib.h>
  5380. +#include <stdlib.h>
  5381. +#include <gio/gio.h>
  5382. +#include <string.h>
  5383. +
  5384. +static void
  5385. +test_error_throw (void)
  5386. +{
  5387. + g_autoptr(GError) error = NULL;
  5388. +
  5389. + g_assert (!glnx_throw (&error, "foo: %s %d", "hello", 42));
  5390. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  5391. + g_assert_cmpstr (error->message, ==, "foo: hello 42");
  5392. + g_clear_error (&error);
  5393. +
  5394. + gpointer dummy = glnx_null_throw (&error, "literal foo");
  5395. + g_assert (dummy == NULL);
  5396. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  5397. + g_assert_cmpstr (error->message, ==, "literal foo");
  5398. + g_clear_error (&error);
  5399. +
  5400. + gpointer dummy2 = glnx_null_throw (&error, "foo: %s %d", "hola", 24);
  5401. + g_assert (dummy2 == NULL);
  5402. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  5403. + g_assert_cmpstr (error->message, ==, "foo: hola 24");
  5404. + g_clear_error (&error);
  5405. +}
  5406. +
  5407. +static void
  5408. +test_error_errno (void)
  5409. +{
  5410. + g_autoptr(GError) error = NULL;
  5411. + const char noent_path[] = "/enoent-this-should-not-exist";
  5412. + int fd;
  5413. +
  5414. + fd = open (noent_path, O_RDONLY);
  5415. + if (fd < 0)
  5416. + {
  5417. + g_assert (!glnx_throw_errno (&error));
  5418. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5419. + g_assert (!glnx_prefix_error (&error, "myprefix"));
  5420. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5421. + g_assert (g_str_has_prefix (error->message, "myprefix: "));
  5422. + g_clear_error (&error);
  5423. + }
  5424. + else
  5425. + g_assert_cmpint (fd, ==, -1);
  5426. +
  5427. + fd = open (noent_path, O_RDONLY);
  5428. + if (fd < 0)
  5429. + {
  5430. + gpointer dummy = glnx_null_throw_errno (&error);
  5431. + g_assert (dummy == NULL);
  5432. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5433. + dummy = glnx_prefix_error_null (&error, "myprefix");
  5434. + g_assert (dummy == NULL);
  5435. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5436. + g_assert (g_str_has_prefix (error->message, "myprefix: "));
  5437. + g_clear_error (&error);
  5438. + }
  5439. + else
  5440. + g_assert_cmpint (fd, ==, -1);
  5441. +
  5442. + fd = open (noent_path, O_RDONLY);
  5443. + if (fd < 0)
  5444. + {
  5445. + g_autofree char *expected_prefix = g_strdup_printf ("Failed to open %s", noent_path);
  5446. + g_assert (!glnx_throw_errno_prefix (&error, "Failed to open %s", noent_path));
  5447. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5448. + g_assert (g_str_has_prefix (error->message, expected_prefix));
  5449. + g_clear_error (&error);
  5450. + /* And test the legacy wrapper */
  5451. + glnx_set_prefix_error_from_errno (&error, "Failed to open %s", noent_path);
  5452. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5453. + g_assert (g_str_has_prefix (error->message, expected_prefix));
  5454. + g_clear_error (&error);
  5455. + }
  5456. + else
  5457. + g_assert_cmpint (fd, ==, -1);
  5458. +
  5459. + fd = open (noent_path, O_RDONLY);
  5460. + if (fd < 0)
  5461. + {
  5462. + gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open file");
  5463. + g_assert (dummy == NULL);
  5464. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5465. + g_assert (g_str_has_prefix (error->message, "Failed to open file"));
  5466. + g_clear_error (&error);
  5467. + }
  5468. + else
  5469. + g_assert_cmpint (fd, ==, -1);
  5470. +
  5471. + fd = open (noent_path, O_RDONLY);
  5472. + if (fd < 0)
  5473. + {
  5474. + gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open %s", noent_path);
  5475. + g_assert (dummy == NULL);
  5476. + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
  5477. + g_assert (g_str_has_prefix (error->message, glnx_strjoina ("Failed to open ", noent_path)));
  5478. + g_clear_error (&error);
  5479. + }
  5480. + else
  5481. + g_assert_cmpint (fd, ==, -1);
  5482. +}
  5483. +
  5484. +static void
  5485. +test_error_auto_nothrow (GError **error)
  5486. +{
  5487. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  5488. + /* Side effect to avoid otherwise empty function */
  5489. + g_assert_no_error (*error);
  5490. +}
  5491. +
  5492. +static void
  5493. +test_error_auto_throw (GError **error)
  5494. +{
  5495. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  5496. + (void) glnx_throw (error, "oops");
  5497. +}
  5498. +
  5499. +static void
  5500. +test_error_auto_throw_recurse (GError **error)
  5501. +{
  5502. + GLNX_AUTO_PREFIX_ERROR("foo", error);
  5503. +
  5504. + if (TRUE)
  5505. + {
  5506. + GLNX_AUTO_PREFIX_ERROR("bar", error);
  5507. + (void) glnx_throw (error, "oops");
  5508. + }
  5509. +}
  5510. +
  5511. +static void
  5512. +test_error_auto (void)
  5513. +{
  5514. + g_autoptr(GError) error = NULL;
  5515. + test_error_auto_nothrow (&error);
  5516. + g_assert_no_error (error);
  5517. + test_error_auto_throw (&error);
  5518. + g_assert_nonnull (error);
  5519. + g_assert_cmpstr (error->message, ==, "foo: oops");
  5520. + g_clear_error (&error);
  5521. + test_error_auto_throw_recurse (&error);
  5522. + g_assert_nonnull (error);
  5523. + g_assert_cmpstr (error->message, ==, "foo: bar: oops");
  5524. +}
  5525. +
  5526. +int main (int argc, char **argv)
  5527. +{
  5528. + int ret;
  5529. +
  5530. + g_test_init (&argc, &argv, NULL);
  5531. +
  5532. + g_test_add_func ("/error-throw", test_error_throw);
  5533. + g_test_add_func ("/error-errno", test_error_errno);
  5534. + g_test_add_func ("/error-auto", test_error_auto);
  5535. +
  5536. + ret = g_test_run();
  5537. +
  5538. + return ret;
  5539. +}
  5540. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-fdio.c flatpak-builder-0.10.6/libglnx/tests/test-libglnx-fdio.c
  5541. --- flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-fdio.c 1970-01-01 02:00:00.000000000 +0200
  5542. +++ flatpak-builder-0.10.6/libglnx/tests/test-libglnx-fdio.c 2018-02-11 12:03:43.450373307 +0300
  5543. @@ -0,0 +1,206 @@
  5544. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5545. + *
  5546. + * Copyright (C) 2017 Red Hat, Inc.
  5547. + *
  5548. + * This library is free software; you can redistribute it and/or
  5549. + * modify it under the terms of the GNU Lesser General Public
  5550. + * License as published by the Free Software Foundation; either
  5551. + * version 2 of the License, or (at your option) any later version.
  5552. + *
  5553. + * This library is distributed in the hope that it will be useful,
  5554. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5555. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5556. + * Lesser General Public License for more details.
  5557. + *
  5558. + * You should have received a copy of the GNU Lesser General Public
  5559. + * License along with this library; if not, write to the
  5560. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5561. + * Boston, MA 02111-1307, USA.
  5562. + */
  5563. +
  5564. +#include "config.h"
  5565. +#include "libglnx.h"
  5566. +#include <glib.h>
  5567. +#include <stdlib.h>
  5568. +#include <gio/gio.h>
  5569. +#include <err.h>
  5570. +#include <string.h>
  5571. +
  5572. +static gboolean
  5573. +renameat_test_setup (int *out_srcfd, int *out_destfd,
  5574. + GError **error)
  5575. +{
  5576. + glnx_fd_close int srcfd = -1;
  5577. + glnx_fd_close int destfd = -1;
  5578. +
  5579. + (void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL);
  5580. + if (mkdir ("srcdir", 0755) < 0)
  5581. + err (1, "mkdir");
  5582. + if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error))
  5583. + return FALSE;
  5584. + (void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL);
  5585. + if (mkdir ("destdir", 0755) < 0)
  5586. + err (1, "mkdir");
  5587. + if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error))
  5588. + return FALSE;
  5589. +
  5590. + if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"),
  5591. + GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
  5592. + return FALSE;
  5593. + if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"),
  5594. + GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
  5595. + return FALSE;
  5596. +
  5597. + *out_srcfd = srcfd; srcfd = -1;
  5598. + *out_destfd = destfd; destfd = -1;
  5599. + return TRUE;
  5600. +}
  5601. +
  5602. +static void
  5603. +test_renameat2_noreplace (void)
  5604. +{
  5605. + g_autoptr(GError) local_error = NULL;
  5606. + GError **error = &local_error;
  5607. + glnx_fd_close int srcfd = -1;
  5608. + glnx_fd_close int destfd = -1;
  5609. + struct stat stbuf;
  5610. +
  5611. + if (!renameat_test_setup (&srcfd, &destfd, error))
  5612. + goto out;
  5613. +
  5614. + if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0)
  5615. + g_assert_not_reached ();
  5616. + else
  5617. + {
  5618. + g_assert_cmpint (errno, ==, EEXIST);
  5619. + }
  5620. +
  5621. + if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0)
  5622. + {
  5623. + glnx_set_error_from_errno (error);
  5624. + goto out;
  5625. + }
  5626. + if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
  5627. + goto out;
  5628. +
  5629. + if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
  5630. + g_assert_not_reached ();
  5631. + else
  5632. + g_assert_cmpint (errno, ==, ENOENT);
  5633. +
  5634. + out:
  5635. + g_assert_no_error (local_error);
  5636. +}
  5637. +
  5638. +static void
  5639. +test_renameat2_exchange (void)
  5640. +{
  5641. + g_autoptr(GError) local_error = NULL;
  5642. + GError **error = &local_error;
  5643. + glnx_fd_close int srcfd = -1;
  5644. + glnx_fd_close int destfd = -1;
  5645. + struct stat stbuf;
  5646. +
  5647. + if (!renameat_test_setup (&srcfd, &destfd, error))
  5648. + goto out;
  5649. +
  5650. + if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0)
  5651. + {
  5652. + glnx_set_error_from_errno (error);
  5653. + goto out;
  5654. + }
  5655. +
  5656. + /* Ensure the dir fds are the same */
  5657. + if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
  5658. + {
  5659. + glnx_set_error_from_errno (error);
  5660. + goto out;
  5661. + }
  5662. + if (fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
  5663. + {
  5664. + glnx_set_error_from_errno (error);
  5665. + goto out;
  5666. + }
  5667. + /* But the dirs should be swapped */
  5668. + if (fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
  5669. + {
  5670. + glnx_set_error_from_errno (error);
  5671. + goto out;
  5672. + }
  5673. + if (fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
  5674. + {
  5675. + glnx_set_error_from_errno (error);
  5676. + goto out;
  5677. + }
  5678. +
  5679. + out:
  5680. + g_assert_no_error (local_error);
  5681. +}
  5682. +
  5683. +static void
  5684. +test_tmpfile (void)
  5685. +{
  5686. + g_autoptr(GError) local_error = NULL;
  5687. + GError **error = &local_error;
  5688. + g_auto(GLnxTmpfile) tmpf = { 0, };
  5689. +
  5690. + if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error))
  5691. + goto out;
  5692. +
  5693. + if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0)
  5694. + {
  5695. + (void)glnx_throw_errno_prefix (error, "write");
  5696. + goto out;
  5697. + }
  5698. +
  5699. + if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error))
  5700. + goto out;
  5701. +
  5702. + out:
  5703. + g_assert_no_error (local_error);
  5704. +}
  5705. +
  5706. +static void
  5707. +test_stdio_file (void)
  5708. +{
  5709. + g_autoptr(GError) local_error = NULL;
  5710. + GError **error = &local_error;
  5711. + g_auto(GLnxTmpfile) tmpf = { 0, };
  5712. + g_autoptr(FILE) f = NULL;
  5713. +
  5714. + if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error))
  5715. + goto out;
  5716. + f = fdopen (tmpf.fd, "w");
  5717. + if (!f)
  5718. + {
  5719. + (void)glnx_throw_errno_prefix (error, "fdopen");
  5720. + goto out;
  5721. + }
  5722. +
  5723. + if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello"))
  5724. + {
  5725. + (void)glnx_throw_errno_prefix (error, "fwrite");
  5726. + goto out;
  5727. + }
  5728. + if (!glnx_stdio_file_flush (f, error))
  5729. + goto out;
  5730. +
  5731. + out:
  5732. + g_assert_no_error (local_error);
  5733. +}
  5734. +
  5735. +int main (int argc, char **argv)
  5736. +{
  5737. + int ret;
  5738. +
  5739. + g_test_init (&argc, &argv, NULL);
  5740. +
  5741. + g_test_add_func ("/tmpfile", test_tmpfile);
  5742. + g_test_add_func ("/stdio-file", test_stdio_file);
  5743. + g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace);
  5744. + g_test_add_func ("/renameat2-exchange", test_renameat2_exchange);
  5745. +
  5746. + ret = g_test_run();
  5747. +
  5748. + return ret;
  5749. +}
  5750. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-macros.c flatpak-builder-0.10.6/libglnx/tests/test-libglnx-macros.c
  5751. --- flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-macros.c 1970-01-01 02:00:00.000000000 +0200
  5752. +++ flatpak-builder-0.10.6/libglnx/tests/test-libglnx-macros.c 2018-02-11 12:03:43.450373307 +0300
  5753. @@ -0,0 +1,109 @@
  5754. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5755. + *
  5756. + * Copyright (C) 2017 Red Hat, Inc.
  5757. + *
  5758. + * This library is free software; you can redistribute it and/or
  5759. + * modify it under the terms of the GNU Lesser General Public
  5760. + * License as published by the Free Software Foundation; either
  5761. + * version 2 of the License, or (at your option) any later version.
  5762. + *
  5763. + * This library is distributed in the hope that it will be useful,
  5764. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5765. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5766. + * Lesser General Public License for more details.
  5767. + *
  5768. + * You should have received a copy of the GNU Lesser General Public
  5769. + * License along with this library; if not, write to the
  5770. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5771. + * Boston, MA 02111-1307, USA.
  5772. + */
  5773. +
  5774. +#include "config.h"
  5775. +#include "libglnx.h"
  5776. +#include <glib.h>
  5777. +#include <stdlib.h>
  5778. +#include <gio/gio.h>
  5779. +#include <string.h>
  5780. +
  5781. +static void
  5782. +test_inset (void)
  5783. +{
  5784. + g_assert (G_IN_SET (7, 7));
  5785. + g_assert (G_IN_SET (7, 42, 7));
  5786. + g_assert (G_IN_SET (7, 7,42,3,9));
  5787. + g_assert (G_IN_SET (42, 7,42,3,9));
  5788. + g_assert (G_IN_SET (3, 7,42,3,9));
  5789. + g_assert (G_IN_SET (9, 7,42,3,9));
  5790. + g_assert (!G_IN_SET (8, 7,42,3,9));
  5791. + g_assert (!G_IN_SET (-1, 7,42,3,9));
  5792. + g_assert (G_IN_SET ('x', 'a', 'x', 'c'));
  5793. + g_assert (!G_IN_SET ('y', 'a', 'x', 'c'));
  5794. +}
  5795. +
  5796. +static void
  5797. +test_hash_table_foreach (void)
  5798. +{
  5799. + /* use var names all different from the macro metavars to ensure proper
  5800. + * substitution */
  5801. + g_autoptr(GHashTable) table = g_hash_table_new (g_str_hash, g_str_equal);
  5802. + const char *keys[] = {"key1", "key2"};
  5803. + const char *vals[] = {"val1", "val2"};
  5804. + g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
  5805. + g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
  5806. +
  5807. + guint i = 0;
  5808. + GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
  5809. + {
  5810. + g_assert_cmpstr (key, ==, keys[i]);
  5811. + g_assert_cmpstr (val, ==, vals[i]);
  5812. + i++;
  5813. + }
  5814. + g_assert_cmpuint (i, ==, 2);
  5815. +
  5816. + i = 0;
  5817. + GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
  5818. + {
  5819. + g_hash_table_iter_remove (&it);
  5820. + break;
  5821. + }
  5822. + g_assert_cmpuint (g_hash_table_size (table), ==, 1);
  5823. +
  5824. + g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
  5825. + g_assert_cmpuint (g_hash_table_size (table), ==, 1);
  5826. +
  5827. + g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
  5828. + g_assert_cmpuint (g_hash_table_size (table), ==, 2);
  5829. +
  5830. + i = 0;
  5831. + GLNX_HASH_TABLE_FOREACH_KV (table, const char*, key, const char*, val)
  5832. + {
  5833. + g_assert_cmpstr (key, ==, keys[i]);
  5834. + g_assert_cmpstr (val, ==, vals[i]);
  5835. + i++;
  5836. + }
  5837. + g_assert_cmpuint (i, ==, 2);
  5838. +
  5839. + i = 0;
  5840. + GLNX_HASH_TABLE_FOREACH (table, const char*, key)
  5841. + {
  5842. + g_assert_cmpstr (key, ==, keys[i]);
  5843. + i++;
  5844. + }
  5845. + g_assert_cmpuint (i, ==, 2);
  5846. +
  5847. + i = 0;
  5848. + GLNX_HASH_TABLE_FOREACH_V (table, const char*, val)
  5849. + {
  5850. + g_assert_cmpstr (val, ==, vals[i]);
  5851. + i++;
  5852. + }
  5853. + g_assert_cmpuint (i, ==, 2);
  5854. +}
  5855. +
  5856. +int main (int argc, char **argv)
  5857. +{
  5858. + g_test_init (&argc, &argv, NULL);
  5859. + g_test_add_func ("/inset", test_inset);
  5860. + g_test_add_func ("/hash_table_foreach", test_hash_table_foreach);
  5861. + return g_test_run();
  5862. +}
  5863. diff -Nuar flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-xattrs.c flatpak-builder-0.10.6/libglnx/tests/test-libglnx-xattrs.c
  5864. --- flatpak-builder-0.10.6.orig/libglnx/tests/test-libglnx-xattrs.c 1970-01-01 02:00:00.000000000 +0200
  5865. +++ flatpak-builder-0.10.6/libglnx/tests/test-libglnx-xattrs.c 2018-02-11 12:03:43.450373307 +0300
  5866. @@ -0,0 +1,287 @@
  5867. +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  5868. + *
  5869. + * Copyright (C) 2017 Red Hat, Inc.
  5870. + *
  5871. + * This library is free software; you can redistribute it and/or
  5872. + * modify it under the terms of the GNU Lesser General Public
  5873. + * License as published by the Free Software Foundation; either
  5874. + * version 2 of the License, or (at your option) any later version.
  5875. + *
  5876. + * This library is distributed in the hope that it will be useful,
  5877. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5878. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5879. + * Lesser General Public License for more details.
  5880. + *
  5881. + * You should have received a copy of the GNU Lesser General Public
  5882. + * License along with this library; if not, write to the
  5883. + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5884. + * Boston, MA 02111-1307, USA.
  5885. + */
  5886. +
  5887. +#include "config.h"
  5888. +#include "libglnx.h"
  5889. +#include <glib.h>
  5890. +#include <stdlib.h>
  5891. +#include <gio/gio.h>
  5892. +#include <string.h>
  5893. +
  5894. +#define XATTR_THREAD_RUN_TIME_USECS (5 * G_USEC_PER_SEC)
  5895. +
  5896. +struct XattrWorker {
  5897. + int dfd;
  5898. + gboolean is_writer;
  5899. + guint n_attrs_read;
  5900. +};
  5901. +
  5902. +typedef enum {
  5903. + WRITE_RUN_MUTATE,
  5904. + WRITE_RUN_CREATE,
  5905. +} WriteType;
  5906. +
  5907. +static gboolean
  5908. +set_random_xattr_value (int fd, const char *name, GError **error)
  5909. +{
  5910. + const guint8 randxattrbyte = g_random_int_range (0, 256);
  5911. + const guint32 randxattrvalue_len = (g_random_int () % 256) + 1; /* Picked to be not too small or large */
  5912. + g_autofree char *randxattrvalue = g_malloc (randxattrvalue_len);
  5913. +
  5914. + memset (randxattrvalue, randxattrbyte, randxattrvalue_len);
  5915. +
  5916. + if (fsetxattr (fd, name, randxattrvalue, randxattrvalue_len, 0) < 0)
  5917. + {
  5918. + glnx_set_error_from_errno (error);
  5919. + return FALSE;
  5920. + }
  5921. +
  5922. + return TRUE;
  5923. +}
  5924. +
  5925. +static gboolean
  5926. +add_random_xattrs (int fd, GError **error)
  5927. +{
  5928. + const guint nattrs = MIN (2, g_random_int () % 16);
  5929. +
  5930. + for (guint i = 0; i < nattrs; i++)
  5931. + {
  5932. + guint32 randxattrname_v = g_random_int ();
  5933. + g_autofree char *randxattrname = g_strdup_printf ("user.test%u", randxattrname_v);
  5934. +
  5935. + if (!set_random_xattr_value (fd, randxattrname, error))
  5936. + return FALSE;
  5937. + }
  5938. +
  5939. + return TRUE;
  5940. +}
  5941. +
  5942. +static gboolean
  5943. +do_write_run (GLnxDirFdIterator *dfd_iter, GError **error)
  5944. +{
  5945. + WriteType wtype = g_random_int () % 2;
  5946. +
  5947. + if (wtype == WRITE_RUN_CREATE)
  5948. + {
  5949. + guint32 randname_v = g_random_int ();
  5950. + g_autofree char *randname = g_strdup_printf ("file%u", randname_v);
  5951. + glnx_fd_close int fd = -1;
  5952. +
  5953. + again:
  5954. + fd = openat (dfd_iter->fd, randname, O_CREAT | O_EXCL, 0644);
  5955. + if (fd < 0)
  5956. + {
  5957. + if (errno == EEXIST)
  5958. + {
  5959. + g_printerr ("Congratulations! I suggest purchasing a lottery ticket today!\n");
  5960. + goto again;
  5961. + }
  5962. + else
  5963. + {
  5964. + glnx_set_error_from_errno (error);
  5965. + return FALSE;
  5966. + }
  5967. + }
  5968. +
  5969. + if (!add_random_xattrs (fd, error))
  5970. + return FALSE;
  5971. + }
  5972. + else if (wtype == WRITE_RUN_MUTATE)
  5973. + {
  5974. + while (TRUE)
  5975. + {
  5976. + struct dirent *dent;
  5977. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
  5978. + return FALSE;
  5979. + if (!dent)
  5980. + break;
  5981. +
  5982. + glnx_fd_close int fd = -1;
  5983. + if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
  5984. + return FALSE;
  5985. +
  5986. + g_autoptr(GVariant) current_xattrs = NULL;
  5987. + if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
  5988. + return FALSE;
  5989. +
  5990. + for (int i = 0; i < g_variant_n_children (current_xattrs); i++)
  5991. + {
  5992. + const char *name, *value;
  5993. + g_variant_get_child (current_xattrs, i, "(^&ay^&ay)", &name, &value);
  5994. +
  5995. + /* We don't want to potentially test/change xattrs like security.selinux
  5996. + * that were injected by the system.
  5997. + */
  5998. + if (!g_str_has_prefix (name, "user.test"))
  5999. + continue;
  6000. +
  6001. + if (!set_random_xattr_value (fd, name, error))
  6002. + return FALSE;
  6003. + }
  6004. + }
  6005. + }
  6006. + else
  6007. + g_assert_not_reached ();
  6008. +
  6009. + return TRUE;
  6010. +}
  6011. +
  6012. +static gboolean
  6013. +do_read_run (GLnxDirFdIterator *dfd_iter,
  6014. + guint *out_n_read,
  6015. + GError **error)
  6016. +{
  6017. + guint nattrs = 0;
  6018. + while (TRUE)
  6019. + {
  6020. + struct dirent *dent;
  6021. + if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
  6022. + return FALSE;
  6023. + if (!dent)
  6024. + break;
  6025. +
  6026. + glnx_fd_close int fd = -1;
  6027. + if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
  6028. + return FALSE;
  6029. +
  6030. + g_autoptr(GVariant) current_xattrs = NULL;
  6031. + if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
  6032. + return FALSE;
  6033. +
  6034. + /* We don't actually care about the values, just use the variable
  6035. + * to avoid compiler warnings.
  6036. + */
  6037. + nattrs += g_variant_n_children (current_xattrs);
  6038. + }
  6039. +
  6040. + *out_n_read = nattrs;
  6041. + return TRUE;
  6042. +}
  6043. +
  6044. +static gpointer
  6045. +xattr_thread (gpointer data)
  6046. +{
  6047. + g_autoptr(GError) local_error = NULL;
  6048. + GError **error = &local_error;
  6049. + struct XattrWorker *worker = data;
  6050. + guint64 end_time = g_get_monotonic_time () + XATTR_THREAD_RUN_TIME_USECS;
  6051. + guint n_read = 0;
  6052. +
  6053. + while (g_get_monotonic_time () < end_time)
  6054. + {
  6055. + g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
  6056. +
  6057. + if (!glnx_dirfd_iterator_init_at (worker->dfd, ".", TRUE, &dfd_iter, error))
  6058. + goto out;
  6059. +
  6060. + if (worker->is_writer)
  6061. + {
  6062. + if (!do_write_run (&dfd_iter, error))
  6063. + goto out;
  6064. + }
  6065. + else
  6066. + {
  6067. + if (!do_read_run (&dfd_iter, &n_read, error))
  6068. + goto out;
  6069. + }
  6070. + }
  6071. +
  6072. + out:
  6073. + g_assert_no_error (local_error);
  6074. +
  6075. + return GINT_TO_POINTER (n_read);
  6076. +}
  6077. +
  6078. +static void
  6079. +test_xattr_races (void)
  6080. +{
  6081. + /* If for some reason we're built in a VM which only has one vcpu, let's still
  6082. + * at least make the test do something.
  6083. + */
  6084. + /* FIXME - this deadlocks for me on 4.9.4-201.fc25.x86_64, whether
  6085. + * using overlayfs or xfs as source/dest.
  6086. + */
  6087. + const guint nprocs = MAX (4, g_get_num_processors ());
  6088. + struct XattrWorker wdata[nprocs];
  6089. + GThread *threads[nprocs];
  6090. + g_autoptr(GError) local_error = NULL;
  6091. + GError **error = &local_error;
  6092. + glnx_fd_close int dfd = -1;
  6093. + g_autofree char *tmpdir = g_strdup_printf ("%s/libglnx-xattrs-XXXXXX",
  6094. + getenv ("TMPDIR") ?: "/var/tmp");
  6095. + guint nread = 0;
  6096. +
  6097. + if (!glnx_mkdtempat (AT_FDCWD, tmpdir, 0700, error))
  6098. + goto out;
  6099. +
  6100. + if (!glnx_opendirat (AT_FDCWD, tmpdir, TRUE, &dfd, error))
  6101. + goto out;
  6102. +
  6103. + /* Support people building/testing on tmpfs https://github.com/flatpak/flatpak/issues/686 */
  6104. + if (fsetxattr (dfd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
  6105. + {
  6106. + if (errno == EOPNOTSUPP)
  6107. + {
  6108. + g_test_skip ("no xattr support");
  6109. + return;
  6110. + }
  6111. + else
  6112. + {
  6113. + glnx_set_error_from_errno (error);
  6114. + goto out;
  6115. + }
  6116. + }
  6117. +
  6118. + for (guint i = 0; i < nprocs; i++)
  6119. + {
  6120. + struct XattrWorker *worker = &wdata[i];
  6121. + worker->dfd = dfd;
  6122. + worker->is_writer = i % 2 == 0;
  6123. + threads[i] = g_thread_new (NULL, xattr_thread, worker);
  6124. + }
  6125. +
  6126. + for (guint i = 0; i < nprocs; i++)
  6127. + {
  6128. + if (wdata[i].is_writer)
  6129. + (void) g_thread_join (threads[i]);
  6130. + else
  6131. + nread += GPOINTER_TO_UINT (g_thread_join (threads[i]));
  6132. + }
  6133. +
  6134. + g_print ("Read %u xattrs race free!\n", nread);
  6135. +
  6136. + (void) glnx_shutil_rm_rf_at (AT_FDCWD, tmpdir, NULL, NULL);
  6137. +
  6138. + out:
  6139. + g_assert_no_error (local_error);
  6140. +}
  6141. +
  6142. +int main (int argc, char **argv)
  6143. +{
  6144. + int ret;
  6145. +
  6146. + g_test_init (&argc, &argv, NULL);
  6147. +
  6148. + g_test_add_func ("/xattr-races", test_xattr_races);
  6149. +
  6150. + ret = g_test_run();
  6151. +
  6152. + return ret;
  6153. +}