cpio.c 173 KB

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