cpio.c 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175
  1. /*
  2. * cpio - copy file archives in and out
  3. *
  4. * Gunnar Ritter, Freiburg i. Br., Germany, April 2003.
  5. */
  6. /*
  7. * Copyright (c) 2003 Gunnar Ritter
  8. *
  9. * This software is provided 'as-is', without any express or implied
  10. * warranty. In no event will the authors be held liable for any damages
  11. * arising from the use of this software.
  12. *
  13. * Permission is granted to anyone to use this software for any purpose,
  14. * including commercial applications, and to alter it and redistribute
  15. * it freely, subject to the following restrictions:
  16. *
  17. * 1. The origin of this software must not be misrepresented; you must not
  18. * claim that you wrote the original software. If you use this software
  19. * in a product, an acknowledgment in the product documentation would be
  20. * appreciated but is not required.
  21. *
  22. * 2. Altered source versions must be plainly marked as such, and must not be
  23. * misrepresented as being the original software.
  24. *
  25. * 3. This notice may not be removed or altered from any source distribution.
  26. */
  27. /*
  28. * Sccsid @(#)cpio.c 1.304 (gritter) 2/14/09
  29. */
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #ifdef __linux__
  33. #if !defined (__UCLIBC__) && !defined (__dietlibc__)
  34. #include <linux/fs.h>
  35. #endif /* !__UCLIBC__, !__dietlibc__ */
  36. #include <linux/fd.h>
  37. #undef WNOHANG
  38. #undef WUNTRACED
  39. #undef P_ALL
  40. #undef P_PID
  41. #undef P_PGID
  42. #ifdef __dietlibc__
  43. #undef NR_OPEN
  44. #undef PATH_MAX
  45. #endif /* __dietlibc__ */
  46. #endif /* __linux__ */
  47. #include <sys/wait.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52. #include <signal.h>
  53. #include "sigset.h"
  54. #include <time.h>
  55. #include <utime.h>
  56. #include <pwd.h>
  57. #include <grp.h>
  58. #include <limits.h>
  59. #include <stdio.h>
  60. #include <libgen.h>
  61. #include <errno.h>
  62. #include <inttypes.h>
  63. #include <stdarg.h>
  64. #include <locale.h>
  65. #include <ctype.h>
  66. #include "memalign.h"
  67. int sysv3;
  68. #if USE_ZLIB
  69. #include <zlib.h>
  70. #endif /* USE_ZLIB */
  71. #if USE_BZLIB
  72. #include <bzlib.h>
  73. #endif /* USE_BZLIB */
  74. #include <sys/ioctl.h>
  75. #if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
  76. defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
  77. defined (__OpenBSD__) || defined (__DragonFly__) || \
  78. defined (__CYGWIN__)
  79. #include <sys/mtio.h>
  80. #endif
  81. #include <iblok.h>
  82. #include <sfile.h>
  83. #include <atoll.h>
  84. #ifdef _AIX
  85. #include <sys/sysmacros.h>
  86. #endif /* _AIX */
  87. #ifndef major
  88. #include <sys/mkdev.h>
  89. #endif /* !major */
  90. #include "cpio.h"
  91. #include "blast.h"
  92. #ifdef __GLIBC__
  93. #ifdef _IO_putc_unlocked
  94. #undef putc
  95. #define putc(c, f) _IO_putc_unlocked(c, f)
  96. #undef putchar
  97. #define putchar(c) _IO_putc_unlocked(c, stdout)
  98. #endif /* _IO_putc_unlocked */
  99. #endif /* __GLIBC__ */
  100. /*
  101. * The cpio code assumes that all following S_IFMT bits are the same as
  102. * those of the mode fields in cpio headers. The only real Unix system
  103. * known to deviate from this de facto standard is UNICOS which uses
  104. * 0130000 for S_IFLNK. But this software does not run on UNICOS for
  105. * a variety of other reasons anyway, so this should not be of much
  106. * concern.
  107. */
  108. #if S_IFIFO != 0010000 || \
  109. S_IFCHR != 0020000 || \
  110. S_IFDIR != 0040000 || \
  111. S_IFBLK != 0060000 || \
  112. S_IFREG != 0100000 || \
  113. S_IFLNK != 0120000 || \
  114. S_IFSOCK!= 0140000 || \
  115. S_IFMT != 0170000
  116. #error non-standard S_IFMT bits
  117. #endif
  118. /*
  119. * File types that are not present on all systems but that we want to
  120. * recognize nevertheless.
  121. */
  122. #ifndef S_IFDOOR
  123. #define S_IFDOOR 0150000 /* Solaris door */
  124. #endif
  125. #ifndef S_IFNAM
  126. #define S_IFNAM 0050000 /* XENIX special named file */
  127. #endif
  128. #ifndef S_INSEM
  129. #define S_INSEM 0x1 /* XENIX semaphore subtype of IFNAM */
  130. #endif
  131. #ifndef S_INSHD
  132. #define S_INSHD 0x2 /* XENIX shared data subtype of IFNAM */
  133. #endif
  134. #ifndef S_IFNWK
  135. #define S_IFNWK 0110000 /* HP-UX network special file */
  136. #endif
  137. #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
  138. defined (__DragonFly__) || defined (__APPLE__)
  139. /*
  140. * For whatever reason, FreeBSD casts the return values of major() and
  141. * minor() to signed values so that normal limit comparisons will fail.
  142. */
  143. static unsigned long
  144. mymajor(long dev)
  145. {
  146. return major(dev) & 0xFFFFFFFFUL;
  147. }
  148. #undef major
  149. #define major(a) mymajor(a)
  150. static unsigned long
  151. myminor(long dev)
  152. {
  153. return minor(dev) & 0xFFFFFFFFUL;
  154. }
  155. #undef minor
  156. #define minor(a) myminor(a)
  157. #endif /* __FreeBSD__, __NetBSD__, __OpenBSD__, __DragonFly__, __APPLE__ */
  158. /*
  159. * Device and inode counts in cpio formats are too small to store the
  160. * information used to detect hard links on today's systems. Keep track
  161. * of all devices and inodes and store fake counts in the archive.
  162. */
  163. struct ilink {
  164. struct ilink *l_nxt; /* next link to same i-node */
  165. char *l_nam; /* link name */
  166. size_t l_siz; /* link name size */
  167. };
  168. struct islot {
  169. struct islot *i_lln; /* left link */
  170. struct islot *i_rln; /* right link */
  171. struct ilink *i_lnk; /* links list */
  172. struct stat *i_st; /* stat information */
  173. char *i_name;/* name of first link encountered */
  174. ino_t i_ino; /* real inode number */
  175. uint32_t i_fino; /* fake inode number */
  176. nlink_t i_nlk; /* number of remaining links */
  177. };
  178. struct dslot {
  179. struct dslot *d_nxt; /* next device */
  180. struct islot *d_isl; /* inode slots */
  181. uint32_t d_cnt; /* used inode number count */
  182. uint32_t d_fake; /* faked device id */
  183. dev_t d_dev; /* real device id */
  184. };
  185. union types2 {
  186. uint8_t byte[2];
  187. uint16_t sword;
  188. };
  189. union types4 {
  190. uint8_t byte[4];
  191. uint16_t sword[2];
  192. uint32_t lword;
  193. };
  194. /*
  195. * Store and retrieve integers in a defined endian order.
  196. */
  197. static uint16_t
  198. ple16(const char *cp)
  199. {
  200. return (uint16_t)(cp[0]&0377) +
  201. ((uint16_t)(cp[1]&0377) << 8);
  202. }
  203. static uint16_t
  204. pbe16(const char *cp)
  205. {
  206. return (uint16_t)(cp[1]&0377) +
  207. ((uint16_t)(cp[0]&0377) << 8);
  208. }
  209. static uint32_t
  210. ple32(const char *cp)
  211. {
  212. return (uint32_t)(cp[0]&0377) +
  213. ((uint32_t)(cp[1]&0377) << 8) +
  214. ((uint32_t)(cp[2]&0377) << 16) +
  215. ((uint32_t)(cp[3]&0377) << 24);
  216. }
  217. static uint32_t
  218. pbe32(const char *cp)
  219. {
  220. return (uint32_t)(cp[3]&0377) +
  221. ((uint32_t)(cp[2]&0377) << 8) +
  222. ((uint32_t)(cp[1]&0377) << 16) +
  223. ((uint32_t)(cp[0]&0377) << 24);
  224. }
  225. static uint32_t
  226. pme32(const char *cp)
  227. {
  228. return (uint32_t)(cp[2]&0377) +
  229. ((uint32_t)(cp[3]&0377) << 8) +
  230. ((uint32_t)(cp[0]&0377) << 16) +
  231. ((uint32_t)(cp[1]&0377) << 24);
  232. }
  233. static uint64_t
  234. ple64(const char *cp)
  235. {
  236. return (uint64_t)(cp[0]&0377) +
  237. ((uint64_t)(cp[1]&0377) << 8) +
  238. ((uint64_t)(cp[2]&0377) << 16) +
  239. ((uint64_t)(cp[3]&0377) << 24) +
  240. ((uint64_t)(cp[4]&0377) << 32) +
  241. ((uint64_t)(cp[5]&0377) << 40) +
  242. ((uint64_t)(cp[6]&0377) << 48) +
  243. ((uint64_t)(cp[7]&0377) << 56);
  244. }
  245. static uint64_t
  246. pbe64(const char *cp)
  247. {
  248. return (uint64_t)(cp[7]&0377) +
  249. ((uint64_t)(cp[6]&0377) << 8) +
  250. ((uint64_t)(cp[5]&0377) << 16) +
  251. ((uint64_t)(cp[4]&0377) << 24) +
  252. ((uint64_t)(cp[3]&0377) << 32) +
  253. ((uint64_t)(cp[2]&0377) << 40) +
  254. ((uint64_t)(cp[1]&0377) << 48) +
  255. ((uint64_t)(cp[0]&0377) << 56);
  256. }
  257. static void
  258. le16p(uint16_t n, char *cp)
  259. {
  260. cp[0] = (n&0x00ff);
  261. cp[1] = (n&0xff00) >> 8;
  262. }
  263. static void
  264. be16p(uint16_t n, char *cp)
  265. {
  266. cp[1] = (n&0x00ff);
  267. cp[0] = (n&0xff00) >> 8;
  268. }
  269. static void
  270. le32p(uint32_t n, char *cp)
  271. {
  272. cp[0] = (n&0x000000ff);
  273. cp[1] = (n&0x0000ff00) >> 8;
  274. cp[2] = (n&0x00ff0000) >> 16;
  275. cp[3] = (n&0xff000000) >> 24;
  276. }
  277. static void
  278. be32p(uint32_t n, char *cp)
  279. {
  280. cp[3] = (n&0x000000ff);
  281. cp[2] = (n&0x0000ff00) >> 8;
  282. cp[1] = (n&0x00ff0000) >> 16;
  283. cp[0] = (n&0xff000000) >> 24;
  284. }
  285. static void
  286. me32p(uint32_t n, char *cp)
  287. {
  288. cp[2] = (n&0x000000ff);
  289. cp[3] = (n&0x0000ff00) >> 8;
  290. cp[0] = (n&0x00ff0000) >> 16;
  291. cp[1] = (n&0xff000000) >> 24;
  292. }
  293. static void
  294. le64p(uint64_t n, char *cp)
  295. {
  296. cp[0] = (n&0x00000000000000ffLL);
  297. cp[1] = (n&0x000000000000ff00LL) >> 8;
  298. cp[2] = (n&0x0000000000ff0000LL) >> 16;
  299. cp[3] = (n&0x00000000ff000000LL) >> 24;
  300. cp[4] = (n&0x000000ff00000000LL) >> 32;
  301. cp[5] = (n&0x0000ff0000000000LL) >> 40;
  302. cp[6] = (n&0x00ff000000000000LL) >> 48;
  303. cp[7] = (n&0xff00000000000000LL) >> 56;
  304. }
  305. static void
  306. be64p(uint64_t n, char *cp)
  307. {
  308. cp[7] = (n&0x00000000000000ffLL);
  309. cp[6] = (n&0x000000000000ff00LL) >> 8;
  310. cp[5] = (n&0x0000000000ff0000LL) >> 16;
  311. cp[4] = (n&0x00000000ff000000LL) >> 24;
  312. cp[3] = (n&0x000000ff00000000LL) >> 32;
  313. cp[2] = (n&0x0000ff0000000000LL) >> 40;
  314. cp[1] = (n&0x00ff000000000000LL) >> 48;
  315. cp[0] = (n&0xff00000000000000LL) >> 56;
  316. }
  317. #define TNAMSIZ 100
  318. #define TPFXSIZ 155
  319. #define TMAGSIZ 6
  320. #define TTIMSIZ 12
  321. /*
  322. * Structure of an archive header.
  323. */
  324. union bincpio {
  325. char data[4096];
  326. #define SIZEOF_hdr_cpio 26
  327. struct hdr_cpio {
  328. char c_magic[2];
  329. char c_dev[2];
  330. char c_ino[2];
  331. char c_mode[2];
  332. char c_uid[2];
  333. char c_gid[2];
  334. char c_nlink[2];
  335. char c_rdev[2];
  336. char c_mtime[4];
  337. char c_namesize[2];
  338. char c_filesize[4];
  339. } Hdr;
  340. #define SIZEOF_cray_hdr 152
  341. struct cray_hdr { /* with thanks to Cray-Cyber.org */
  342. char C_magic[8];
  343. char C_dev[8];
  344. char C_ino[8];
  345. char C_mode[8];
  346. char C_uid[8];
  347. char C_gid[8];
  348. char C_nlink[8];
  349. char C_rdev[8];
  350. /*
  351. * The C_param field was introduced with
  352. * UNICOS 6 and is simply not present in
  353. * the earlier format. Following fields
  354. * are the same for both revisions again.
  355. */
  356. #define CRAY_PARAMSZ (8*8)
  357. char C_param[CRAY_PARAMSZ];
  358. char C_mtime[8];
  359. char C_namesize[8];
  360. char C_filesize[8];
  361. } Crayhdr;
  362. #define SIZEOF_c_hdr 76
  363. struct c_hdr {
  364. char c_magic[6];
  365. char c_dev[6];
  366. char c_ino[6];
  367. char c_mode[6];
  368. char c_uid[6];
  369. char c_gid[6];
  370. char c_nlink[6];
  371. char c_rdev[6];
  372. char c_mtime[11];
  373. char c_namesz[6];
  374. char c_filesz[11];
  375. } Cdr;
  376. #define SIZEOF_d_hdr 86
  377. struct d_hdr {
  378. char d_magic[6];
  379. char d_dev[6];
  380. char d_ino[6];
  381. char d_mode[6];
  382. char d_uid[6];
  383. char d_gid[6];
  384. char d_nlink[6];
  385. char d_rmaj[8];
  386. char d_rmin[8];
  387. char d_mtime[11];
  388. char d_namesz[6];
  389. char d_filesz[11];
  390. } Ddr;
  391. #define SIZEOF_Exp_cpio_hdr 110
  392. struct Exp_cpio_hdr {
  393. char E_magic[6];
  394. char E_ino[8];
  395. char E_mode[8];
  396. char E_uid[8];
  397. char E_gid[8];
  398. char E_nlink[8];
  399. char E_mtime[8];
  400. char E_filesize[8];
  401. char E_maj[8];
  402. char E_min[8];
  403. char E_rmaj[8];
  404. char E_rmin[8];
  405. char E_namesize[8];
  406. char E_chksum[8];
  407. } Edr;
  408. struct tar_header {
  409. char t_name[TNAMSIZ];
  410. char t_mode[8];
  411. char t_uid[8];
  412. char t_gid[8];
  413. char t_size[12];
  414. char t_mtime[TTIMSIZ];
  415. char t_chksum[8];
  416. char t_linkflag;
  417. char t_linkname[TNAMSIZ];
  418. char t_magic[TMAGSIZ];
  419. char t_version[2];
  420. char t_uname[32];
  421. char t_gname[32];
  422. char t_devmajor[8];
  423. char t_devminor[8];
  424. char t_prefix[TPFXSIZ];
  425. } Tdr;
  426. #define SIZEOF_bar_header 84
  427. struct bar_header {
  428. char b_mode[8];
  429. char b_uid[8];
  430. char b_gid[8];
  431. char b_size[12];
  432. char b_mtime[12];
  433. char b_chksum[8];
  434. char b_rdev[8];
  435. char b_linkflag;
  436. char b_bar_magic[2];
  437. char b_volume_num[4];
  438. char b_compressed;
  439. char b_date[12];
  440. } Bdr;
  441. #define SIZEOF_zip_header 30
  442. struct zip_header {
  443. char z_signature[4];
  444. char z_version[2];
  445. char z_gflag[2];
  446. char z_cmethod[2];
  447. char z_modtime[2];
  448. char z_moddate[2];
  449. char z_crc32[4];
  450. char z_csize[4];
  451. char z_nsize[4];
  452. char z_namelen[2];
  453. char z_extralen[2];
  454. } Zdr;
  455. };
  456. #define BCUT 0177777
  457. #define OCUT 0777777
  458. #define ECUT 0xFFFFFFFFUL
  459. static const char trailer[] = "TRAILER!!!";
  460. /*
  461. * Structure of per-file extra data for zip format.
  462. */
  463. union zextra {
  464. char data[1];
  465. #define SIZEOF_zextra_gn 4
  466. struct zextra_gn {
  467. char ze_gn_tag[2];
  468. char ze_gn_tsize[2];
  469. } Ze_gn;
  470. #define SIZEOF_zextra_64 32 /* regular size */
  471. #define SIZEOF_zextra_64_a 28 /* size without startn field */
  472. #define SIZEOF_zextra_64_b 20 /* size without reloff field */
  473. struct zextra_64 {
  474. char ze_64_tag[2];
  475. char ze_64_tsize[2];
  476. char ze_64_nsize[8];
  477. char ze_64_csize[8];
  478. char ze_64_reloff[8];
  479. char ze_64_startn[4];
  480. } Ze_64;
  481. #define SIZEOF_zextra_pk 16
  482. struct zextra_pk {
  483. char ze_pk_tag[2];
  484. char ze_pk_tsize[2];
  485. char ze_pk_atime[4];
  486. char ze_pk_mtime[4];
  487. char ze_pk_uid[2];
  488. char ze_pk_gid[2];
  489. } Ze_pk;
  490. #define SIZEOF_zextra_ek 17
  491. struct zextra_et {
  492. char ze_et_tag[2];
  493. char ze_et_tsize[2];
  494. char ze_et_flags[1];
  495. char ze_et_mtime[4];
  496. char ze_et_atime[4];
  497. char ze_et_ctime[4];
  498. } Ze_et;
  499. #define SIZEOF_zextra_i1 16
  500. struct zextra_i1 {
  501. char ze_i1_tag[2];
  502. char ze_i1_tsize[2];
  503. char ze_i1_atime[4];
  504. char ze_i1_mtime[4];
  505. char ze_i1_uid[2];
  506. char ze_i1_gid[2];
  507. } Ze_i1;
  508. #define SIZEOF_zextra_i2 8
  509. struct zextra_i2 {
  510. char ze_i2_tag[2];
  511. char ze_i2_tsize[2];
  512. char ze_i2_uid[2];
  513. char ze_i2_gid[2];
  514. } Ze_i2;
  515. #define SIZEOF_zextra_as 16
  516. struct zextra_as {
  517. char ze_as_tag[2];
  518. char ze_as_tsize[2];
  519. char ze_as_crc[4];
  520. char ze_as_mode[2];
  521. char ze_as_sizdev[4];
  522. char ze_as_uid[2];
  523. char ze_as_gid[2];
  524. } Ze_as;
  525. #define SIZEOF_zextra_cp 40
  526. struct zextra_cp {
  527. char ze_cp_tag[2];
  528. char ze_cp_tsize[2];
  529. char ze_cp_dev[4];
  530. char ze_cp_ino[4];
  531. char ze_cp_mode[4];
  532. char ze_cp_uid[4];
  533. char ze_cp_gid[4];
  534. char ze_cp_nlink[4];
  535. char ze_cp_rdev[4];
  536. char ze_cp_mtime[4];
  537. char ze_cp_atime[4];
  538. } Ze_cp;
  539. };
  540. static struct zipstuff { /* stuff for central directory at EOF */
  541. struct zipstuff *zs_next;
  542. char *zs_name; /* file name */
  543. long long zs_size; /* file size */
  544. long long zs_relative; /* offset of local header */
  545. long long zs_csize; /* compressed size */
  546. uint32_t zs_crc32; /* CRC */
  547. time_t zs_mtime; /* modification time */
  548. enum cmethod zs_cmethod; /* compression method */
  549. int zs_gflag; /* general flag */
  550. mode_t zs_mode; /* file mode */
  551. } *zipbulk;
  552. /*
  553. * Structure of the central zip directory at the end of the file. This
  554. * (obligatory) part of a zip file is written by this implementation,
  555. * but is completely ignored on copy-in. This means that we miss the
  556. * mode_t stored in zc_extralen if zc_versionmade[1] is 3 (Unix). We
  557. * have to do this since it contains the S_IFMT bits, thus telling us
  558. * whether something is a symbolic link and resulting in different
  559. * behavior - but as the input had to be seekable in order to do this,
  560. * we had to store entire archives in temporary files if input came
  561. * from a pipe to be consistent.
  562. */
  563. #define SIZEOF_zipcentral 46
  564. struct zipcentral {
  565. char zc_signature[4];
  566. char zc_versionmade[2];
  567. char zc_versionextr[2];
  568. char zc_gflag[2];
  569. char zc_cmethod[2];
  570. char zc_modtime[2];
  571. char zc_moddate[2];
  572. char zc_crc32[4];
  573. char zc_csize[4];
  574. char zc_nsize[4];
  575. char zc_namelen[2];
  576. char zc_extralen[2];
  577. char zc_commentlen[2];
  578. char zc_disknstart[2];
  579. char zc_internal[2];
  580. char zc_external[4];
  581. char zc_relative[4];
  582. };
  583. #define SIZEOF_zip64end 56
  584. struct zip64end {
  585. char z6_signature[4];
  586. char z6_recsize[8];
  587. char z6_versionmade[2];
  588. char z6_versionextr[2];
  589. char z6_thisdiskn[4];
  590. char z6_alldiskn[4];
  591. char z6_thisentries[8];
  592. char z6_allentries[8];
  593. char z6_dirsize[8];
  594. char z6_startsize[8];
  595. };
  596. #define SIZEOF_zip64loc 20
  597. struct zip64loc {
  598. char z4_signature[4];
  599. char z4_startno[4];
  600. char z4_reloff[8];
  601. char z4_alldiskn[4];
  602. };
  603. #define SIZEOF_zipend 22
  604. struct zipend {
  605. char ze_signature[4];
  606. char ze_thisdiskn[2];
  607. char ze_alldiskn[2];
  608. char ze_thisentries[2];
  609. char ze_allentries[2];
  610. char ze_dirsize[4];
  611. char ze_startsize[4];
  612. char ze_commentlen[2];
  613. };
  614. #define SIZEOF_zipddesc 16
  615. struct zipddesc {
  616. char zd_signature[4];
  617. char zd_crc32[4];
  618. char zd_csize[4];
  619. char zd_nsize[4];
  620. };
  621. #define SIZEOF_zipddesc64 24
  622. struct zipddesc64 {
  623. char zd_signature[4];
  624. char zd_crc32[4];
  625. char zd_csize[8];
  626. char zd_nsize[8];
  627. };
  628. /*
  629. * Magic numbers and strings.
  630. */
  631. static const uint16_t mag_bin = 070707;
  632. /*static const uint16_t mag_bbs = 0143561;*/
  633. static const char mag_asc[6] = "070701";
  634. static const char mag_crc[6] = "070702";
  635. static const char mag_odc[6] = "070707";
  636. static const long mag_sco = 0x7ffffe00;
  637. static const char mag_ustar[6] = "ustar\0";
  638. static const char mag_gnutar[8] = "ustar \0";
  639. static const char mag_bar[2] = "V\0";
  640. static const char mag_zipctr[4] = "PK\1\2";
  641. static const char mag_zipsig[4] = "PK\3\4";
  642. static const char mag_zipend[4] = "PK\5\6";
  643. static const char mag_zip64e[4] = "PK\6\6";
  644. static const char mag_zip64l[4] = "PK\6\7";
  645. static const char mag_zipdds[4] = "PK\7\10";
  646. #define mag_zip64f 0x0001
  647. #define mag_zipcpio 0x0707
  648. /*
  649. * Fields for the extended pax header.
  650. */
  651. static enum paxrec {
  652. PR_NONE = 0000,
  653. PR_ATIME = 0001,
  654. PR_GID = 0002,
  655. PR_LINKPATH = 0004,
  656. PR_MTIME = 0010,
  657. PR_PATH = 0020,
  658. PR_SIZE = 0040,
  659. PR_UID = 0100,
  660. PR_SUN_DEVMAJOR = 0200,
  661. PR_SUN_DEVMINOR = 0400
  662. } paxrec, globrec;
  663. /*
  664. * Prototype structure, collecting user-defined information
  665. * about a file.
  666. */
  667. struct prototype {
  668. mode_t pt_mode; /* type and permission bits */
  669. uid_t pt_uid; /* owner */
  670. gid_t pt_gid; /* group owner */
  671. time_t pt_atime; /* time of last access */
  672. time_t pt_mtime; /* time of last modification */
  673. dev_t pt_rdev; /* device major/minor */
  674. enum {
  675. PT_NONE = 0000,
  676. PT_TYPE = 0001,
  677. PT_OWNER = 0002,
  678. PT_GROUP = 0004,
  679. PT_MODE = 0010,
  680. PT_ATIME = 0020,
  681. PT_MTIME = 0040,
  682. PT_RDEV = 0100
  683. } pt_spec; /* specified information */
  684. };
  685. static struct stat globst;
  686. /*
  687. * This sets a sanity check limit on path names. If a longer path name
  688. * occurs in an archive, it is treated as corrupt. This is because no
  689. * known Unix system can handle path names of arbitrary length; limits
  690. * are typically between 1024 and 4096. Trying to extract longer path
  691. * names would fail anyway and will cpio eventually fail to allocate
  692. * memory.
  693. */
  694. #define SANELIMIT 0177777
  695. char *progname; /* argv[0] to main() */
  696. static struct dslot *devices; /* devices table */
  697. static struct dslot *markeddevs; /* unusable device numbers */
  698. static char *blkbuf; /* block buffer */
  699. int blksiz; /* block buffer size */
  700. static int blktop; /* top of filled part of buffer */
  701. static int curpos; /* position in blkbuf */
  702. static uint32_t fakedev; /* fake device for single link inodes */
  703. static uint32_t fakeino; /* fake inode for single link inodes */
  704. static uint32_t harddev; /* fake device used for hard links */
  705. static unsigned long long maxsize;/* maximum size for format */
  706. static unsigned long long maxrdev;/* maximum st_rdev for format */
  707. static unsigned long long maxmajor;/* maximum major(st_rdev) for format */
  708. static unsigned long long maxminor;/* maximum minor(st_rdev) for format */
  709. static unsigned long long maxuid; /* maximum user id for format */
  710. static unsigned long long maxgid; /* maximum group id for format */
  711. static unsigned long long maxnlink;/* maximum link count for format */
  712. static int mt; /* magtape file descriptor */
  713. static int mfl; /* magtape flags */
  714. static struct stat mtst; /* fstat() on mt */
  715. int aflag; /* reset access times */
  716. int Aflag; /* append to archive */
  717. int bflag; /* swap bytes */
  718. int Bflag; /* 5120 blocking */
  719. int cflag; /* ascii format */
  720. int Cflag; /* user-defined blocking */
  721. int dflag; /* create directories */
  722. int Dflag; /* do not ask for next device */
  723. int eflag; /* DEC format */
  724. int cray_eflag; /* do not archive if values too large */
  725. const char *Eflag; /* filename for files to be extracted */
  726. int fflag; /* pattern excludes */
  727. int Hflag; /* header format */
  728. const char *Iflag; /* input archive name */
  729. int kflag; /* skipt corrupted parts */
  730. int Kflag; /* IRIX-style large file support */
  731. int lflag; /* link of possible */
  732. int Lflag; /* follow symbolic links */
  733. int mflag; /* retain modification times */
  734. const char *Mflag; /* message when switching media */
  735. const char *Oflag; /* output archive name */
  736. int Pflag; /* prototype file list */
  737. int rflag; /* rename files */
  738. const char *Rflag; /* reassign ownerships */
  739. static uid_t Ruid; /* uid to assign */
  740. static gid_t Rgid; /* gid to assign */
  741. int sflag; /* swap half word bytes */
  742. int Sflag; /* swap word bytes */
  743. int tflag; /* print toc */
  744. int uflag; /* overwrite files unconditionally */
  745. int hp_Uflag; /* use umask when creating files */
  746. int vflag; /* verbose */
  747. int Vflag; /* special verbose */
  748. int sixflag; /* 6th Edition archives */
  749. int action; /* -i -o -p */
  750. long long errcnt; /* error status */
  751. static unsigned long long maxpath;/* maximum path length with -i */
  752. static uint32_t maxino; /* maximum inode number with -i */
  753. static uid_t myuid; /* user id of caller */
  754. static gid_t mygid; /* group id of caller */
  755. static long long blocks; /* copying statistics: full blocks */
  756. static long long bytes; /* copying statistics: partial blocks */
  757. static long long nwritten; /* bytes written to archive */
  758. static off_t aoffs; /* offset in archive */
  759. static off_t poffs; /* physical offset in archive */
  760. static int tapeblock = -1; /* physical tape block size */
  761. struct glist *patterns; /* patterns for -i */
  762. static int tty; /* terminal file descriptor */
  763. static const char *cur_ofile; /* current original file */
  764. static const char *cur_tfile; /* current temporary file */
  765. static mode_t umsk; /* user's umask */
  766. static int zipclevel; /* zip compression level */
  767. static struct islot *inull; /* splay tree null element */
  768. int printsev; /* print message severity strings */
  769. static int compressed_bar; /* this is a compressed bar archive */
  770. static int formatforced; /* -k -i -Hfmt forces a format */
  771. static long long lineno; /* input line number */
  772. int pax_dflag; /* directory matches only itself */
  773. int pax_kflag; /* do not overwrite files */
  774. int pax_nflag; /* select first archive member only */
  775. int pax_sflag; /* substitute file names */
  776. int pax_uflag; /* add only recent files to archive */
  777. int pax_Xflag; /* do not cross device boundaries */
  778. static enum {
  779. PO_NONE = 0,
  780. PO_LINKDATA = 01, /* include link data in type 2 */
  781. PO_TIMES = 02, /* create atime and mtime fields */
  782. } pax_oflag; /* recognized -o options */
  783. static void copyout(int (*)(const char *, struct stat *));
  784. static size_t ofiles_cpio(char **, size_t *);
  785. static void dooutp(void);
  786. static int outfile(const char *, struct stat *);
  787. static int addfile(const char *, struct stat *, uint32_t, uint32_t, int,
  788. const char *);
  789. static void iflush(struct islot *, uint32_t);
  790. static void lflush(void);
  791. static int bigendian(void);
  792. static void getbuf(char **, size_t *, size_t);
  793. static void prdot(int);
  794. static void newmedia(int);
  795. static void mclose(void);
  796. static ssize_t mwrite(int);
  797. static void bwrite(const char *, size_t);
  798. static void bflush(void);
  799. static int sum(int, const char *, struct stat *, char *);
  800. static int rstime(const char *, struct stat *, const char *);
  801. static struct islot *isplay(ino_t, struct islot *);
  802. static struct islot *ifind(ino_t, struct islot **);
  803. static void iput(struct islot *, struct islot **);
  804. static struct dslot *dfind(struct dslot **, dev_t);
  805. static void done(int);
  806. static void dopass(const char *);
  807. static int passdata(struct file *, const char *, int);
  808. static int passfile(const char *, struct stat *);
  809. static int filein(struct file *, int (*)(struct file *, const char *, int),
  810. char *);
  811. static int linkunlink(const char *, const char *);
  812. static void tunlink(char **);
  813. static int filet(struct file *, int (*)(struct file *, const char *, int));
  814. static void filev(struct file *);
  815. static int typec(struct stat *);
  816. static void permbits(mode_t);
  817. static void prtime_cpio(time_t);
  818. static void getpath(const char *, char **, char **, size_t *, size_t *);
  819. static void setpath(const char *, char **, char **,
  820. size_t, size_t *, size_t *);
  821. static int imdir(char *);
  822. static int setattr(const char *, struct stat *);
  823. static int setowner(const char *, struct stat *);
  824. static int canlink(const char *, struct stat *, int);
  825. static void doinp(void);
  826. static void storelink(struct file *);
  827. static void flushlinks(struct file *);
  828. static void flushnode(struct islot *, struct file *);
  829. static void flushrest(int);
  830. static void flushtree(struct islot *, int);
  831. static int inpone(struct file *, int);
  832. static int readhdr(struct file *, union bincpio *);
  833. static void whathdr(void);
  834. static int infile(struct file *);
  835. static int skipfile(struct file *);
  836. static int skipdata(struct file *f,
  837. int (*)(struct file *, const char *, int));
  838. static int indata(struct file *, const char *, int);
  839. static int totrailer(void);
  840. static long long rdoct(const char *, int);
  841. static long long rdhex(const char *, int);
  842. static ssize_t mread(void);
  843. static void mstat(void);
  844. static int skippad(unsigned long long, int);
  845. static int allzero(const char *, int);
  846. static const char *getuser(uid_t);
  847. static const char *getgroup(gid_t);
  848. static struct glist *want(struct file *, struct glist **);
  849. static void patfile(void);
  850. static int ckodd(long long, int, const char *, const char *);
  851. static int rname(char **, size_t *);
  852. static int redirect(const char *, const char *);
  853. static char *tnameof(struct tar_header *, char *);
  854. static int tmkname(struct tar_header *, const char *);
  855. static void tlinkof(struct tar_header *, struct file *);
  856. static int tmklink(struct tar_header *, const char *);
  857. static int tlflag(struct stat *);
  858. static void tchksum(union bincpio *);
  859. static int tcssum(union bincpio *, int);
  860. static int trdsum(union bincpio *);
  861. static mode_t tifmt(int);
  862. static void bchksum(union bincpio *);
  863. static int bcssum(union bincpio *);
  864. static void blinkof(const char *, struct file *, int);
  865. static void dump_barhdr(void);
  866. static int zcreat(const char *, mode_t);
  867. static int zclose(int);
  868. static void markdev(dev_t);
  869. static int marked(dev_t);
  870. static void cantsup(int, const char *);
  871. static void onint(int);
  872. static int zipread(struct file *, const char *, int, int);
  873. static void zipreaddesc(struct file *);
  874. static int cantunzip(struct file *, const char *);
  875. static time_t gdostime(const char *, const char *);
  876. static void mkdostime(time_t, char *, char *);
  877. static ssize_t ziprxtra(struct file *, struct zip_header *);
  878. static void ziptrailer(void);
  879. static void zipdefer(const char *, struct stat *, long long,
  880. uint32_t, long long, const struct zip_header *);
  881. static int zipwrite(int, const char *, struct stat *,
  882. union bincpio *, size_t, uint32_t, uint32_t,
  883. uint32_t *, long long *);
  884. static int zipwtemp(int, const char *, struct stat *,
  885. union bincpio *, size_t, uint32_t, uint32_t,
  886. uint32_t *, long long *);
  887. #if USE_ZLIB
  888. static int zipwdesc(int, const char *, struct stat *,
  889. union bincpio *, size_t, uint32_t, uint32_t,
  890. uint32_t *, long long *);
  891. #endif /* USE_ZLIB */
  892. static int zipwxtra(const char *, struct stat *, uint32_t, uint32_t);
  893. static void zipinfo(struct file *);
  894. static void readK2hdr(struct file *);
  895. static int readgnuname(char **, size_t *, long);
  896. static void writegnuname(const char *, long, int);
  897. static void tgetpax(struct tar_header *, struct file *);
  898. static enum paxrec tgetrec(char **, char **, char **);
  899. static void wrpax(const char *, const char *, struct stat *);
  900. static void addrec(char **, long *, long *,
  901. const char *, const char *, long long);
  902. static void paxnam(struct tar_header *, const char *);
  903. static char *sequence(void);
  904. static char *joinpath(const char *, char *);
  905. static int utf8(const char *);
  906. static char *getproto(char *, struct prototype *);
  907. size_t (*ofiles)(char **, size_t *) = ofiles_cpio;
  908. void (*prtime)(time_t) = prtime_cpio;
  909. int
  910. main(int argc, char **argv)
  911. {
  912. myuid = getuid();
  913. mygid = getgid();
  914. umask(umsk = umask(0));
  915. progname = basename(argv[0]);
  916. setlocale(LC_CTYPE, "");
  917. setlocale(LC_TIME, "");
  918. inull = scalloc(1, sizeof *inull);
  919. inull->i_lln = inull->i_rln = inull;
  920. flags(argc, argv);
  921. switch (action) {
  922. case 'i':
  923. if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
  924. sigset(SIGINT, onint);
  925. doinp();
  926. break;
  927. case 'o':
  928. dooutp();
  929. break;
  930. case 'p':
  931. if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
  932. sigset(SIGINT, onint);
  933. dopass(argv[optind]);
  934. break;
  935. }
  936. if (tflag)
  937. fflush(stdout);
  938. else if (Vflag)
  939. prdot(1);
  940. if (pax != PAX_TYPE_CPIO)
  941. pax_onexit();
  942. //fprintf(stderr, "%llu blocks\n", blocks + ((bytes + 0777) >> 9));
  943. mclose();
  944. if (errcnt && sysv3 == 0)
  945. fprintf(stderr, "%llu error(s)\n", errcnt);
  946. return errcnt ? sysv3 ? 1 : 2 : 0;
  947. }
  948. static size_t
  949. ofiles_cpio(char **name, size_t *namsiz)
  950. {
  951. static struct iblok *ip;
  952. if (ip == NULL)
  953. ip = ib_alloc(0, 0);
  954. return ib_getlin(ip, name, namsiz, srealloc);
  955. }
  956. /*
  957. * Read the file name list for -o and -p and do initial processing
  958. * for each name.
  959. */
  960. static void
  961. copyout(int (*copyfn)(const char *, struct stat *))
  962. {
  963. char *name = NULL, *np;
  964. size_t namsiz = 0, namlen;
  965. struct stat st;
  966. struct prototype pt;
  967. while ((namlen = ofiles(&name, &namsiz)) != 0) {
  968. lineno++;
  969. if (name[namlen-1] == '\n')
  970. name[--namlen] = '\0';
  971. if (Pflag)
  972. np = getproto(name, &pt);
  973. else
  974. np = name;
  975. while (np[0] == '.' && np[1] == '/') {
  976. np += 2;
  977. while (*np == '/')
  978. np++;
  979. if (*np == '\0') {
  980. np = name;
  981. break;
  982. }
  983. }
  984. if (lstat(np, &st) < 0) {
  985. if (Pflag && *np && ((pt.pt_spec &
  986. (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE|PT_RDEV) &&
  987. ((pt.pt_mode&S_IFMT) == S_IFBLK ||
  988. (pt.pt_mode&S_IFMT) == S_IFCHR)) ||
  989. (pt.pt_spec &
  990. (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE) &&
  991. ((pt.pt_mode&S_IFMT) == S_IFDIR ||
  992. (pt.pt_mode&S_IFMT) == S_IFIFO ||
  993. (pt.pt_mode&S_IFMT) == S_IFREG)))) {
  994. memset(&st, 0, sizeof st);
  995. st.st_mode = pt.pt_mode;
  996. st.st_blksize = 4096;
  997. st.st_nlink = 1;
  998. goto missingok;
  999. }
  1000. else if (sysv3 < 0)
  1001. msg(2, 0, "< %s > ?\n", np);
  1002. else if (sysv3 > 0)
  1003. msg(2, 0, "Cannot obtain information "
  1004. "about file: \"%s\".\n",
  1005. np);
  1006. else
  1007. emsg(2, "Error with lstat of \"%s\"", np);
  1008. errcnt++;
  1009. continue;
  1010. }
  1011. missingok:
  1012. if (Lflag && (st.st_mode&S_IFMT) == S_IFLNK) {
  1013. if (stat(np, &st) < 0) {
  1014. emsg(2, "Cannot follow \"%s\"", np);
  1015. errcnt++;
  1016. continue;
  1017. }
  1018. }
  1019. /*
  1020. * These file types are essentially useless in an archive
  1021. * since they are recreated by any process that needs them.
  1022. * We thus ignore them and do not even issue a warning,
  1023. * because that would only displace more important messages
  1024. * on a terminal and confuse people who just want to copy
  1025. * directory hierarchies.--But for pax, POSIX.1-2001 requires
  1026. * us to fail!
  1027. */
  1028. if ((st.st_mode&S_IFMT) == S_IFSOCK ||
  1029. (st.st_mode&S_IFMT) == S_IFDOOR) {
  1030. if (pax >= PAX_TYPE_PAX2001) {
  1031. msg(2, 0, "Cannot handle %s \"%s\".\n",
  1032. (st.st_mode&S_IFMT) == S_IFSOCK ?
  1033. "socket" : "door", np);
  1034. errcnt++;
  1035. }
  1036. continue;
  1037. }
  1038. if (Pflag) {
  1039. if (pt.pt_spec & PT_TYPE)
  1040. if ((st.st_mode&S_IFMT) != (pt.pt_mode&S_IFMT))
  1041. msg(4, 0, "line %lld: types "
  1042. "do not match\n", lineno);
  1043. if (pt.pt_spec & PT_OWNER)
  1044. st.st_uid = pt.pt_uid;
  1045. if (pt.pt_spec & PT_GROUP)
  1046. st.st_gid = pt.pt_gid;
  1047. if (pt.pt_spec & PT_MODE) {
  1048. st.st_mode &= ~(mode_t)07777;
  1049. st.st_mode |= pt.pt_mode;
  1050. }
  1051. if (pt.pt_spec & PT_ATIME)
  1052. st.st_atime = pt.pt_atime;
  1053. if (pt.pt_spec & PT_MTIME)
  1054. st.st_mtime = pt.pt_mtime;
  1055. if (pt.pt_spec & PT_RDEV) {
  1056. if ((st.st_mode&S_IFMT) != S_IFBLK &&
  1057. (st.st_mode&S_IFMT) != S_IFCHR)
  1058. msg(4, 0, "line %lld: device type "
  1059. "specified for non-device "
  1060. "file\n", lineno);
  1061. st.st_rdev = pt.pt_rdev;
  1062. }
  1063. }
  1064. if (pax_track(np, st.st_mtime) == 0)
  1065. continue;
  1066. if ((fmttype == FMT_ZIP ||
  1067. fmttype & TYP_BAR ||
  1068. fmttype == FMT_GNUTAR)
  1069. && (st.st_mode&S_IFMT) == S_IFDIR &&
  1070. name[namlen-1] != '/') {
  1071. if (namlen+2 >= namsiz) {
  1072. size_t diff = np - name;
  1073. name = srealloc(name, namsiz = namlen+2);
  1074. np = &name[diff];
  1075. }
  1076. name[namlen++] = '/';
  1077. name[namlen] = '\0';
  1078. }
  1079. errcnt += copyfn(np, &st);
  1080. }
  1081. }
  1082. /*
  1083. * Execution for -o.
  1084. */
  1085. static void
  1086. dooutp(void)
  1087. {
  1088. if (Oflag) {
  1089. if ((mt = Aflag ? open(Oflag, O_RDWR, 0666) :
  1090. creat(Oflag, 0666)) < 0) {
  1091. if (sysv3) {
  1092. emsg(013, "Cannot open <%s> for %s.", Oflag,
  1093. Aflag ? "append" : "output");
  1094. done(1);
  1095. } else
  1096. msg(3, -2, "Cannot open \"%s\" for %s\n", Oflag,
  1097. Aflag ? "append" : "output");
  1098. }
  1099. } else
  1100. mt = dup(1);
  1101. mstat();
  1102. blkbuf = svalloc(blksiz, 1);
  1103. if (Aflag) {
  1104. if (totrailer() != 0)
  1105. return;
  1106. } else if (fmttype == FMT_NONE)
  1107. fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
  1108. if (fmttype & TYP_BINARY) {
  1109. maxino = 0177777;
  1110. fakeino = 0177777;
  1111. maxpath = 256;
  1112. if (fmttype & TYP_SGI) {
  1113. maxsize = 0x7FFFFFFFFFFFFFFFLL;
  1114. maxmajor = 037777;
  1115. maxminor = 0777777;
  1116. } else {
  1117. maxsize = 0x7FFFFFFFLL;
  1118. maxrdev = 0177777;
  1119. }
  1120. maxuid = 0177777;
  1121. maxgid = 0177777;
  1122. maxnlink = 0177777;
  1123. } else if (fmttype == FMT_ODC) {
  1124. maxino = 0777777;
  1125. fakeino = 0777777;
  1126. maxpath = 256;
  1127. maxsize = 077777777777LL;
  1128. maxrdev = 0777777;
  1129. maxuid = 0777777;
  1130. maxgid = 0777777;
  1131. maxnlink = 0777777;
  1132. } else if (fmttype == FMT_DEC) {
  1133. maxino = 0777777;
  1134. fakeino = 0777777;
  1135. maxpath = 256;
  1136. maxsize = 077777777777LL;
  1137. maxmajor = 077777777;
  1138. maxminor = 077777777;
  1139. maxuid = 0777777;
  1140. maxgid = 0777777;
  1141. maxnlink = 0777777;
  1142. } else if (fmttype & TYP_NCPIO) {
  1143. maxino = 0xFFFFFFFFUL;
  1144. fakeino = 0xFFFFFFFFUL;
  1145. maxpath = 1024;
  1146. maxsize = fmttype&TYP_SCO ? 0x7FFFFFFFFFFFFFFFLL : 0xFFFFFFFFUL;
  1147. maxmajor = 0xFFFFFFFFUL;
  1148. maxminor = 0xFFFFFFFFUL;
  1149. maxuid = 0xFFFFFFFFUL;
  1150. maxgid = 0xFFFFFFFFUL;
  1151. maxnlink = 0xFFFFFFFFUL;
  1152. } else if (fmttype & TYP_CRAY) {
  1153. maxino = 0xFFFFFFFFUL;
  1154. fakeino = 0xFFFFFFFFUL;
  1155. maxpath = SANELIMIT;
  1156. maxsize = 0x7FFFFFFFFFFFFFFFLL;
  1157. maxrdev = 0x7FFFFFFFFFFFFFFFLL;
  1158. maxuid = 0x7FFFFFFFFFFFFFFFLL;
  1159. maxgid = 0x7FFFFFFFFFFFFFFFLL;
  1160. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1161. } else if (fmttype == FMT_GNUTAR) {
  1162. maxino = 0xFFFFFFFFUL;
  1163. fakeino = 0xFFFFFFFFUL;
  1164. maxpath = SANELIMIT;
  1165. maxsize = 0x7FFFFFFFFFFFFFFFLL;
  1166. maxmajor = 0x7FFFFFFFFFFFFFFFLL;
  1167. maxminor = 0x7FFFFFFFFFFFFFFFLL;
  1168. maxuid = 0x7FFFFFFFFFFFFFFFLL;
  1169. maxgid = 0x7FFFFFFFFFFFFFFFLL;
  1170. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1171. } else if (fmttype & TYP_PAX) {
  1172. maxino = 0xFFFFFFFFUL;
  1173. fakeino = 0xFFFFFFFFUL;
  1174. maxpath = SANELIMIT;
  1175. maxsize = 0x7FFFFFFFFFFFFFFFLL;
  1176. maxmajor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
  1177. maxminor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
  1178. maxuid = 0x7FFFFFFFFFFFFFFFLL;
  1179. maxgid = 0x7FFFFFFFFFFFFFFFLL;
  1180. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1181. if (pax_oflag & PO_TIMES)
  1182. globrec |= PR_ATIME|PR_MTIME;
  1183. } else if (fmttype & TYP_BAR) {
  1184. maxino = 0xFFFFFFFFUL;
  1185. fakeino = 0xFFFFFFFFUL;
  1186. maxpath = 512 - SIZEOF_bar_header - 1;
  1187. maxsize = 077777777777LL;
  1188. maxrdev = 07777777;
  1189. maxuid = 07777777;
  1190. maxgid = 07777777;
  1191. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1192. if (nwritten == 0)
  1193. dump_barhdr();
  1194. } else if (fmttype & TYP_USTAR) {
  1195. maxino = 0xFFFFFFFFUL;
  1196. fakeino = 0xFFFFFFFFUL;
  1197. maxpath = 256;
  1198. maxsize = 077777777777LL;
  1199. maxmajor = 07777777;
  1200. maxminor = 07777777;
  1201. maxuid = 07777777;
  1202. maxgid = 07777777;
  1203. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1204. } else if (fmttype & TYP_OTAR) {
  1205. maxino = 0xFFFFFFFFUL;
  1206. fakeino = 0xFFFFFFFFUL;
  1207. maxpath = 99;
  1208. maxsize = 077777777777LL;
  1209. maxuid = 07777777;
  1210. maxgid = 07777777;
  1211. maxnlink = 0x7FFFFFFFFFFFFFFFLL;
  1212. } else if (fmttype == FMT_ZIP) {
  1213. maxino = 0xFFFFFFFFUL;
  1214. fakeino = 0xFFFFFFFFUL;
  1215. maxpath = 60000;
  1216. maxsize = 0x7FFFFFFFFFFFFFFFLL;
  1217. maxrdev = 0xFFFFFFFFUL;
  1218. maxuid = 0xFFFFFFFFUL;
  1219. maxgid = 0xFFFFFFFFUL;
  1220. maxnlink = 0xFFFFFFFFUL;
  1221. } else
  1222. abort();
  1223. fakedev = 0177777;
  1224. harddev = 1;
  1225. copyout(outfile);
  1226. if (fmttype & TYP_NCPIO)
  1227. lflush();
  1228. if (fmttype & TYP_CPIO) {
  1229. struct stat st;
  1230. memset(&st, 0, sizeof st);
  1231. st.st_nlink = 1;
  1232. outfile(trailer, &st);
  1233. }
  1234. if (fmttype & TYP_TAR) {
  1235. char b[512];
  1236. memset(b, 0, sizeof b);
  1237. bwrite(b, sizeof b);
  1238. bwrite(b, sizeof b);
  1239. }
  1240. if (fmttype == FMT_ZIP)
  1241. ziptrailer();
  1242. bflush();
  1243. }
  1244. /*
  1245. * Handle a single file for -o, do sanity checks and detect hard links.
  1246. */
  1247. static int
  1248. outfile(const char *file, struct stat *st)
  1249. {
  1250. uint32_t dev, ino;
  1251. size_t pathsz;
  1252. if ((st->st_mode&S_IFMT) == S_IFREG)
  1253. if (mtst.st_dev == st->st_dev && mtst.st_ino == st->st_ino)
  1254. return 0;
  1255. if (st->st_size > maxsize) {
  1256. msg(2, 0, "Size of %c%s%c >%lluGB. Not dumped\n",
  1257. sysv3 ? '<' : '"',
  1258. file,
  1259. sysv3 ? '>' : '"',
  1260. (maxsize+1) / (1024*1024*1024));
  1261. return 1;
  1262. }
  1263. if (((st->st_mode&S_IFMT)==S_IFBLK||(st->st_mode&S_IFMT)==S_IFCHR) &&
  1264. (maxrdev &&
  1265. (unsigned long long)st->st_rdev > maxrdev ||
  1266. maxmajor &&
  1267. (unsigned long long)major(st->st_rdev) > maxmajor ||
  1268. maxminor &&
  1269. (unsigned long long)minor(st->st_rdev) > maxminor)) {
  1270. cantsup(1, file);
  1271. return 1;
  1272. }
  1273. if ((unsigned long long)st->st_uid > maxuid) {
  1274. if (cray_eflag) {
  1275. cantsup(1, file);
  1276. return 1;
  1277. }
  1278. cantsup(0, file);
  1279. st->st_uid = 60001;
  1280. if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0111)
  1281. st->st_mode &= ~(mode_t)S_ISUID;
  1282. if ((unsigned long long)st->st_gid > maxgid) {
  1283. st->st_gid = 60001;
  1284. if ((st->st_mode&S_IFMT)==S_IFREG && st->st_mode&0010)
  1285. st->st_mode &= ~(mode_t)S_ISGID;
  1286. }
  1287. } else if ((unsigned long long)st->st_gid > maxgid) {
  1288. if (cray_eflag) {
  1289. cantsup(1, file);
  1290. return 1;
  1291. }
  1292. cantsup(0, file);
  1293. st->st_gid = 60001;
  1294. if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0010)
  1295. st->st_mode &= ~(mode_t)S_ISGID;
  1296. }
  1297. if ((pathsz = strlen(file)) > maxpath) {
  1298. msg(2, 0, "%s: file name too long\n", file);
  1299. return 1;
  1300. }
  1301. /*
  1302. * Detect hard links and compute fake inode counts. The mechanism
  1303. * is as follows: If a file has more than one link, a fake device
  1304. * number starting at one is used for its device, and a fake inode
  1305. * number is used starting at one too.
  1306. *
  1307. * The information on links of directories is useless, so it is
  1308. * dropped and handled like a file with a single link only: Fake
  1309. * devices are allocated just below the format's limit, fake
  1310. * i-nodes the same.
  1311. *
  1312. * This way even the binary cpio format can have up to ~4G files.
  1313. */
  1314. if (maxino && st->st_nlink > 1 && (st->st_mode&S_IFMT) != S_IFDIR) {
  1315. struct dslot *ds, *dp;
  1316. struct islot *ip;
  1317. dev = 1;
  1318. ds = devices;
  1319. dp = NULL;
  1320. nextdev:
  1321. for (; ds; dp = ds, ds = ds->d_nxt, dev++ /* see below! */)
  1322. if (ds->d_dev == st->st_dev)
  1323. break;
  1324. if (markeddevs && marked(dev)) {
  1325. dev++;
  1326. goto nextdev;
  1327. }
  1328. if (dev >= fakedev)
  1329. msg(4, 1, "Too many devices in archive, exiting\n");
  1330. if (ds == NULL) {
  1331. ds = scalloc(1, sizeof *ds);
  1332. ds->d_dev = st->st_dev;
  1333. ds->d_fake = dev;
  1334. if (devices == NULL)
  1335. devices = ds;
  1336. else
  1337. dp->d_nxt = ds;
  1338. }
  1339. harddev = dev;
  1340. if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL) {
  1341. if (ds->d_cnt >= maxino) {
  1342. /* corresponds to for loop above */
  1343. dev++, dp = ds, ds = ds->d_nxt;
  1344. goto nextdev;
  1345. }
  1346. ip = scalloc(1, sizeof *ip);
  1347. ip->i_ino = st->st_ino;
  1348. ip->i_fino = ++ds->d_cnt;
  1349. ip->i_nlk = st->st_nlink;
  1350. if (fmttype & TYP_TAR)
  1351. ip->i_name = sstrdup(file);
  1352. if (fmttype & TYP_NCPIO) {
  1353. ip->i_st = smalloc(sizeof *ip->i_st);
  1354. *ip->i_st = *st;
  1355. }
  1356. iput(ip, &ds->d_isl);
  1357. }
  1358. ino = ip->i_fino;
  1359. if (fmttype & TYP_NCPIO) {
  1360. /*
  1361. * In SVR4 ascii cpio format, files with multiple
  1362. * links are stored with a zero size except for the
  1363. * last link, which contains the actual file content.
  1364. * As one cannot know which is the last link in
  1365. * advance since some links may be outside the
  1366. * archive content, all links have to be collected
  1367. * and written out at once.
  1368. */
  1369. struct ilink *il, *ik;
  1370. switch (ip->i_nlk) {
  1371. case 1:
  1372. /*
  1373. * This was the last link to a file. Write
  1374. * all previous links and break to write
  1375. * the actual file content. Free the pointers
  1376. * in islot; islot remains within the tree
  1377. * with a remaining link count of zero.
  1378. */
  1379. ip->i_nlk--;
  1380. free(ip->i_st);
  1381. ip->i_st = NULL;
  1382. for (il = ip->i_lnk, ik = NULL; il;
  1383. ik = il, il = il->l_nxt,
  1384. ik ? free(ik), 0 : 0) {
  1385. errcnt += addfile(il->l_nam, st,
  1386. dev, ino, 1, 0);
  1387. free(il->l_nam);
  1388. }
  1389. break;
  1390. case 0:
  1391. /*
  1392. * This file got a link during operation, or
  1393. * -L was specified and we encountered a link
  1394. * more than once. Start with a fresh link
  1395. * count again.
  1396. */
  1397. ip->i_nlk = st->st_nlink;
  1398. ip->i_lnk = NULL;
  1399. ip->i_st = smalloc(sizeof *ip->i_st);
  1400. *ip->i_st = *st;
  1401. /*FALLTHRU*/
  1402. default:
  1403. /*
  1404. * There are more links to this file. Store
  1405. * only the name and return.
  1406. */
  1407. ip->i_nlk--;
  1408. if (ip->i_lnk) {
  1409. for (il = ip->i_lnk; il->l_nxt;
  1410. il = il->l_nxt);
  1411. il->l_nxt = scalloc(1,sizeof*il->l_nxt);
  1412. il = il->l_nxt;
  1413. } else {
  1414. ip->i_lnk = scalloc(1,sizeof*ip->i_lnk);
  1415. il = ip->i_lnk;
  1416. }
  1417. il->l_nam = smalloc(pathsz + 1);
  1418. strcpy(il->l_nam, file);
  1419. return 0;
  1420. }
  1421. } else if (fmttype & TYP_TAR) {
  1422. if (strcmp(ip->i_name, file))
  1423. return addfile(file, st, dev, ino, 1,
  1424. ip->i_name);
  1425. }
  1426. } else { /* single-linked or directory */
  1427. dev = fakedev;
  1428. while (markeddevs && marked(dev))
  1429. dev--;
  1430. if ((ino = fakeino--) == 0) {
  1431. if (--dev <= harddev)
  1432. msg(4, 1, "Too many devices in archive, "
  1433. "exiting\n");
  1434. fakedev = dev;
  1435. ino = maxino;
  1436. fakeino = ino - 1;
  1437. }
  1438. }
  1439. return addfile(file, st, dev, ino, 0, 0);
  1440. }
  1441. /*
  1442. * Add a single file to the archive with -o.
  1443. */
  1444. static int
  1445. addfile(const char *realfile, struct stat *st,
  1446. uint32_t dev, uint32_t ino, int zerolink, const char *linkname)
  1447. {
  1448. union bincpio bc;
  1449. int fd = -1;
  1450. long long size;
  1451. int pad, i;
  1452. ssize_t rsz = 0, wsz = 0, hsz, fsz, psz;
  1453. long long remsz, relative, nlink;
  1454. long long Kbase = 0, Krest = 0, Ksize = 0;
  1455. struct hdr_cpio K2hdr;
  1456. uint32_t crc = 0;
  1457. long long csize = 0;
  1458. char *file;
  1459. char *symblink = NULL;
  1460. int failure = 1;
  1461. file = sstrdup(realfile);
  1462. if (pax != PAX_TYPE_CPIO && strcmp(file, trailer)) {
  1463. size_t junk = 0;
  1464. if (pax_sflag && pax_sname(&file, &junk) == 0)
  1465. goto cleanup;
  1466. if (rflag && rname(&file, &junk) == 0)
  1467. goto cleanup;
  1468. }
  1469. fsz = strlen(file);
  1470. relative = nwritten;
  1471. memset(bc.data, 0, sizeof bc.data);
  1472. if (fmttype == FMT_PAX && pax_oflag & PO_LINKDATA &&
  1473. (st->st_mode&S_IFMT) == S_IFREG)
  1474. size = st->st_size;
  1475. else if (zerolink)
  1476. size = 0;
  1477. else if ((st->st_mode&S_IFMT) == S_IFREG)
  1478. size = st->st_size;
  1479. else if ((st->st_mode&S_IFMT) == S_IFLNK) {
  1480. i = st->st_size ? st->st_size : PATH_MAX;
  1481. symblink = smalloc(i+1);
  1482. if ((size = readlink(realfile, symblink, i)) < 0) {
  1483. emsg(3, "Cannot read symbolic link \"%s\"", realfile);
  1484. goto cleanup;
  1485. }
  1486. symblink[size] = '\0';
  1487. } else
  1488. size = 0;
  1489. nlink = ((unsigned long long)st->st_nlink>maxnlink ?
  1490. maxnlink : st->st_nlink);
  1491. if (fmttype & TYP_NCPIO) {
  1492. long size1;
  1493. if (fmttype & TYP_SCO && size >= mag_sco) {
  1494. char *ofile = file;
  1495. size1 = mag_sco;
  1496. fsz += 22;
  1497. file = smalloc(fsz + 1);
  1498. snprintf(file, fsz + 1, "%s%csize=%016llx",
  1499. ofile, 0, size);
  1500. free(ofile);
  1501. } else
  1502. size1 = size;
  1503. pad = 4;
  1504. sprintf(bc.data, "%*.*s%08lx%08lx%08lx%08lx%08lx%08lx"
  1505. "%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
  1506. (int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
  1507. (int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
  1508. fmttype & TYP_CRC ? mag_crc : mag_asc,
  1509. (long)ino & ECUT,
  1510. (long)st->st_mode & ECUT,
  1511. (long)st->st_uid & ECUT,
  1512. (long)st->st_gid & ECUT,
  1513. (long)nlink & ECUT,
  1514. (long)st->st_mtime & ECUT,
  1515. (long)size1 & ECUT,
  1516. (long)major(dev) & ECUT,
  1517. (long)minor(dev) & ECUT,
  1518. (long)major(st->st_rdev) & ECUT,
  1519. (long)minor(st->st_rdev) & ECUT,
  1520. (long)++fsz,
  1521. 0L);
  1522. hsz = SIZEOF_Exp_cpio_hdr;
  1523. if ((psz = (hsz + fsz) % pad) != 0)
  1524. psz = pad - psz;
  1525. } else if (fmttype == FMT_ODC) {
  1526. pad = 1;
  1527. sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo%06lo"
  1528. "%011lo%06lo%011lo",
  1529. (int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
  1530. (long)dev & OCUT,
  1531. (long)ino & OCUT,
  1532. (long)st->st_mode & OCUT,
  1533. (long)st->st_uid & OCUT,
  1534. (long)st->st_gid & OCUT,
  1535. (long)nlink & OCUT,
  1536. (long)st->st_rdev & OCUT,
  1537. (long)st->st_mtime,
  1538. (long)++fsz,
  1539. (long)size);
  1540. hsz = SIZEOF_c_hdr;
  1541. if ((psz = (hsz + fsz) % pad) != 0)
  1542. psz = pad - psz;
  1543. } else if (fmttype == FMT_DEC) {
  1544. pad = 1;
  1545. sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo"
  1546. "%08lo%08lo%011lo%06lo%011lo",
  1547. (int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
  1548. (long)dev & OCUT,
  1549. (long)ino & OCUT,
  1550. (long)st->st_mode & OCUT,
  1551. (long)st->st_uid & OCUT,
  1552. (long)st->st_gid & OCUT,
  1553. (long)nlink & OCUT,
  1554. (long)major(st->st_rdev) & 077777777,
  1555. (long)minor(st->st_rdev) & 077777777,
  1556. (long)st->st_mtime,
  1557. (long)++fsz,
  1558. (long)size);
  1559. hsz = SIZEOF_d_hdr;
  1560. if ((psz = (hsz + fsz) % pad) != 0)
  1561. psz = pad - psz;
  1562. } else if (fmttype & TYP_BINARY) {
  1563. /*
  1564. * To avoid gcc's stupid 'comparison is always false due to
  1565. * limited range of data type' warning.
  1566. */
  1567. unsigned long long gcccrap;
  1568. pad = 2;
  1569. if (fmttype & TYP_BE) {
  1570. be16p(mag_bin, bc.Hdr.c_magic);
  1571. be16p(dev, bc.Hdr.c_dev);
  1572. be16p(ino, bc.Hdr.c_ino);
  1573. be16p(st->st_mode, bc.Hdr.c_mode);
  1574. be16p(st->st_uid, bc.Hdr.c_uid);
  1575. be16p(st->st_gid, bc.Hdr.c_gid);
  1576. be16p(nlink, bc.Hdr.c_nlink);
  1577. be16p(st->st_rdev, bc.Hdr.c_rdev);
  1578. be32p(st->st_mtime, bc.Hdr.c_mtime);
  1579. be16p(++fsz, bc.Hdr.c_namesize);
  1580. } else {
  1581. le16p(mag_bin, bc.Hdr.c_magic);
  1582. le16p(dev, bc.Hdr.c_dev);
  1583. le16p(ino, bc.Hdr.c_ino);
  1584. le16p(st->st_mode, bc.Hdr.c_mode);
  1585. le16p(st->st_uid, bc.Hdr.c_uid);
  1586. le16p(st->st_gid, bc.Hdr.c_gid);
  1587. le16p(nlink, bc.Hdr.c_nlink);
  1588. le16p(st->st_rdev, bc.Hdr.c_rdev);
  1589. me32p(st->st_mtime, bc.Hdr.c_mtime);
  1590. le16p(++fsz, bc.Hdr.c_namesize);
  1591. }
  1592. if (fmttype & TYP_SGI && size > 0x7FFFFFFFLL) {
  1593. Krest = size & 0x7FFFFFFFLL;
  1594. Kbase = size - Krest;
  1595. Ksize = 0x100000000LL - (Kbase >> 31);
  1596. if (fmttype & TYP_BE)
  1597. be32p(Ksize, bc.Hdr.c_filesize);
  1598. else
  1599. me32p(Ksize, bc.Hdr.c_filesize);
  1600. K2hdr = bc.Hdr;
  1601. if (fmttype & TYP_BE)
  1602. be32p(Krest, K2hdr.c_filesize);
  1603. else
  1604. me32p(Krest, K2hdr.c_filesize);
  1605. } else {
  1606. if (fmttype & TYP_BE)
  1607. be32p(size, bc.Hdr.c_filesize);
  1608. else
  1609. me32p(size, bc.Hdr.c_filesize);
  1610. }
  1611. if (fmttype & TYP_SGI &&
  1612. (((st->st_mode&S_IFMT) == S_IFBLK ||
  1613. (st->st_mode&S_IFMT) == S_IFCHR) &&
  1614. ((unsigned long long)major(st->st_rdev)>0xFF ||
  1615. (unsigned long long)minor(st->st_rdev)>0xFF) ||
  1616. (gcccrap = st->st_rdev) > 0177777)) {
  1617. uint32_t rdev;
  1618. rdev = (minor(st->st_rdev) & 0x0003FFFF) +
  1619. ((major(st->st_rdev)<<18) & 0xFFFC0000);
  1620. if (fmttype & TYP_BE) {
  1621. be16p(0xFFFF, bc.Hdr.c_rdev);
  1622. be32p(rdev, bc.Hdr.c_filesize);
  1623. } else {
  1624. le16p(0xFFFF, bc.Hdr.c_rdev);
  1625. me32p(rdev, bc.Hdr.c_filesize);
  1626. }
  1627. }
  1628. hsz = SIZEOF_hdr_cpio;
  1629. psz = (hsz + fsz) % 2;
  1630. } else if (fmttype & TYP_CRAY) {
  1631. int diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
  1632. mode_t mo;
  1633. pad = 1;
  1634. be64p(mag_bin, bc.Crayhdr.C_magic);
  1635. be64p(dev, bc.Crayhdr.C_dev);
  1636. be64p(ino, bc.Crayhdr.C_ino);
  1637. if ((st->st_mode&S_IFMT) == S_IFLNK) /* non-standard */
  1638. mo = st->st_mode&07777|0130000; /* S_IFLNK on Cray */
  1639. else
  1640. mo = st->st_mode;
  1641. be64p(mo, bc.Crayhdr.C_mode);
  1642. be64p(st->st_uid, bc.Crayhdr.C_uid);
  1643. be64p(st->st_gid, bc.Crayhdr.C_gid);
  1644. be64p(nlink, bc.Crayhdr.C_nlink);
  1645. be64p(st->st_rdev, bc.Crayhdr.C_rdev);
  1646. be64p(st->st_mtime, bc.Crayhdr.C_mtime - diff5);
  1647. be64p(++fsz, bc.Crayhdr.C_namesize - diff5);
  1648. be64p(size, bc.Crayhdr.C_filesize - diff5);
  1649. hsz = SIZEOF_cray_hdr - diff5;
  1650. psz = 0;
  1651. } else if (fmttype & TYP_BAR) {
  1652. int c, n = 0;
  1653. pad = 512;
  1654. sprintf(bc.Bdr.b_mode, "%7.7o",(int)st->st_mode&(07777|S_IFMT));
  1655. sprintf(bc.Bdr.b_uid, "%7.7lo", (long)st->st_uid);
  1656. sprintf(bc.Bdr.b_gid, "%7.7lo", (long)st->st_gid);
  1657. sprintf(bc.Bdr.b_size, "%11.11llo",
  1658. (st->st_mode&S_IFMT) == S_IFREG && !zerolink ?
  1659. (long long)st->st_size&077777777777LL : 0LL);
  1660. sprintf(bc.Bdr.b_mtime, "%11.11lo", (long)st->st_mtime);
  1661. sprintf(bc.Bdr.b_rdev, "%7.7lo", (long)st->st_rdev);
  1662. strcpy(&bc.data[SIZEOF_bar_header], file);
  1663. c = tlflag(st);
  1664. if (zerolink == 0) {
  1665. bc.Bdr.b_linkflag = c;
  1666. if (c == '2') {
  1667. strncpy(&bc.data[SIZEOF_bar_header+fsz+1],
  1668. symblink,
  1669. 512-SIZEOF_bar_header-fsz);
  1670. n = size;
  1671. }
  1672. } else {
  1673. bc.Bdr.b_linkflag = '1';
  1674. strncpy(&bc.data[SIZEOF_bar_header+fsz+1], linkname,
  1675. 512-SIZEOF_bar_header-fsz-1);
  1676. n = strlen(linkname);
  1677. }
  1678. if (n > 512-SIZEOF_bar_header-fsz-1) {
  1679. msg(3, 0, "%s: linked name too long\n", realfile);
  1680. goto cleanup;
  1681. }
  1682. bchksum(&bc);
  1683. hsz = 512;
  1684. psz = 0;
  1685. fsz = 0;
  1686. } else if (fmttype & TYP_TAR) {
  1687. const char *cp;
  1688. int c;
  1689. /*
  1690. * Many SVR4 cpio derivatives expect the mode field
  1691. * to contain S_IFMT bits. The meaning of these bits
  1692. * in the mode field of the ustar header is left
  1693. * unspecified by IEEE Std 1003.1, 1996, 10.1.1.
  1694. */
  1695. int mmask = fmttype == FMT_USTAR || fmttype == FMT_PAX ?
  1696. 07777 : 07777|S_IFMT;
  1697. paxrec = globrec;
  1698. pad = 512;
  1699. if (tmkname(&bc.Tdr, file) != 0)
  1700. goto cleanup;
  1701. sprintf(bc.Tdr.t_mode, "%7.7o", (int)st->st_mode & mmask);
  1702. if (fmttype == FMT_GNUTAR && st->st_uid > 07777777) {
  1703. be64p(st->st_uid, bc.Tdr.t_uid);
  1704. bc.Tdr.t_uid[0] |= 0200;
  1705. } else {
  1706. sprintf(bc.Tdr.t_uid, "%7.7lo",
  1707. (long)st->st_uid&07777777);
  1708. if (fmttype & TYP_PAX && st->st_uid > 07777777)
  1709. paxrec |= PR_UID;
  1710. }
  1711. if (fmttype == FMT_GNUTAR && st->st_gid > 07777777) {
  1712. be64p(st->st_gid, bc.Tdr.t_gid);
  1713. bc.Tdr.t_gid[0] |= 0200;
  1714. } else {
  1715. sprintf(bc.Tdr.t_gid, "%7.7lo",
  1716. (long)st->st_gid&07777777);
  1717. if (fmttype & TYP_PAX && st->st_gid > 07777777)
  1718. paxrec |= PR_GID;
  1719. }
  1720. if (fmttype == FMT_GNUTAR && (st->st_mode&S_IFMT) == S_IFREG &&
  1721. st->st_size > 077777777777LL && !zerolink) {
  1722. bc.Tdr.t_size[0] = '\200';
  1723. be64p(st->st_size, &bc.Tdr.t_size[4]);
  1724. } else {
  1725. sprintf(bc.Tdr.t_size, "%11.11llo",
  1726. (st->st_mode&S_IFMT) == S_IFREG &&
  1727. (!zerolink || fmttype == FMT_PAX &&
  1728. pax_oflag & PO_LINKDATA) ?
  1729. (long long)st->st_size&077777777777LL : 0LL);
  1730. if (fmttype & TYP_PAX &&
  1731. (st->st_mode&S_IFMT) == S_IFREG &&
  1732. st->st_size > 077777777777LL &&
  1733. (!zerolink || fmttype == FMT_PAX &&
  1734. pax_oflag & PO_LINKDATA))
  1735. paxrec |= PR_SIZE;
  1736. }
  1737. sprintf(bc.Tdr.t_mtime, "%11.11lo", (long)st->st_mtime);
  1738. if ((c = tlflag(st)) < 0) {
  1739. if ((st->st_mode&S_IFMT) != S_IFDIR) {
  1740. msg(2, 0, "%s is not a file. Not dumped\n",
  1741. realfile);
  1742. errcnt++;
  1743. } else
  1744. failure = 0;
  1745. goto cleanup;
  1746. }
  1747. if (zerolink == 0) {
  1748. bc.Tdr.t_linkflag = c;
  1749. if (c == '2') {
  1750. if (tmklink(&bc.Tdr, symblink) != 0)
  1751. goto cleanup;
  1752. }
  1753. } else {
  1754. bc.Tdr.t_linkflag = '1';
  1755. if (tmklink(&bc.Tdr, linkname) != 0)
  1756. goto cleanup;
  1757. }
  1758. if (fmttype & TYP_USTAR) {
  1759. if (fmttype == FMT_GNUTAR)
  1760. strcpy(bc.Tdr.t_magic, mag_gnutar);
  1761. else {
  1762. strcpy(bc.Tdr.t_magic, mag_ustar);
  1763. bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
  1764. }
  1765. if ((cp = getuser(st->st_uid)) != NULL)
  1766. sprintf(bc.Tdr.t_uname, "%.31s", cp);
  1767. else
  1768. msg(1, 0, "could not get passwd information "
  1769. "for %s\n", realfile);
  1770. if ((cp = getgroup(st->st_gid)) != NULL)
  1771. sprintf(bc.Tdr.t_gname, "%.31s", cp);
  1772. else
  1773. msg(1, 0, "could not get group information "
  1774. "for %s\n", realfile);
  1775. if (fmttype == FMT_GNUTAR &&
  1776. (unsigned long long)major(st->st_rdev)
  1777. > 077777777) {
  1778. be64p(major(st->st_rdev), bc.Tdr.t_devmajor);
  1779. bc.Tdr.t_devmajor[0] |= 0200;
  1780. } else {
  1781. if (fmttype == FMT_SUN &&
  1782. (unsigned long long)major(st->st_rdev)
  1783. > 077777777 &&
  1784. ((st->st_mode&S_IFMT)==S_IFBLK||
  1785. (st->st_mode&S_IFMT)==S_IFCHR))
  1786. paxrec |= PR_SUN_DEVMAJOR;
  1787. sprintf(bc.Tdr.t_devmajor, "%7.7o",
  1788. (int)major(st->st_rdev)&07777777);
  1789. }
  1790. if (fmttype == FMT_GNUTAR &&
  1791. (unsigned long long)minor(st->st_rdev)
  1792. > 077777777) {
  1793. be64p(minor(st->st_rdev), bc.Tdr.t_devminor);
  1794. bc.Tdr.t_devminor[0] |= 0200;
  1795. } else {
  1796. if (fmttype == FMT_SUN &&
  1797. (unsigned long long)minor(st->st_rdev)
  1798. > 077777777 &&
  1799. ((st->st_mode&S_IFMT)==S_IFBLK||
  1800. (st->st_mode&S_IFMT)==S_IFCHR))
  1801. paxrec |= PR_SUN_DEVMINOR;
  1802. sprintf(bc.Tdr.t_devminor, "%7.7o",
  1803. (int)minor(st->st_rdev)&07777777);
  1804. }
  1805. }
  1806. tchksum(&bc);
  1807. hsz = 512;
  1808. psz = 0;
  1809. fsz = 0;
  1810. } else if (fmttype == FMT_ZIP) {
  1811. pad = 1;
  1812. memcpy(bc.Zdr.z_signature, mag_zipsig, sizeof mag_zipsig);
  1813. bc.Zdr.z_version[0] = 10;
  1814. mkdostime(st->st_mtime, bc.Zdr.z_modtime, bc.Zdr.z_moddate);
  1815. if ((st->st_mode&S_IFMT) == S_IFREG ||
  1816. (st->st_mode&S_IFMT) == S_IFLNK) {
  1817. le32p(size, bc.Zdr.z_csize);
  1818. le32p(size, bc.Zdr.z_nsize);
  1819. csize = size;
  1820. }
  1821. le16p(fsz, bc.Zdr.z_namelen);
  1822. le16p(SIZEOF_zextra_cp, bc.Zdr.z_extralen);
  1823. hsz = SIZEOF_zip_header;
  1824. psz = 0;
  1825. } else
  1826. abort();
  1827. /*
  1828. * Start writing the file to the archive.
  1829. */
  1830. if ((st->st_mode&S_IFMT) == S_IFREG && st->st_size != 0 &&
  1831. (zerolink == 0 || fmttype == FMT_PAX &&
  1832. pax_oflag & PO_LINKDATA)) {
  1833. char *buf;
  1834. size_t bufsize;
  1835. int readerr = 0;
  1836. if ((fd = open(realfile, O_RDONLY)) < 0) {
  1837. if (sysv3 < 0)
  1838. msg(0, 0, "< %s > ?\n", realfile);
  1839. else if (sysv3 > 0)
  1840. fprintf(stderr, "<%s> ?\n", realfile);
  1841. else
  1842. msg(0, 0, "\"%s\" ?\n", realfile);
  1843. goto cleanup;
  1844. }
  1845. if (fmttype == FMT_ZIP) {
  1846. if (zipwrite(fd, file, st, &bc, fsz, dev, ino,
  1847. &crc, &csize) < 0)
  1848. goto cleanup2;
  1849. goto done;
  1850. }
  1851. if (fmttype & TYP_CRC)
  1852. if (sum(fd, realfile, st, bc.Edr.E_chksum) < 0)
  1853. goto cleanup2;
  1854. if (fmttype & TYP_PAX && paxrec != PR_NONE)
  1855. wrpax(file, symblink?symblink:linkname, st);
  1856. bwrite(bc.data, hsz);
  1857. if (fsz)
  1858. bwrite(file, fsz);
  1859. if (psz)
  1860. bwrite(&bc.data[hsz], psz);
  1861. if (Kbase)
  1862. remsz = Kbase;
  1863. else
  1864. remsz = st->st_size;
  1865. getbuf(&buf, &bufsize, st->st_blksize);
  1866. again: while (remsz > 0) {
  1867. if (fd < 0 || (rsz = read(fd, &buf[wsz],
  1868. bufsize - wsz)) < 0) {
  1869. if (readerr == 0) {
  1870. emsg(3, "Cannot read \"%s\"", realfile);
  1871. if (fd >= 0)
  1872. errcnt++;
  1873. readerr = 1;
  1874. }
  1875. if (fd >= 0 && lseek(fd, bufsize - wsz,
  1876. SEEK_CUR) < 0) {
  1877. close(fd);
  1878. fd = -1;
  1879. }
  1880. rsz = bufsize - wsz;
  1881. if (rsz > remsz)
  1882. rsz = remsz;
  1883. memset(&buf[wsz], 0, rsz);
  1884. }
  1885. if (rsz > remsz)
  1886. rsz = remsz;
  1887. wsz += rsz;
  1888. remsz -= rsz;
  1889. bwrite(buf, wsz);
  1890. memset(buf, 0, wsz);
  1891. size = wsz;
  1892. wsz = 0;
  1893. }
  1894. wsz = size;
  1895. if (Kbase) {
  1896. if ((i = Ksize % pad) != 0)
  1897. bwrite(&bc.data[hsz], i);
  1898. bwrite((char *)&K2hdr, hsz);
  1899. if (fsz)
  1900. bwrite(file, fsz);
  1901. if (psz)
  1902. bwrite(&bc.data[hsz], psz);
  1903. remsz = Krest;
  1904. Kbase = 0;
  1905. wsz = 0;
  1906. goto again;
  1907. } else if (Ksize)
  1908. wsz = Krest;
  1909. } else if ((fmttype == FMT_ZIP || fmttype & TYP_CPIO) &&
  1910. (st->st_mode&S_IFMT) == S_IFLNK) {
  1911. wsz = size;
  1912. if (fmttype == FMT_ZIP) {
  1913. crc = zipcrc(0, (unsigned char *)symblink, wsz);
  1914. le32p(crc, bc.Zdr.z_crc32);
  1915. bwrite(bc.data, SIZEOF_zip_header);
  1916. bwrite(file, fsz);
  1917. zipwxtra(file, st, dev, ino);
  1918. bwrite(symblink, wsz);
  1919. } else {
  1920. bwrite(bc.data, hsz);
  1921. if (fsz)
  1922. bwrite(file, fsz);
  1923. if (psz)
  1924. bwrite(&bc.data[hsz], psz);
  1925. bwrite(symblink, wsz);
  1926. }
  1927. } else {
  1928. if (fmttype & TYP_PAX && paxrec != PR_NONE)
  1929. wrpax(file, symblink?symblink:linkname, st);
  1930. bwrite(bc.data, hsz);
  1931. if (fsz)
  1932. bwrite(file, fsz);
  1933. if (psz)
  1934. bwrite(&bc.data[hsz], psz);
  1935. if (fmttype == FMT_ZIP)
  1936. zipwxtra(file, st, dev, ino);
  1937. }
  1938. done: if (fmttype == FMT_ZIP) {
  1939. zipdefer(file, st, relative, crc, csize, &bc.Zdr);
  1940. }
  1941. if ((i = wsz % pad) != 0)
  1942. bwrite(&bc.data[hsz], pad - i);
  1943. if (vflag && strcmp(file, trailer))
  1944. fprintf(stderr, "%s\n", file);
  1945. else if (Vflag)
  1946. prdot(0);
  1947. failure = 0;
  1948. cleanup2:
  1949. if ((st->st_mode&S_IFMT) == S_IFREG) {
  1950. if (fd >= 0)
  1951. close(fd);
  1952. if (aflag)
  1953. errcnt += rstime(realfile, st, "access");
  1954. }
  1955. cleanup:
  1956. free(file);
  1957. free(symblink);
  1958. return failure;
  1959. }
  1960. /*
  1961. * Flush a SVR4 cpio format inode tree for -o.
  1962. */
  1963. static void
  1964. iflush(struct islot *ip, uint32_t dev)
  1965. {
  1966. if (ip == inull)
  1967. return;
  1968. iflush(ip->i_lln, dev);
  1969. iflush(ip->i_rln, dev);
  1970. if (ip->i_nlk > 0 && ip->i_st) {
  1971. struct ilink *il;
  1972. for (il = ip->i_lnk; il->l_nxt; il = il->l_nxt)
  1973. errcnt += addfile(il->l_nam, ip->i_st,
  1974. dev, ip->i_fino, 1, 0);
  1975. errcnt += addfile(il->l_nam, ip->i_st, dev, ip->i_fino, 0, 0);
  1976. }
  1977. }
  1978. /*
  1979. * Flush the SVR4 cpio link forest for -o.
  1980. */
  1981. static void
  1982. lflush(void)
  1983. {
  1984. struct dslot *ds;
  1985. for (ds = devices; ds; ds = ds->d_nxt)
  1986. iflush(ds->d_isl, ds->d_fake);
  1987. }
  1988. int
  1989. setfmt(char *s)
  1990. {
  1991. int i, j;
  1992. struct {
  1993. const char *ucs;
  1994. const char *lcs;
  1995. int type;
  1996. int bits;
  1997. } fs[] = {
  1998. { "NEWC", "newc", FMT_ASC, 00 },
  1999. { "SCO", "sco", FMT_SCOASC, 00 },
  2000. { "CRC", "crc", FMT_CRC, 00 },
  2001. { "SCOCRC", "scocrc", FMT_SCOCRC, 00 },
  2002. { "ODC", "odc", FMT_ODC, 00 },
  2003. { "DEC", "dec", FMT_DEC, 00 },
  2004. { "BIN", "bin", FMT_NONE, 00 },
  2005. { "BBS", "bbs", TYP_BE, 00 },
  2006. { "SGI", "sgi", FMT_SGIBE, 00 },
  2007. { "CRAY", "cray", FMT_CRAY, 00 },
  2008. { "CRAY5", "cray5", FMT_CRAY5, 00 },
  2009. { "TAR", "tar", FMT_TAR, 00 },
  2010. { "USTAR", "ustar", FMT_USTAR, 00 },
  2011. { "PAX:", "pax:", FMT_PAX, 00 },
  2012. { "SUN", "sun", FMT_SUN, 00 },
  2013. { "GNU", "gnu", FMT_GNUTAR, 00 },
  2014. { "OTAR", "otar", FMT_OTAR, 00 },
  2015. { "BAR", "bar", FMT_BAR, 00 },
  2016. { "ZIP:", "zip:", FMT_ZIP, 00 },
  2017. { NULL, NULL, 0, 00 }
  2018. };
  2019. for (i = 0; fs[i].ucs; i++) {
  2020. for (j = 0; s[j] &&
  2021. (s[j] == fs[i].ucs[j] || s[j] == fs[i].lcs[j]);
  2022. j++)
  2023. if (fs[i].ucs[j] == ':')
  2024. break;
  2025. if (s[j] == '\0' &&
  2026. (fs[i].ucs[j] == '\0' || fs[i].ucs[j] == ':') ||
  2027. s[j] == ':' && fs[i].ucs[j] == ':') {
  2028. fmttype = fs[i].type;
  2029. if (fmttype == FMT_ZIP && s[j] == ':') {
  2030. #if USE_ZLIB
  2031. if (strcmp(&s[j+1], "en") == 0)
  2032. zipclevel = 00;
  2033. else if (strcmp(&s[j+1], "ex") == 0)
  2034. zipclevel = 01;
  2035. else if (strcmp(&s[j+1], "ef") == 0)
  2036. zipclevel = 02;
  2037. else if (strcmp(&s[j+1], "es") == 0)
  2038. zipclevel = 03;
  2039. else
  2040. #endif /* USE_ZLIB */
  2041. if (strcmp(&s[j+1], "e0") == 0)
  2042. zipclevel = 04;
  2043. else
  2044. #if USE_BZLIB
  2045. if (strcmp(&s[j+1], "bz2") == 0)
  2046. zipclevel = 07;
  2047. else
  2048. #endif /* USE_BZLIB */
  2049. continue;
  2050. } else if (fmttype == FMT_NONE)
  2051. fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
  2052. else if (fmttype == TYP_BE)
  2053. fmttype = bigendian() ? FMT_BINLE : FMT_BINBE;
  2054. else if (fmttype == FMT_PAX && s[j] == ':') {
  2055. if (pax_options(&s[j+1], 0) < 0)
  2056. continue;
  2057. }
  2058. return 0;
  2059. }
  2060. }
  2061. msg(3, 0, "Invalid header \"%s\" specified.\n", s);
  2062. return -1;
  2063. }
  2064. static int
  2065. bigendian(void)
  2066. {
  2067. union {
  2068. char u_c[2];
  2069. int16_t u_i;
  2070. } u;
  2071. u.u_i = 1;
  2072. return u.u_c[1] == 1;
  2073. }
  2074. int
  2075. setreassign(const char *s)
  2076. {
  2077. struct passwd *pwd;
  2078. int val = 0;
  2079. if (myuid != 0) {
  2080. msg(3, 0, "R option only valid for super-user.\n");
  2081. val = -1;
  2082. }
  2083. if ((pwd = getpwnam(s)) == NULL) {
  2084. msg(3, 0, "\"%s\" is not a valid user id\n", s);
  2085. val = -1;
  2086. } else {
  2087. Ruid = pwd->pw_uid;
  2088. Rgid = pwd->pw_gid;
  2089. }
  2090. return val;
  2091. }
  2092. void *
  2093. srealloc(void *m, size_t n)
  2094. {
  2095. if ((m = realloc(m, n)) == NULL) {
  2096. write(2, "Out of memory.\n", 15);
  2097. _exit(sysv3 ? 2 : 3);
  2098. }
  2099. return m;
  2100. }
  2101. void *
  2102. smalloc(size_t n)
  2103. {
  2104. return srealloc(NULL, n);
  2105. }
  2106. void *
  2107. scalloc(size_t nmemb, size_t size)
  2108. {
  2109. void *vp;
  2110. if ((vp = calloc(nmemb, size)) == NULL) {
  2111. write(2, "Out of memory.\n", 15);
  2112. _exit(sysv3 ? 2 : 3);
  2113. }
  2114. return vp;
  2115. }
  2116. void *
  2117. svalloc(size_t n, int force)
  2118. {
  2119. static long pagesize;
  2120. void *vp;
  2121. if (pagesize == 0)
  2122. if ((pagesize = sysconf(_SC_PAGESIZE)) < 0)
  2123. pagesize = 4096;
  2124. if ((vp = memalign(pagesize, n)) == NULL && force) {
  2125. write(2, "Out of memory.\n", 15);
  2126. _exit(sysv3 ? 2 : 3);
  2127. }
  2128. return vp;
  2129. }
  2130. /*
  2131. * A single static buffer is used for intermediate copying from file
  2132. * data to the tape buffer and vice versa, for creating checksums, and
  2133. * for data transfer with -p.
  2134. */
  2135. static void
  2136. getbuf(char **bufp, size_t *sizep, size_t best)
  2137. {
  2138. static char *buf;
  2139. static size_t size;
  2140. if (size != best) {
  2141. if (buf)
  2142. free(buf);
  2143. size = best;
  2144. if (size == 0 || (buf = svalloc(size, 0)) == NULL)
  2145. buf = svalloc(size = 512, 1);
  2146. }
  2147. *bufp = buf;
  2148. *sizep = size;
  2149. }
  2150. static void
  2151. sevprnt(int sev)
  2152. {
  2153. if (printsev) switch (sev) {
  2154. case 1:
  2155. fprintf(stderr, "INFORM: ");
  2156. break;
  2157. case 2:
  2158. fprintf(stderr, "WARNING: ");
  2159. break;
  2160. case 3:
  2161. fprintf(stderr, "ERROR: ");
  2162. break;
  2163. case 4:
  2164. fprintf(stderr, "HALT: ");
  2165. break;
  2166. }
  2167. }
  2168. void
  2169. msg(int sev, int err, const char *fmt, ...)
  2170. {
  2171. va_list ap;
  2172. /*
  2173. * The error message should appear near the file it refers to.
  2174. */
  2175. if (tflag)
  2176. fflush(stdout);
  2177. else if (Vflag)
  2178. prdot(1);
  2179. if (sysv3 >= 0 && sev >= (printsev ? 0 : -1))
  2180. fprintf(stderr, "%s: ", progname);
  2181. sevprnt(sev);
  2182. va_start(ap, fmt);
  2183. vfprintf(stderr, fmt, ap);
  2184. va_end(ap);
  2185. if (err > 0)
  2186. done(err);
  2187. else if (err == -2) {
  2188. if (sysv3)
  2189. done(1);
  2190. usage();
  2191. }
  2192. }
  2193. void
  2194. emsg(int sev, const char *fmt, ...)
  2195. {
  2196. char _fmt[60];
  2197. int i, fl = sev & 030, n;
  2198. va_list ap;
  2199. sev &= ~030;
  2200. i = errno;
  2201. if (tflag)
  2202. fflush(stdout);
  2203. else if (Vflag)
  2204. prdot(1);
  2205. fprintf(stderr, "%s: ", progname);
  2206. sevprnt(sev);
  2207. va_start(ap, fmt);
  2208. if (sysv3) {
  2209. if (fmt[(n=strlen(fmt))-1] == '"' && fmt[n-2] == 's' &&
  2210. fmt[n-3] == '%' && fmt[n-4] == '"' &&
  2211. n < sizeof _fmt) {
  2212. strcpy(_fmt, fmt);
  2213. _fmt[n-1] = '>';
  2214. _fmt[n-4] = '<';
  2215. fmt = _fmt;
  2216. }
  2217. }
  2218. vfprintf(stderr, fmt, ap);
  2219. va_end(ap);
  2220. if (sysv3 > 0 && sev >= 0 && fl & 010)
  2221. fprintf(stderr, "\n\t%s\n", strerror(i));
  2222. else if (sysv3 < 0) {
  2223. if (fl & 020)
  2224. putc('\n', stderr);
  2225. else
  2226. fprintf(stderr, " (errno:%d)\n", i);
  2227. } else
  2228. fprintf(stderr, ", errno %d, %s\n", i, strerror(i));
  2229. }
  2230. static void
  2231. prdot(int flush)
  2232. {
  2233. static int column;
  2234. if (flush && column != 0 || column >= 50) {
  2235. write(action == 'o' && !Oflag ? 2 : 1, "\n", 1);
  2236. column = 0;
  2237. }
  2238. if (!flush) {
  2239. write(action == 'o' && !Oflag ? 2 : 1, ".", 1);
  2240. column++;
  2241. }
  2242. }
  2243. /*
  2244. * Ask the user for new media if applicable, or exit.
  2245. */
  2246. static void
  2247. newmedia(int err)
  2248. {
  2249. static int mediacnt = 1;
  2250. static char answer[PATH_MAX+1];
  2251. const char *mesf = action == 'i' ?
  2252. (Iflag && !sysv3 ? Iflag : "input") :
  2253. (Oflag && !sysv3 ? Oflag : "output");
  2254. char c;
  2255. int i, j;
  2256. if (mfl == 0 && close(mt) < 0) {
  2257. emsg(3, "Close error on \"%s\"", mesf);
  2258. errcnt++;
  2259. }
  2260. mfl = -1;
  2261. if ((mtst.st_mode&S_IFMT)!=S_IFCHR && (mtst.st_mode&S_IFMT)!=S_IFBLK ||
  2262. Dflag) {
  2263. if (action == 'o') {
  2264. switch (err) {
  2265. case 0:
  2266. break;
  2267. case EFBIG:
  2268. msg(3, 0, "ulimit reached for output file.\n");
  2269. break;
  2270. case ENOSPC:
  2271. msg(3, 0, "No space left for output file.\n");
  2272. break;
  2273. default:
  2274. msg(3, 0, "I/O error - cannot continue, "
  2275. "errno %d, %s\n",
  2276. err, strerror(err));
  2277. }
  2278. }
  2279. return;
  2280. }
  2281. if (err == ENOSPC || err == ENXIO || err == 0)
  2282. msg(-2, 0, sysv3 ? "Reached end of medium on %s.\a\n" :
  2283. "End of medium on \"%s\".\a\n", mesf);
  2284. else
  2285. msg(3, 0, "I/O error on \"%s\", errno %d, %s\a\n", mesf,
  2286. err, strerror(err));
  2287. mediacnt++;
  2288. while (mfl < 0) {
  2289. if (Iflag || Oflag)
  2290. msg(-2, 0, Mflag ? Mflag :
  2291. "Change to part %d and press "
  2292. "RETURN key. [q] ", mediacnt);
  2293. else
  2294. msg(-2, 0, sysv3 ? "If you want to go on, "
  2295. "type device/file name when ready.\n" :
  2296. "To continue, type device/file name "
  2297. "when ready.\n");
  2298. if (tty == 0)
  2299. if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
  2300. fcntl(tty, F_SETFD, FD_CLOEXEC) < 0) {
  2301. cantrt: errcnt++;
  2302. msg(4, 1, "Cannot read tty.\n");
  2303. }
  2304. i = 0;
  2305. while ((j = read(tty, &c, 1)) == 1 && c != '\n')
  2306. if (i < sizeof answer - 1)
  2307. answer[i++] = c;
  2308. if (j != 1)
  2309. goto cantrt;
  2310. answer[i] = 0;
  2311. if (Iflag || Oflag) {
  2312. if (answer[0] == '\0')
  2313. snprintf(answer, sizeof answer, Iflag ? Iflag :
  2314. Oflag);
  2315. else if (answer[0] == 'q')
  2316. exit(errcnt != 0 ? sysv3 ? 1 : 2 : 0);
  2317. else if (Iflag)
  2318. Iflag = sstrdup(answer);
  2319. else if (Oflag)
  2320. Oflag = sstrdup(answer);
  2321. } else if (answer[0] == '\0')
  2322. return;
  2323. if ((mt = action == 'i' ? open(answer, O_RDONLY) :
  2324. creat(answer, 0666)) < 0) {
  2325. if (sysv3)
  2326. msg(-2, 0, "That didn't work, "
  2327. "cannot open \"%s\"\n%s\n",
  2328. answer, strerror(errno));
  2329. else
  2330. emsg(3, "Cannot open \"%s\"", answer);
  2331. }
  2332. else
  2333. mfl = 0;
  2334. }
  2335. mstat();
  2336. }
  2337. static void
  2338. mclose(void)
  2339. {
  2340. if (action == 'o' && mt >= 0 && close(mt) < 0) {
  2341. emsg(3, "Close error on \"%s\"",
  2342. Oflag && !sysv3 ? Oflag : "output");
  2343. errcnt++;
  2344. }
  2345. }
  2346. /*
  2347. * Write the archive buffer to tape.
  2348. */
  2349. static ssize_t
  2350. mwrite(int max)
  2351. {
  2352. ssize_t wo, wt = 0;
  2353. do {
  2354. if ((wo = write(mt, blkbuf + wt, (max?max:blksiz) - wt)) < 0) {
  2355. if (errno == EINTR) {
  2356. continue;
  2357. } else {
  2358. newmedia(errno);
  2359. if (mfl == 0) {
  2360. if (fmttype & TYP_BAR)
  2361. dump_barhdr();
  2362. continue;
  2363. }
  2364. else
  2365. done(1);
  2366. }
  2367. }
  2368. poffs += wo;
  2369. wt += wo;
  2370. } while (wt < (max?max:blksiz));
  2371. blocks += wt >> 9;
  2372. bytes += wt & 0777;
  2373. return wt;
  2374. }
  2375. /*
  2376. * Buffered writes to tape.
  2377. */
  2378. static void
  2379. bwrite(const char *data, size_t sz)
  2380. {
  2381. size_t di;
  2382. nwritten += sz;
  2383. while (curpos + sz > blksiz) {
  2384. di = blksiz - curpos;
  2385. sz -= di;
  2386. memcpy(&blkbuf[curpos], data, di);
  2387. mwrite(0);
  2388. data += di;
  2389. curpos = 0;
  2390. }
  2391. memcpy(&blkbuf[curpos], data, sz);
  2392. curpos += sz;
  2393. }
  2394. /*
  2395. * Flush the tape write buffer.
  2396. */
  2397. static void
  2398. bflush(void)
  2399. {
  2400. if (curpos) {
  2401. memset(&blkbuf[curpos], 0, blksiz - curpos);
  2402. mwrite(fmttype==FMT_ZIP && (mtst.st_mode&S_IFMT) == S_IFREG ?
  2403. curpos : 0);
  2404. }
  2405. curpos = 0;
  2406. }
  2407. /*
  2408. * CRC format checksum calculation with -i.
  2409. */
  2410. static int
  2411. sum(int fd, const char *fn, struct stat *sp, char *tg)
  2412. {
  2413. char *buf;
  2414. size_t bufsize;
  2415. uint32_t size = sp->st_size, sum = 0;
  2416. ssize_t rd;
  2417. char c;
  2418. getbuf(&buf, &bufsize, sp->st_blksize);
  2419. /*
  2420. * Unfortunately, SVR4 cpio derivatives (as on Solaris 8 and
  2421. * UnixWare 2.1) compute the checksum of signed char values,
  2422. * whereas GNU cpio and the pax implementations of AT&T and
  2423. * BSD use unsigned chars. Since there is no 'open' standard
  2424. * for the SVR4 CRC format, the SVR4 implementation should be
  2425. * taken as a de facto reference and we thus create SVR4 type
  2426. * checksums.
  2427. */
  2428. while ((rd = read(fd, buf, size>bufsize ? bufsize : size )) > 0) {
  2429. size -= rd;
  2430. do
  2431. sum += ((signed char *)buf)[--rd];
  2432. while (rd);
  2433. }
  2434. if (rd < 0) {
  2435. msg(3, 0, "Error computing checksum\n");
  2436. return 1;
  2437. }
  2438. if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
  2439. emsg(3, "Cannot reset file after checksum");
  2440. return 1;
  2441. }
  2442. c = tg[8];
  2443. sprintf(tg, "%08lx", (long)sum);
  2444. tg[8] = c;
  2445. return 0;
  2446. }
  2447. static int
  2448. rstime(const char *fn, struct stat *st, const char *which)
  2449. {
  2450. struct utimbuf utb;
  2451. utb.actime = st->st_atime;
  2452. utb.modtime = st->st_mtime;
  2453. if (pax != PAX_TYPE_CPIO &&
  2454. (pax_preserve&(PAX_P_ATIME|PAX_P_MTIME)) != 0 &&
  2455. (pax_preserve&PAX_P_EVERY) == 0) {
  2456. struct stat xst;
  2457. if (stat(fn, &xst) < 0)
  2458. goto fail;
  2459. if (pax_preserve&PAX_P_ATIME)
  2460. utb.actime = xst.st_atime;
  2461. if (pax_preserve&PAX_P_MTIME)
  2462. utb.modtime = xst.st_mtime;
  2463. }
  2464. if (utime(fn, &utb) < 0) {
  2465. fail: emsg(2, "Unable to reset %s time for \"%s\"", which, fn);
  2466. return 1;
  2467. }
  2468. return 0;
  2469. }
  2470. /*
  2471. * Top-down splay function for inode tree.
  2472. */
  2473. static struct islot *
  2474. isplay(ino_t ino, struct islot *x)
  2475. {
  2476. struct islot hdr;
  2477. struct islot *leftmax, *rightmin;
  2478. struct islot *y;
  2479. hdr.i_lln = hdr.i_rln = inull;
  2480. leftmax = rightmin = &hdr;
  2481. inull->i_ino = ino;
  2482. while (ino != x->i_ino) {
  2483. if (ino < x->i_ino) {
  2484. if (ino < x->i_lln->i_ino) {
  2485. y = x->i_lln;
  2486. x->i_lln = y->i_rln;
  2487. y->i_rln = x;
  2488. x = y;
  2489. }
  2490. if (x->i_lln == inull)
  2491. break;
  2492. rightmin->i_lln = x;
  2493. rightmin = x;
  2494. x = x->i_lln;
  2495. } else {
  2496. if (ino > x->i_rln->i_ino) {
  2497. y = x->i_rln;
  2498. x->i_rln = y->i_lln;
  2499. y->i_lln = x;
  2500. x = y;
  2501. }
  2502. if (x->i_rln == inull)
  2503. break;
  2504. leftmax->i_rln = x;
  2505. leftmax = x;
  2506. x = x->i_rln;
  2507. }
  2508. }
  2509. leftmax->i_rln = x->i_lln;
  2510. rightmin->i_lln = x->i_rln;
  2511. x->i_lln = hdr.i_rln;
  2512. x->i_rln = hdr.i_lln;
  2513. inull->i_ino = !ino;
  2514. return x;
  2515. }
  2516. /*
  2517. * Find the inode number ino.
  2518. */
  2519. static struct islot *
  2520. ifind(ino_t ino, struct islot **it)
  2521. {
  2522. if (*it == NULL)
  2523. return NULL;
  2524. *it = isplay(ino, *it);
  2525. return (*it)->i_ino == ino ? *it : NULL;
  2526. }
  2527. /*
  2528. * Put ik into the tree.
  2529. */
  2530. static void
  2531. iput(struct islot *ik, struct islot **it)
  2532. {
  2533. if ((*it) == NULL) {
  2534. ik->i_lln = ik->i_rln = inull;
  2535. (*it) = ik;
  2536. } else {
  2537. /* ifind() is always called before */
  2538. /*(*it) = isplay(ik->i_ino, (*it));*/
  2539. if (ik->i_ino < (*it)->i_ino) {
  2540. ik->i_lln = (*it)->i_lln;
  2541. ik->i_rln = (*it);
  2542. (*it)->i_lln = inull;
  2543. (*it) = ik;
  2544. } else if ((*it)->i_ino < ik->i_ino) {
  2545. ik->i_rln = (*it)->i_rln;
  2546. ik->i_lln = (*it);
  2547. (*it)->i_rln = inull;
  2548. (*it) = ik;
  2549. }
  2550. }
  2551. }
  2552. /*
  2553. * Find the device dev or add it to the device/inode forest if not
  2554. * already present.
  2555. */
  2556. static struct dslot *
  2557. dfind(struct dslot **root, dev_t dev)
  2558. {
  2559. struct dslot *ds, *dp;
  2560. for (ds = *root, dp = NULL; ds; dp = ds, ds = ds->d_nxt)
  2561. if (ds->d_dev == dev)
  2562. break;
  2563. if (ds == NULL) {
  2564. ds = scalloc(1, sizeof *ds);
  2565. ds->d_dev = dev;
  2566. if (*root == NULL)
  2567. *root = ds;
  2568. else
  2569. dp->d_nxt = ds;
  2570. }
  2571. return ds;
  2572. }
  2573. /*
  2574. * Exit on fatal error conditions.
  2575. */
  2576. static void
  2577. done(int i)
  2578. {
  2579. if (tflag)
  2580. fflush(stdout);
  2581. errcnt += i;
  2582. mclose();
  2583. if (errcnt && !sysv3)
  2584. fprintf(stderr, "%llu errors\n", errcnt);
  2585. exit(sysv3 ? 2 : 3);
  2586. }
  2587. static char *pcopy, *pcend;
  2588. static size_t psz, pslen, pss;
  2589. /*
  2590. * Execution for -p.
  2591. */
  2592. static void
  2593. dopass(const char *target)
  2594. {
  2595. struct stat st;
  2596. if (access(target, W_OK) < 0) {
  2597. emsg(033, sysv3 ? "cannot write in <%s>" :
  2598. "Error during access() of \"%s\"", target);
  2599. if (sysv3)
  2600. done(1);
  2601. usage();
  2602. }
  2603. if (stat(target, &st) < 0) {
  2604. emsg(023, "Error during stat() of \"%s\"", target);
  2605. done(1);
  2606. }
  2607. if ((st.st_mode&S_IFMT) != S_IFDIR)
  2608. msg(3, 1, sysv3 ? "<%s> not a directory.\n" :
  2609. "\"%s\" is not a directory\n", target);
  2610. getpath(target, &pcopy, &pcend, &psz, &pslen);
  2611. copyout(passfile);
  2612. }
  2613. /*
  2614. * Callback for sfile().
  2615. */
  2616. /*ARGSUSED*/
  2617. void
  2618. writerr(void *vp, int count, int written)
  2619. {
  2620. }
  2621. /*
  2622. * Copy file data of regular files with -p.
  2623. */
  2624. static int
  2625. passdata(struct file *f, const char *tgt, int tfd)
  2626. {
  2627. char *buf;
  2628. size_t bufsize;
  2629. ssize_t rd = 0;
  2630. if (f->f_fd < 0) /* is a zero-sized unreadable file */
  2631. return 0;
  2632. #ifdef __linux__
  2633. if (f->f_st.st_size > 0) {
  2634. long long sent;
  2635. sent = sfile(tfd, f->f_fd, f->f_st.st_mode, f->f_st.st_size);
  2636. blocks += (sent + 0777) >> 9;
  2637. if (sent == f->f_st.st_size)
  2638. return 0;
  2639. if (sent < 0)
  2640. goto rerr;
  2641. }
  2642. #endif /* __linux__ */
  2643. getbuf(&buf, &bufsize, f->f_st.st_blksize);
  2644. while ((rd = read(f->f_fd, buf, bufsize)) > 0) {
  2645. blocks += (rd + 0777) >> 9;
  2646. if (write(tfd, buf, rd) != rd) {
  2647. emsg(3, "Cannot write \"%s\"", tgt);
  2648. return -1;
  2649. }
  2650. }
  2651. if (rd < 0) {
  2652. #ifdef __linux__
  2653. rerr:
  2654. #endif /* __linux__ */
  2655. emsg(3, "Cannot read \"%s\"", f->f_name);
  2656. return -1;
  2657. }
  2658. return 0;
  2659. }
  2660. /*
  2661. * Handle a single file for -p.
  2662. */
  2663. static int
  2664. passfile(const char *fn, struct stat *st)
  2665. {
  2666. struct file f;
  2667. ssize_t sz;
  2668. int val;
  2669. char *newfn;
  2670. size_t newsz = 0;
  2671. newfn = sstrdup(fn);
  2672. if (pax != PAX_TYPE_CPIO) {
  2673. if (pax_sflag && pax_sname(&newfn, &newsz) == 0)
  2674. return 0;
  2675. if (rflag && rname(&newfn, &newsz) == 0)
  2676. return 0;
  2677. }
  2678. setpath(newfn, &pcopy, &pcend, pslen, &psz, &pss);
  2679. free(newfn);
  2680. memset(&f, 0, sizeof f);
  2681. f.f_name = sstrdup(fn);
  2682. f.f_nsiz = strlen(fn) + 1;
  2683. f.f_st = *st;
  2684. f.f_fd = -1;
  2685. if ((st->st_mode&S_IFMT) == S_IFLNK) {
  2686. sz = st->st_size ? st->st_size : PATH_MAX;
  2687. f.f_lnam = smalloc(sz+1);
  2688. if ((sz = readlink(fn, f.f_lnam, sz+1)) < 0) {
  2689. emsg(3, "Cannot read symbolic link \"%s\"", fn);
  2690. free(f.f_lnam);
  2691. return 1;
  2692. }
  2693. f.f_lnam[sz] = '\0';
  2694. } else if ((st->st_mode&S_IFMT) == S_IFREG) {
  2695. if ((f.f_fd = open(fn, O_RDONLY)) < 0) {
  2696. if (sysv3)
  2697. msg(2, 0, "Cannot open file \"%s\".\n", fn);
  2698. else
  2699. emsg(2, "Cannot open \"%s\", skipped", fn);
  2700. if (st->st_size != 0)
  2701. return 1;
  2702. }
  2703. }
  2704. val = filein(&f, passdata, pcopy);
  2705. if (f.f_lnam)
  2706. free(f.f_lnam);
  2707. free(f.f_name);
  2708. if (f.f_fd >= 0)
  2709. close(f.f_fd);
  2710. if (val <= 1 && aflag && (st->st_mode&S_IFMT) == S_IFREG)
  2711. errcnt += rstime(fn, st, "access");
  2712. return val != 0;
  2713. }
  2714. /*
  2715. * Processing of a single file common to -i and -p. Return value: 0 if
  2716. * successful, 1 at failure if data was read, 2 at failure if no data
  2717. * was read.
  2718. */
  2719. static int
  2720. filein(struct file *f, int (*copydata)(struct file *, const char *, int),
  2721. char *tgt)
  2722. {
  2723. struct stat nst;
  2724. char *temp = NULL;
  2725. size_t len;
  2726. int fd, i, j, new;
  2727. int failure = 2;
  2728. if (fmttype == FMT_ZIP && (f->f_st.st_mode&S_IFMT) != S_IFREG &&
  2729. (f->f_st.st_mode&S_IFMT) != S_IFLNK &&
  2730. (f->f_csize > 0 || f->f_gflag & FG_DESC))
  2731. skipfile(f);
  2732. if ((new = lstat(tgt, &nst)) == 0) {
  2733. if (action == 'p' && f->f_st.st_dev == nst.st_dev &&
  2734. f->f_st.st_ino == nst.st_ino) {
  2735. msg(3, 0, sysv3 ?
  2736. "Attempt to pass file to self!\n" :
  2737. "Attempt to pass a file to itself.\n");
  2738. return 1;
  2739. }
  2740. if ((f->f_st.st_mode&S_IFMT) == S_IFDIR) {
  2741. if ((nst.st_mode&S_IFMT) == S_IFDIR)
  2742. return setattr(tgt, &f->f_st);
  2743. rmdir(tgt);
  2744. } else {
  2745. if (pax_kflag) {
  2746. failure = 0;
  2747. goto skip;
  2748. }
  2749. if (uflag == 0 && f->f_st.st_mtime <= nst.st_mtime) {
  2750. if (pax == PAX_TYPE_CPIO)
  2751. msg(-1, 0, sysv3 ?
  2752. "current <%s> newer or same age\n" :
  2753. "Existing \"%s\" same age or newer\n",
  2754. tgt);
  2755. else
  2756. failure = 0;
  2757. goto skip;
  2758. }
  2759. }
  2760. } else {
  2761. if (imdir(tgt) < 0)
  2762. goto skip;
  2763. }
  2764. if (Vflag && !vflag)
  2765. prdot(0);
  2766. if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && lflag) {
  2767. if (Lflag) {
  2768. char *symblink, *name;
  2769. struct stat xst;
  2770. name = f->f_name;
  2771. for (;;) {
  2772. if (lstat(name, &xst) < 0) {
  2773. emsg(3, "Cannot lstat \"%s\"", name);
  2774. if (name != f->f_name)
  2775. free(name);
  2776. goto cantlink;
  2777. }
  2778. if ((xst.st_mode&S_IFMT) != S_IFLNK)
  2779. break;
  2780. i = xst.st_size ? xst.st_size : PATH_MAX;
  2781. symblink = smalloc(i+1);
  2782. if ((j = readlink(name, symblink, i)) < 0) {
  2783. emsg(3, "Cannot read symbolic link "
  2784. "\"%s\"", name);
  2785. free(symblink);
  2786. if (name != f->f_name)
  2787. free(name);
  2788. goto cantlink;
  2789. }
  2790. symblink[j] = '\0';
  2791. symblink = joinpath(name, symblink);
  2792. if (name != f->f_name)
  2793. free(name);
  2794. name = symblink;
  2795. }
  2796. if (linkunlink(name, tgt) == 0) {
  2797. if (vflag)
  2798. fprintf(stderr, "%s\n", tgt);
  2799. if (name != f->f_name)
  2800. free(name);
  2801. return 0;
  2802. }
  2803. if (name != f->f_name)
  2804. free(name);
  2805. } else if (linkunlink(f->f_name, tgt) == 0) {
  2806. if (vflag)
  2807. fprintf(stderr, "%s\n", tgt);
  2808. return 0;
  2809. }
  2810. cantlink: errcnt += 1;
  2811. }
  2812. if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && f->f_st.st_nlink > 1 &&
  2813. (fmttype & TYP_CPIO || fmttype == FMT_ZIP
  2814. || action == 'p') &&
  2815. (i = canlink(tgt, &f->f_st, 1)) != 0) {
  2816. if (i < 0)
  2817. goto skip;
  2818. /*
  2819. * At this point, hard links in SVR4 cpio format have
  2820. * been reordered and data is associated with the first
  2821. * link; remaining links have st_size == 0 so don't
  2822. * overwrite the data here.
  2823. */
  2824. if (fmttype & TYP_NCPIO && f->f_st.st_size == 0 ||
  2825. (f->f_st.st_mode&S_IFMT) != S_IFREG) {
  2826. if (vflag)
  2827. fprintf(stderr, "%s\n", f->f_name);
  2828. return 0;
  2829. }
  2830. /*
  2831. * Make sure we can creat() this file later.
  2832. */
  2833. chmod(tgt, 0600);
  2834. } else if (fmttype & TYP_TAR && f->f_st.st_nlink > 1) {
  2835. if (linkunlink(f->f_lnam, f->f_name) == 0) {
  2836. if (fmttype & TYP_USTAR && f->f_st.st_size > 0)
  2837. chmod(tgt, 0600);
  2838. else {
  2839. if (vflag)
  2840. fprintf(stderr, "%s\n", f->f_name);
  2841. return 0;
  2842. }
  2843. } else {
  2844. goto restore;
  2845. }
  2846. } else if (new == 0 && (f->f_st.st_mode&S_IFMT) != S_IFDIR) {
  2847. len = strlen(tgt);
  2848. temp = smalloc(len + 7);
  2849. strcpy(temp, tgt);
  2850. strcpy(&temp[len], "XXXXXX");
  2851. if ((fd = mkstemp(temp)) < 0 || close(fd) < 0) {
  2852. emsg(3, "Cannot create temporary file");
  2853. if (fd < 0) {
  2854. free(temp);
  2855. temp = NULL;
  2856. }
  2857. goto skip;
  2858. }
  2859. cur_ofile = tgt;
  2860. cur_tfile = temp;
  2861. if (rename(tgt, temp) < 0) {
  2862. emsg(3, "Cannot rename current \"%s\"", tgt);
  2863. tunlink(&temp);
  2864. goto skip;
  2865. }
  2866. }
  2867. switch (f->f_st.st_mode & S_IFMT) {
  2868. case S_IFDIR:
  2869. if (!dflag) {
  2870. if (action == 'p')
  2871. msg(-1, 0, "Use -d option to copy \"%s\"\n",
  2872. f->f_name);
  2873. goto restore;
  2874. }
  2875. if (mkdir(tgt, 0777) < 0 && errno != EEXIST) {
  2876. emsg(-1, "Cannot create directory \"%s\"", tgt);
  2877. goto restore;
  2878. }
  2879. break;
  2880. case S_IFLNK:
  2881. if (symlink(f->f_lnam, tgt) < 0) {
  2882. emsg(3, "Cannot create \"%s\"", tgt);
  2883. goto restore;
  2884. }
  2885. break;
  2886. case S_IFREG:
  2887. if (temp && f->f_fd < 0)
  2888. goto restore;
  2889. cur_ofile = tgt;
  2890. if ((fd = (compressed_bar ? zcreat : creat)(tgt,
  2891. f->f_st.st_mode & 0777)) < 0) {
  2892. emsg(3, "Cannot create \"%s\"", tgt);
  2893. goto skip;
  2894. }
  2895. failure = 1;
  2896. if (copydata(f, tgt, fd) != 0) {
  2897. close(fd);
  2898. goto restore;
  2899. }
  2900. if ((compressed_bar ? zclose : close)(fd) < 0) {
  2901. emsg(3, "Close error on \"%s\"", tgt);
  2902. goto restore;
  2903. }
  2904. break;
  2905. case S_IFBLK:
  2906. case S_IFCHR:
  2907. case S_IFIFO:
  2908. case S_IFNAM:
  2909. case S_IFNWK:
  2910. if (mknod(tgt, f->f_st.st_mode&(S_IFMT|0777),
  2911. f->f_st.st_rdev) < 0) {
  2912. emsg(3, "Cannot mknod() \"%s\"", tgt);
  2913. goto restore;
  2914. }
  2915. break;
  2916. default:
  2917. msg(-1, 0, "Impossible file type\n");
  2918. goto skip;
  2919. }
  2920. if (vflag)
  2921. fprintf(stderr, "%s\n", f->f_name);
  2922. tunlink(&temp);
  2923. return setattr(tgt, &f->f_st);
  2924. skip: if (copydata == indata)
  2925. skipdata(f, copydata);
  2926. restore:
  2927. if (temp) {
  2928. if (rename(temp, tgt) < 0) {
  2929. emsg(3, "Cannot recover original version of \"%s\"",
  2930. tgt);
  2931. tunlink(&temp);
  2932. } else
  2933. fprintf(stderr, "Restoring existing \"%s\"\n", tgt);
  2934. cur_tfile = cur_ofile = NULL;
  2935. }
  2936. return failure;
  2937. }
  2938. static int
  2939. linkunlink(const char *path1, const char *path2)
  2940. {
  2941. int twice = 0;
  2942. do {
  2943. if (link(path1, path2) == 0) {
  2944. if (vflag && pax == PAX_TYPE_CPIO)
  2945. printf("%s linked to %s\n", path1, path2);
  2946. return 0;
  2947. }
  2948. if (errno == EEXIST && unlink(path2) < 0)
  2949. emsg(3, sysv3 ? "cannot unlink <%s>" :
  2950. "Error cannot unlink \"%s\"", path2);
  2951. } while (twice++ == 0);
  2952. emsg(023, sysv3 ? "Cannot link <%s> & <%s>" :
  2953. "Cannot link \"%s\" and \"%s\"", path1, path2);
  2954. return -1;
  2955. }
  2956. static void
  2957. tunlink(char **fn)
  2958. {
  2959. cur_tfile = cur_ofile = NULL;
  2960. if (*fn) {
  2961. if (unlink(*fn) < 0)
  2962. emsg(3, "Cannot unlink() temp file \"%s\"", *fn);
  2963. free(*fn);
  2964. *fn = NULL;
  2965. }
  2966. }
  2967. /*
  2968. * For -it[v] option.
  2969. */
  2970. static int
  2971. filet(struct file *f, int (*copydata)(struct file *, const char *, int))
  2972. {
  2973. if (vflag && (
  2974. (fmttype == FMT_ZIP && f->f_gflag & FG_DESC &&
  2975. (f->f_cmethod == C_DEFLATED || f->f_cmethod == C_ENHDEFLD) &&
  2976. (f->f_gflag & FG_CRYPT) == 0) ||
  2977. f->f_Kbase)) {
  2978. /*
  2979. * Need to read zip data descriptor located after the
  2980. * file contents in order to know the file size. Or
  2981. * need to read second header of -K archive.
  2982. */
  2983. int i;
  2984. i = skipdata(f, copydata);
  2985. filev(f);
  2986. return i;
  2987. }
  2988. if (vflag)
  2989. filev(f);
  2990. else
  2991. puts(f->f_name);
  2992. return skipdata(f, copydata);
  2993. }
  2994. static void
  2995. filev(struct file *f)
  2996. {
  2997. const char *cp;
  2998. long c;
  2999. if (pax == PAX_TYPE_CPIO && fmttype & TYP_TAR && f->f_st.st_nlink > 1)
  3000. printf("%s linked to %s\n", f->f_lnam, f->f_name);
  3001. if (sysv3)
  3002. printf("%-6o", (int)f->f_st.st_mode&(07777|S_IFMT));
  3003. else {
  3004. c = typec(&f->f_st);
  3005. putchar(c);
  3006. permbits(f->f_st.st_mode);
  3007. }
  3008. if (fmttype == FMT_ZIP && vflag > 1)
  3009. zipinfo(f);
  3010. else {
  3011. if (sysv3 == 0)
  3012. printf(" %3d", fmttype&TYP_TAR &&
  3013. (f->f_st.st_mode&S_IFMT) == S_IFLNK ?
  3014. 2 : (int)f->f_st.st_nlink);
  3015. if ((cp = getuser(f->f_st.st_uid)) != NULL)
  3016. printf(sysv3 ? " %-6s" : " %-8s", cp);
  3017. else
  3018. printf(sysv3 ? " %-6lu" : " %-8lu",
  3019. (long)f->f_st.st_uid);
  3020. if (sysv3 == 0) {
  3021. if ((cp = getgroup(f->f_st.st_gid)) != NULL)
  3022. printf(" %-8s", cp);
  3023. else
  3024. printf(" %-8lu", (long)f->f_st.st_gid);
  3025. }
  3026. }
  3027. if (sysv3 || (f->f_st.st_mode&S_IFMT)!=S_IFCHR &&
  3028. (f->f_st.st_mode&S_IFMT)!=S_IFBLK &&
  3029. (f->f_st.st_mode&S_IFMT)!=S_IFNAM &&
  3030. (f->f_st.st_mode&S_IFMT)!=S_IFNWK)
  3031. printf(pax != PAX_TYPE_CPIO ? "%8llu" :
  3032. sysv3 ? "%7llu" : " %-7llu", f->f_dsize);
  3033. else
  3034. printf(" %3lu,%3lu", (long)f->f_rmajor, (long)f->f_rminor);
  3035. prtime(f->f_st.st_mtime);
  3036. printf("%s", f->f_name);
  3037. if ((f->f_st.st_mode&S_IFMT) == S_IFLNK)
  3038. printf(" -> %s", f->f_lnam);
  3039. if (pax != PAX_TYPE_CPIO && (f->f_st.st_mode&S_IFMT) != S_IFDIR &&
  3040. f->f_st.st_nlink>1) {
  3041. if (fmttype & TYP_TAR)
  3042. printf(" == %s", f->f_lnam);
  3043. else
  3044. canlink(f->f_name, &f->f_st, 0);
  3045. }
  3046. putchar('\n');
  3047. }
  3048. static int
  3049. typec(struct stat *st)
  3050. {
  3051. switch (st->st_mode&S_IFMT) {
  3052. case S_IFREG:
  3053. return '-';
  3054. case S_IFDIR:
  3055. return 'd';
  3056. case S_IFLNK:
  3057. return 'l';
  3058. case S_IFCHR:
  3059. return 'c';
  3060. case S_IFBLK:
  3061. return 'b';
  3062. case S_IFIFO:
  3063. return 'p';
  3064. case S_IFNWK:
  3065. return 'n';
  3066. case S_IFNAM:
  3067. switch (st->st_rdev) {
  3068. case S_INSEM:
  3069. return 's';
  3070. case S_INSHD:
  3071. return 'm';
  3072. }
  3073. /*FALLTHRU*/
  3074. default:
  3075. msg(-1, 0, "Impossible file type\n");
  3076. errcnt++;
  3077. return '-';
  3078. }
  3079. }
  3080. static void
  3081. permbits(mode_t mode)
  3082. {
  3083. mode_t mask = 0700, shft = 6;
  3084. while (mask) {
  3085. if (((mode & mask) >> shft) & 04)
  3086. putchar('r');
  3087. else
  3088. putchar('-');
  3089. if (((mode & mask) >> shft) & 02)
  3090. putchar('w');
  3091. else
  3092. putchar('-');
  3093. if (mask == 0700 && mode & 04000)
  3094. putchar('s');
  3095. else if (mask == 0070 && (mode & 02010) == 02010)
  3096. putchar('s');
  3097. else if (mask == 0070 && mode & 02000)
  3098. putchar('l');
  3099. else if (mask == 0007 && mode & 01000)
  3100. putchar('t');
  3101. else if (((mode & mask) >> shft) & 01)
  3102. putchar('x');
  3103. else
  3104. putchar('-');
  3105. mask >>= 3;
  3106. shft -= 3;
  3107. }
  3108. }
  3109. static void
  3110. prtime_cpio(time_t t)
  3111. {
  3112. char b[30];
  3113. strftime(b, sizeof b, sysv3 ? "%b %e %H:%M:%S %Y" : "%b %e %H:%M %Y",
  3114. localtime(&t));
  3115. printf(sysv3 ? " %s " : " %s, ", b);
  3116. }
  3117. /*
  3118. * Prepare path to add names of files below it later.
  3119. */
  3120. static void
  3121. getpath(const char *path, char **file, char **filend, size_t *sz, size_t *slen)
  3122. {
  3123. *sz = 14 + strlen(path) + 2;
  3124. *file = smalloc(*sz);
  3125. *filend = *file;
  3126. if (path[0] == '/' && path[1] == '\0')
  3127. *(*filend)++ = '/';
  3128. else {
  3129. const char *cp = path;
  3130. while ((*(*filend)++ = *cp++) != '\0');
  3131. (*filend)[-1] = '/';
  3132. }
  3133. *slen = *filend - *file;
  3134. }
  3135. /*
  3136. * Concatenate base (prepared with getpath()) and file.
  3137. */
  3138. static void
  3139. setpath(const char *base, char **file, char **filend,
  3140. size_t slen, size_t *sz, size_t *ss)
  3141. {
  3142. if (slen + (*ss = strlen(base)) >= *sz) {
  3143. *sz += slen + *ss + 15;
  3144. *file = srealloc(*file, *sz);
  3145. *filend = &(*file)[slen];
  3146. }
  3147. strcpy(*filend, base);
  3148. }
  3149. /*
  3150. * Create intermediate directories with -m.
  3151. */
  3152. static int
  3153. imdir(char *fn)
  3154. {
  3155. struct stat st;
  3156. char *cp;
  3157. int dfl = dflag != 0;
  3158. for (cp = fn; *cp == '/'; cp++);
  3159. do {
  3160. while (*cp && *cp != '/')
  3161. cp++;
  3162. if (*cp == '/') {
  3163. *cp = '\0';
  3164. if (stat(fn, &st) < 0) {
  3165. if (dfl) {
  3166. if (mkdir(fn, 0777) < 0) {
  3167. *cp = '/';
  3168. emsg(-1, "Cannot create "
  3169. "directory for \"%s\"",
  3170. fn);
  3171. return -1;
  3172. }
  3173. } else {
  3174. msg(-1, 0, sysv3 ?
  3175. "missing 'd' option\n" :
  3176. "Missing -d option\n");
  3177. dfl = 2;
  3178. }
  3179. }
  3180. *cp = '/';
  3181. while (*cp == '/')
  3182. cp++;
  3183. }
  3184. } while (*cp);
  3185. return 0;
  3186. }
  3187. /*
  3188. * Set file attributes and make sure that suid/sgid bits are only restored
  3189. * if the owner is the same as on the tape.
  3190. */
  3191. static int
  3192. setattr(const char *fn, struct stat *st)
  3193. {
  3194. mode_t mode = st->st_mode & 07777;
  3195. uid_t uid = Rflag ? Ruid : myuid;
  3196. gid_t gid = Rflag ? Rgid : mygid;
  3197. if ((pax != PAX_TYPE_CPIO || myuid == 0) &&
  3198. (pax == PAX_TYPE_CPIO ||
  3199. pax_preserve&(PAX_P_OWNER|PAX_P_EVERY))) {
  3200. if (setowner(fn, st) != 0)
  3201. return 1;
  3202. }
  3203. if (pax != PAX_TYPE_CPIO &&
  3204. (pax_preserve&(PAX_P_OWNER|PAX_P_EVERY)) == 0)
  3205. mode &= ~(mode_t)(S_ISUID|S_ISGID);
  3206. if (myuid != 0 || Rflag) {
  3207. if (st->st_uid != uid || st->st_gid != gid) {
  3208. mode &= ~(mode_t)S_ISUID;
  3209. if ((st->st_mode&S_IFMT) != S_IFDIR && mode & 0010)
  3210. mode &= ~(mode_t)S_ISGID;
  3211. if ((st->st_mode&S_IFMT) == S_IFDIR && st->st_gid!=gid)
  3212. mode &= ~(mode_t)S_ISGID;
  3213. }
  3214. }
  3215. if ((st->st_mode&S_IFMT) == S_IFLNK)
  3216. return 0;
  3217. if (hp_Uflag)
  3218. mode &= ~umsk|S_IFMT;
  3219. if (pax != PAX_TYPE_CPIO &&
  3220. (pax_preserve&(PAX_P_MODE|PAX_P_OWNER|PAX_P_EVERY))==0)
  3221. mode &= 01777|S_IFMT;
  3222. if ((pax == PAX_TYPE_CPIO || pax_preserve&(PAX_P_MODE|PAX_P_EVERY)) &&
  3223. chmod(fn, mode) < 0) {
  3224. emsg(2, "Cannot chmod() \"%s\"", fn);
  3225. return 1;
  3226. }
  3227. if (pax != PAX_TYPE_CPIO ?
  3228. (pax_preserve&(PAX_P_ATIME|PAX_P_MTIME|PAX_P_EVERY)) !=
  3229. (PAX_P_ATIME|PAX_P_MTIME) : mflag)
  3230. return rstime(fn, st, "modification");
  3231. else
  3232. return 0;
  3233. }
  3234. static int
  3235. setowner(const char *fn, struct stat *st)
  3236. {
  3237. uid_t uid = Rflag ? Ruid : myuid ? myuid : st->st_uid;
  3238. gid_t gid = Rflag ? Rgid : st->st_gid;
  3239. if (((st->st_mode&S_IFMT)==S_IFLNK?lchown:chown)(fn, uid, gid) < 0) {
  3240. emsg(2, "Cannot chown() \"%s\"", fn);
  3241. return 1;
  3242. }
  3243. if (pax >= PAX_TYPE_PAX2001 && myuid && myuid != st->st_uid &&
  3244. pax_preserve & (PAX_P_OWNER|PAX_P_EVERY)) {
  3245. /*
  3246. * Do not even try to preserve user ownership in this case.
  3247. * It would either fail, or, without _POSIX_CHOWN_RESTRICTED,
  3248. * leave us with a file we do not own and which we thus could
  3249. * not chmod() later.
  3250. */
  3251. errno = EPERM;
  3252. emsg(2, "Cannot chown() \"%s\"", fn);
  3253. return 1;
  3254. }
  3255. return 0;
  3256. }
  3257. /*
  3258. * For -i and -p: Check if device/inode have been created already and
  3259. * if so, link path (or print the link name).
  3260. */
  3261. static int
  3262. canlink(const char *path, struct stat *st, int really)
  3263. {
  3264. struct dslot *ds;
  3265. struct islot *ip;
  3266. ds = dfind(&devices, st->st_dev);
  3267. if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL ||
  3268. ip->i_name == NULL) {
  3269. if (ip == NULL) {
  3270. ip = scalloc(1, sizeof *ip);
  3271. ip->i_ino = st->st_ino;
  3272. if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0)
  3273. ip->i_nlk = st->st_nlink;
  3274. iput(ip, &ds->d_isl);
  3275. }
  3276. ip->i_name = sstrdup(path);
  3277. } else {
  3278. if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0) {
  3279. /*
  3280. * If an archive inode has more links than given in
  3281. * st_nlink, write a new disk inode. See the comments
  3282. * to storelink() for the rationale.
  3283. */
  3284. if (--ip->i_nlk == 0) {
  3285. free(ip->i_name);
  3286. ip->i_name = sstrdup(path);
  3287. ip->i_nlk = st->st_nlink;
  3288. return 0;
  3289. }
  3290. }
  3291. if (really) {
  3292. /*
  3293. * If there was file data before the last link with
  3294. * SVR4 cpio formats, do not create a hard link.
  3295. */
  3296. if (fmttype & TYP_NCPIO && ip->i_nlk == 0)
  3297. return 0;
  3298. if (linkunlink(ip->i_name, path) == 0)
  3299. return 1;
  3300. else
  3301. return -1;
  3302. } else {
  3303. printf(" == %s", ip->i_name);
  3304. return 1;
  3305. }
  3306. }
  3307. return 0;
  3308. }
  3309. /*
  3310. * Execution for -i.
  3311. */
  3312. static void
  3313. doinp(void)
  3314. {
  3315. union bincpio bc;
  3316. struct file f;
  3317. int n;
  3318. memset(&f, 0, sizeof f);
  3319. if (Eflag)
  3320. patfile();
  3321. if (Iflag) {
  3322. if ((mt = open(Iflag, O_RDONLY)) < 0) {
  3323. if (sysv3) {
  3324. emsg(3, "Cannot open <%s> for input.", Iflag);
  3325. done(1);
  3326. }
  3327. else
  3328. msg(3, -2, "Cannot open \"%s\" for input\n",
  3329. Iflag);
  3330. }
  3331. } else
  3332. mt = dup(0);
  3333. mstat();
  3334. blkbuf = svalloc(blksiz, 1);
  3335. if ((n = mread()) == 0)
  3336. unexeoa();
  3337. else if (n < 0) {
  3338. emsg(3, "Read error on \"%s\"",
  3339. Iflag && !sysv3 ? Iflag : "input");
  3340. done(1);
  3341. }
  3342. if (kflag == 0 && sixflag == 0)
  3343. fmttype = FMT_NONE;
  3344. if (fmttype == FMT_NONE)
  3345. whathdr();
  3346. else
  3347. formatforced = 1;
  3348. while (readhdr(&f, &bc) == 0) {
  3349. if (fmttype & TYP_NCPIO && f.f_st.st_nlink > 1 &&
  3350. (f.f_st.st_mode&S_IFMT) != S_IFDIR) {
  3351. if (f.f_st.st_size != 0)
  3352. flushlinks(&f);
  3353. else
  3354. storelink(&f);
  3355. } else
  3356. inpone(&f, 1);
  3357. f.f_Kbase = f.f_Krest = f.f_Ksize = 0;
  3358. f.f_oflag = 0;
  3359. }
  3360. if (fmttype & TYP_NCPIO)
  3361. flushrest(f.f_pad);
  3362. }
  3363. /*
  3364. * SVR4 cpio link handling with -i; called if we assume that more links
  3365. * to this file will follow.
  3366. */
  3367. static void
  3368. storelink(struct file *f)
  3369. {
  3370. struct dslot *ds;
  3371. struct islot *ip;
  3372. struct ilink *il, *iz;
  3373. ds = dfind(&devices, f->f_st.st_dev);
  3374. if ((ip = ifind(f->f_st.st_ino, &ds->d_isl)) == NULL) {
  3375. ip = scalloc(1, sizeof *ip);
  3376. ip->i_ino = f->f_st.st_ino;
  3377. ip->i_nlk = f->f_st.st_nlink;
  3378. ip->i_st = smalloc(sizeof *ip->i_st);
  3379. *ip->i_st = f->f_st;
  3380. iput(ip, &ds->d_isl);
  3381. } else {
  3382. if (ip->i_nlk == 0) {
  3383. /*
  3384. * More links to an inode than given in the first
  3385. * st_nlink occurence are found within this archive.
  3386. * This happens if -L was specified and soft links
  3387. * point to a file with multiple hard links. We do
  3388. * the same as SVR4 cpio implementations here and
  3389. * associate these links with a new inode, since it
  3390. * is related to the way a file with a single hard
  3391. * link but referenced by soft links is unpacked.
  3392. */
  3393. ip->i_nlk = f->f_st.st_nlink;
  3394. if (ip->i_name)
  3395. free(ip->i_name);
  3396. ip->i_name = NULL;
  3397. ip->i_st = smalloc(sizeof *ip->i_st);
  3398. *ip->i_st = f->f_st;
  3399. } else if (ip->i_nlk <= 2) {
  3400. /*
  3401. * We get here if
  3402. * - a file with multiple links is empty;
  3403. * - a broken implementation has stored file data
  3404. * before the last link;
  3405. * - a linked file has been added to the archive later
  3406. * again (does not happen with our implementation).
  3407. */
  3408. flushnode(ip, f);
  3409. return;
  3410. } else
  3411. ip->i_nlk--;
  3412. }
  3413. for (il = ip->i_lnk, iz = NULL; il; il = il->l_nxt)
  3414. iz = il;
  3415. il = scalloc(1, sizeof *il);
  3416. il->l_siz = strlen(f->f_name) + 1;
  3417. il->l_nam = smalloc(il->l_siz);
  3418. strcpy(il->l_nam, f->f_name);
  3419. if (iz)
  3420. iz->l_nxt = il;
  3421. else
  3422. ip->i_lnk = il;
  3423. }
  3424. /*
  3425. * Flush all links of a file with SVR4 cpio format and -i.
  3426. */
  3427. static void
  3428. flushlinks(struct file *f)
  3429. {
  3430. struct dslot *ds;
  3431. struct islot *ip;
  3432. ds = dfind(&devices, f->f_st.st_dev);
  3433. ip = ifind(f->f_st.st_ino, &ds->d_isl);
  3434. flushnode(ip, f);
  3435. }
  3436. /*
  3437. * Data of a multi-linked file shall be transferred now for SVR4 cpio
  3438. * format and -i.
  3439. */
  3440. static void
  3441. flushnode(struct islot *ip, struct file *f)
  3442. {
  3443. struct file nf;
  3444. struct ilink *il, *iz;
  3445. f->f_dsize = f->f_st.st_size;
  3446. if (ip && ip->i_nlk > 0) {
  3447. /*
  3448. * Write out the file data with the first link the user
  3449. * wants to be extracted, but show the same display size
  3450. * for all links.
  3451. */
  3452. for (il = ip->i_lnk, iz = NULL; il;
  3453. iz = il, il = il->l_nxt, iz ? free(iz), 0 : 0) {
  3454. memset(&nf, 0, sizeof nf);
  3455. nf.f_name = il->l_nam;
  3456. nf.f_nsiz = il->l_siz;
  3457. nf.f_st = f->f_st;
  3458. nf.f_chksum = f->f_chksum;
  3459. nf.f_pad = f->f_pad;
  3460. nf.f_dsize = f->f_dsize;
  3461. if (inpone(&nf, 0) == 0) {
  3462. f->f_chksum = 0;
  3463. f->f_st.st_size = 0;
  3464. }
  3465. free(il->l_nam);
  3466. }
  3467. ip->i_lnk = NULL;
  3468. if (ip->i_st)
  3469. free(ip->i_st);
  3470. ip->i_st = NULL;
  3471. }
  3472. if (f->f_name)
  3473. inpone(f, 1);
  3474. if (ip) {
  3475. if (ip->i_nlk <= 2 && ip->i_name) {
  3476. free(ip->i_name);
  3477. ip->i_name = NULL;
  3478. }
  3479. ip->i_nlk = 0;
  3480. }
  3481. }
  3482. /*
  3483. * This writes all remaining multiply linked files, i. e. those with
  3484. * st_size == 0 and st_nlink > number of links within the archive.
  3485. */
  3486. static void
  3487. flushrest(int pad)
  3488. {
  3489. struct dslot *ds;
  3490. for (ds = devices; ds; ds = ds->d_nxt)
  3491. if (ds->d_isl != NULL)
  3492. flushtree(ds->d_isl, pad);
  3493. }
  3494. static void
  3495. flushtree(struct islot *ip, int pad)
  3496. {
  3497. struct file f;
  3498. if (ip == inull)
  3499. return;
  3500. flushtree(ip->i_lln, pad);
  3501. flushtree(ip->i_rln, pad);
  3502. if (ip->i_nlk > 0 && ip->i_st) {
  3503. memset(&f, 0, sizeof f);
  3504. f.f_st = *ip->i_st;
  3505. f.f_pad = pad;
  3506. flushnode(ip, &f);
  3507. }
  3508. }
  3509. /*
  3510. * See if the user wants to have this file with -i, and extract it or
  3511. * skip its data on the tape.
  3512. */
  3513. static int
  3514. inpone(struct file *f, int shallskip)
  3515. {
  3516. struct glist *gp = NULL, *gb = NULL;
  3517. int val = -1, selected = 0;
  3518. if ((patterns == NULL || (gp = want(f, &gb)) != NULL ^ fflag) &&
  3519. pax_track(f->f_name, f->f_st.st_mtime)) {
  3520. selected = 1;
  3521. if ((pax_sflag == 0 || pax_sname(&f->f_name, &f->f_nsiz)) &&
  3522. (rflag == 0 || rname(&f->f_name, &f->f_nsiz))) {
  3523. errcnt += infile(f);
  3524. val = 0;
  3525. }
  3526. } else if (shallskip)
  3527. errcnt += skipfile(f);
  3528. if (gp != NULL && selected) {
  3529. gp->g_gotcha = 1;
  3530. if (gp->g_nxt && gp->g_nxt->g_art)
  3531. gp->g_nxt->g_gotcha = 1;
  3532. else if (gp->g_art && gb)
  3533. gb->g_gotcha = 1;
  3534. }
  3535. return val;
  3536. }
  3537. /*
  3538. * Read the header for a file with -i. Return values: 0 okay, 1 end of
  3539. * archive, -1 failure.
  3540. */
  3541. static int
  3542. readhdr(struct file *f, union bincpio *bp)
  3543. {
  3544. long namlen, l1, l2, rd, hsz;
  3545. static long attempts;
  3546. long long skipped = 0;
  3547. if (fmttype & TYP_TAR) {
  3548. if (f->f_name)
  3549. f->f_name[0] = '\0';
  3550. if (f->f_lnam)
  3551. f->f_lnam[0] = '\0';
  3552. }
  3553. paxrec = globrec;
  3554. retry: f->f_st = globst;
  3555. retry2: if (fmttype & TYP_BINARY) {
  3556. hsz = SIZEOF_hdr_cpio;
  3557. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3558. unexeoa();
  3559. if ((fmttype&TYP_BE ? pbe16(bp->Hdr.c_magic) :
  3560. ple16(bp->Hdr.c_magic)) != mag_bin)
  3561. goto badhdr;
  3562. if (fmttype & TYP_BE) {
  3563. f->f_st.st_dev = pbe16(bp->Hdr.c_dev)&0177777;
  3564. f->f_st.st_ino = pbe16(bp->Hdr.c_ino)&0177777;
  3565. f->f_st.st_mode = pbe16(bp->Hdr.c_mode);
  3566. } else {
  3567. f->f_st.st_dev = ple16(bp->Hdr.c_dev)&0177777;
  3568. f->f_st.st_ino = ple16(bp->Hdr.c_ino)&0177777;
  3569. f->f_st.st_mode = ple16(bp->Hdr.c_mode);
  3570. }
  3571. if (sixflag) {
  3572. /*
  3573. * relevant Unix 6th Edition style mode bits
  3574. * (according to /usr/sys/inode.h, untested:)
  3575. *
  3576. * 060000 IFMT type of file
  3577. * 040000 IFDIR directory
  3578. * 020000 IFCHR character special
  3579. * 060000 IFBLK block special, 0 is regular
  3580. * 007777 permission bits as today
  3581. */
  3582. f->f_st.st_mode &= 067777;
  3583. if ((f->f_st.st_mode & 060000) == 0)
  3584. f->f_st.st_mode |= S_IFREG;
  3585. }
  3586. if (fmttype & TYP_BE) {
  3587. f->f_st.st_uid = pbe16(bp->Hdr.c_uid)&0177777;
  3588. f->f_st.st_gid = pbe16(bp->Hdr.c_gid)&0177777;
  3589. f->f_st.st_nlink = pbe16(bp->Hdr.c_nlink)&0177777;
  3590. f->f_st.st_mtime = pbe32(bp->Hdr.c_mtime);
  3591. f->f_st.st_rdev = pbe16(bp->Hdr.c_rdev);
  3592. namlen = pbe16(bp->Hdr.c_namesize);
  3593. f->f_st.st_size = pbe32(bp->Hdr.c_filesize)&0xFFFFFFFF;
  3594. } else {
  3595. f->f_st.st_uid = ple16(bp->Hdr.c_uid)&0177777;
  3596. f->f_st.st_gid = ple16(bp->Hdr.c_gid)&0177777;
  3597. f->f_st.st_nlink = ple16(bp->Hdr.c_nlink)&0177777;
  3598. f->f_st.st_mtime = pme32(bp->Hdr.c_mtime);
  3599. f->f_st.st_rdev = ple16(bp->Hdr.c_rdev);
  3600. namlen = ple16(bp->Hdr.c_namesize);
  3601. f->f_st.st_size = pme32(bp->Hdr.c_filesize)&0xFFFFFFFF;
  3602. }
  3603. f->f_rmajor = major(f->f_st.st_rdev);
  3604. f->f_rminor = minor(f->f_st.st_rdev);
  3605. if ((f->f_st.st_rdev&0xFFFF) == 0xFFFF &&
  3606. f->f_st.st_size > 0 &&
  3607. ((f->f_st.st_mode&S_IFMT) == S_IFBLK ||
  3608. (f->f_st.st_mode&S_IFMT) == S_IFCHR &&
  3609. (!formatforced || fmttype & TYP_SGI))) {
  3610. fmttype |= TYP_SGI;
  3611. f->f_rmajor = (f->f_st.st_size&0xFFFC0000)>>18;
  3612. f->f_rminor = f->f_st.st_size&0x0003FFFF;
  3613. f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
  3614. f->f_st.st_size = 0;
  3615. }
  3616. if ((f->f_st.st_mode&S_IFMT) == S_IFREG &&
  3617. f->f_st.st_size&0x80000000 &&
  3618. (!formatforced || fmttype & TYP_SGI)) {
  3619. fmttype |= TYP_SGI;
  3620. f->f_Ksize = f->f_st.st_size&0xFFFFFFFF;
  3621. f->f_Kbase = (0x100000000LL-f->f_st.st_size) *
  3622. 0x80000000;
  3623. f->f_st.st_size = 0;
  3624. }
  3625. f->f_pad = 2;
  3626. rd = SIZEOF_hdr_cpio;
  3627. } else if (fmttype == FMT_ODC) {
  3628. hsz = SIZEOF_c_hdr;
  3629. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3630. unexeoa();
  3631. if(memcmp(bp->Cdr.c_magic, mag_odc, sizeof mag_odc))
  3632. goto badhdr;
  3633. f->f_st.st_dev = rdoct(bp->Cdr.c_dev, 6);
  3634. f->f_st.st_ino = rdoct(bp->Cdr.c_ino, 6);
  3635. f->f_st.st_mode = rdoct(bp->Cdr.c_mode, 6);
  3636. f->f_st.st_uid = rdoct(bp->Cdr.c_uid, 6);
  3637. f->f_st.st_gid = rdoct(bp->Cdr.c_gid, 6);
  3638. f->f_st.st_nlink = rdoct(bp->Cdr.c_nlink, 6);
  3639. f->f_st.st_rdev = rdoct(bp->Cdr.c_rdev, 6);
  3640. f->f_rmajor = major(f->f_st.st_rdev);
  3641. f->f_rminor = minor(f->f_st.st_rdev);
  3642. f->f_st.st_mtime = rdoct(bp->Cdr.c_mtime, 11);
  3643. namlen = rdoct(bp->Cdr.c_namesz, 6);
  3644. f->f_st.st_size = rdoct(bp->Cdr.c_filesz, 11);
  3645. f->f_pad = 1;
  3646. rd = SIZEOF_c_hdr;
  3647. } else if (fmttype == FMT_DEC) {
  3648. hsz = SIZEOF_d_hdr;
  3649. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3650. unexeoa();
  3651. if(memcmp(bp->Ddr.d_magic, mag_odc, sizeof mag_odc))
  3652. goto badhdr;
  3653. f->f_st.st_dev = rdoct(bp->Ddr.d_dev, 6);
  3654. f->f_st.st_ino = rdoct(bp->Ddr.d_ino, 6);
  3655. f->f_st.st_mode = rdoct(bp->Ddr.d_mode, 6);
  3656. f->f_st.st_uid = rdoct(bp->Ddr.d_uid, 6);
  3657. f->f_st.st_gid = rdoct(bp->Ddr.d_gid, 6);
  3658. f->f_st.st_nlink = rdoct(bp->Ddr.d_nlink, 6);
  3659. f->f_rmajor = rdoct(bp->Ddr.d_rmaj, 8);
  3660. f->f_rminor = rdoct(bp->Ddr.d_rmin, 8);
  3661. f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
  3662. f->f_st.st_mtime = rdoct(bp->Ddr.d_mtime, 11);
  3663. namlen = rdoct(bp->Ddr.d_namesz, 6);
  3664. f->f_st.st_size = rdoct(bp->Ddr.d_filesz, 11);
  3665. f->f_pad = 1;
  3666. rd = SIZEOF_d_hdr;
  3667. } else if (fmttype & TYP_NCPIO) {
  3668. hsz = SIZEOF_Exp_cpio_hdr;
  3669. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3670. unexeoa();
  3671. if (memcmp(bp->Edr.E_magic, mag_asc, sizeof mag_asc) &&
  3672. memcmp(bp->Edr.E_magic, mag_crc, sizeof mag_crc))
  3673. goto badhdr;
  3674. f->f_st.st_ino = rdhex(bp->Edr.E_ino, 8);
  3675. f->f_st.st_mode = rdhex(bp->Edr.E_mode, 8);
  3676. f->f_st.st_uid = rdhex(bp->Edr.E_uid, 8);
  3677. f->f_st.st_gid = rdhex(bp->Edr.E_gid, 8);
  3678. f->f_st.st_nlink = rdhex(bp->Edr.E_nlink, 8);
  3679. f->f_st.st_mtime = rdhex(bp->Edr.E_mtime, 8);
  3680. f->f_st.st_size = rdhex(bp->Edr.E_filesize, 8);
  3681. l1 = rdhex(bp->Edr.E_maj, 8);
  3682. l2 = rdhex(bp->Edr.E_min, 8);
  3683. f->f_st.st_dev = makedev(l1, l2);
  3684. f->f_rmajor = rdhex(bp->Edr.E_rmaj, 8);
  3685. f->f_rminor = rdhex(bp->Edr.E_rmin, 8);
  3686. f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
  3687. namlen = rdhex(bp->Edr.E_namesize, 8);
  3688. f->f_chksum = rdhex(bp->Edr.E_chksum, 8);
  3689. f->f_pad = 4;
  3690. rd = SIZEOF_Exp_cpio_hdr;
  3691. } else if (fmttype & TYP_CRAY) {
  3692. int diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
  3693. hsz = SIZEOF_cray_hdr - diff5;
  3694. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3695. unexeoa();
  3696. if (pbe64(bp->Crayhdr.C_magic) != mag_bin)
  3697. goto badhdr;
  3698. f->f_st.st_dev = pbe64(bp->Crayhdr.C_dev);
  3699. f->f_st.st_ino = pbe64(bp->Crayhdr.C_ino);
  3700. f->f_st.st_mode = pbe64(bp->Crayhdr.C_mode);
  3701. /*
  3702. * Some of the S_IFMT bits on Cray UNICOS 9 differ from
  3703. * the common Unix values, namely:
  3704. *
  3705. * S_IFLNK 0130000 symbolic link
  3706. * S_IFOFD 0110000 off line, with data
  3707. * S_IFOFL 0120000 off line, with no data
  3708. *
  3709. * We treat the latter ones as regular files.
  3710. */
  3711. if ((f->f_st.st_mode&S_IFMT) == 0130000)
  3712. f->f_st.st_mode = f->f_st.st_mode&07777|S_IFLNK;
  3713. else if ((f->f_st.st_mode&S_IFMT) == 0110000 ||
  3714. (f->f_st.st_mode&S_IFMT) == 0120000)
  3715. f->f_st.st_mode = f->f_st.st_mode&07777|S_IFREG;
  3716. f->f_st.st_uid = pbe64(bp->Crayhdr.C_uid);
  3717. f->f_st.st_gid = pbe64(bp->Crayhdr.C_gid);
  3718. f->f_st.st_nlink = pbe64(bp->Crayhdr.C_nlink);
  3719. f->f_st.st_mtime = pbe64(bp->Crayhdr.C_mtime - diff5);
  3720. f->f_st.st_rdev = pbe64(bp->Crayhdr.C_rdev);
  3721. f->f_rmajor = major(f->f_st.st_rdev);
  3722. f->f_rminor = minor(f->f_st.st_rdev);
  3723. f->f_st.st_size = pbe64(bp->Crayhdr.C_filesize - diff5);
  3724. namlen = pbe64(bp->Crayhdr.C_namesize - diff5);
  3725. f->f_pad = 1;
  3726. rd = SIZEOF_cray_hdr - diff5;
  3727. } else if (fmttype & TYP_BAR) {
  3728. hsz = 512;
  3729. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3730. unexeoa();
  3731. if (bp->data[SIZEOF_bar_header] == '\0') {
  3732. if (kflag && !allzero(bp->data, hsz))
  3733. goto badhdr;
  3734. return 1;
  3735. }
  3736. if (trdsum(bp) != 0 && kflag == 0)
  3737. goto badhdr;
  3738. bp->data[512] = bp->data[513] = '\0';
  3739. if (f->f_name == NULL || f->f_name[0] == '\0') {
  3740. if (f->f_nsiz < 512) {
  3741. free(f->f_name);
  3742. f->f_name = smalloc(f->f_nsiz = 512);
  3743. }
  3744. strcpy(f->f_name, &bp->data[SIZEOF_bar_header]);
  3745. }
  3746. namlen = strlen(f->f_name) + 1;
  3747. f->f_st.st_mode = rdoct(bp->Bdr.b_mode, 8);
  3748. f->f_st.st_uid = rdoct(bp->Bdr.b_uid, 8);
  3749. f->f_st.st_gid = rdoct(bp->Bdr.b_gid, 8);
  3750. f->f_st.st_size = rdoct(bp->Bdr.b_size, 12);
  3751. f->f_st.st_mtime = rdoct(bp->Bdr.b_mtime, 12);
  3752. f->f_st.st_rdev = rdoct(bp->Bdr.b_rdev, 8);
  3753. f->f_rmajor = major(f->f_st.st_rdev);
  3754. f->f_rminor = minor(f->f_st.st_rdev);
  3755. switch (bp->Bdr.b_linkflag) {
  3756. case '0':
  3757. f->f_st.st_mode &= 07777;
  3758. if (f->f_name[namlen-2] == '/')
  3759. f->f_st.st_mode |= S_IFDIR;
  3760. else
  3761. f->f_st.st_mode |= S_IFREG;
  3762. break;
  3763. case '1':
  3764. if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
  3765. blinkof(bp->data, f, namlen);
  3766. f->f_st.st_nlink = 2;
  3767. if ((f->f_st.st_mode&S_IFMT) == 0)
  3768. f->f_st.st_mode |= S_IFREG;
  3769. break;
  3770. case '2':
  3771. if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
  3772. blinkof(bp->data, f, namlen);
  3773. f->f_st.st_mode &= 07777;
  3774. f->f_st.st_mode |= S_IFLNK;
  3775. }
  3776. f->f_pad = 512;
  3777. rd = 512;
  3778. } else if (fmttype & TYP_TAR) {
  3779. hsz = 512;
  3780. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3781. unexeoa();
  3782. if (bp->Tdr.t_name[0] == '\0') {
  3783. if (kflag && !allzero(bp->data, hsz))
  3784. goto badhdr;
  3785. return 1;
  3786. }
  3787. if (fmttype == FMT_GNUTAR) {
  3788. if (memcmp(bp->Tdr.t_magic, mag_gnutar, 8))
  3789. goto badhdr;
  3790. } else if (fmttype & TYP_USTAR) {
  3791. if (memcmp(bp->Tdr.t_magic, mag_ustar, 6))
  3792. goto badhdr;
  3793. }
  3794. if (trdsum(bp) != 0 && kflag == 0)
  3795. goto badhdr;
  3796. if ((fmttype & TYP_PAX) == 0 && (fmttype == FMT_GNUTAR ||
  3797. strcmp(bp->Tdr.t_name, "././@LongLink") == 0)) {
  3798. switch (bp->Tdr.t_linkflag) {
  3799. case 'L':
  3800. if (readgnuname(&f->f_name, &f->f_nsiz,
  3801. rdoct(bp->Tdr.t_size, 12)) < 0)
  3802. goto badhdr;
  3803. goto retry;
  3804. case 'K':
  3805. if (readgnuname(&f->f_lnam, &f->f_lsiz,
  3806. rdoct(bp->Tdr.t_size, 12)) < 0)
  3807. goto badhdr;
  3808. goto retry;
  3809. }
  3810. }
  3811. if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR) {
  3812. switch (bp->Tdr.t_linkflag) {
  3813. case 'g':
  3814. case 'x':
  3815. if (formatforced && fmttype != FMT_PAX)
  3816. break;
  3817. fmttype = FMT_PAX;
  3818. tgetpax(&bp->Tdr, f);
  3819. goto retry2;
  3820. case 'X':
  3821. if (formatforced && fmttype != FMT_SUN)
  3822. break;
  3823. fmttype = FMT_SUN;
  3824. tgetpax(&bp->Tdr, f);
  3825. goto retry2;
  3826. }
  3827. }
  3828. if (f->f_name == NULL || f->f_name[0] == '\0') {
  3829. if (f->f_nsiz < TNAMSIZ+TPFXSIZ+2) {
  3830. free(f->f_name);
  3831. f->f_name= smalloc(f->f_nsiz=TNAMSIZ+TPFXSIZ+2);
  3832. }
  3833. tnameof(&bp->Tdr, f->f_name);
  3834. }
  3835. namlen = strlen(f->f_name) + 1;
  3836. f->f_st.st_mode = rdoct(bp->Tdr.t_mode, 8) & 07777;
  3837. if (paxrec & PR_UID)
  3838. /*EMPTY*/;
  3839. else if (fmttype == FMT_GNUTAR && bp->Tdr.t_uid[0] & 0200)
  3840. f->f_st.st_uid = pbe64(bp->Tdr.t_uid) &
  3841. 0x7FFFFFFFFFFFFFFFLL;
  3842. else
  3843. f->f_st.st_uid = rdoct(bp->Tdr.t_uid, 8);
  3844. if (paxrec & PR_GID)
  3845. /*EMPTY*/;
  3846. else if (fmttype == FMT_GNUTAR && bp->Tdr.t_gid[0] & 0200)
  3847. f->f_st.st_gid = pbe64(bp->Tdr.t_gid) &
  3848. 0x7FFFFFFFFFFFFFFFLL;
  3849. else
  3850. f->f_st.st_gid = rdoct(bp->Tdr.t_gid, 8);
  3851. if (paxrec & PR_SIZE)
  3852. /*EMPTY*/;
  3853. else if (fmttype == FMT_GNUTAR && bp->Tdr.t_size[0] == '\200')
  3854. f->f_st.st_size = pbe64(&bp->Tdr.t_size[4]);
  3855. else
  3856. f->f_st.st_size = rdoct(bp->Tdr.t_size, 12);
  3857. if ((paxrec & PR_MTIME) == 0)
  3858. f->f_st.st_mtime = rdoct(bp->Tdr.t_mtime, TTIMSIZ);
  3859. if (bp->Tdr.t_linkflag == '1') {
  3860. if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
  3861. tlinkof(&bp->Tdr, f);
  3862. f->f_st.st_mode |= S_IFREG; /* for -tv */
  3863. f->f_st.st_nlink = 2;
  3864. } else if (bp->Tdr.t_linkflag == '2') {
  3865. if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
  3866. tlinkof(&bp->Tdr, f);
  3867. f->f_st.st_mode |= S_IFLNK;
  3868. } else if (tflag == 0 && (f->f_name)[namlen-2] == '/')
  3869. f->f_st.st_mode |= S_IFDIR;
  3870. else
  3871. f->f_st.st_mode |= tifmt(bp->Tdr.t_linkflag);
  3872. if (paxrec & PR_SUN_DEVMAJOR)
  3873. /*EMPTY*/;
  3874. else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devmajor[0] & 0200)
  3875. f->f_rmajor = pbe64(bp->Tdr.t_devmajor) &
  3876. 0x7FFFFFFFFFFFFFFFLL;
  3877. else
  3878. f->f_rmajor = rdoct(bp->Tdr.t_devmajor, 8);
  3879. if (paxrec & PR_SUN_DEVMINOR)
  3880. /*EMPTY*/;
  3881. else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devminor[0] & 0200)
  3882. f->f_rminor = pbe64(bp->Tdr.t_devminor) &
  3883. 0x7FFFFFFFFFFFFFFFLL;
  3884. else
  3885. f->f_rminor = rdoct(bp->Tdr.t_devminor, 8);
  3886. f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
  3887. f->f_pad = 512;
  3888. rd = 512;
  3889. } else if (fmttype == FMT_ZIP) {
  3890. hsz = SIZEOF_zip_header;
  3891. if (attempts == 0 && bread(bp->data, hsz) != hsz)
  3892. unexeoa();
  3893. if (memcmp(bp->Zdr.z_signature, mag_zipctr,
  3894. sizeof mag_zipctr) == 0 ||
  3895. memcmp(bp->Zdr.z_signature, mag_zipend,
  3896. sizeof mag_zipend) == 0 ||
  3897. memcmp(bp->Zdr.z_signature, mag_zip64e,
  3898. sizeof mag_zip64e) == 0 ||
  3899. memcmp(bp->Zdr.z_signature, mag_zip64l,
  3900. sizeof mag_zip64l) == 0)
  3901. return 1;
  3902. if (memcmp(bp->Zdr.z_signature, mag_zipsig, sizeof mag_zipsig))
  3903. goto badhdr;
  3904. f->f_st.st_uid = myuid;
  3905. f->f_st.st_gid = mygid;
  3906. f->f_st.st_nlink = 1;
  3907. f->f_st.st_mtime =
  3908. gdostime(bp->Zdr.z_modtime, bp->Zdr.z_moddate);
  3909. f->f_st.st_size = ple32(bp->Zdr.z_nsize);
  3910. f->f_csize = ple32(bp->Zdr.z_csize);
  3911. f->f_cmethod = ple16(bp->Zdr.z_cmethod);
  3912. f->f_chksum = ple32(bp->Zdr.z_crc32);
  3913. f->f_gflag = ple16(bp->Zdr.z_gflag);
  3914. rd = SIZEOF_zip_header;
  3915. namlen = ple16(bp->Zdr.z_namelen)&0177777;
  3916. if (f->f_nsiz < namlen+1) {
  3917. free(f->f_name);
  3918. f->f_name = smalloc(f->f_nsiz = namlen+1);
  3919. }
  3920. if (bread(f->f_name, namlen) != namlen)
  3921. goto corrupt;
  3922. (f->f_name)[namlen] = '\0';
  3923. if (f->f_name[namlen-1] == '/')
  3924. f->f_st.st_mode = 0777&~(mode_t)umsk|S_IFDIR;
  3925. else
  3926. f->f_st.st_mode = 0666&~(mode_t)umsk|S_IFREG;
  3927. rd += namlen;
  3928. if ((l1 = ziprxtra(f, &bp->Zdr)) < 0)
  3929. goto corrupt;
  3930. rd += l1;
  3931. f->f_pad = 1;
  3932. } else
  3933. abort();
  3934. if (fmttype & TYP_CPIO) {
  3935. if (f->f_st.st_nlink <= 0 || namlen <= 0 || namlen >= SANELIMIT)
  3936. goto corrupt;
  3937. if (namlen > f->f_nsiz) {
  3938. if (f->f_name)
  3939. free(f->f_name);
  3940. f->f_name = smalloc(f->f_nsiz = namlen);
  3941. }
  3942. if (bread(f->f_name, namlen) != namlen)
  3943. unexeoa();
  3944. if (f->f_name[namlen-1] != '\0')
  3945. goto corrupt;
  3946. rd += namlen;
  3947. skippad(rd, f->f_pad);
  3948. if (fmttype & TYP_NCPIO &&
  3949. (!formatforced || fmttype & TYP_SCO)) {
  3950. /*
  3951. * The UnixWare format is a hack, but not a bad
  3952. * one. It is backwards compatible as far as
  3953. * possible; old implementations can use the -k
  3954. * option and will then get only the beginning
  3955. * of a large file, but all other files in the
  3956. * archive.
  3957. */
  3958. if (namlen >= 24 && memcmp(&f->f_name[namlen-23],
  3959. "\0size=", 6) == 0) {
  3960. fmttype |= TYP_SCO;
  3961. f->f_st.st_size = rdhex(&f->f_name[namlen-17],
  3962. 16);
  3963. }
  3964. }
  3965. }
  3966. if (attempts) {
  3967. if (tflag)
  3968. fflush(stdout);
  3969. msg(0, 0, sysv3 < 0 ?
  3970. "Re-synced after skipping %lld bytes.\n" :
  3971. "Re-synchronized on magic number/header.\n",
  3972. skipped);
  3973. }
  3974. attempts = 0;
  3975. if (f->f_st.st_atime == 0 || (pax_preserve&(PAX_P_ATIME|PAX_P_EVERY)) ==
  3976. PAX_P_ATIME)
  3977. f->f_st.st_atime = f->f_st.st_mtime;
  3978. if ((f->f_dsize = f->f_st.st_size) < 0)
  3979. goto badhdr;
  3980. if (fmttype & TYP_CPIO && strcmp(f->f_name, trailer) == 0)
  3981. return 1;
  3982. return 0;
  3983. corrupt:
  3984. if (kflag) {
  3985. if (tflag)
  3986. fflush(stdout);
  3987. msg(3, 0, "Corrupt header, file(s) may be lost.\n");
  3988. }
  3989. badhdr:
  3990. if (kflag) {
  3991. int next = fmttype & TYP_TAR ? 512 : 1;
  3992. if (attempts++ == 0) {
  3993. if (tflag)
  3994. fflush(stdout);
  3995. msg(1, 0, sysv3 < 0 ? "Out of phase; resyncing.\n" :
  3996. "Searching for magic number/header.\n");
  3997. errcnt++;
  3998. }
  3999. for (l1 = next; l1 < hsz; l1++)
  4000. bp->data[l1-next] = bp->data[l1];
  4001. if (bread(&bp->data[l1-next], next) != next)
  4002. unexeoa();
  4003. skipped++;
  4004. goto retry;
  4005. }
  4006. if (tflag)
  4007. fflush(stdout);
  4008. msg(3, 1, sysv3 < 0 ? "Out of phase--get help\n" :
  4009. sysv3 > 0 ? "Out of sync, bad magic number/header.\n" :
  4010. "Bad magic number/header.\n");
  4011. /*NOTREACHED*/
  4012. return -1;
  4013. }
  4014. /*
  4015. * Determine the kind of archive on tape.
  4016. */
  4017. static void
  4018. whathdr(void)
  4019. {
  4020. again: if (blktop >= SIZEOF_hdr_cpio &&
  4021. ple16(blkbuf) == mag_bin) {
  4022. fmttype = FMT_BINLE;
  4023. } else if (blktop >= SIZEOF_hdr_cpio &&
  4024. pbe16(blkbuf) == mag_bin) {
  4025. fmttype = FMT_BINBE;
  4026. } else if (blktop >= SIZEOF_c_hdr &&
  4027. memcmp(blkbuf, mag_odc, sizeof mag_odc) == 0) {
  4028. /*
  4029. * The DEC format is rubbish. Instead of introducing a new
  4030. * archive magic, its engineers reused the POSIX/odc magic
  4031. * and changed the fields. But there's a workaround: For a
  4032. * real odc archive, the byte following the file name is a
  4033. * \0 byte (unless the archive is damaged, of course). If
  4034. * it is not a \0 byte, but a \0 byte appears at the
  4035. * corresponding location for the DEC format, this is
  4036. * treated as a DEC archive. It must be noted that the
  4037. * Tru64 UNIX 5.1 cpio command is too stupid even for
  4038. * doing that - it will spill out a list of complaints
  4039. * if an extended archive is read but -e is not given.
  4040. */
  4041. int ns1, ns2;
  4042. ns1 = rdoct(((struct c_hdr *)blkbuf)->c_namesz, 6);
  4043. ns2 = rdoct(((struct d_hdr *)blkbuf)->d_namesz, 6);
  4044. if (blktop >= SIZEOF_d_hdr &&
  4045. (ns1 >= blktop - SIZEOF_c_hdr ||
  4046. blkbuf[SIZEOF_c_hdr+ns1-1] != '\0') &&
  4047. ns2 <= blktop - SIZEOF_d_hdr &&
  4048. blkbuf[SIZEOF_d_hdr+ns2-1] == '\0')
  4049. fmttype = FMT_DEC;
  4050. else
  4051. fmttype = FMT_ODC;
  4052. } else if (blktop >= SIZEOF_Exp_cpio_hdr &&
  4053. memcmp(blkbuf, mag_asc, sizeof mag_asc) == 0) {
  4054. fmttype = FMT_ASC;
  4055. } else if (blktop >= SIZEOF_Exp_cpio_hdr &&
  4056. memcmp(blkbuf, mag_crc, sizeof mag_crc) == 0) {
  4057. fmttype = FMT_CRC;
  4058. } else if (blktop >= SIZEOF_cray_hdr &&
  4059. pbe64(blkbuf) == mag_bin) {
  4060. /*
  4061. * Old and new Cray headers are identical in the first
  4062. * 64 header bytes (including the magic). cpio(5) on
  4063. * UNICOS does not describe what the new param field
  4064. * is for.
  4065. *
  4066. * An archive is treated as old format if the mtime
  4067. * and namesize fields make sense and all characters
  4068. * of the name are non-null.
  4069. */
  4070. struct cray_hdr *Cp = (struct cray_hdr *)blkbuf;
  4071. long long mtime, namesize;
  4072. fmttype = FMT_CRAY;
  4073. mtime = pbe64(Cp->C_mtime - CRAY_PARAMSZ);
  4074. namesize = pbe64(Cp->C_namesize - CRAY_PARAMSZ);
  4075. if (mtime > 0 && mtime < (1LL<<31) &&
  4076. namesize > 0 && namesize < 2048 &&
  4077. blktop > SIZEOF_cray_hdr-
  4078. CRAY_PARAMSZ+namesize+1) {
  4079. int i;
  4080. for (i = 0; i < namesize; i++)
  4081. if (blkbuf[SIZEOF_cray_hdr-
  4082. CRAY_PARAMSZ+i] == '\0')
  4083. break;
  4084. if (i == namesize-1)
  4085. fmttype = FMT_CRAY5;
  4086. }
  4087. } else if (blktop >= 512 &&
  4088. memcmp(&blkbuf[257], mag_ustar, 6) == 0 &&
  4089. tcssum((union bincpio *)blkbuf, 1) == 0) {
  4090. fmttype = FMT_USTAR;
  4091. } else if (blktop >= 512 &&
  4092. memcmp(&blkbuf[257], mag_gnutar, 8) == 0 &&
  4093. tcssum((union bincpio *)blkbuf, 1) == 0) {
  4094. fmttype = FMT_GNUTAR;
  4095. } else if (blktop >= 512 && blkbuf[0] && /* require filename
  4096. to avoid match on
  4097. /dev/zero etc. */
  4098. memcmp(&blkbuf[257], "\0\0\0\0\0", 5) == 0 &&
  4099. tcssum((union bincpio *)blkbuf, 0) == 0) {
  4100. fmttype = FMT_OTAR;
  4101. } else if (blktop >= SIZEOF_zip_header &&
  4102. (memcmp(blkbuf, mag_zipctr, sizeof mag_zipctr) == 0 ||
  4103. memcmp(blkbuf, mag_zipsig, sizeof mag_zipsig) == 0 ||
  4104. memcmp(blkbuf, mag_zipend, sizeof mag_zipend) == 0 ||
  4105. memcmp(blkbuf, mag_zip64e, sizeof mag_zip64e) == 0 ||
  4106. memcmp(blkbuf, mag_zip64l, sizeof mag_zip64l) == 0)) {
  4107. fmttype = FMT_ZIP;
  4108. } else if (blktop >= 512 && memcmp(blkbuf,"\0\0\0\0\0\0\0\0",8) == 0 &&
  4109. memcmp(&blkbuf[65], mag_bar, sizeof mag_bar) == 0 &&
  4110. bcssum((union bincpio *)blkbuf) == 0) {
  4111. fmttype = FMT_BAR;
  4112. compressed_bar = blkbuf[71] == '1';
  4113. curpos = 512;
  4114. } else if (!Aflag && blktop > 3 && memcmp(blkbuf, "BZh", 3) == 0 &&
  4115. redirect("bzip2", "-cd") == 0) {
  4116. goto zip;
  4117. } else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\235", 2) == 0 &&
  4118. redirect("zcat", NULL) == 0) {
  4119. goto zip;
  4120. } else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\213", 2) == 0 &&
  4121. redirect("gzip", "-cd") == 0) {
  4122. goto zip;
  4123. } else if (!Aflag && blktop > 4 &&
  4124. memcmp(blkbuf, "\355\253\356\333", 4) == 0 &&
  4125. redirect("rpm2cpio", "-") == 0) {
  4126. goto zip;
  4127. } else {
  4128. msg(3, 0, sysv3 ? "This is not a cpio file, bad header.\n" :
  4129. "Not a cpio file, bad header.\n");
  4130. done(1);
  4131. }
  4132. return;
  4133. zip:
  4134. blktop = curpos = 0;
  4135. blocks = 0;
  4136. bytes = 0;
  4137. mfl = 0;
  4138. mstat();
  4139. if (mread() == 0)
  4140. unexeoa();
  4141. goto again;
  4142. }
  4143. /*
  4144. * Processing of a single file with -i.
  4145. */
  4146. static int
  4147. infile(struct file *f)
  4148. {
  4149. int val;
  4150. if ((fmttype & TYP_CPIO || fmttype == FMT_ZIP && f->f_st.st_size) &&
  4151. (f->f_st.st_mode&S_IFMT) == S_IFLNK) {
  4152. if (f->f_lsiz < f->f_st.st_size+1)
  4153. f->f_lnam = srealloc(f->f_lnam,
  4154. f->f_lsiz = f->f_st.st_size+1);
  4155. if (bread(f->f_lnam, f->f_st.st_size) != f->f_st.st_size)
  4156. unexeoa();
  4157. f->f_lnam[f->f_st.st_size] = '\0';
  4158. skippad(f->f_st.st_size, f->f_pad);
  4159. }
  4160. if (tflag)
  4161. val = filet(f, indata);
  4162. else
  4163. val = filein(f, indata, f->f_name);
  4164. return val != 0;
  4165. }
  4166. /*
  4167. * Fetch the data of regular files from tape and write it to tfd or, if
  4168. * tfd < 0, discard it.
  4169. */
  4170. static int
  4171. indata(struct file *f, const char *tgt, int tfd)
  4172. {
  4173. char *buf;
  4174. size_t bufsize;
  4175. struct stat ts;
  4176. long long size;
  4177. ssize_t rd;
  4178. uint32_t ssum = 0, usum = 0;
  4179. int val = 0;
  4180. int doswap = 0;
  4181. size = fmttype == FMT_ZIP ? f->f_csize :
  4182. f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
  4183. doswap = ((bflag|sflag) == 0 ||
  4184. ckodd(size, 2, "bytes", f->f_name) == 0) &
  4185. ((bflag|Sflag) == 0 ||
  4186. ckodd(size, 4, "halfwords", f->f_name) == 0) &
  4187. (bflag|sflag|Sflag);
  4188. if (fmttype == FMT_ZIP && f->f_cmethod != C_STORED)
  4189. return zipread(f, tgt, tfd, doswap);
  4190. if (tfd < 0 || fstat(tfd, &ts) < 0)
  4191. ts.st_blksize = 4096;
  4192. getbuf(&buf, &bufsize, ts.st_blksize);
  4193. again: while (size) {
  4194. if ((rd = bread(buf, size > bufsize ? bufsize : size)) <= 0)
  4195. unexeoa();
  4196. if (doswap)
  4197. swap(buf, rd, bflag || sflag, bflag || Sflag);
  4198. if (tfd >= 0 && write(tfd, buf, rd) != rd) {
  4199. emsg(3, "Cannot write \"%s\"", tgt);
  4200. tfd = -1;
  4201. val = -1;
  4202. }
  4203. size -= rd;
  4204. if (fmttype & TYP_CRC)
  4205. do {
  4206. rd--;
  4207. ssum += ((signed char *)buf)[rd];
  4208. usum += ((unsigned char *)buf)[rd];
  4209. } while (rd);
  4210. }
  4211. if (f->f_Kbase) {
  4212. skippad(f->f_Ksize, f->f_pad);
  4213. readK2hdr(f);
  4214. size = f->f_Krest;
  4215. goto again;
  4216. }
  4217. skippad(fmttype==FMT_ZIP ? f->f_csize :
  4218. f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
  4219. if (fmttype & TYP_CRC) {
  4220. if (f->f_chksum != ssum && f->f_chksum != usum) {
  4221. msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
  4222. if (kflag)
  4223. errcnt++;
  4224. else
  4225. val = -1;
  4226. }
  4227. }
  4228. return val;
  4229. }
  4230. /*
  4231. * Skip the data for a file on tape.
  4232. */
  4233. static int
  4234. skipfile(struct file *f)
  4235. {
  4236. char b[4096];
  4237. long long size;
  4238. ssize_t rd;
  4239. if (fmttype & TYP_TAR && ((f->f_st.st_mode&S_IFMT) == S_IFLNK ||
  4240. f->f_st.st_nlink > 1)) {
  4241. if (fmttype & TYP_USTAR && (!formatforced ||
  4242. fmttype == FMT_PAX) &&
  4243. f->f_st.st_nlink > 1 &&
  4244. f->f_st.st_size > 0)
  4245. /*EMPTY*/;
  4246. else
  4247. return 0;
  4248. }
  4249. /*
  4250. * SVR4 cpio derivatives always ignore the size of these,
  4251. * even if not reading tar formats. We do the same for now,
  4252. * although POSIX (for -H odc format) says the contrary.
  4253. */
  4254. if (fmttype != FMT_ZIP && ((f->f_st.st_mode&S_IFMT) == S_IFDIR ||
  4255. (f->f_st.st_mode&S_IFMT) == S_IFCHR ||
  4256. (f->f_st.st_mode&S_IFMT) == S_IFBLK ||
  4257. (f->f_st.st_mode&S_IFMT) == S_IFIFO ||
  4258. (f->f_st.st_mode&S_IFMT) == S_IFNAM ||
  4259. (f->f_st.st_mode&S_IFMT) == S_IFNWK))
  4260. return 0;
  4261. if (fmttype == FMT_ZIP && f->f_gflag & FG_DESC)
  4262. return zipread(f, f->f_name, -1, 0);
  4263. size = fmttype == FMT_ZIP ? f->f_csize :
  4264. f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
  4265. again: while (size) {
  4266. if ((rd = bread(b, size > sizeof b ? sizeof b : size)) <= 0)
  4267. unexeoa();
  4268. size -= rd;
  4269. }
  4270. if (f->f_Kbase) {
  4271. skippad(f->f_Ksize, f->f_pad);
  4272. readK2hdr(f);
  4273. size = f->f_Krest;
  4274. goto again;
  4275. }
  4276. skippad(fmttype==FMT_ZIP ? f->f_csize :
  4277. f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
  4278. return 0;
  4279. }
  4280. /*
  4281. * Skip data also, but perform checks as if copying.
  4282. */
  4283. static int
  4284. skipdata(struct file *f, int (*copydata)(struct file *, const char *, int))
  4285. {
  4286. switch (f->f_st.st_mode&S_IFMT) {
  4287. case S_IFLNK:
  4288. break;
  4289. case S_IFDIR:
  4290. case S_IFBLK:
  4291. case S_IFCHR:
  4292. case S_IFIFO:
  4293. case S_IFNAM:
  4294. case S_IFNWK:
  4295. if (fmttype != FMT_ZIP)
  4296. break;
  4297. /*FALLTHRU*/
  4298. case S_IFREG:
  4299. default:
  4300. if (fmttype & TYP_TAR && f->f_st.st_nlink > 1 &&
  4301. ((fmttype & TYP_USTAR) == 0 ||
  4302. formatforced && fmttype != FMT_PAX))
  4303. break;
  4304. if (copydata(f, f->f_name, -1) != 0)
  4305. return 1;
  4306. }
  4307. return 0;
  4308. }
  4309. /*
  4310. * Seek to a position in the archive measured from the begin of
  4311. * operation. Handle media-dependent seeks. n must be a multiple
  4312. * of the tape device's physical block size.
  4313. */
  4314. static int
  4315. tseek(off_t n)
  4316. {
  4317. int fault;
  4318. if (tapeblock > 0) {
  4319. int i = (n - poffs) / tapeblock;
  4320. #if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
  4321. defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
  4322. defined (__OpenBSD__) || defined (__DragonFly__) || \
  4323. defined (__CYGWIN__)
  4324. struct mtop mo;
  4325. mo.mt_op = i > 0 ? MTFSR : MTBSR;
  4326. mo.mt_count = i > 0 ? i : -i;
  4327. fault = ioctl(mt, MTIOCTOP, &mo);
  4328. #endif
  4329. } else
  4330. fault = lseek(mt, n - poffs, SEEK_CUR) == (off_t)-1 ? -1 : 0;
  4331. if (fault == 0)
  4332. poffs = n;
  4333. return fault;
  4334. }
  4335. /*
  4336. * Advance to the trailer on the tape (for -A).
  4337. */
  4338. static int
  4339. totrailer(void)
  4340. {
  4341. union bincpio bc;
  4342. struct file f;
  4343. off_t ooffs, noffs, diff;
  4344. int i;
  4345. if (mread() == 0)
  4346. unexeoa();
  4347. if (fmttype == FMT_NONE)
  4348. whathdr();
  4349. memset(&f, 0, sizeof f);
  4350. for (;;) {
  4351. ooffs = aoffs;
  4352. if ((i = readhdr(&f, &bc)) < 0)
  4353. goto fail;
  4354. if (i > 0)
  4355. break;
  4356. markdev(f.f_st.st_dev);
  4357. if (skipfile(&f) != 0)
  4358. goto fail;
  4359. if (fmttype == FMT_ZIP)
  4360. zipdefer(f.f_name, &f.f_st, ooffs,
  4361. f.f_chksum, f.f_csize, &bc.Zdr);
  4362. pax_track(f.f_name, f.f_st.st_mtime);
  4363. }
  4364. /*
  4365. * Now seek to the position of the trailer, but retain the
  4366. * block alignment.
  4367. */
  4368. diff = ooffs % blksiz;
  4369. noffs = ooffs - diff;
  4370. if (diff ? tseek(noffs) == 0 && mread() >= diff && tseek(noffs) == 0
  4371. : tseek(ooffs) == 0) {
  4372. free(f.f_name);
  4373. free(f.f_lnam);
  4374. nwritten = ooffs;
  4375. curpos = diff;
  4376. return 0;
  4377. }
  4378. fail: msg(4, 1, "Unable to append to this archive\n");
  4379. /*NOTREACHED*/
  4380. return 1;
  4381. }
  4382. static long long
  4383. rdoct(const char *data, int len)
  4384. {
  4385. int i;
  4386. long long val = 0;
  4387. for (i = 0; i < len && data[i] == ' '; i++);
  4388. for ( ; i < len && data[i] && data[i] != ' '; i++) {
  4389. val <<= 3;
  4390. val += data[i] - '0';
  4391. }
  4392. return val;
  4393. }
  4394. static long long
  4395. rdhex(const char *data, int len)
  4396. {
  4397. int i;
  4398. long long val = 0;
  4399. for (i = 0; i < len && data[i] == ' '; i++);
  4400. for ( ; i < len && data[i] && data[i] != ' '; i++) {
  4401. val <<= 4;
  4402. val += data[i] > '9' ? data[i] > 'F' ? data[i] - 'a' + 10 :
  4403. data[i] - 'A' + 10 : data[i] - '0';
  4404. }
  4405. return val;
  4406. }
  4407. void
  4408. unexeoa(void)
  4409. {
  4410. if (sysv3) {
  4411. fprintf(stderr,
  4412. "Can't read input: end of file encountered "
  4413. "prior to expected end of archive.\n");
  4414. done(1);
  4415. }
  4416. else
  4417. msg(3, 1, "Unexpected end-of-archive encountered.\n");
  4418. }
  4419. static char *peekdata;
  4420. static size_t peekbot, peektop, peeksize;
  4421. void
  4422. bunread(const char *data, size_t sz)
  4423. {
  4424. peekdata = srealloc(peekdata, peeksize += sz);
  4425. memcpy(&peekdata[peekbot], data, sz);
  4426. peektop += sz;
  4427. aoffs -= sz;
  4428. }
  4429. /*
  4430. * Buffered read of data from tape. sz is the amount of data required
  4431. * by the archive format; if it cannot be retrieved, processing fails.
  4432. */
  4433. ssize_t
  4434. bread(char *data, size_t sz)
  4435. {
  4436. ssize_t rd = 0;
  4437. if (peekdata) {
  4438. rd = sz>peektop-peekbot ? peektop-peekbot : sz;
  4439. memcpy(&data[0], &peekdata[peekbot], rd);
  4440. sz -= rd;
  4441. peekbot += rd;
  4442. if (peekbot == peektop) {
  4443. free(peekdata);
  4444. peekdata = 0;
  4445. peeksize = 0;
  4446. peekbot = peektop = 0;
  4447. }
  4448. }
  4449. while (sz) {
  4450. if (blktop - curpos >= sz) {
  4451. memcpy(&data[rd], &blkbuf[curpos], sz);
  4452. curpos += sz;
  4453. rd += sz;
  4454. aoffs += rd;
  4455. return rd;
  4456. }
  4457. if (blktop > curpos) {
  4458. memcpy(&data[rd], &blkbuf[curpos], blktop - curpos);
  4459. rd += blktop - curpos;
  4460. sz -= blktop - curpos;
  4461. curpos = blktop;
  4462. }
  4463. if (mfl < 0) {
  4464. if (mfl == -1) {
  4465. emsg(3, "I/O error on \"%s\"",
  4466. Iflag ? Iflag : "input");
  4467. if (kflag == 0)
  4468. break;
  4469. if (tapeblock < 0 && (
  4470. (mtst.st_mode&S_IFMT)==S_IFBLK||
  4471. (mtst.st_mode&S_IFMT)==S_IFCHR||
  4472. (mtst.st_mode&S_IFMT)==S_IFREG
  4473. ) && lseek(mt, blksiz, SEEK_CUR)
  4474. == (off_t)-1) {
  4475. emsg(3, "Cannot lseek()");
  4476. done(1);
  4477. }
  4478. }
  4479. if (kflag == 0 || mfl == -2) {
  4480. if ((mtst.st_mode&S_IFMT)!=S_IFCHR &&
  4481. (mtst.st_mode&S_IFMT)!=S_IFBLK)
  4482. break;
  4483. newmedia(mfl == -1 ? errno : 0);
  4484. if (mfl == -1) {
  4485. mfl = -2;
  4486. break;
  4487. }
  4488. if (fmttype & TYP_BAR)
  4489. curpos = 512;
  4490. }
  4491. }
  4492. mread();
  4493. }
  4494. aoffs += rd;
  4495. return rd;
  4496. }
  4497. /*
  4498. * Read a block of data from tape.
  4499. */
  4500. static ssize_t
  4501. mread(void)
  4502. {
  4503. ssize_t ro, rt = 0;
  4504. do {
  4505. if ((ro = read(mt, blkbuf + rt, blksiz - rt)) <= 0) {
  4506. if (ro < 0) {
  4507. if (errno == EINTR)
  4508. continue;
  4509. mfl = -1;
  4510. } else
  4511. mfl = -2;
  4512. if (rt > 0) {
  4513. rt += ro;
  4514. break;
  4515. }
  4516. curpos = blktop = 0;
  4517. return ro;
  4518. }
  4519. rt += ro;
  4520. poffs += ro;
  4521. if (tapeblock == 0) {
  4522. tapeblock = ro;
  4523. if (!Bflag && !Cflag)
  4524. blksiz = ro;
  4525. }
  4526. } while (rt < blksiz);
  4527. curpos = 0;
  4528. blocks += rt >> 9;
  4529. bytes += rt & 0777;
  4530. blktop = rt;
  4531. return rt;
  4532. }
  4533. /*
  4534. * Look what kind of tape or other archive media we are working on and
  4535. * set the buffer size appropriately (if not specified by the user).
  4536. */
  4537. static void
  4538. mstat(void)
  4539. {
  4540. if (fstat(mt, &mtst) < 0) {
  4541. emsg(3, "Error during stat() of archive");
  4542. done(1);
  4543. }
  4544. #if defined (__linux__)
  4545. if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
  4546. struct mtget mg;
  4547. if (ioctl(mt, MTIOCGET, &mg) == 0)
  4548. tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
  4549. MT_ST_BLKSIZE_SHIFT;
  4550. } else if ((mtst.st_mode&S_IFMT) == S_IFBLK) {
  4551. /*
  4552. * If using a block device, write blocks of the floppy
  4553. * disk sector with direct i/o. This enables signals
  4554. * after each block is written instead of being ~40
  4555. * seconds in uninterruptible sleep when calling close()
  4556. * later. For block devices other than floppies, use the
  4557. * kernel defined i/o block size. For floppies, use direct
  4558. * i/o even when reading since it is faster.
  4559. */
  4560. struct floppy_struct fs;
  4561. int floppy = -1;
  4562. int blkbsz;
  4563. if (blksiz == 0) {
  4564. if ((floppy = ioctl(mt, FDGETPRM, &fs)) == 0)
  4565. blksiz = fs.sect * FD_SECTSIZE(&fs);
  4566. #ifdef BLKBSZGET
  4567. else if (ioctl(mt, BLKBSZGET, &blkbsz) == 0)
  4568. blksiz = blkbsz;
  4569. #endif /* BLKBSZGET */
  4570. }
  4571. #ifdef O_DIRECT
  4572. if ((action == 'o' || floppy == 0) && blksiz != 0) {
  4573. int flags;
  4574. if ((flags = fcntl(mt, F_GETFL)) != -1)
  4575. fcntl(mt, F_SETFL, flags | O_DIRECT);
  4576. }
  4577. }
  4578. #endif /* O_DIRECT */
  4579. #elif defined (__sun)
  4580. if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
  4581. struct mtdrivetype_request mr;
  4582. static struct mtdrivetype md;
  4583. mr.size = sizeof md;
  4584. mr.mtdtp = &md;
  4585. if (ioctl(mt, MTIOCGETDRIVETYPE, &mr) == 0)
  4586. tapeblock = md.bsize;
  4587. }
  4588. #elif defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
  4589. || defined (__DragonFly__)
  4590. if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
  4591. struct mtget mg;
  4592. if (ioctl(mt, MTIOCGET, &mg) == 0)
  4593. tapeblock = mg.mt_blksiz;
  4594. }
  4595. #elif defined (__CYGWIN__)
  4596. if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
  4597. struct mtget mg;
  4598. if (ioctl(mt, MTIOCGET, &mg) == 0)
  4599. tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
  4600. MT_ST_BLKSIZE_SHIFT;
  4601. }
  4602. #elif defined (__hpux) || defined (_AIX)
  4603. #endif /* SVR4.2MP */
  4604. if (blksiz == 0)
  4605. switch (mtst.st_mode&S_IFMT) {
  4606. case S_IFREG:
  4607. case S_IFBLK:
  4608. blksiz = 4096;
  4609. break;
  4610. case S_IFCHR:
  4611. if (action == 'o' && !Aflag) {
  4612. if (pax != PAX_TYPE_CPIO) {
  4613. if (fmttype & TYP_PAX)
  4614. blksiz = 5120;
  4615. else if (fmttype & TYP_TAR)
  4616. blksiz = 10240;
  4617. else if (fmttype & TYP_CPIO)
  4618. blksiz = 5120;
  4619. else
  4620. blksiz = 512;
  4621. } else
  4622. blksiz = tapeblock>0 ? tapeblock : 512;
  4623. } else
  4624. blksiz = tapeblock > 0 && tapeblock % 1024 ?
  4625. tapeblock : tapeblock > 10240 ?
  4626. tapeblock : 10240;
  4627. break;
  4628. default:
  4629. blksiz = 512;
  4630. }
  4631. }
  4632. /*
  4633. * Skip tape data such that size becomes aligned to pad.
  4634. */
  4635. static int
  4636. skippad(unsigned long long size, int pad)
  4637. {
  4638. char b[512];
  4639. int to;
  4640. if ((to = size % pad) != 0) {
  4641. if (bread(b, pad - to) != pad - to)
  4642. unexeoa();
  4643. }
  4644. return 0;
  4645. }
  4646. static int
  4647. allzero(const char *bp, int n)
  4648. {
  4649. int i;
  4650. for (i = 0; i < n; i++)
  4651. if (bp[i] != '\0')
  4652. return 0;
  4653. return 1;
  4654. }
  4655. #define CACHESIZE 16
  4656. static const char *
  4657. getuser(uid_t uid)
  4658. {
  4659. static struct {
  4660. char *name;
  4661. uid_t uid;
  4662. } cache[CACHESIZE];
  4663. static int last;
  4664. int i;
  4665. struct passwd *pwd;
  4666. const char *name;
  4667. for (i = 0; i < CACHESIZE && cache[i].name; i++)
  4668. if (cache[i].uid == uid)
  4669. goto found;
  4670. if ((pwd = getpwuid(uid)) != NULL)
  4671. name = pwd->pw_name;
  4672. else
  4673. name = "";
  4674. if (i >= CACHESIZE) {
  4675. if (last >= CACHESIZE)
  4676. last = 0;
  4677. i = last++;
  4678. }
  4679. if (cache[i].name)
  4680. free(cache[i].name);
  4681. cache[i].name = sstrdup(name);
  4682. cache[i].uid = uid;
  4683. found: return cache[i].name[0] ? cache[i].name : NULL;
  4684. }
  4685. static const char *
  4686. getgroup(gid_t gid)
  4687. {
  4688. static struct {
  4689. char *name;
  4690. gid_t gid;
  4691. } cache[CACHESIZE];
  4692. static int last;
  4693. int i;
  4694. struct group *grp;
  4695. const char *name;
  4696. for (i = 0; i < CACHESIZE && cache[i].name; i++)
  4697. if (cache[i].gid == gid)
  4698. goto found;
  4699. if ((grp = getgrgid(gid)) != NULL)
  4700. name = grp->gr_name;
  4701. else
  4702. name = "";
  4703. if (i >= CACHESIZE) {
  4704. if (last >= CACHESIZE)
  4705. last = 0;
  4706. i = last++;
  4707. }
  4708. if (cache[i].name)
  4709. free(cache[i].name);
  4710. cache[i].name = sstrdup(name);
  4711. cache[i].gid = gid;
  4712. found: return cache[i].name[0] ? cache[i].name : NULL;
  4713. }
  4714. /*
  4715. * Return a version of the passed string that contains at most one '%d'
  4716. * and no other printf formats.
  4717. */
  4718. char *
  4719. oneintfmt(const char *op)
  4720. {
  4721. char *new, *np;
  4722. int no = 0;
  4723. np = new = smalloc(2 * strlen(op) + 1);
  4724. do {
  4725. if (*op == '%') {
  4726. *np++ = *op++;
  4727. if (*op != '%')
  4728. if (*op != 'd' || no++)
  4729. *np++ = '%';
  4730. }
  4731. *np++ = *op;
  4732. } while (*op++);
  4733. return new;
  4734. }
  4735. char *
  4736. sstrdup(const char *op)
  4737. {
  4738. char *np;
  4739. np = smalloc(strlen(op) + 1);
  4740. strcpy(np, op);
  4741. return np;
  4742. }
  4743. /*
  4744. * Add this pattern to the extraction list with -i.
  4745. */
  4746. void
  4747. addg(const char *pattern, int art)
  4748. {
  4749. struct glist *gp;
  4750. gp = scalloc(1, sizeof *gp);
  4751. if (pax == PAX_TYPE_CPIO && pattern[0] == '!') {
  4752. gp->g_not = 1;
  4753. pattern++;
  4754. }
  4755. gp->g_pat = sstrdup(pattern);
  4756. gp->g_art = art;
  4757. if (pax != PAX_TYPE_CPIO) {
  4758. struct glist *gb = NULL, *gc;
  4759. for (gc = patterns; gc; gc = gc->g_nxt)
  4760. gb = gc;
  4761. if (gb)
  4762. gb->g_nxt = gp;
  4763. else
  4764. patterns = gp;
  4765. } else {
  4766. gp->g_nxt = patterns;
  4767. patterns = gp;
  4768. }
  4769. }
  4770. /*
  4771. * Check if the file name s matches any of the given patterns.
  4772. */
  4773. static struct glist *
  4774. want(struct file *f, struct glist **gb)
  4775. {
  4776. extern int gmatch(const char *, const char *);
  4777. struct glist *gp;
  4778. for (gp = patterns; gp; gp = gp->g_nxt) {
  4779. if ((gmatch(f->f_name, gp->g_pat) != 0) ^ gp->g_not &&
  4780. (pax_nflag == 0 || gp->g_gotcha == 0)) {
  4781. return gp;
  4782. }
  4783. *gb = gp;
  4784. }
  4785. return NULL;
  4786. }
  4787. static void
  4788. patfile(void)
  4789. {
  4790. struct iblok *ip;
  4791. char *name = NULL;
  4792. size_t namsiz = 0, namlen;
  4793. if ((ip = ib_open(Eflag, 0)) == NULL)
  4794. msg(3, -2, "Cannot open \"%s\" to read patterns\n", Eflag);
  4795. while ((namlen = ib_getlin(ip, &name, &namsiz, srealloc)) != 0) {
  4796. if (name[namlen-1] == '\n')
  4797. name[--namlen] = '\0';
  4798. addg(name, 0);
  4799. }
  4800. ib_close(ip);
  4801. }
  4802. void
  4803. swap(char *b, size_t sz, int s8, int s16)
  4804. {
  4805. uint8_t u8;
  4806. uint16_t u16;
  4807. union types2 *t2;
  4808. union types4 *t4;
  4809. int i;
  4810. if (s8) {
  4811. for (i = 0; i < (sz >> 1); i++) {
  4812. t2 = &((union types2 *)b)[i];
  4813. u8 = t2->byte[0];
  4814. t2->byte[0] = t2->byte[1];
  4815. t2->byte[1] = u8;
  4816. }
  4817. }
  4818. if (s16) {
  4819. for (i = 0; i < (sz >> 2); i++) {
  4820. t4 = &((union types4 *)b)[i];
  4821. u16 = t4->sword[0];
  4822. t4->sword[0] = t4->sword[1];
  4823. t4->sword[1] = u16;
  4824. }
  4825. }
  4826. }
  4827. static int
  4828. ckodd(long long size, int mod, const char *str, const char *fn)
  4829. {
  4830. if (size % mod) {
  4831. msg(3, 0, "Cannot swap %s of \"%s\", odd number of %s\n",
  4832. str, fn, str);
  4833. errcnt++;
  4834. return 1;
  4835. }
  4836. return 0;
  4837. }
  4838. /*
  4839. * Interactive rename (-r option).
  4840. */
  4841. static int
  4842. rname(char **oldp, size_t *olds)
  4843. {
  4844. char *new = NULL;
  4845. size_t newsize = 0;
  4846. int i, r;
  4847. char c;
  4848. fprintf(stderr, "Rename \"%s\"? ", *oldp);
  4849. if (tty == 0)
  4850. if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
  4851. fcntl(tty, F_SETFD, FD_CLOEXEC) < 0)
  4852. err: msg(3, 1, "Cannot read tty.\n");
  4853. i = 0;
  4854. while ((r = read(tty, &c, 1)) == 1 && c != '\n') {
  4855. if (i+1 >= newsize)
  4856. new = srealloc(new, newsize += 32);
  4857. new[i++] = c;
  4858. }
  4859. if (r <= 0)
  4860. goto err;
  4861. if (new == NULL)
  4862. return 0;
  4863. new[i] = '\0';
  4864. if (new[0] == '.' && new[1] == '\0') {
  4865. free(new);
  4866. } else {
  4867. free(*oldp);
  4868. *oldp = new;
  4869. *olds = newsize;
  4870. }
  4871. return 1;
  4872. }
  4873. /*
  4874. * Filter data from tape through the commands given in arg?.
  4875. */
  4876. static int
  4877. redirect(const char *arg0, const char *arg1)
  4878. {
  4879. int pd[2];
  4880. if (pipe(pd) < 0)
  4881. return -1;
  4882. switch (fork()) {
  4883. case 0:
  4884. if (tapeblock>=0 || lseek(mt, -blktop, SEEK_CUR) == (off_t)-1) {
  4885. int xpd[2];
  4886. if (pipe(xpd) == 0 && fork() == 0) {
  4887. ssize_t rd, wo, wt;
  4888. close(xpd[0]);
  4889. do {
  4890. wo = wt = 0;
  4891. do {
  4892. if ((wo = write(xpd[1],
  4893. blkbuf + wt,
  4894. blktop - wt))
  4895. <= 0) {
  4896. if (errno == EINTR)
  4897. continue;
  4898. _exit(0);
  4899. }
  4900. wt += wo;
  4901. } while (wt < blktop);
  4902. } while ((rd = mread()) >= 0);
  4903. if (rd < 0) {
  4904. emsg(3, "Read error on \"%s\"",
  4905. Iflag && !sysv3 ?
  4906. Iflag : "input");
  4907. }
  4908. _exit(0);
  4909. } else {
  4910. close(xpd[1]);
  4911. dup2(xpd[0], 0);
  4912. close(xpd[0]);
  4913. }
  4914. } else {
  4915. dup2(mt, 0);
  4916. }
  4917. close(mt);
  4918. dup2(pd[1], 1);
  4919. close(pd[0]);
  4920. close(pd[1]);
  4921. execlp(arg0, arg0, arg1, NULL);
  4922. fprintf(stderr, "%s: could not exec %s: %s\n",
  4923. progname, arg0, strerror(errno));
  4924. _exit(0177);
  4925. /*NOTREACHED*/
  4926. default:
  4927. dup2(pd[0], mt);
  4928. close(pd[0]);
  4929. close(pd[1]);
  4930. tapeblock = -1;
  4931. break;
  4932. case -1:
  4933. return -1;
  4934. }
  4935. return 0;
  4936. }
  4937. /*
  4938. * Get the name stored in a tar header. buf is expected to be at least
  4939. * TPFXSIZ+TNAMSIZ+2 bytes.
  4940. */
  4941. static char *
  4942. tnameof(struct tar_header *hp, char *buf)
  4943. {
  4944. const char *cp;
  4945. char *bp = buf;
  4946. if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR &&
  4947. hp->t_prefix[0] != '\0') {
  4948. cp = hp->t_prefix;
  4949. while (cp < &hp->t_prefix[TPFXSIZ] && *cp)
  4950. *bp++ = *cp++;
  4951. if (bp > buf)
  4952. *bp++ = '/';
  4953. }
  4954. cp = hp->t_name;
  4955. while (cp < &hp->t_name[TNAMSIZ] && *cp)
  4956. *bp++ = *cp++;
  4957. *bp = '\0';
  4958. return buf;
  4959. }
  4960. /*
  4961. * Store fn as file name in a tar header.
  4962. */
  4963. static int
  4964. tmkname(struct tar_header *hp, const char *fn)
  4965. {
  4966. const char *cp, *cs = NULL;
  4967. for (cp = fn; *cp; cp++) {
  4968. if (fmttype & TYP_USTAR && *cp == '/' && cp[1] != '\0' &&
  4969. cp > fn && cp-fn <= TPFXSIZ)
  4970. cs = cp;
  4971. }
  4972. if (fmttype == FMT_GNUTAR && cp - fn > 99) {
  4973. writegnuname(fn, cp - fn + 1, 'L');
  4974. cp = &fn[99];
  4975. } else if (cp - (cs ? &cs[1] : fn) > TNAMSIZ) {
  4976. if (fmttype & TYP_PAX && utf8(fn)) {
  4977. paxrec |= PR_PATH;
  4978. strcpy(hp->t_name, sequence());
  4979. return 0;
  4980. }
  4981. msg(3, 0, "%s: file name too long\n", fn);
  4982. return -1;
  4983. }
  4984. if (cs && cp - fn > TNAMSIZ) {
  4985. memcpy(hp->t_prefix, fn, cs - fn);
  4986. if (cs - fn < TPFXSIZ)
  4987. hp->t_prefix[cs - fn] = '\0';
  4988. memcpy(hp->t_name, &cs[1], cp - &cs[1]);
  4989. if (cp - &cs[1] < TNAMSIZ)
  4990. hp->t_name[cp - &cs[1]] = '\0';
  4991. } else {
  4992. memcpy(hp->t_name, fn, cp - fn);
  4993. if (cp - fn < TNAMSIZ)
  4994. hp->t_name[cp - fn] = '\0';
  4995. }
  4996. return 0;
  4997. }
  4998. /*
  4999. * Get the link name of a tar header.
  5000. */
  5001. static void
  5002. tlinkof(struct tar_header *hp, struct file *f)
  5003. {
  5004. const char *cp;
  5005. char *bp;
  5006. if (f->f_lsiz < TNAMSIZ+1)
  5007. f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = TNAMSIZ+1);
  5008. cp = hp->t_linkname;
  5009. bp = f->f_lnam;
  5010. while (cp < &hp->t_linkname[TNAMSIZ] && *cp)
  5011. *bp++ = *cp++;
  5012. *bp = '\0';
  5013. }
  5014. /*
  5015. * Create the link name in a tar header.
  5016. */
  5017. static int
  5018. tmklink(struct tar_header *hp, const char *fn)
  5019. {
  5020. const char *cp;
  5021. for (cp = fn; *cp; cp++);
  5022. if (fmttype == FMT_GNUTAR && cp - fn > 99) {
  5023. writegnuname(fn, cp - fn + 1, 'K');
  5024. cp = &fn[99];
  5025. } else if (cp - fn > TNAMSIZ) {
  5026. if (fmttype & TYP_PAX && utf8(fn)) {
  5027. paxrec |= PR_LINKPATH;
  5028. strcpy(hp->t_linkname, sequence());
  5029. return 0;
  5030. }
  5031. msg(3, 0, "%s: linked name too long\n", fn);
  5032. return -1;
  5033. }
  5034. memcpy(hp->t_linkname, fn, cp - fn);
  5035. if (cp - fn < TNAMSIZ)
  5036. hp->t_linkname[cp - fn] = '\0';
  5037. return 0;
  5038. }
  5039. static int
  5040. tlflag(struct stat *st)
  5041. {
  5042. if (fmttype & TYP_BAR) {
  5043. switch (st->st_mode & S_IFMT) {
  5044. case S_IFREG:
  5045. case S_IFDIR:
  5046. return '0';
  5047. case S_IFLNK:
  5048. return '2';
  5049. default:
  5050. return '3';
  5051. }
  5052. } else if (fmttype & TYP_USTAR) {
  5053. switch (st->st_mode & S_IFMT) {
  5054. case S_IFREG:
  5055. return '0';
  5056. case S_IFLNK:
  5057. return '2';
  5058. case S_IFCHR:
  5059. return '3';
  5060. case S_IFBLK:
  5061. return '4';
  5062. case S_IFDIR:
  5063. return '5';
  5064. case S_IFIFO:
  5065. return '6';
  5066. default:
  5067. return -1;
  5068. }
  5069. } else {
  5070. switch (st->st_mode & S_IFMT) {
  5071. case S_IFREG:
  5072. return '\0';
  5073. case S_IFLNK:
  5074. return '2';
  5075. default:
  5076. return -1;
  5077. }
  5078. }
  5079. }
  5080. /*
  5081. * Ustar checksums are created using unsigned chars, as specified by
  5082. * POSIX. Traditional tar implementations use signed chars. Some
  5083. * implementations (notably SVR4 cpio derivatives) use signed chars
  5084. * even for ustar archives, but this is clearly an implementation bug.
  5085. */
  5086. static void
  5087. tchksum(union bincpio *bp)
  5088. {
  5089. uint32_t sum;
  5090. char *cp;
  5091. memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
  5092. sum = 0;
  5093. if (fmttype & TYP_USTAR)
  5094. for (cp = bp->data; cp < &bp->data[512]; cp++)
  5095. sum += *((unsigned char *)cp);
  5096. else
  5097. for (cp = bp->data; cp < &bp->data[512]; cp++)
  5098. sum += *((signed char *)cp);
  5099. sprintf(bp->Tdr.t_chksum, "%7.7o", sum);
  5100. }
  5101. static int
  5102. tcssum(union bincpio *bp, int ustar)
  5103. {
  5104. uint32_t ssum = 0, usum = 0, osum;
  5105. char ochk[sizeof bp->Tdr.t_chksum];
  5106. char *cp;
  5107. osum = rdoct(bp->Tdr.t_chksum, 8);
  5108. memcpy(ochk, bp->Tdr.t_chksum, sizeof ochk);
  5109. memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
  5110. for (cp = bp->data; cp < &bp->data[512]; cp++) {
  5111. ssum += *((signed char *)cp);
  5112. usum += *((unsigned char *)cp);
  5113. }
  5114. memcpy(bp->Tdr.t_chksum, ochk, sizeof bp->Tdr.t_chksum);
  5115. return ssum != osum && usum != osum;
  5116. }
  5117. static int
  5118. trdsum(union bincpio *bp)
  5119. {
  5120. int i;
  5121. if (fmttype & TYP_BAR)
  5122. i = bcssum(bp);
  5123. else
  5124. i = tcssum(bp, fmttype & TYP_USTAR);
  5125. if (i)
  5126. msg(3, 0, "Bad header - checksum error.\n");
  5127. return i;
  5128. }
  5129. static mode_t
  5130. tifmt(int c)
  5131. {
  5132. switch (c) {
  5133. default:
  5134. case '\0':
  5135. case '0':
  5136. return S_IFREG;
  5137. case '2':
  5138. return S_IFLNK;
  5139. case '3':
  5140. return S_IFCHR;
  5141. case '4':
  5142. return S_IFBLK;
  5143. case '5':
  5144. return S_IFDIR;
  5145. case '6':
  5146. return S_IFIFO;
  5147. }
  5148. }
  5149. /*
  5150. * bar format support functions.
  5151. */
  5152. static void
  5153. bchksum(union bincpio *bp)
  5154. {
  5155. uint32_t sum;
  5156. char *cp;
  5157. memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
  5158. sum = 0;
  5159. for (cp = bp->data; cp < &bp->data[512]; cp++)
  5160. sum += *((signed char *)cp);
  5161. sprintf(bp->Bdr.b_chksum, "%7.7o", sum);
  5162. }
  5163. static int
  5164. bcssum(union bincpio *bp)
  5165. {
  5166. uint32_t sum, osum;
  5167. char ochk[sizeof bp->Bdr.b_chksum];
  5168. char *cp;
  5169. osum = rdoct(bp->Bdr.b_chksum, 8);
  5170. memcpy(ochk, bp->Bdr.b_chksum, sizeof ochk);
  5171. memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
  5172. sum = 0;
  5173. for (cp = bp->data; cp < &bp->data[512]; cp++)
  5174. sum += *((signed char *)cp);
  5175. memcpy(bp->Bdr.b_chksum, ochk, sizeof bp->Bdr.b_chksum);
  5176. return sum != osum;
  5177. }
  5178. static void
  5179. blinkof(const char *cp, struct file *f, int namlen)
  5180. {
  5181. if (f->f_lsiz < 512)
  5182. f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = 512);
  5183. strcpy(f->f_lnam, &cp[SIZEOF_bar_header + namlen]);
  5184. }
  5185. static void
  5186. dump_barhdr(void)
  5187. {
  5188. union bincpio bc;
  5189. static int volno;
  5190. static time_t now = -1;
  5191. memset(&bc, 0, 512);
  5192. sprintf(bc.Bdr.b_uid, "%d", myuid & 07777777);
  5193. sprintf(bc.Bdr.b_gid, "%d", mygid & 07777777);
  5194. bc.Bdr.b_size[0] = '0';
  5195. memcpy(bc.Bdr.b_bar_magic, mag_bar, sizeof bc.Bdr.b_bar_magic);
  5196. sprintf(bc.Bdr.b_volume_num, "%d", ++volno & 0777);
  5197. bc.Bdr.b_compressed = '0';
  5198. if (now == (time_t)-1)
  5199. time(&now);
  5200. sprintf(bc.Bdr.b_date, "%llo", now & 077777777777LL);
  5201. bchksum(&bc);
  5202. bwrite(bc.data, 512);
  5203. }
  5204. /*
  5205. * Support for compressed bar format (any regular file is piped through zcat).
  5206. */
  5207. static pid_t zpid;
  5208. static int
  5209. zcreat(const char *name, mode_t mode)
  5210. {
  5211. int pd[2];
  5212. int fd;
  5213. if (pipe(pd) < 0)
  5214. return -1;
  5215. if ((fd = creat(name, mode)) < 0) {
  5216. fd = errno;
  5217. close(pd[0]);
  5218. close(pd[1]);
  5219. errno = fd;
  5220. return -1;
  5221. }
  5222. switch (zpid = fork()) {
  5223. case -1:
  5224. return -1;
  5225. case 0:
  5226. dup2(pd[0], 0);
  5227. dup2(fd, 1);
  5228. close(pd[0]);
  5229. close(pd[1]);
  5230. close(fd);
  5231. execlp("zcat", "zcat", NULL);
  5232. _exit(0177);
  5233. /*NOTREACHED*/
  5234. }
  5235. close(pd[0]);
  5236. close(fd);
  5237. sigset(SIGPIPE, SIG_IGN);
  5238. return pd[1];
  5239. }
  5240. static int
  5241. zclose(int fd)
  5242. {
  5243. int c, s;
  5244. c = close(fd);
  5245. while (waitpid(zpid, &s, 0) != zpid);
  5246. return c != 0 || s != 0 ? -1 : 0;
  5247. }
  5248. /*
  5249. * If using the -A option, device numbers that appear in the archive
  5250. * are not reused for appended files. This avoids wrong hardlink
  5251. * connections on extraction.
  5252. *
  5253. * In fact, this should be done even if we did not fake device and
  5254. * inode numbers, since it is not guaranteed that the archive was
  5255. * created on the same machine, or even on the same machine, inode
  5256. * number could have changed after a file was unlinked.
  5257. */
  5258. static void
  5259. markdev(dev_t dev)
  5260. {
  5261. struct dslot *dp, *dq = NULL;;
  5262. for (dp = markeddevs; dp; dp = dp->d_nxt) {
  5263. if (dp->d_dev == dev)
  5264. return;
  5265. dq = dp;
  5266. }
  5267. dp = scalloc(1, sizeof *dp);
  5268. dp->d_dev = dev;
  5269. if (markeddevs == NULL)
  5270. markeddevs = dp;
  5271. else
  5272. dq->d_nxt = dp;
  5273. }
  5274. static int
  5275. marked(dev_t dev)
  5276. {
  5277. struct dslot *dp;
  5278. for (dp = markeddevs; dp; dp = dp->d_nxt)
  5279. if (dp->d_dev == dev)
  5280. return 1;
  5281. return 0;
  5282. }
  5283. static void
  5284. cantsup(int err, const char *file)
  5285. {
  5286. if (sysv3)
  5287. msg(err ? 3 : 2, 0,
  5288. "format can't support expanded types on %s\n",
  5289. file);
  5290. else
  5291. msg(0, 0, "%s format can't support expanded types on %s\n",
  5292. err ? "Error" : "Warning", file);
  5293. errcnt++;
  5294. }
  5295. static void
  5296. onint(int signo)
  5297. {
  5298. if (cur_ofile && cur_tfile && rename(cur_tfile, cur_ofile) < 0)
  5299. emsg(3, "Cannot recover original \"%s\"", cur_ofile);
  5300. if (cur_ofile && cur_tfile == NULL && unlink(cur_ofile) < 0)
  5301. emsg(3, "Cannot remove incomplete \"%s\"", cur_ofile);
  5302. exit(signo | 0200);
  5303. }
  5304. /*
  5305. * Read the compressed zip data part for a file.
  5306. */
  5307. static int
  5308. zipread(struct file *f, const char *tgt, int tfd, int doswap)
  5309. {
  5310. int val = 0;
  5311. uint32_t crc = 0;
  5312. if (f->f_gflag & FG_DESC) {
  5313. if (f->f_cmethod != C_DEFLATED && f->f_cmethod != C_ENHDEFLD ||
  5314. f->f_gflag & FG_CRYPT)
  5315. msg(4, 1, "Cannot handle zip data descriptor\n");
  5316. f->f_csize = 0x7FFFFFFFFFFFFFFFLL;
  5317. f->f_st.st_size = 0x7FFFFFFFFFFFFFFFLL;
  5318. } else if (tfd < 0)
  5319. return skipfile(f);
  5320. if (f->f_gflag & FG_CRYPT)
  5321. return cantunzip(f, "encrypted");
  5322. switch (f->f_cmethod) {
  5323. case C_DEFLATED:
  5324. case C_ENHDEFLD:
  5325. val = zipinflate(f, tgt, tfd, doswap, &crc);
  5326. break;
  5327. case C_SHRUNK:
  5328. val = zipunshrink(f, tgt, tfd, doswap, &crc);
  5329. break;
  5330. case C_IMPLODED:
  5331. val = zipexplode(f, tgt, tfd, doswap, &crc);
  5332. break;
  5333. case C_REDUCED1:
  5334. case C_REDUCED2:
  5335. case C_REDUCED3:
  5336. case C_REDUCED4:
  5337. val = zipexpand(f, tgt, tfd, doswap, &crc);
  5338. break;
  5339. case C_TOKENIZED:
  5340. return cantunzip(f, "tokenized");
  5341. case C_DCLIMPLODED:
  5342. val = zipblast(f, tgt, tfd, doswap, &crc);
  5343. break;
  5344. case C_BZIP2:
  5345. #if USE_BZLIB
  5346. val = zipunbz2(f, tgt, tfd, doswap, &crc);
  5347. break;
  5348. #else /* !USE_BZLIB */
  5349. return cantunzip(f, "bzip2 compressed");
  5350. #endif /* !USE_BZLIB */
  5351. default:
  5352. return cantunzip(f, "compressed");
  5353. }
  5354. if (f->f_gflag & FG_DESC)
  5355. zipreaddesc(f);
  5356. if (val == 0 && crc != f->f_chksum) {
  5357. msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
  5358. return -1;
  5359. }
  5360. return val;
  5361. }
  5362. /*
  5363. * Read a zip data descriptor (i. e. a field after the compressed data
  5364. * that contains the actual values for sizes and crc).
  5365. */
  5366. static void
  5367. zipreaddesc(struct file *f)
  5368. {
  5369. if (f->f_oflag & OF_ZIP64) {
  5370. struct zipddesc64 zd64;
  5371. bread((char *)&zd64, SIZEOF_zipddesc64);
  5372. if (memcmp(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds))
  5373. msg(4, 1, "Invalid zip data descriptor\n");
  5374. f->f_chksum = ple32(zd64.zd_crc32);
  5375. f->f_dsize = f->f_st.st_size = ple64(zd64.zd_nsize) &
  5376. 0xFFFFFFFFFFFFFFFFULL;
  5377. f->f_csize = ple64(zd64.zd_csize) &
  5378. 0xFFFFFFFFFFFFFFFFULL;
  5379. } else {
  5380. struct zipddesc zd;
  5381. bread((char *)&zd, SIZEOF_zipddesc);
  5382. if (memcmp(zd.zd_signature, mag_zipdds, sizeof mag_zipdds))
  5383. msg(4, 1, "Invalid zip data descriptor\n");
  5384. f->f_chksum = ple32(zd.zd_crc32);
  5385. f->f_dsize = f->f_st.st_size = ple32(zd.zd_nsize)&0xFFFFFFFFUL;
  5386. f->f_csize = ple32(zd.zd_csize)&0xFFFFFFFFUL;
  5387. }
  5388. }
  5389. static int
  5390. cantunzip(struct file *f, const char *method)
  5391. {
  5392. msg(3, 0, "Cannot unzip %s file \"%s\"\n", method, f->f_name);
  5393. errcnt++;
  5394. return skipfile(f);
  5395. }
  5396. /*
  5397. * PC-DOS time format:
  5398. *
  5399. * 31 24 20 15 10 4 0
  5400. * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  5401. * | | | | |
  5402. * |year |months |days |hours |minutes |biseconds|
  5403. *
  5404. * refers to local time on the machine it was created on.
  5405. */
  5406. static time_t
  5407. gdostime(const char *tp, const char *dp)
  5408. {
  5409. uint32_t v;
  5410. struct tm tm;
  5411. v = (int)(tp[0]&0377) +
  5412. ((int)(tp[1]&0377) << 8) +
  5413. ((int)(dp[0]&0377) << 16) +
  5414. ((int)(dp[1]&0377) << 24);
  5415. memset(&tm, 0, sizeof tm);
  5416. tm.tm_sec = (v&0x1F) << 1;
  5417. tm.tm_min = (v&0x7E0) >> 5;
  5418. tm.tm_hour = (v&0xF800) >> 11;
  5419. tm.tm_mday = ((v&0x1F0000) >> 16);
  5420. tm.tm_mon = ((v&0x1E00000) >> 21) - 1;
  5421. tm.tm_year = ((v&0xFE000000) >> 25) + 80;
  5422. tm.tm_isdst = -1;
  5423. return mktime(&tm);
  5424. }
  5425. static void
  5426. mkdostime(time_t t, char *tp, char *dp)
  5427. {
  5428. uint32_t v;
  5429. struct tm *tm;
  5430. tm = localtime(&t);
  5431. v = (tm->tm_sec >> 1) + (tm->tm_sec&1) + (tm->tm_min << 5) +
  5432. (tm->tm_hour << 11) + (tm->tm_mday << 16) +
  5433. ((tm->tm_mon+1) << 21) + ((tm->tm_year - 80) << 25);
  5434. le16p(v&0x0000ffff, tp);
  5435. le16p((v&0xffff0000) >> 16, dp);
  5436. }
  5437. /*
  5438. * Read and interpret the zip extra field for a file.
  5439. */
  5440. static ssize_t
  5441. ziprxtra(struct file *f, struct zip_header *z)
  5442. {
  5443. union zextra *x, *xp;
  5444. short tag, size;
  5445. ssize_t len;
  5446. len = ple16(z->z_extralen)&0177777;
  5447. if (len > 0) {
  5448. x = smalloc(len);
  5449. if (bread((char *)x, len) != len)
  5450. return -1;
  5451. if (len < 4)
  5452. return len;
  5453. xp = x;
  5454. while (len > 0) {
  5455. if (len < 4)
  5456. return -1;
  5457. tag = ple16(xp->Ze_gn.ze_gn_tag);
  5458. size = (ple16(xp->Ze_gn.ze_gn_tsize)&0177777) + 4;
  5459. switch (tag) {
  5460. case mag_zip64f: /* ZIP64 extended information */
  5461. if (size != SIZEOF_zextra_64 &&
  5462. size != SIZEOF_zextra_64_a &&
  5463. size != SIZEOF_zextra_64_b)
  5464. break;
  5465. if (f->f_st.st_size == 0xffffffff)
  5466. f->f_st.st_size =
  5467. ple64(xp->Ze_64.ze_64_nsize);
  5468. if (f->f_csize == 0xffffffff)
  5469. f->f_csize =
  5470. ple64(xp->Ze_64.ze_64_csize);
  5471. f->f_oflag |= OF_ZIP64;
  5472. break;
  5473. case 0x000d: /* PKWARE Unix Extra Field */
  5474. if (size != SIZEOF_zextra_pk)
  5475. break;
  5476. f->f_st.st_atime = ple32(xp->Ze_pk.ze_pk_atime);
  5477. f->f_st.st_mtime = ple32(xp->Ze_pk.ze_pk_mtime);
  5478. f->f_st.st_uid = ple16(xp->Ze_pk.ze_pk_uid) &
  5479. 0177777;
  5480. f->f_st.st_gid = ple16(xp->Ze_pk.ze_pk_gid) &
  5481. 0177777;
  5482. break;
  5483. case 0x5455: /* Extended Timestamp Extra Field */
  5484. if (xp->Ze_et.ze_et_flags[0] & 1)
  5485. f->f_st.st_atime =
  5486. ple32(xp->Ze_et.ze_et_atime);
  5487. if (xp->Ze_et.ze_et_flags[0] & 2)
  5488. f->f_st.st_mtime =
  5489. ple32(xp->Ze_et.ze_et_mtime);
  5490. if (xp->Ze_et.ze_et_flags[0] & 3)
  5491. f->f_st.st_ctime =
  5492. ple32(xp->Ze_et.ze_et_ctime);
  5493. break;
  5494. case 0x5855: /* Info-ZIP Unix Extra Field #1 */
  5495. if (size != SIZEOF_zextra_i1)
  5496. break;
  5497. f->f_st.st_atime = ple32(xp->Ze_i1.ze_i1_atime);
  5498. f->f_st.st_mtime = ple32(xp->Ze_i1.ze_i1_mtime);
  5499. f->f_st.st_uid = ple16(xp->Ze_i1.ze_i1_uid) &
  5500. 0177777;
  5501. f->f_st.st_gid = ple16(xp->Ze_i1.ze_i1_gid) &
  5502. 0177777;
  5503. break;
  5504. case 0x7855: /* Info-ZIP Unix Extra Field #2 */
  5505. if (size != SIZEOF_zextra_i2)
  5506. break;
  5507. f->f_st.st_uid = ple16(xp->Ze_i2.ze_i2_uid) &
  5508. 0177777;
  5509. f->f_st.st_gid = ple16(xp->Ze_i2.ze_i2_gid) &
  5510. 0177777;
  5511. break;
  5512. case 0x756e: /* ASi Unix Extra Field */
  5513. if (size < SIZEOF_zextra_as)
  5514. break;
  5515. f->f_st.st_mode = ple16(xp->Ze_as.ze_as_mode);
  5516. f->f_st.st_uid = ple16(xp->Ze_as.ze_as_uid) &
  5517. 0177777;
  5518. f->f_st.st_gid = ple16(xp->Ze_as.ze_as_gid) &
  5519. 0177777;
  5520. if ((f->f_st.st_mode&S_IFMT) == S_IFLNK) {
  5521. if (f->f_lsiz < size-14+1)
  5522. f->f_lnam = srealloc(f->f_lnam,
  5523. f->f_lsiz =
  5524. size-18+1);
  5525. memcpy(f->f_lnam, &((char *)xp)[18],
  5526. size-18);
  5527. f->f_lnam[size-18] = '\0';
  5528. f->f_st.st_size = size-18;
  5529. } else {
  5530. f->f_st.st_rdev =
  5531. ple32(xp->Ze_as.ze_as_sizdev);
  5532. f->f_rmajor = major(f->f_st.st_rdev);
  5533. f->f_rminor = minor(f->f_st.st_rdev);
  5534. }
  5535. break;
  5536. case mag_zipcpio:
  5537. if (size != SIZEOF_zextra_cp)
  5538. break;
  5539. f->f_st.st_dev = ple32(xp->Ze_cp.ze_cp_dev);
  5540. f->f_st.st_ino = ple32(xp->Ze_cp.ze_cp_ino);
  5541. f->f_st.st_mode = ple32(xp->Ze_cp.ze_cp_mode);
  5542. f->f_st.st_uid = ple32(xp->Ze_cp.ze_cp_uid) &
  5543. 0xFFFFFFFFUL;
  5544. f->f_st.st_gid = ple32(xp->Ze_cp.ze_cp_gid) &
  5545. 0xFFFFFFFFUL;
  5546. f->f_st.st_nlink = ple32(xp->Ze_cp.ze_cp_nlink)&
  5547. 0xFFFFFFFFUL;
  5548. f->f_st.st_rdev = ple32(xp->Ze_cp.ze_cp_rdev);
  5549. f->f_rmajor = major(f->f_st.st_rdev);
  5550. f->f_rminor = minor(f->f_st.st_rdev);
  5551. f->f_st.st_mtime = ple32(xp->Ze_cp.ze_cp_mtime);
  5552. f->f_st.st_atime = ple32(xp->Ze_cp.ze_cp_atime);
  5553. break;
  5554. }
  5555. xp = (union zextra *)&((char *)xp)[size];
  5556. len -= size;
  5557. }
  5558. free(x);
  5559. }
  5560. return len;
  5561. }
  5562. /*
  5563. * Write the central directory and the end of a zip file.
  5564. */
  5565. static void
  5566. ziptrailer(void)
  5567. {
  5568. struct zipstuff *zs;
  5569. struct zipcentral zc;
  5570. struct zipend ze;
  5571. long long cpstart, cpend, entries = 0;
  5572. size_t sz;
  5573. cpstart = nwritten;
  5574. for (zs = zipbulk; zs; zs = zs->zs_next) {
  5575. entries++;
  5576. memset(&zc, 0, SIZEOF_zipcentral);
  5577. memcpy(zc.zc_signature, mag_zipctr, 4);
  5578. zc.zc_versionmade[0] = 20;
  5579. zc.zc_versionextr[0] = zs->zs_cmethod == 8 ? 20 : 10;
  5580. mkdostime(zs->zs_mtime, zc.zc_modtime, zc.zc_moddate);
  5581. le32p(zs->zs_crc32, zc.zc_crc32);
  5582. le16p(zs->zs_cmethod, zc.zc_cmethod);
  5583. le16p(zs->zs_gflag, zc.zc_gflag);
  5584. /*
  5585. * We flag files as created on PC-DOS / FAT filesystem
  5586. * and thus set PC-DOS attributes here.
  5587. */
  5588. if ((zs->zs_mode&0222) == 0)
  5589. zc.zc_external[0] |= 0x01; /* readonly attribute */
  5590. if ((zs->zs_mode&S_IFMT) == S_IFDIR)
  5591. zc.zc_external[0] |= 0x10; /* directory attr. */
  5592. sz = strlen(zs->zs_name);
  5593. le16p(sz, zc.zc_namelen);
  5594. if (zs->zs_size >= 0xffffffff || zs->zs_csize >= 0xffffffff ||
  5595. zs->zs_relative >= 0xffffffff) {
  5596. struct zextra_64 zf;
  5597. memset(&zf, 0, SIZEOF_zextra_64);
  5598. le16p(mag_zip64f, zf.ze_64_tag);
  5599. le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
  5600. if ((zs->zs_mode&S_IFMT) == S_IFREG ||
  5601. (zs->zs_mode&S_IFMT) == S_IFLNK) {
  5602. le32p(0xffffffff, zc.zc_csize);
  5603. le32p(0xffffffff, zc.zc_nsize);
  5604. le64p(zs->zs_csize, zf.ze_64_csize);
  5605. le64p(zs->zs_size, zf.ze_64_nsize);
  5606. }
  5607. le64p(zs->zs_relative, zf.ze_64_reloff);
  5608. le32p(0xffffffff, zc.zc_relative);
  5609. le16p(SIZEOF_zextra_64, zc.zc_extralen);
  5610. bwrite((char *)&zc, SIZEOF_zipcentral);
  5611. bwrite(zs->zs_name, sz);
  5612. bwrite((char *)&zf, SIZEOF_zextra_64);
  5613. } else {
  5614. if ((zs->zs_mode&S_IFMT) == S_IFREG ||
  5615. (zs->zs_mode&S_IFMT) == S_IFLNK) {
  5616. le32p(zs->zs_csize, zc.zc_csize);
  5617. le32p(zs->zs_size, zc.zc_nsize);
  5618. }
  5619. le32p(zs->zs_relative, zc.zc_relative);
  5620. bwrite((char *)&zc, SIZEOF_zipcentral);
  5621. bwrite(zs->zs_name, sz);
  5622. }
  5623. }
  5624. cpend = nwritten;
  5625. memset(&ze, 0, SIZEOF_zipend);
  5626. memcpy(ze.ze_signature, mag_zipend, 4);
  5627. if (cpend >= 0xffffffff || entries >= 0xffff) {
  5628. struct zip64end z6;
  5629. struct zip64loc z4;
  5630. memset(&z6, 0, SIZEOF_zip64end);
  5631. memcpy(z6.z6_signature, mag_zip64e, 4);
  5632. le64p(SIZEOF_zip64end - 12, z6.z6_recsize);
  5633. z6.z6_versionmade[0] = 20;
  5634. z6.z6_versionextr[0] = 20;
  5635. le64p(entries, z6.z6_thisentries);
  5636. le64p(entries, z6.z6_allentries);
  5637. le64p(cpend - cpstart, z6.z6_dirsize);
  5638. le64p(cpstart, z6.z6_startsize);
  5639. bwrite((char *)&z6, SIZEOF_zip64end);
  5640. memset(&z4, 0, SIZEOF_zip64loc);
  5641. memcpy(z4.z4_signature, mag_zip64l, 4);
  5642. le64p(cpend, z4.z4_reloff);
  5643. le32p(1, z4.z4_alldiskn);
  5644. bwrite((char *)&z4, SIZEOF_zip64loc);
  5645. le16p(0xffff, ze.ze_thisentries);
  5646. le16p(0xffff, ze.ze_allentries);
  5647. le32p(0xffffffff, ze.ze_dirsize);
  5648. le32p(0xffffffff, ze.ze_startsize);
  5649. } else {
  5650. le16p(entries, ze.ze_thisentries);
  5651. le16p(entries, ze.ze_allentries);
  5652. le32p(cpend - cpstart, ze.ze_dirsize);
  5653. le32p(cpstart, ze.ze_startsize);
  5654. }
  5655. bwrite((char *)&ze, SIZEOF_zipend);
  5656. }
  5657. /*
  5658. * Store the data later needed for the central directory.
  5659. */
  5660. static void
  5661. zipdefer(const char *fn, struct stat *st, long long relative,
  5662. uint32_t crc, long long csize, const struct zip_header *zh)
  5663. {
  5664. struct zipstuff *zp;
  5665. zp = scalloc(1, sizeof *zp);
  5666. zp->zs_name = sstrdup(fn);
  5667. zp->zs_size = st->st_size;
  5668. zp->zs_mtime = st->st_mtime;
  5669. zp->zs_mode = st->st_mode;
  5670. zp->zs_relative = relative;
  5671. zp->zs_cmethod = ple16(zh->z_cmethod);
  5672. zp->zs_gflag = ple16(zh->z_gflag);
  5673. zp->zs_csize = csize;
  5674. zp->zs_crc32 = crc;
  5675. zp->zs_next = zipbulk;
  5676. zipbulk = zp;
  5677. }
  5678. #define ziptrlevel() ( \
  5679. zipclevel == 01 ? 9 : /* maximum */ \
  5680. zipclevel == 02 ? 3 : /* fast */ \
  5681. zipclevel == 03 ? 1 : /* super fast */\
  5682. /*zipclevel==00*/ 6 /* normal */ \
  5683. )
  5684. /*
  5685. * Write (and compress) data for a regular file to a zip archive.
  5686. */
  5687. static int
  5688. zipwrite(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
  5689. uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
  5690. {
  5691. #if USE_ZLIB
  5692. struct z_stream_s z;
  5693. int i;
  5694. size_t osize = 0;
  5695. #endif /* USE_ZLIB */
  5696. char *ibuf, *obuf = 0;
  5697. if (st->st_size > 196608 || (ibuf = malloc(st->st_size)) == 0) {
  5698. #if USE_ZLIB
  5699. if (zipclevel < 04)
  5700. return zipwdesc(fd, fn, st, bp, sz, dev, ino,
  5701. crc, csize);
  5702. #endif /* USE_ZLIB */
  5703. return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
  5704. }
  5705. *csize = 0;
  5706. if (read(fd, ibuf, st->st_size) != st->st_size) {
  5707. free(ibuf);
  5708. emsg(3, "Cannot read \"%s\"", fn);
  5709. close(fd);
  5710. return -1;
  5711. }
  5712. *crc = zipcrc(0, (unsigned char *)ibuf, st->st_size);
  5713. #if USE_BZLIB
  5714. if (zipclevel == 07) {
  5715. unsigned int sb;
  5716. if ((obuf = malloc(sb = st->st_size)) == 0)
  5717. goto store;
  5718. if (BZ2_bzBuffToBuffCompress(obuf, &sb, ibuf, st->st_size,
  5719. 9, 0, 0) != BZ_OK)
  5720. goto store;
  5721. *csize = sb;
  5722. bp->Zdr.z_cmethod[0] = C_BZIP2;
  5723. bp->Zdr.z_version[0] = 0x2e;
  5724. goto out;
  5725. }
  5726. #endif /* USE_BZLIB */
  5727. if (zipclevel > 03)
  5728. goto store;
  5729. #if USE_ZLIB
  5730. memset(&z, 0, sizeof z);
  5731. if (deflateInit2(&z, ziptrlevel(), Z_DEFLATED, -15,
  5732. 8, Z_DEFAULT_STRATEGY) < 0)
  5733. goto store;
  5734. z.next_in = (unsigned char *)ibuf;
  5735. z.avail_in = z.total_in = st->st_size;
  5736. do {
  5737. if (z.avail_out == 0) {
  5738. if ((obuf = realloc(obuf, osize += 4096)) == 0) {
  5739. deflateEnd(&z);
  5740. goto store;
  5741. }
  5742. z.next_out = (unsigned char *)&obuf[*csize];
  5743. z.avail_out = osize - *csize;
  5744. }
  5745. if ((i = deflate(&z, z.avail_in ? Z_NO_FLUSH : Z_FINISH)) < 0) {
  5746. deflateEnd(&z);
  5747. goto store;
  5748. }
  5749. *csize = osize - z.avail_out;
  5750. } while (z.avail_in || i != Z_STREAM_END);
  5751. deflateEnd(&z);
  5752. if (*csize < st->st_size) {
  5753. bp->Zdr.z_cmethod[0] = C_DEFLATED;
  5754. bp->Zdr.z_gflag[0] |= zipclevel << 1;
  5755. bp->Zdr.z_version[0] = 20;
  5756. } else
  5757. #endif /* USE_ZLIB */
  5758. store: *csize = st->st_size;
  5759. #if USE_BZLIB
  5760. out:
  5761. #endif /* USE_BZLIB */
  5762. le32p(*crc, bp->Zdr.z_crc32);
  5763. le32p(*csize, bp->Zdr.z_csize);
  5764. bwrite((char *)bp, SIZEOF_zip_header);
  5765. bwrite(fn, sz);
  5766. zipwxtra(fn, st, dev, ino);
  5767. switch (bp->Zdr.z_cmethod[0]) {
  5768. case C_DEFLATED:
  5769. case C_BZIP2:
  5770. bwrite(obuf, *csize);
  5771. break;
  5772. default:
  5773. bwrite(ibuf, *csize);
  5774. }
  5775. free(ibuf);
  5776. free(obuf);
  5777. close(fd);
  5778. return 0;
  5779. }
  5780. /*
  5781. * Write and compress data to a zip archive for a file that is to large
  5782. * too be kept in memory. If there is an error with the temporary file
  5783. * (e. g. no space left on device), the file is stored in uncompressed
  5784. * form.
  5785. */
  5786. static int
  5787. zipwtemp(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
  5788. uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
  5789. {
  5790. static int tf = -1;
  5791. static char tlate[] = "/var/tmp/cpioXXXXXX";
  5792. char ibuf[32768];
  5793. #if USE_ZLIB || USE_BZLIB
  5794. char obuf[32768];
  5795. #endif /* USE_ZLIB || USE_BZLIB */
  5796. struct zextra_64 zf;
  5797. struct zextra_64 *zfp = 0;
  5798. long long size = st->st_size;
  5799. const char *sname;
  5800. int cuse, sf;
  5801. ssize_t rd;
  5802. *csize = 0;
  5803. *crc = 0;
  5804. #if USE_ZLIB || USE_BZLIB
  5805. if (tf < 0) {
  5806. if ((tf = mkstemp(tlate)) >= 0)
  5807. unlink(tlate);
  5808. } else if (lseek(tf, 0, SEEK_SET) != 0) {
  5809. close(tf);
  5810. tf = -1;
  5811. }
  5812. #endif /* USE_ZLIB || USE_BZLIB */
  5813. #if USE_ZLIB
  5814. if (zipclevel < 04) {
  5815. struct z_stream_s z;
  5816. memset(&z, 0, sizeof z);
  5817. if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
  5818. -15, 8, Z_DEFAULT_STRATEGY)) < 0)
  5819. goto store;
  5820. do {
  5821. if (z.avail_in == 0 && size > 0) {
  5822. if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
  5823. emsg(3, "Cannot read \"%s\"", fn);
  5824. close(fd);
  5825. ftruncate(tf, 0);
  5826. return -1;
  5827. }
  5828. z.next_in = (unsigned char *)ibuf;
  5829. z.avail_in = z.total_in = rd;
  5830. size -= rd;
  5831. *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
  5832. }
  5833. if (z.next_out == NULL || (char *)z.next_out > obuf) {
  5834. if (z.next_out && tf >= 0) {
  5835. if (write(tf, obuf,
  5836. (char *)z.next_out-obuf) !=
  5837. (char *)z.next_out-obuf) {
  5838. close(tf);
  5839. tf = -1;
  5840. }
  5841. *csize += (char *)z.next_out - obuf;
  5842. }
  5843. z.next_out = (unsigned char *)obuf;
  5844. z.avail_out = sizeof obuf;
  5845. }
  5846. if (cuse >= 0 && cuse != Z_STREAM_END)
  5847. cuse = deflate(&z,
  5848. z.avail_in?Z_NO_FLUSH:Z_FINISH);
  5849. else
  5850. z.avail_in = 0;
  5851. } while (size>0 || (char *)z.next_out>obuf ||
  5852. cuse>=0 && cuse!=Z_STREAM_END);
  5853. deflateEnd(&z);
  5854. goto out;
  5855. }
  5856. #endif /* USE_ZLIB */
  5857. #if USE_BZLIB
  5858. if (zipclevel == 07) {
  5859. bz_stream bs;
  5860. int ok, on;
  5861. memset(&bs, sizeof bs, 0);
  5862. if ((ok = BZ2_bzCompressInit(&bs, 9, 0, 0)) != BZ_OK)
  5863. goto store;
  5864. cuse = 1;
  5865. do {
  5866. if (bs.avail_in == 0 && size > 0) {
  5867. if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
  5868. emsg(3, "Cannot read \"%s\"", fn);
  5869. close(fd);
  5870. ftruncate(tf, 0);
  5871. return -1;
  5872. }
  5873. bs.next_in = ibuf;
  5874. bs.avail_in = rd;
  5875. size -= rd;
  5876. *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
  5877. }
  5878. if (bs.next_out == NULL || bs.next_out > obuf) {
  5879. if (bs.next_out && tf >= 0) {
  5880. on = bs.next_out - obuf;
  5881. if (write(tf, obuf, on) != on) {
  5882. close(tf);
  5883. tf = -1;
  5884. }
  5885. *csize += on;
  5886. }
  5887. bs.next_out = obuf;
  5888. bs.avail_out = sizeof obuf;
  5889. }
  5890. if (ok != BZ_STREAM_END) {
  5891. switch (ok = BZ2_bzCompress(&bs,
  5892. bs.avail_in?BZ_RUN:BZ_FINISH)) {
  5893. case BZ_RUN_OK:
  5894. case BZ_FINISH_OK:
  5895. case BZ_STREAM_END:
  5896. break;
  5897. default:
  5898. msg(3, 1, "Compression error %d "
  5899. "on \"%s\"\n", ok, fn);
  5900. close(fd);
  5901. return -1;
  5902. }
  5903. }
  5904. } while (size > 0 || bs.next_out > obuf || ok != BZ_STREAM_END);
  5905. BZ2_bzCompressEnd(&bs);
  5906. goto out;
  5907. }
  5908. #endif /* USE_BZLIB */
  5909. store: cuse = -1;
  5910. while (size > 0) {
  5911. if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
  5912. emsg(3, "Cannot read \"%s\"", fn);
  5913. close(fd);
  5914. return -1;
  5915. }
  5916. size -= rd;
  5917. *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
  5918. }
  5919. out: if (tf >= 0 && cuse >= 0 && *csize < st->st_size) {
  5920. if (zipclevel == 07) {
  5921. bp->Zdr.z_cmethod[0] = C_BZIP2;
  5922. bp->Zdr.z_version[0] = 0x2e;
  5923. } else {
  5924. bp->Zdr.z_cmethod[0] = C_DEFLATED;
  5925. bp->Zdr.z_gflag[0] |= zipclevel << 1;
  5926. bp->Zdr.z_version[0] = 20;
  5927. }
  5928. sf = tf;
  5929. sname = tlate;
  5930. } else {
  5931. *csize = st->st_size;
  5932. sf = fd;
  5933. sname = fn;
  5934. }
  5935. if ((lseek(sf, 0, SEEK_SET)) != 0) {
  5936. emsg(3, "Cannot rewind \"%s\"", sname);
  5937. errcnt++;
  5938. close(fd);
  5939. ftruncate(tf, 0);
  5940. return -1;
  5941. }
  5942. le32p(*crc, bp->Zdr.z_crc32);
  5943. if (st->st_size >= 0xffffffff || *csize >= 0xffffffff) {
  5944. int n;
  5945. zfp = &zf;
  5946. memset(&zf, 0, SIZEOF_zextra_64);
  5947. le16p(mag_zip64f, zf.ze_64_tag);
  5948. le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
  5949. le64p(st->st_size, zf.ze_64_nsize);
  5950. le64p(*csize, zf.ze_64_csize);
  5951. le32p(0xffffffff, bp->Zdr.z_csize);
  5952. le32p(0xffffffff, bp->Zdr.z_nsize);
  5953. n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
  5954. le16p(n, bp->Zdr.z_extralen);
  5955. } else
  5956. le32p(*csize, bp->Zdr.z_csize);
  5957. bwrite((char *)bp, SIZEOF_zip_header);
  5958. bwrite(fn, sz);
  5959. if (zfp)
  5960. bwrite((char *)zfp, SIZEOF_zextra_64);
  5961. zipwxtra(fn, st, dev, ino);
  5962. size = *csize;
  5963. while (size) {
  5964. if ((rd=read(sf, ibuf, size>sizeof ibuf?sizeof ibuf:size)) <= 0)
  5965. msg(3, 1, "Cannot read \"%s\"\n", sname);
  5966. bwrite(ibuf, rd);
  5967. size -= rd;
  5968. }
  5969. ftruncate(tf, 0);
  5970. close(fd);
  5971. return 0;
  5972. }
  5973. #if USE_ZLIB
  5974. /*
  5975. * Write a zip archive entry using the data descriptor structure.
  5976. */
  5977. static int
  5978. zipwdesc(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
  5979. uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
  5980. {
  5981. struct zextra_64 zf;
  5982. struct zextra_64 *zfp = 0;
  5983. char ibuf[32768], obuf[32768];
  5984. long long size = st->st_size;
  5985. ssize_t rd;
  5986. struct z_stream_s z;
  5987. int cuse;
  5988. *csize = 0;
  5989. *crc = 0;
  5990. memset(&z, 0, sizeof z);
  5991. if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
  5992. -15, 8, Z_DEFAULT_STRATEGY)) < 0)
  5993. return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
  5994. bp->Zdr.z_cmethod[0] = C_DEFLATED;
  5995. bp->Zdr.z_gflag[0] |= zipclevel << 1 | FG_DESC;
  5996. bp->Zdr.z_version[0] = 20;
  5997. /*
  5998. * RFC 1951 states that deflate compression needs 5 bytes additional
  5999. * space per 32k block in the worst case. Thus a compressed size
  6000. * greater than 4G-1 can be reached if at least 131052 blocks are
  6001. * used.
  6002. */
  6003. if (st->st_size >= 131052LL*32768) {
  6004. int n;
  6005. zfp = &zf;
  6006. memset(&zf, 0, SIZEOF_zextra_64);
  6007. le16p(mag_zip64f, zf.ze_64_tag);
  6008. le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
  6009. le32p(0xffffffff, bp->Zdr.z_csize);
  6010. le32p(0xffffffff, bp->Zdr.z_nsize);
  6011. n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
  6012. le16p(n, bp->Zdr.z_extralen);
  6013. }
  6014. bwrite((char *)bp, SIZEOF_zip_header);
  6015. bwrite(fn, sz);
  6016. if (zfp)
  6017. bwrite((char *)zfp, SIZEOF_zextra_64);
  6018. zipwxtra(fn, st, dev, ino);
  6019. do {
  6020. if (z.avail_in == 0 && size > 0) {
  6021. if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
  6022. emsg(3, "Cannot read \"%s\"", fn);
  6023. st->st_size -= size;
  6024. size = 0; /* can't simply stop here */
  6025. rd = 0; /* no data */
  6026. }
  6027. z.next_in = (unsigned char *)ibuf;
  6028. z.avail_in = z.total_in = rd;
  6029. size -= rd;
  6030. *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
  6031. }
  6032. if (z.next_out == NULL || (char *)z.next_out > obuf) {
  6033. if (z.next_out) {
  6034. bwrite(obuf, (char *)z.next_out - obuf);
  6035. *csize += (char *)z.next_out - obuf;
  6036. }
  6037. z.next_out = (unsigned char *)obuf;
  6038. z.avail_out = sizeof obuf;
  6039. }
  6040. if (cuse >= 0 && cuse != Z_STREAM_END)
  6041. cuse = deflate(&z, z.avail_in?Z_NO_FLUSH:Z_FINISH);
  6042. else
  6043. z.avail_in = 0;
  6044. } while (size > 0 || (char *)z.next_out > obuf ||
  6045. cuse >= 0 && cuse != Z_STREAM_END);
  6046. deflateEnd(&z);
  6047. if (zfp) {
  6048. struct zipddesc64 zd64;
  6049. memcpy(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds);
  6050. le32p(*crc, zd64.zd_crc32);
  6051. le64p(st->st_size, zd64.zd_nsize);
  6052. le64p(*csize, zd64.zd_csize);
  6053. bwrite((char *)&zd64, SIZEOF_zipddesc64);
  6054. } else {
  6055. struct zipddesc zd;
  6056. memcpy(zd.zd_signature, mag_zipdds, sizeof mag_zipdds);
  6057. le32p(*crc, zd.zd_crc32);
  6058. le32p(st->st_size, zd.zd_nsize);
  6059. le32p(*csize, zd.zd_csize);
  6060. bwrite((char *)&zd, SIZEOF_zipddesc);
  6061. }
  6062. close(fd);
  6063. return 0;
  6064. }
  6065. #endif /* USE_ZLIB */
  6066. /*
  6067. * Write the extra fields for a file to a zip archive (currently
  6068. * our own field type). Note that the z_extralen field in the file
  6069. * header must correspond to the size of the data written here.
  6070. */
  6071. static int
  6072. zipwxtra(const char *fn, struct stat *st, uint32_t dev, uint32_t ino)
  6073. {
  6074. struct zextra_cp z;
  6075. memset(&z, 0, SIZEOF_zextra_cp);
  6076. le16p(mag_zipcpio, z.ze_cp_tag);
  6077. le16p(SIZEOF_zextra_cp - 4, z.ze_cp_tsize);
  6078. le32p(dev, z.ze_cp_mode);
  6079. le32p(ino, z.ze_cp_ino);
  6080. le32p(st->st_mode&(S_IFMT|07777), z.ze_cp_mode);
  6081. le32p(st->st_uid, z.ze_cp_uid);
  6082. le32p(st->st_gid, z.ze_cp_gid);
  6083. le32p(st->st_nlink, z.ze_cp_nlink);
  6084. le32p(st->st_rdev, z.ze_cp_rdev);
  6085. le32p(st->st_mtime, z.ze_cp_mtime);
  6086. le32p(st->st_atime, z.ze_cp_atime);
  6087. bwrite((char *)&z, SIZEOF_zextra_cp);
  6088. return SIZEOF_zextra_cp;
  6089. }
  6090. static void
  6091. zipinfo(struct file *f)
  6092. {
  6093. const char *cp;
  6094. char b[5];
  6095. int i;
  6096. printf(" %7llu", f->f_csize);
  6097. if (f->f_dsize) {
  6098. i = f->f_csize*100 / f->f_dsize;
  6099. i += f->f_csize*200 / f->f_dsize & 1;
  6100. i = 100 - i;
  6101. } else
  6102. i = 0;
  6103. printf(" %3d%%", i);
  6104. switch (f->f_cmethod) {
  6105. case C_STORED:
  6106. cp = "stor";
  6107. break;
  6108. case C_SHRUNK:
  6109. cp = "shrk";
  6110. break;
  6111. case C_REDUCED1:
  6112. cp = "re:1";
  6113. break;
  6114. case C_REDUCED2:
  6115. cp = "re:2";
  6116. break;
  6117. case C_REDUCED3:
  6118. cp = "re:3";
  6119. break;
  6120. case C_REDUCED4:
  6121. cp = "re:4";
  6122. break;
  6123. case C_IMPLODED:
  6124. b[0] = 'i';
  6125. b[1] = f->f_gflag & FG_BIT1 ? '8' : '4';
  6126. b[2] = ':';
  6127. b[3] = f->f_gflag & FG_BIT2 ? '3' : '2';
  6128. b[4] = '\0';
  6129. cp = b;
  6130. break;
  6131. case C_TOKENIZED:
  6132. cp = "tokn";
  6133. break;
  6134. case C_DEFLATED:
  6135. b[0] = 'd', b[1] = 'e', b[2] = 'f', b[4] = '\0';
  6136. if (f->f_gflag & FG_BIT2)
  6137. b[3] = f->f_gflag & FG_BIT1 ? 'S' : 'F';
  6138. else
  6139. b[3] = f->f_gflag & FG_BIT1 ? 'X' : 'N';
  6140. cp = b;
  6141. break;
  6142. case C_ENHDEFLD:
  6143. cp = "edef";
  6144. break;
  6145. case C_DCLIMPLODED:
  6146. cp = "dcli";
  6147. break;
  6148. case C_BZIP2:
  6149. cp = "bz2 ";
  6150. break;
  6151. default:
  6152. snprintf(b, sizeof b, "%4.4X", f->f_cmethod);
  6153. cp = b;
  6154. }
  6155. printf(" %s", cp);
  6156. }
  6157. #if USE_BZLIB
  6158. int
  6159. zipunbz2(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
  6160. {
  6161. bz_stream bs;
  6162. long long isize = f->f_csize;
  6163. char ibuf[4096], obuf[8192];
  6164. int in, on, val = 0, ok;
  6165. memset(&bs, 0, sizeof bs);
  6166. if ((ok = BZ2_bzDecompressInit(&bs, 0, 0)) != BZ_OK) {
  6167. msg(3, 0, "bzip2 initialization error %d on \"%s\"\n",
  6168. ok, f->f_name);
  6169. errcnt++;
  6170. return skipfile(f);
  6171. }
  6172. while (isize > 0 || ok == BZ_OK) {
  6173. if (bs.avail_in == 0 && isize > 0) {
  6174. in = sizeof ibuf < isize ? sizeof ibuf : isize;
  6175. isize -= in;
  6176. if (bread(ibuf, in) != in)
  6177. unexeoa();
  6178. if (doswap)
  6179. swap(ibuf, in, bflag || sflag, bflag || Sflag);
  6180. bs.next_in = ibuf;
  6181. bs.avail_in = in;
  6182. }
  6183. if (ok == BZ_OK) {
  6184. bs.next_out = obuf;
  6185. bs.avail_out = sizeof obuf;
  6186. switch (ok = BZ2_bzDecompress(&bs)) {
  6187. case BZ_OK:
  6188. case BZ_STREAM_END:
  6189. on = sizeof obuf - bs.avail_out;
  6190. if (tfd >= 0 && write(tfd, obuf, on) != on) {
  6191. emsg(3, "Cannot write \"%s\"", tgt);
  6192. tfd = -1;
  6193. val = 1;
  6194. }
  6195. *crc = zipcrc(*crc, (unsigned char *)obuf, on);
  6196. break;
  6197. default:
  6198. msg(3, 0, "compression error %d on \"%s\"\n",
  6199. ok, f->f_name);
  6200. errcnt++;
  6201. val = 1;
  6202. }
  6203. }
  6204. }
  6205. BZ2_bzDecompressEnd(&bs);
  6206. return val;
  6207. }
  6208. #endif /* USE_BZLIB */
  6209. struct blasthow {
  6210. struct file *bh_f;
  6211. const char *bh_tgt;
  6212. long long bh_isize;
  6213. uint32_t *bh_crc;
  6214. int bh_tfd;
  6215. int bh_doswap;
  6216. int bh_val;
  6217. };
  6218. static unsigned
  6219. blastin(void *how, unsigned char **buf)
  6220. {
  6221. const int chunk = 16384;
  6222. static unsigned char *hold;
  6223. struct blasthow *bp = how;
  6224. unsigned sz;
  6225. if (bp->bh_isize <= 0)
  6226. return 0;
  6227. if (hold == NULL)
  6228. hold = smalloc(chunk);
  6229. sz = bp->bh_isize > chunk ? chunk : bp->bh_isize;
  6230. bp->bh_isize -= sz;
  6231. if (bread((char *)hold, sz) != sz)
  6232. unexeoa();
  6233. if (bp->bh_doswap)
  6234. swap((char *)hold, sz, bflag || sflag, bflag || Sflag);
  6235. *buf = hold;
  6236. return sz;
  6237. }
  6238. static int
  6239. blastout(void *how, unsigned char *buf, unsigned len)
  6240. {
  6241. struct blasthow *bp = how;
  6242. if (bp->bh_tfd >= 0 && write(bp->bh_tfd, buf, len) != len) {
  6243. emsg(3, "Cannot write \"%s\"", bp->bh_tgt);
  6244. bp->bh_tfd = -1;
  6245. bp->bh_val = 1;
  6246. }
  6247. *bp->bh_crc = zipcrc(*bp->bh_crc, buf, len);
  6248. return 0;
  6249. }
  6250. int
  6251. zipblast(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
  6252. {
  6253. struct blasthow bh;
  6254. int n;
  6255. bh.bh_f = f;
  6256. bh.bh_tgt = tgt;
  6257. bh.bh_isize = f->f_csize;
  6258. bh.bh_crc = crc;
  6259. bh.bh_tfd = tfd;
  6260. bh.bh_doswap = doswap;
  6261. bh.bh_val = 0;
  6262. switch (n = blast(blastin, &bh, blastout, &bh)) {
  6263. case 0:
  6264. break;
  6265. default:
  6266. msg(3, 0, "compression error %d on \"%s\"\n", n, f->f_name);
  6267. errcnt++;
  6268. bh.bh_val = 1;
  6269. }
  6270. while (bh.bh_isize) {
  6271. char buf[4096];
  6272. unsigned n;
  6273. n = bh.bh_isize > sizeof buf ? sizeof buf : bh.bh_isize;
  6274. if (bread(buf, n) != n)
  6275. unexeoa();
  6276. bh.bh_isize -= n;
  6277. }
  6278. return bh.bh_val;
  6279. }
  6280. /*
  6281. * The SGI -K format was introduced with SGI's IRIX 5.X. It is essentially
  6282. * a slightly extended binary format. The known additions are:
  6283. *
  6284. * - If the major or minor st_rdev device number exceeds the limit of
  6285. * 255 imposed by the 16-bit c_rdev field, this field is set to 0xFFFF
  6286. * and st_rdev is stored in the c_filesize 32-bit field. The first 14
  6287. * bits of this field compose the major device number, the minor is
  6288. * stored in the remaining 18 bits. This enables implementations to
  6289. * read the modified format without special support if they ignore
  6290. * the size of device files; otherwise they will try to read a lot
  6291. * of archive data and fail.
  6292. *
  6293. * - If the file is larger than 2 GB - 1 byte, two nearly identical
  6294. * archive headers are stored for it. The only difference is in
  6295. * the c_filesize field:
  6296. *
  6297. * [first header: file size X times 2 GB]
  6298. * [first data part: X times 2 GB]
  6299. * [second header: file size modulo 2 GB]
  6300. * [second data part: rest of data]
  6301. *
  6302. * The first header can be recognized by a negative c_filesize. A
  6303. * value of 0xFFFFFFFF means that 2 GB follow, 0xFFFFFFFE -> 4 GB
  6304. * 0xFFFFFFFD -> 6 GB, 0xFFFFFFFC -> 8 GB, and so forth. The second
  6305. * is a standard binary cpio header, although the following data is
  6306. * meant to be appended to the preceding file.
  6307. *
  6308. * It is important to note that padding follows the number in
  6309. * c_filesize, not the amount of data written; thus all data parts
  6310. * with odd c_filesize fields (0xFFFFFFFF = 2+ GB, 0xFFFFFFFD = 6+ GB
  6311. * etc.) cause the following archive entries to be aligned on an odd
  6312. * offset. This seems to be an implementation artifact but has to be
  6313. * followed for compatibility.
  6314. *
  6315. * This extension seems a bit weird since no known cpio implementation
  6316. * is able to read these archive entries without special support. Thus
  6317. * a more straightforward extension (such as storing the size just past
  6318. * the file name) would well have had the same effect. Nevertheless,
  6319. * the cpio -K format is useful, so it is implemented here.
  6320. *
  6321. * --Note that IRIX 6.X tar also has a -K option. This option extends
  6322. * the tar format in essentially the same way as the second extension
  6323. * to cpio described above. Unfortunately, the result is definitively
  6324. * broken. Contrasting to the binary cpio format, the standard POSIX
  6325. * tar format is well able to hold files of size 0xFFFFFFFF and below
  6326. * in a regular manner. Thus, a tar -K archive entry is _exactly_ the
  6327. * same as two regular POSIX tar entries for the same file. And this
  6328. * situation even occurs on a regular basis with tar -r! For this
  6329. * reason, we do not implement the IRIX tar -K format and will probably
  6330. * never do so unless it is changed (the tar format really has a lot
  6331. * of options to indicate extensions that might be used for this).
  6332. *
  6333. * Many thanks to Sven Mascheck who made archiving tests on the IRIX
  6334. * machine.
  6335. */
  6336. /*
  6337. * This function reads the second header of a SGI -K format archive.
  6338. */
  6339. static void
  6340. readK2hdr(struct file *f)
  6341. {
  6342. struct file n;
  6343. union bincpio bc;
  6344. n.f_name = n.f_lnam = NULL;
  6345. n.f_nsiz = n.f_lsiz = 0;
  6346. readhdr(&n, &bc);
  6347. f->f_Krest = n.f_st.st_size;
  6348. f->f_dsize = f->f_st.st_size = n.f_st.st_size + f->f_Kbase;
  6349. f->f_Kbase = 0;
  6350. free(n.f_name);
  6351. free(n.f_lnam);
  6352. }
  6353. /*
  6354. * Read the data of a GNU filename extra header.
  6355. */
  6356. static int
  6357. readgnuname(char **np, size_t *sp, long length)
  6358. {
  6359. if (length > SANELIMIT)
  6360. return -1;
  6361. if (*sp == 0 || *sp <= length)
  6362. *np = srealloc(*np, *sp = length+1);
  6363. bread(*np, length);
  6364. (*np)[length] = '\0';
  6365. skippad(length, 512);
  6366. return 0;
  6367. }
  6368. /*
  6369. * Write a GNU filename extra header and its data.
  6370. */
  6371. static void
  6372. writegnuname(const char *fn, long length, int flag)
  6373. {
  6374. union bincpio bc;
  6375. memset(bc.data, 0, 512);
  6376. strcpy(bc.Tdr.t_name, "././@LongLink");
  6377. sprintf(bc.Tdr.t_mode, "%7.7o", 0);
  6378. sprintf(bc.Tdr.t_uid, "%7.7o", 0);
  6379. sprintf(bc.Tdr.t_gid, "%7.7o", 0);
  6380. sprintf(bc.Tdr.t_size, "%11.11lo", length);
  6381. sprintf(bc.Tdr.t_mtime, "%11.11lo", 0L);
  6382. bc.Tdr.t_linkflag = flag;
  6383. memcpy(bc.Tdr.t_magic, mag_gnutar, 8);
  6384. strcpy(bc.Tdr.t_uname, "root");
  6385. strcpy(bc.Tdr.t_gname, "root");
  6386. tchksum(&bc);
  6387. bwrite(bc.data, 512);
  6388. bwrite(fn, length);
  6389. length %= 512;
  6390. memset(bc.data, 0, 512 - length);
  6391. bwrite(bc.data, 512 - length);
  6392. }
  6393. /*
  6394. * POSIX.1-2001 pax format support.
  6395. */
  6396. static void
  6397. tgetpax(struct tar_header *tp, struct file *f)
  6398. {
  6399. char *keyword, *value;
  6400. char *block, *bp;
  6401. long long n;
  6402. enum paxrec pr;
  6403. n = rdoct(tp->t_size, 12);
  6404. bp = block = smalloc(n+1);
  6405. bread(block, n);
  6406. skippad(n, 512);
  6407. block[n] = '\0';
  6408. while (bp < &block[n]) {
  6409. int c;
  6410. pr = tgetrec(&bp, &keyword, &value);
  6411. switch (pr) {
  6412. case PR_ATIME:
  6413. f->f_st.st_atime = strtoll(value, NULL, 10);
  6414. break;
  6415. case PR_GID:
  6416. f->f_st.st_gid = strtoll(value, NULL, 10);
  6417. break;
  6418. case PR_LINKPATH:
  6419. c = strlen(value);
  6420. if (f->f_lnam == NULL || f->f_lsiz < c+1) {
  6421. f->f_lsiz = c+1;
  6422. f->f_lnam = srealloc(f->f_lnam, c+1);
  6423. }
  6424. strcpy(f->f_lnam, value);
  6425. break;
  6426. case PR_MTIME:
  6427. f->f_st.st_mtime = strtoll(value, NULL, 10);
  6428. break;
  6429. case PR_PATH:
  6430. c = strlen(value);
  6431. if (f->f_name == NULL || f->f_nsiz < c+1) {
  6432. f->f_nsiz = c+1;
  6433. f->f_name = srealloc(f->f_name, c+1);
  6434. }
  6435. strcpy(f->f_name, value);
  6436. break;
  6437. case PR_SIZE:
  6438. f->f_st.st_size = strtoll(value, NULL, 10);
  6439. break;
  6440. case PR_UID:
  6441. f->f_st.st_uid = strtoll(value, NULL, 10);
  6442. break;
  6443. case PR_SUN_DEVMAJOR:
  6444. f->f_rmajor = strtoll(value, NULL, 10);
  6445. break;
  6446. case PR_SUN_DEVMINOR:
  6447. f->f_rminor = strtoll(value, NULL, 10);
  6448. break;
  6449. }
  6450. paxrec |= pr;
  6451. }
  6452. if (tp->t_linkflag == 'g') {
  6453. globrec = paxrec & ~(PR_LINKPATH|PR_PATH|PR_SIZE);
  6454. globst = f->f_st;
  6455. }
  6456. free(block);
  6457. }
  6458. static enum paxrec
  6459. tgetrec(char **bp, char **keyword, char **value)
  6460. {
  6461. char *x;
  6462. long n = 0;
  6463. enum paxrec pr;
  6464. *keyword = "";
  6465. *value = "";
  6466. while (**bp && (n = strtol(*bp, &x, 10)) <= 0 && (*x!=' ' || *x!='\t'))
  6467. do
  6468. (*bp)++;
  6469. while (**bp && **bp != '\n');
  6470. if (*x == '\0' || **bp == '\0') {
  6471. (*bp)++;
  6472. return PR_NONE;
  6473. }
  6474. while (x < &(*bp)[n] && (*x == ' ' || *x == '\t'))
  6475. x++;
  6476. if (x == &(*bp)[n] || *x == '=')
  6477. goto out;
  6478. *keyword = x;
  6479. while (x < &(*bp)[n] && *x != '=')
  6480. x++;
  6481. if (x == &(*bp)[n])
  6482. goto out;
  6483. *x = '\0';
  6484. if (&x[1] < &(*bp)[n])
  6485. *value = &x[1];
  6486. (*bp)[n-1] = '\0';
  6487. out: *bp = &(*bp)[n];
  6488. if (strcmp(*keyword, "atime") == 0)
  6489. pr = PR_ATIME;
  6490. else if (strcmp(*keyword, "gid") == 0)
  6491. pr = PR_GID;
  6492. else if (strcmp(*keyword, "linkpath") == 0)
  6493. pr = PR_LINKPATH;
  6494. else if (strcmp(*keyword, "mtime") == 0)
  6495. pr = PR_MTIME;
  6496. else if (strcmp(*keyword, "path") == 0)
  6497. pr = PR_PATH;
  6498. else if (strcmp(*keyword, "size") == 0)
  6499. pr = PR_SIZE;
  6500. else if (strcmp(*keyword, "uid") == 0)
  6501. pr = PR_UID;
  6502. else if (strcmp(*keyword, "SUN.devmajor") == 0)
  6503. pr = PR_SUN_DEVMAJOR;
  6504. else if (strcmp(*keyword, "SUN.devminor") == 0)
  6505. pr = PR_SUN_DEVMINOR;
  6506. else
  6507. pr = PR_NONE;
  6508. return pr;
  6509. }
  6510. static void
  6511. wrpax(const char *longname, const char *linkname, struct stat *sp)
  6512. {
  6513. union bincpio bc;
  6514. char *pdata = NULL;
  6515. long psize = 0, pcur = 0;
  6516. long long blocks;
  6517. memset(bc.data, 0, 512);
  6518. if (paxrec & PR_ATIME)
  6519. addrec(&pdata, &psize, &pcur, "atime", NULL, sp->st_atime);
  6520. if (paxrec & PR_GID)
  6521. addrec(&pdata, &psize, &pcur, "gid", NULL, sp->st_gid);
  6522. if (paxrec & PR_LINKPATH)
  6523. addrec(&pdata, &psize, &pcur, "linkpath", linkname, 0);
  6524. if (paxrec & PR_MTIME)
  6525. addrec(&pdata, &psize, &pcur, "mtime", NULL, sp->st_mtime);
  6526. if (paxrec & PR_PATH)
  6527. addrec(&pdata, &psize, &pcur, "path", longname, 0);
  6528. if (paxrec & PR_SIZE)
  6529. addrec(&pdata, &psize, &pcur, "size", NULL, sp->st_size);
  6530. if (paxrec & PR_UID)
  6531. addrec(&pdata, &psize, &pcur, "uid", NULL, sp->st_uid);
  6532. if (paxrec & PR_SUN_DEVMAJOR)
  6533. addrec(&pdata, &psize, &pcur, "SUN.devmajor", NULL,
  6534. major(sp->st_rdev));
  6535. if (paxrec & PR_SUN_DEVMINOR)
  6536. addrec(&pdata, &psize, &pcur, "SUN.devminor", NULL,
  6537. minor(sp->st_rdev));
  6538. paxnam(&bc.Tdr, longname);
  6539. sprintf(bc.Tdr.t_mode, "%7.7o", fmttype==FMT_SUN ? 0444|S_IFREG : 0444);
  6540. sprintf(bc.Tdr.t_uid, "%7.7o", 0);
  6541. sprintf(bc.Tdr.t_gid, "%7.7o", 0);
  6542. sprintf(bc.Tdr.t_size, "%11.11lo", pcur);
  6543. sprintf(bc.Tdr.t_mtime, "%11.11o", 0);
  6544. strcpy(bc.Tdr.t_magic, "ustar");
  6545. bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
  6546. strcpy(bc.Tdr.t_uname, "root");
  6547. strcpy(bc.Tdr.t_gname, "root");
  6548. bc.Tdr.t_linkflag = fmttype==FMT_SUN ? 'X' : 'x';
  6549. tchksum(&bc);
  6550. bwrite(bc.data, 512);
  6551. memset(&pdata[pcur], 0, psize - pcur);
  6552. blocks = (pcur + (512-1)) / 512;
  6553. bwrite(pdata, blocks * 512);
  6554. free(pdata);
  6555. }
  6556. static void
  6557. addrec(char **pdata, long *psize, long *pcur,
  6558. const char *keyword, const char *sval, long long lval)
  6559. {
  6560. char dval[25], xval[25];
  6561. long od, d, r;
  6562. if (sval == 0) {
  6563. sprintf(xval, "%lld", lval);
  6564. sval = xval;
  6565. }
  6566. r = strlen(keyword) + strlen(sval) + 3;
  6567. d = 0;
  6568. do {
  6569. od = d;
  6570. d = sprintf(dval, "%ld", od + r);
  6571. } while (d != od);
  6572. *psize += d + r + 1 + 512;
  6573. *pdata = srealloc(*pdata, *psize);
  6574. sprintf(&(*pdata)[*pcur], "%s %s=%s\n", dval, keyword, sval);
  6575. *pcur += d + r;
  6576. }
  6577. static void
  6578. paxnam(struct tar_header *hp, const char *name)
  6579. {
  6580. char buf[257], *bp;
  6581. const char *cp, *np;
  6582. int bl = 0;
  6583. static int pid;
  6584. if (pid == 0)
  6585. pid = getpid();
  6586. for (np = name; *np; np++);
  6587. while (np > name && *np != '/') {
  6588. np--;
  6589. bl++;
  6590. }
  6591. if ((np > name || *name == '/') && np-name <= 120)
  6592. for (bp = buf, cp = name; cp < np; bp++, cp++)
  6593. *bp = *cp;
  6594. else {
  6595. *buf = '.';
  6596. bp = &buf[1];
  6597. }
  6598. snprintf(bp, sizeof buf - (bp - buf), "/PaxHeaders.%d/%s",
  6599. pid, bl < 100 ? np>name?&np[1]:name : sequence());
  6600. tmkname(hp, buf);
  6601. }
  6602. static char *
  6603. sequence(void)
  6604. {
  6605. static char buf[25];
  6606. static long long d;
  6607. sprintf(buf, "%10.10lld", ++d);
  6608. return buf;
  6609. }
  6610. static int
  6611. pax_oneopt(const char *s, int warn)
  6612. {
  6613. if (strcmp(s, "linkdata") == 0)
  6614. pax_oflag |= PO_LINKDATA;
  6615. else if (strcmp(s, "times") == 0)
  6616. pax_oflag |= PO_TIMES;
  6617. else {
  6618. if (warn)
  6619. msg(2, 0, "Unknown flag \"-o %s\"\n", s);
  6620. return -1;
  6621. }
  6622. return 0;
  6623. }
  6624. int
  6625. pax_options(char *s, int warn)
  6626. {
  6627. char *o = s, c;
  6628. int val = 0, word = 0;
  6629. do {
  6630. if (word == 0) {
  6631. if (isspace(*s&0377))
  6632. o = &s[1];
  6633. else
  6634. word = 1;
  6635. }
  6636. if (*s == ',' || *s == '\0') {
  6637. c = *s;
  6638. *s = '\0';
  6639. val |= pax_oneopt(o, warn);
  6640. *s = c;
  6641. o = &s[1];
  6642. word = 0;
  6643. }
  6644. } while (*s++);
  6645. return val;
  6646. }
  6647. /*
  6648. * Given a symbolic link "base" and the result of readlink "name", form
  6649. * a valid path name for the link target.
  6650. */
  6651. static char *
  6652. joinpath(const char *base, char *name)
  6653. {
  6654. const char *bp = NULL, *cp;
  6655. char *new, *np;
  6656. if (*name == '/')
  6657. return name;
  6658. for (cp = base; *cp; cp++)
  6659. if (*cp == '/')
  6660. bp = cp;
  6661. if (bp == NULL)
  6662. return name;
  6663. np = new = smalloc(bp - base + strlen(name) + 2);
  6664. for (cp = base; cp < bp; cp++)
  6665. *np++ = *cp;
  6666. *np++ = '/';
  6667. for (cp = name; *cp; cp++)
  6668. *np++ = *cp;
  6669. *np = '\0';
  6670. free(name);
  6671. return new;
  6672. }
  6673. static int
  6674. utf8(const char *cp)
  6675. {
  6676. int c, n;
  6677. while (*cp) if ((c = *cp++ & 0377) & 0200) {
  6678. if (c == (c & 037 | 0300))
  6679. n = 1;
  6680. else if (c == (c & 017 | 0340))
  6681. n = 2;
  6682. else if (c == (c & 07 | 0360))
  6683. n = 3;
  6684. else if (c == (c & 03 | 0370))
  6685. n = 4;
  6686. else if (c == (c & 01 | 0374))
  6687. n = 5;
  6688. else
  6689. return 0;
  6690. while (n--) {
  6691. c = *cp++ & 0377;
  6692. if (c != (c & 077 | 0200))
  6693. return 0;
  6694. }
  6695. }
  6696. return 1;
  6697. }
  6698. static time_t
  6699. fetchtime(const char *cp)
  6700. {
  6701. struct tm tm;
  6702. time_t t;
  6703. char *xp;
  6704. int n;
  6705. t = strtoll(cp, &xp, 10);
  6706. if (*xp == '\0')
  6707. return t;
  6708. memset(&tm, 0, sizeof tm);
  6709. n = sscanf(cp, "%4d%2d%2dT%2d%2d%2d",
  6710. &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
  6711. &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
  6712. tm.tm_year -= 1900;
  6713. tm.tm_mon--;
  6714. tm.tm_isdst = -1;
  6715. t = mktime(&tm);
  6716. if (n < 3 || t == (time_t)-1)
  6717. msg(4, 1, "line %lld: illegal time \"%s\"\n",
  6718. lineno, cp);
  6719. return t;
  6720. }
  6721. static char *
  6722. nextfield(char *cp, const char *fieldname)
  6723. {
  6724. while (*cp && *cp != ':')
  6725. cp++;
  6726. if (*cp == 0)
  6727. msg(4, 1, "line %lld: unterminated \"%s\" field\n",
  6728. lineno, fieldname);
  6729. *cp++ = 0;
  6730. return cp;
  6731. }
  6732. static char *
  6733. getproto(char *np, struct prototype *pp)
  6734. {
  6735. char *tp, *xp;
  6736. long long t, u;
  6737. memset(pp, 0, sizeof *pp);
  6738. if (*np == ':')
  6739. np++;
  6740. else {
  6741. tp = nextfield(np, "type");
  6742. if (np[1])
  6743. goto notype;
  6744. switch (np[0]) {
  6745. case 'b':
  6746. pp->pt_mode |= S_IFBLK;
  6747. break;
  6748. case 'c':
  6749. pp->pt_mode |= S_IFCHR;
  6750. break;
  6751. case 'd':
  6752. pp->pt_mode |= S_IFDIR;
  6753. break;
  6754. case 'f':
  6755. pp->pt_mode |= S_IFREG;
  6756. break;
  6757. case 'p':
  6758. pp->pt_mode |= S_IFIFO;
  6759. break;
  6760. case 's':
  6761. pp->pt_mode |= S_IFLNK;
  6762. break;
  6763. default:
  6764. notype:
  6765. msg(4, 1, "line %lld: unknown type \"%s\"\n",
  6766. lineno, np);
  6767. }
  6768. pp->pt_spec |= PT_TYPE;
  6769. np = tp;
  6770. }
  6771. if (*np == ':')
  6772. np++;
  6773. else {
  6774. struct passwd *pwd;
  6775. tp = nextfield(np, "owner");
  6776. t = strtoll(np, &xp, 10);
  6777. if (*xp == '\0')
  6778. pp->pt_uid = t;
  6779. else {
  6780. if ((pwd = getpwnam(np)) == NULL)
  6781. msg(4, 1, "line %lld: unknown user \"%s\"\n",
  6782. lineno, np);
  6783. pp->pt_uid = pwd->pw_uid;
  6784. }
  6785. pp->pt_spec |= PT_OWNER;
  6786. np = tp;
  6787. }
  6788. if (*np == ':')
  6789. np++;
  6790. else {
  6791. struct group *grp;
  6792. tp = nextfield(np, "group");
  6793. t = strtoll(np, &xp, 10);
  6794. if (*xp == '\0')
  6795. pp->pt_gid = t;
  6796. else {
  6797. if ((grp = getgrnam(np)) == NULL)
  6798. msg(4, 1, "line %lld: unknown group \"%s\"\n",
  6799. lineno, np);
  6800. pp->pt_gid = grp->gr_gid;
  6801. }
  6802. pp->pt_spec |= PT_GROUP;
  6803. np = tp;
  6804. }
  6805. if (*np == ':')
  6806. np++;
  6807. else {
  6808. tp = nextfield(np, "mode");
  6809. t = strtol(np, &xp, 8);
  6810. if (t & ~07777 || *xp)
  6811. msg(4, 1, "line %lld: illegal mode \"%s\"\n",
  6812. lineno, np);
  6813. pp->pt_mode |= t;
  6814. pp->pt_spec |= PT_MODE;
  6815. np = tp;
  6816. }
  6817. if (*np == ':')
  6818. np++;
  6819. else {
  6820. tp = nextfield(np, "access time");
  6821. pp->pt_atime = fetchtime(np);
  6822. pp->pt_spec |= PT_ATIME;
  6823. np = tp;
  6824. }
  6825. if (*np == ':')
  6826. np++;
  6827. else {
  6828. tp = nextfield(np, "modification time");
  6829. pp->pt_mtime = fetchtime(np);
  6830. pp->pt_spec |= PT_MTIME;
  6831. np = tp;
  6832. }
  6833. if (*np == ':') {
  6834. np++;
  6835. if (*np++ != ':')
  6836. majmin: msg(4, 1, "line %lld: need either both major and "
  6837. "minor or none\n",
  6838. lineno);
  6839. } else {
  6840. tp = nextfield(np, "major");
  6841. t = strtoll(np, &xp, 10);
  6842. if (*xp)
  6843. msg(4, 1, "line %lld: illegal major \"%s\"\n",
  6844. lineno, np);
  6845. np = tp;
  6846. if (*np == ':')
  6847. goto majmin;
  6848. tp = nextfield(np, "minor");
  6849. u = strtoll(np, &xp, 10);
  6850. if (*xp)
  6851. msg(4, 1, "line %lld: illegal minor \"%s\"\n",
  6852. lineno, np);
  6853. np = tp;
  6854. pp->pt_rdev = makedev(t, u);
  6855. pp->pt_spec |= PT_RDEV;
  6856. }
  6857. return np;
  6858. }