cpio.c 173 KB

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