cpio.c 172 KB

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