12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193 |
- /*
- * cpio - copy file archives in and out
- *
- * Gunnar Ritter, Freiburg i. Br., Germany, April 2003.
- */
- /*
- * Copyright (c) 2003 Gunnar Ritter
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute
- * it freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- *
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- *
- * 3. This notice may not be removed or altered from any source distribution.
- */
- /*
- * Sccsid @(#)cpio.c 1.304 (gritter) 2/14/09
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifdef __linux__
- #if !defined (__UCLIBC__) && !defined (__dietlibc__)
- #include <linux/fs.h>
- #endif /* !__UCLIBC__, !__dietlibc__ */
- #include <linux/fd.h>
- #undef WNOHANG
- #undef WUNTRACED
- #undef P_ALL
- #undef P_PID
- #undef P_PGID
- #ifdef __dietlibc__
- #undef NR_OPEN
- #undef PATH_MAX
- #endif /* __dietlibc__ */
- #endif /* __linux__ */
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <signal.h>
- #include "sigset.h"
- #include <time.h>
- #include <utime.h>
- #include <pwd.h>
- #include <grp.h>
- #include <limits.h>
- #include <stdio.h>
- #include <libgen.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <stdarg.h>
- #include <locale.h>
- #include <ctype.h>
- #include "memalign.h"
- int sysv3;
- #if USE_ZLIB
- #include <zlib.h>
- #endif /* USE_ZLIB */
- #if USE_BZLIB
- #include <bzlib.h>
- #endif /* USE_BZLIB */
- #include <sys/ioctl.h>
- #if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
- defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
- defined (__OpenBSD__) || defined (__DragonFly__) || \
- defined (__APPLE__) || defined (__CYGWIN__)
- #include <sys/mtio.h>
- #else /* SVR4.2MP */
- #include <sys/scsi.h>
- #include <sys/st01.h>
- #endif /* SVR4.2MP */
- #include <iblok.h>
- #include <sfile.h>
- #include <atoll.h>
- #ifdef _AIX
- #include <sys/sysmacros.h>
- #endif /* _AIX */
- #ifndef major
- #include <sys/mkdev.h>
- #endif /* !major */
- #include "cpio.h"
- #include "blast.h"
- #ifdef __GLIBC__
- #ifdef _IO_putc_unlocked
- #undef putc
- #define putc(c, f) _IO_putc_unlocked(c, f)
- #undef putchar
- #define putchar(c) _IO_putc_unlocked(c, stdout)
- #endif /* _IO_putc_unlocked */
- #endif /* __GLIBC__ */
- /*
- * The cpio code assumes that all following S_IFMT bits are the same as
- * those of the mode fields in cpio headers. The only real Unix system
- * known to deviate from this de facto standard is UNICOS which uses
- * 0130000 for S_IFLNK. But this software does not run on UNICOS for
- * a variety of other reasons anyway, so this should not be of much
- * concern.
- */
- #if S_IFIFO != 0010000 || \
- S_IFCHR != 0020000 || \
- S_IFDIR != 0040000 || \
- S_IFBLK != 0060000 || \
- S_IFREG != 0100000 || \
- S_IFLNK != 0120000 || \
- S_IFSOCK!= 0140000 || \
- S_IFMT != 0170000
- #error non-standard S_IFMT bits
- #endif
- /*
- * File types that are not present on all systems but that we want to
- * recognize nevertheless.
- */
- #ifndef S_IFDOOR
- #define S_IFDOOR 0150000 /* Solaris door */
- #endif
- #ifndef S_IFNAM
- #define S_IFNAM 0050000 /* XENIX special named file */
- #endif
- #ifndef S_INSEM
- #define S_INSEM 0x1 /* XENIX semaphore subtype of IFNAM */
- #endif
- #ifndef S_INSHD
- #define S_INSHD 0x2 /* XENIX shared data subtype of IFNAM */
- #endif
- #ifndef S_IFNWK
- #define S_IFNWK 0110000 /* HP-UX network special file */
- #endif
- #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
- defined (__DragonFly__) || defined (__APPLE__)
- /*
- * For whatever reason, FreeBSD casts the return values of major() and
- * minor() to signed values so that normal limit comparisons will fail.
- */
- static unsigned long
- mymajor(long dev)
- {
- return major(dev) & 0xFFFFFFFFUL;
- }
- #undef major
- #define major(a) mymajor(a)
- static unsigned long
- myminor(long dev)
- {
- return minor(dev) & 0xFFFFFFFFUL;
- }
- #undef minor
- #define minor(a) myminor(a)
- #endif /* __FreeBSD__, __NetBSD__, __OpenBSD__, __DragonFly__, __APPLE__ */
- /*
- * Device and inode counts in cpio formats are too small to store the
- * information used to detect hard links on today's systems. Keep track
- * of all devices and inodes and store fake counts in the archive.
- */
- struct ilink {
- struct ilink *l_nxt; /* next link to same i-node */
- char *l_nam; /* link name */
- size_t l_siz; /* link name size */
- };
- struct islot {
- struct islot *i_lln; /* left link */
- struct islot *i_rln; /* right link */
- struct ilink *i_lnk; /* links list */
- struct stat *i_st; /* stat information */
- char *i_name;/* name of first link encountered */
- ino_t i_ino; /* real inode number */
- uint32_t i_fino; /* fake inode number */
- nlink_t i_nlk; /* number of remaining links */
- };
- struct dslot {
- struct dslot *d_nxt; /* next device */
- struct islot *d_isl; /* inode slots */
- uint32_t d_cnt; /* used inode number count */
- uint32_t d_fake; /* faked device id */
- dev_t d_dev; /* real device id */
- };
- union types2 {
- uint8_t byte[2];
- uint16_t sword;
- };
- union types4 {
- uint8_t byte[4];
- uint16_t sword[2];
- uint32_t lword;
- };
- /*
- * Store and retrieve integers in a defined endian order.
- */
- static uint16_t
- ple16(const char *cp)
- {
- return (uint16_t)(cp[0]&0377) +
- ((uint16_t)(cp[1]&0377) << 8);
- }
- static uint16_t
- pbe16(const char *cp)
- {
- return (uint16_t)(cp[1]&0377) +
- ((uint16_t)(cp[0]&0377) << 8);
- }
- static uint32_t
- ple32(const char *cp)
- {
- return (uint32_t)(cp[0]&0377) +
- ((uint32_t)(cp[1]&0377) << 8) +
- ((uint32_t)(cp[2]&0377) << 16) +
- ((uint32_t)(cp[3]&0377) << 24);
- }
- static uint32_t
- pbe32(const char *cp)
- {
- return (uint32_t)(cp[3]&0377) +
- ((uint32_t)(cp[2]&0377) << 8) +
- ((uint32_t)(cp[1]&0377) << 16) +
- ((uint32_t)(cp[0]&0377) << 24);
- }
- static uint32_t
- pme32(const char *cp)
- {
- return (uint32_t)(cp[2]&0377) +
- ((uint32_t)(cp[3]&0377) << 8) +
- ((uint32_t)(cp[0]&0377) << 16) +
- ((uint32_t)(cp[1]&0377) << 24);
- }
- static uint64_t
- ple64(const char *cp)
- {
- return (uint64_t)(cp[0]&0377) +
- ((uint64_t)(cp[1]&0377) << 8) +
- ((uint64_t)(cp[2]&0377) << 16) +
- ((uint64_t)(cp[3]&0377) << 24) +
- ((uint64_t)(cp[4]&0377) << 32) +
- ((uint64_t)(cp[5]&0377) << 40) +
- ((uint64_t)(cp[6]&0377) << 48) +
- ((uint64_t)(cp[7]&0377) << 56);
- }
- static uint64_t
- pbe64(const char *cp)
- {
- return (uint64_t)(cp[7]&0377) +
- ((uint64_t)(cp[6]&0377) << 8) +
- ((uint64_t)(cp[5]&0377) << 16) +
- ((uint64_t)(cp[4]&0377) << 24) +
- ((uint64_t)(cp[3]&0377) << 32) +
- ((uint64_t)(cp[2]&0377) << 40) +
- ((uint64_t)(cp[1]&0377) << 48) +
- ((uint64_t)(cp[0]&0377) << 56);
- }
- static void
- le16p(uint16_t n, char *cp)
- {
- cp[0] = (n&0x00ff);
- cp[1] = (n&0xff00) >> 8;
- }
- static void
- be16p(uint16_t n, char *cp)
- {
- cp[1] = (n&0x00ff);
- cp[0] = (n&0xff00) >> 8;
- }
- static void
- le32p(uint32_t n, char *cp)
- {
- cp[0] = (n&0x000000ff);
- cp[1] = (n&0x0000ff00) >> 8;
- cp[2] = (n&0x00ff0000) >> 16;
- cp[3] = (n&0xff000000) >> 24;
- }
- static void
- be32p(uint32_t n, char *cp)
- {
- cp[3] = (n&0x000000ff);
- cp[2] = (n&0x0000ff00) >> 8;
- cp[1] = (n&0x00ff0000) >> 16;
- cp[0] = (n&0xff000000) >> 24;
- }
- static void
- me32p(uint32_t n, char *cp)
- {
- cp[2] = (n&0x000000ff);
- cp[3] = (n&0x0000ff00) >> 8;
- cp[0] = (n&0x00ff0000) >> 16;
- cp[1] = (n&0xff000000) >> 24;
- }
- static void
- le64p(uint64_t n, char *cp)
- {
- cp[0] = (n&0x00000000000000ffLL);
- cp[1] = (n&0x000000000000ff00LL) >> 8;
- cp[2] = (n&0x0000000000ff0000LL) >> 16;
- cp[3] = (n&0x00000000ff000000LL) >> 24;
- cp[4] = (n&0x000000ff00000000LL) >> 32;
- cp[5] = (n&0x0000ff0000000000LL) >> 40;
- cp[6] = (n&0x00ff000000000000LL) >> 48;
- cp[7] = (n&0xff00000000000000LL) >> 56;
- }
- static void
- be64p(uint64_t n, char *cp)
- {
- cp[7] = (n&0x00000000000000ffLL);
- cp[6] = (n&0x000000000000ff00LL) >> 8;
- cp[5] = (n&0x0000000000ff0000LL) >> 16;
- cp[4] = (n&0x00000000ff000000LL) >> 24;
- cp[3] = (n&0x000000ff00000000LL) >> 32;
- cp[2] = (n&0x0000ff0000000000LL) >> 40;
- cp[1] = (n&0x00ff000000000000LL) >> 48;
- cp[0] = (n&0xff00000000000000LL) >> 56;
- }
- #define TNAMSIZ 100
- #define TPFXSIZ 155
- #define TMAGSIZ 6
- #define TTIMSIZ 12
- /*
- * Structure of an archive header.
- */
- union bincpio {
- char data[4096];
- #define SIZEOF_hdr_cpio 26
- struct hdr_cpio {
- char c_magic[2];
- char c_dev[2];
- char c_ino[2];
- char c_mode[2];
- char c_uid[2];
- char c_gid[2];
- char c_nlink[2];
- char c_rdev[2];
- char c_mtime[4];
- char c_namesize[2];
- char c_filesize[4];
- } Hdr;
- #define SIZEOF_cray_hdr 152
- struct cray_hdr { /* with thanks to Cray-Cyber.org */
- char C_magic[8];
- char C_dev[8];
- char C_ino[8];
- char C_mode[8];
- char C_uid[8];
- char C_gid[8];
- char C_nlink[8];
- char C_rdev[8];
- /*
- * The C_param field was introduced with
- * UNICOS 6 and is simply not present in
- * the earlier format. Following fields
- * are the same for both revisions again.
- */
- #define CRAY_PARAMSZ (8*8)
- char C_param[CRAY_PARAMSZ];
- char C_mtime[8];
- char C_namesize[8];
- char C_filesize[8];
- } Crayhdr;
- #define SIZEOF_c_hdr 76
- struct c_hdr {
- char c_magic[6];
- char c_dev[6];
- char c_ino[6];
- char c_mode[6];
- char c_uid[6];
- char c_gid[6];
- char c_nlink[6];
- char c_rdev[6];
- char c_mtime[11];
- char c_namesz[6];
- char c_filesz[11];
- } Cdr;
- #define SIZEOF_d_hdr 86
- struct d_hdr {
- char d_magic[6];
- char d_dev[6];
- char d_ino[6];
- char d_mode[6];
- char d_uid[6];
- char d_gid[6];
- char d_nlink[6];
- char d_rmaj[8];
- char d_rmin[8];
- char d_mtime[11];
- char d_namesz[6];
- char d_filesz[11];
- } Ddr;
- #define SIZEOF_Exp_cpio_hdr 110
- struct Exp_cpio_hdr {
- char E_magic[6];
- char E_ino[8];
- char E_mode[8];
- char E_uid[8];
- char E_gid[8];
- char E_nlink[8];
- char E_mtime[8];
- char E_filesize[8];
- char E_maj[8];
- char E_min[8];
- char E_rmaj[8];
- char E_rmin[8];
- char E_namesize[8];
- char E_chksum[8];
- } Edr;
- struct tar_header {
- char t_name[TNAMSIZ];
- char t_mode[8];
- char t_uid[8];
- char t_gid[8];
- char t_size[12];
- char t_mtime[TTIMSIZ];
- char t_chksum[8];
- char t_linkflag;
- char t_linkname[TNAMSIZ];
- char t_magic[TMAGSIZ];
- char t_version[2];
- char t_uname[32];
- char t_gname[32];
- char t_devmajor[8];
- char t_devminor[8];
- char t_prefix[TPFXSIZ];
- } Tdr;
- #define SIZEOF_bar_header 84
- struct bar_header {
- char b_mode[8];
- char b_uid[8];
- char b_gid[8];
- char b_size[12];
- char b_mtime[12];
- char b_chksum[8];
- char b_rdev[8];
- char b_linkflag;
- char b_bar_magic[2];
- char b_volume_num[4];
- char b_compressed;
- char b_date[12];
- } Bdr;
- #define SIZEOF_zip_header 30
- struct zip_header {
- char z_signature[4];
- char z_version[2];
- char z_gflag[2];
- char z_cmethod[2];
- char z_modtime[2];
- char z_moddate[2];
- char z_crc32[4];
- char z_csize[4];
- char z_nsize[4];
- char z_namelen[2];
- char z_extralen[2];
- } Zdr;
- };
- #define BCUT 0177777
- #define OCUT 0777777
- #define ECUT 0xFFFFFFFFUL
- static const char trailer[] = "TRAILER!!!";
- /*
- * Structure of per-file extra data for zip format.
- */
- union zextra {
- char data[1];
- #define SIZEOF_zextra_gn 4
- struct zextra_gn {
- char ze_gn_tag[2];
- char ze_gn_tsize[2];
- } Ze_gn;
- #define SIZEOF_zextra_64 32 /* regular size */
- #define SIZEOF_zextra_64_a 28 /* size without startn field */
- #define SIZEOF_zextra_64_b 20 /* size without reloff field */
- struct zextra_64 {
- char ze_64_tag[2];
- char ze_64_tsize[2];
- char ze_64_nsize[8];
- char ze_64_csize[8];
- char ze_64_reloff[8];
- char ze_64_startn[4];
- } Ze_64;
- #define SIZEOF_zextra_pk 16
- struct zextra_pk {
- char ze_pk_tag[2];
- char ze_pk_tsize[2];
- char ze_pk_atime[4];
- char ze_pk_mtime[4];
- char ze_pk_uid[2];
- char ze_pk_gid[2];
- } Ze_pk;
- #define SIZEOF_zextra_ek 17
- struct zextra_et {
- char ze_et_tag[2];
- char ze_et_tsize[2];
- char ze_et_flags[1];
- char ze_et_mtime[4];
- char ze_et_atime[4];
- char ze_et_ctime[4];
- } Ze_et;
- #define SIZEOF_zextra_i1 16
- struct zextra_i1 {
- char ze_i1_tag[2];
- char ze_i1_tsize[2];
- char ze_i1_atime[4];
- char ze_i1_mtime[4];
- char ze_i1_uid[2];
- char ze_i1_gid[2];
- } Ze_i1;
- #define SIZEOF_zextra_i2 8
- struct zextra_i2 {
- char ze_i2_tag[2];
- char ze_i2_tsize[2];
- char ze_i2_uid[2];
- char ze_i2_gid[2];
- } Ze_i2;
- #define SIZEOF_zextra_as 16
- struct zextra_as {
- char ze_as_tag[2];
- char ze_as_tsize[2];
- char ze_as_crc[4];
- char ze_as_mode[2];
- char ze_as_sizdev[4];
- char ze_as_uid[2];
- char ze_as_gid[2];
- } Ze_as;
- #define SIZEOF_zextra_cp 40
- struct zextra_cp {
- char ze_cp_tag[2];
- char ze_cp_tsize[2];
- char ze_cp_dev[4];
- char ze_cp_ino[4];
- char ze_cp_mode[4];
- char ze_cp_uid[4];
- char ze_cp_gid[4];
- char ze_cp_nlink[4];
- char ze_cp_rdev[4];
- char ze_cp_mtime[4];
- char ze_cp_atime[4];
- } Ze_cp;
- };
- static struct zipstuff { /* stuff for central directory at EOF */
- struct zipstuff *zs_next;
- char *zs_name; /* file name */
- long long zs_size; /* file size */
- long long zs_relative; /* offset of local header */
- long long zs_csize; /* compressed size */
- uint32_t zs_crc32; /* CRC */
- time_t zs_mtime; /* modification time */
- enum cmethod zs_cmethod; /* compression method */
- int zs_gflag; /* general flag */
- mode_t zs_mode; /* file mode */
- } *zipbulk;
- /*
- * Structure of the central zip directory at the end of the file. This
- * (obligatory) part of a zip file is written by this implementation,
- * but is completely ignored on copy-in. This means that we miss the
- * mode_t stored in zc_extralen if zc_versionmade[1] is 3 (Unix). We
- * have to do this since it contains the S_IFMT bits, thus telling us
- * whether something is a symbolic link and resulting in different
- * behavior - but as the input had to be seekable in order to do this,
- * we had to store entire archives in temporary files if input came
- * from a pipe to be consistent.
- */
- #define SIZEOF_zipcentral 46
- struct zipcentral {
- char zc_signature[4];
- char zc_versionmade[2];
- char zc_versionextr[2];
- char zc_gflag[2];
- char zc_cmethod[2];
- char zc_modtime[2];
- char zc_moddate[2];
- char zc_crc32[4];
- char zc_csize[4];
- char zc_nsize[4];
- char zc_namelen[2];
- char zc_extralen[2];
- char zc_commentlen[2];
- char zc_disknstart[2];
- char zc_internal[2];
- char zc_external[4];
- char zc_relative[4];
- };
- #define SIZEOF_zip64end 56
- struct zip64end {
- char z6_signature[4];
- char z6_recsize[8];
- char z6_versionmade[2];
- char z6_versionextr[2];
- char z6_thisdiskn[4];
- char z6_alldiskn[4];
- char z6_thisentries[8];
- char z6_allentries[8];
- char z6_dirsize[8];
- char z6_startsize[8];
- };
- #define SIZEOF_zip64loc 20
- struct zip64loc {
- char z4_signature[4];
- char z4_startno[4];
- char z4_reloff[8];
- char z4_alldiskn[4];
- };
- #define SIZEOF_zipend 22
- struct zipend {
- char ze_signature[4];
- char ze_thisdiskn[2];
- char ze_alldiskn[2];
- char ze_thisentries[2];
- char ze_allentries[2];
- char ze_dirsize[4];
- char ze_startsize[4];
- char ze_commentlen[2];
- };
- #define SIZEOF_zipddesc 16
- struct zipddesc {
- char zd_signature[4];
- char zd_crc32[4];
- char zd_csize[4];
- char zd_nsize[4];
- };
- #define SIZEOF_zipddesc64 24
- struct zipddesc64 {
- char zd_signature[4];
- char zd_crc32[4];
- char zd_csize[8];
- char zd_nsize[8];
- };
- /*
- * Magic numbers and strings.
- */
- static const uint16_t mag_bin = 070707;
- /*static const uint16_t mag_bbs = 0143561;*/
- static const char mag_asc[6] = "070701";
- static const char mag_crc[6] = "070702";
- static const char mag_odc[6] = "070707";
- static const long mag_sco = 0x7ffffe00;
- static const char mag_ustar[6] = "ustar\0";
- static const char mag_gnutar[8] = "ustar \0";
- static const char mag_bar[2] = "V\0";
- static const char mag_zipctr[4] = "PK\1\2";
- static const char mag_zipsig[4] = "PK\3\4";
- static const char mag_zipend[4] = "PK\5\6";
- static const char mag_zip64e[4] = "PK\6\6";
- static const char mag_zip64l[4] = "PK\6\7";
- static const char mag_zipdds[4] = "PK\7\10";
- #define mag_zip64f 0x0001
- #define mag_zipcpio 0x0707
- /*
- * Fields for the extended pax header.
- */
- static enum paxrec {
- PR_NONE = 0000,
- PR_ATIME = 0001,
- PR_GID = 0002,
- PR_LINKPATH = 0004,
- PR_MTIME = 0010,
- PR_PATH = 0020,
- PR_SIZE = 0040,
- PR_UID = 0100,
- PR_SUN_DEVMAJOR = 0200,
- PR_SUN_DEVMINOR = 0400
- } paxrec, globrec;
- /*
- * Prototype structure, collecting user-defined information
- * about a file.
- */
- struct prototype {
- mode_t pt_mode; /* type and permission bits */
- uid_t pt_uid; /* owner */
- gid_t pt_gid; /* group owner */
- time_t pt_atime; /* time of last access */
- time_t pt_mtime; /* time of last modification */
- dev_t pt_rdev; /* device major/minor */
- enum {
- PT_NONE = 0000,
- PT_TYPE = 0001,
- PT_OWNER = 0002,
- PT_GROUP = 0004,
- PT_MODE = 0010,
- PT_ATIME = 0020,
- PT_MTIME = 0040,
- PT_RDEV = 0100
- } pt_spec; /* specified information */
- };
- static struct stat globst;
- /*
- * This sets a sanity check limit on path names. If a longer path name
- * occurs in an archive, it is treated as corrupt. This is because no
- * known Unix system can handle path names of arbitrary length; limits
- * are typically between 1024 and 4096. Trying to extract longer path
- * names would fail anyway and will cpio eventually fail to allocate
- * memory.
- */
- #define SANELIMIT 0177777
- char *progname; /* argv[0] to main() */
- static struct dslot *devices; /* devices table */
- static struct dslot *markeddevs; /* unusable device numbers */
- static char *blkbuf; /* block buffer */
- int blksiz; /* block buffer size */
- static int blktop; /* top of filled part of buffer */
- static int curpos; /* position in blkbuf */
- static uint32_t fakedev; /* fake device for single link inodes */
- static uint32_t fakeino; /* fake inode for single link inodes */
- static uint32_t harddev; /* fake device used for hard links */
- static unsigned long long maxsize;/* maximum size for format */
- static unsigned long long maxrdev;/* maximum st_rdev for format */
- static unsigned long long maxmajor;/* maximum major(st_rdev) for format */
- static unsigned long long maxminor;/* maximum minor(st_rdev) for format */
- static unsigned long long maxuid; /* maximum user id for format */
- static unsigned long long maxgid; /* maximum group id for format */
- static unsigned long long maxnlink;/* maximum link count for format */
- static int mt; /* magtape file descriptor */
- static int mfl; /* magtape flags */
- static struct stat mtst; /* fstat() on mt */
- int aflag; /* reset access times */
- int Aflag; /* append to archive */
- int bflag; /* swap bytes */
- int Bflag; /* 5120 blocking */
- int cflag; /* ascii format */
- int Cflag; /* user-defined blocking */
- int dflag; /* create directories */
- int Dflag; /* do not ask for next device */
- int eflag; /* DEC format */
- int cray_eflag; /* do not archive if values too large */
- const char *Eflag; /* filename for files to be extracted */
- int fflag; /* pattern excludes */
- int Hflag; /* header format */
- const char *Iflag; /* input archive name */
- int kflag; /* skipt corrupted parts */
- int Kflag; /* IRIX-style large file support */
- int lflag; /* link of possible */
- int Lflag; /* follow symbolic links */
- int mflag; /* retain modification times */
- const char *Mflag; /* message when switching media */
- const char *Oflag; /* output archive name */
- int Pflag; /* prototype file list */
- int rflag; /* rename files */
- const char *Rflag; /* reassign ownerships */
- static uid_t Ruid; /* uid to assign */
- static gid_t Rgid; /* gid to assign */
- int sflag; /* swap half word bytes */
- int Sflag; /* swap word bytes */
- int tflag; /* print toc */
- int uflag; /* overwrite files unconditionally */
- int hp_Uflag; /* use umask when creating files */
- int vflag; /* verbose */
- int Vflag; /* special verbose */
- int sixflag; /* 6th Edition archives */
- int action; /* -i -o -p */
- long long errcnt; /* error status */
- static unsigned long long maxpath;/* maximum path length with -i */
- static uint32_t maxino; /* maximum inode number with -i */
- static uid_t myuid; /* user id of caller */
- static gid_t mygid; /* group id of caller */
- static long long blocks; /* copying statistics: full blocks */
- static long long bytes; /* copying statistics: partial blocks */
- static long long nwritten; /* bytes written to archive */
- static off_t aoffs; /* offset in archive */
- static off_t poffs; /* physical offset in archive */
- static int tapeblock = -1; /* physical tape block size */
- struct glist *patterns; /* patterns for -i */
- static int tty; /* terminal file descriptor */
- static const char *cur_ofile; /* current original file */
- static const char *cur_tfile; /* current temporary file */
- static mode_t umsk; /* user's umask */
- static int zipclevel; /* zip compression level */
- static struct islot *inull; /* splay tree null element */
- int printsev; /* print message severity strings */
- static int compressed_bar; /* this is a compressed bar archive */
- static int formatforced; /* -k -i -Hfmt forces a format */
- static long long lineno; /* input line number */
- int pax_dflag; /* directory matches only itself */
- int pax_kflag; /* do not overwrite files */
- int pax_nflag; /* select first archive member only */
- int pax_sflag; /* substitute file names */
- int pax_uflag; /* add only recent files to archive */
- int pax_Xflag; /* do not cross device boundaries */
- static enum {
- PO_NONE = 0,
- PO_LINKDATA = 01, /* include link data in type 2 */
- PO_TIMES = 02, /* create atime and mtime fields */
- } pax_oflag; /* recognized -o options */
- static void copyout(int (*)(const char *, struct stat *));
- static size_t ofiles_cpio(char **, size_t *);
- static void dooutp(void);
- static int outfile(const char *, struct stat *);
- static int addfile(const char *, struct stat *, uint32_t, uint32_t, int,
- const char *);
- static void iflush(struct islot *, uint32_t);
- static void lflush(void);
- static int bigendian(void);
- static void getbuf(char **, size_t *, size_t);
- static void prdot(int);
- static void newmedia(int);
- static void mclose(void);
- static ssize_t mwrite(int);
- static void bwrite(const char *, size_t);
- static void bflush(void);
- static int sum(int, const char *, struct stat *, char *);
- static int rstime(const char *, struct stat *, const char *);
- static struct islot *isplay(ino_t, struct islot *);
- static struct islot *ifind(ino_t, struct islot **);
- static void iput(struct islot *, struct islot **);
- static struct dslot *dfind(struct dslot **, dev_t);
- static void done(int);
- static void dopass(const char *);
- static int passdata(struct file *, const char *, int);
- static int passfile(const char *, struct stat *);
- static int filein(struct file *, int (*)(struct file *, const char *, int),
- char *);
- static int linkunlink(const char *, const char *);
- static void tunlink(char **);
- static int filet(struct file *, int (*)(struct file *, const char *, int));
- static void filev(struct file *);
- static int typec(struct stat *);
- static void permbits(mode_t);
- static void prtime_cpio(time_t);
- static void getpath(const char *, char **, char **, size_t *, size_t *);
- static void setpath(const char *, char **, char **,
- size_t, size_t *, size_t *);
- static int imdir(char *);
- static int setattr(const char *, struct stat *);
- static int setowner(const char *, struct stat *);
- static int canlink(const char *, struct stat *, int);
- static void doinp(void);
- static void storelink(struct file *);
- static void flushlinks(struct file *);
- static void flushnode(struct islot *, struct file *);
- static void flushrest(int);
- static void flushtree(struct islot *, int);
- static int inpone(struct file *, int);
- static int readhdr(struct file *, union bincpio *);
- static void whathdr(void);
- static int infile(struct file *);
- static int skipfile(struct file *);
- static int skipdata(struct file *f,
- int (*)(struct file *, const char *, int));
- static int indata(struct file *, const char *, int);
- static int totrailer(void);
- static long long rdoct(const char *, int);
- static long long rdhex(const char *, int);
- static ssize_t mread(void);
- static void mstat(void);
- static int skippad(unsigned long long, int);
- static int allzero(const char *, int);
- static const char *getuser(uid_t);
- static const char *getgroup(gid_t);
- static struct glist *want(struct file *, struct glist **);
- static void patfile(void);
- static int ckodd(long long, int, const char *, const char *);
- static int rname(char **, size_t *);
- static int redirect(const char *, const char *);
- static char *tnameof(struct tar_header *, char *);
- static int tmkname(struct tar_header *, const char *);
- static void tlinkof(struct tar_header *, struct file *);
- static int tmklink(struct tar_header *, const char *);
- static int tlflag(struct stat *);
- static void tchksum(union bincpio *);
- static int tcssum(union bincpio *, int);
- static int trdsum(union bincpio *);
- static mode_t tifmt(int);
- static void bchksum(union bincpio *);
- static int bcssum(union bincpio *);
- static void blinkof(const char *, struct file *, int);
- static void dump_barhdr(void);
- static int zcreat(const char *, mode_t);
- static int zclose(int);
- static void markdev(dev_t);
- static int marked(dev_t);
- static void cantsup(int, const char *);
- static void onint(int);
- static int zipread(struct file *, const char *, int, int);
- static void zipreaddesc(struct file *);
- static int cantunzip(struct file *, const char *);
- static time_t gdostime(const char *, const char *);
- static void mkdostime(time_t, char *, char *);
- static ssize_t ziprxtra(struct file *, struct zip_header *);
- static void ziptrailer(void);
- static void zipdefer(const char *, struct stat *, long long,
- uint32_t, long long, const struct zip_header *);
- static int zipwrite(int, const char *, struct stat *,
- union bincpio *, size_t, uint32_t, uint32_t,
- uint32_t *, long long *);
- static int zipwtemp(int, const char *, struct stat *,
- union bincpio *, size_t, uint32_t, uint32_t,
- uint32_t *, long long *);
- #if USE_ZLIB
- static int zipwdesc(int, const char *, struct stat *,
- union bincpio *, size_t, uint32_t, uint32_t,
- uint32_t *, long long *);
- #endif /* USE_ZLIB */
- static int zipwxtra(const char *, struct stat *, uint32_t, uint32_t);
- static void zipinfo(struct file *);
- static void readK2hdr(struct file *);
- static int readgnuname(char **, size_t *, long);
- static void writegnuname(const char *, long, int);
- static void tgetpax(struct tar_header *, struct file *);
- static enum paxrec tgetrec(char **, char **, char **);
- static void wrpax(const char *, const char *, struct stat *);
- static void addrec(char **, long *, long *,
- const char *, const char *, long long);
- static void paxnam(struct tar_header *, const char *);
- static char *sequence(void);
- static char *joinpath(const char *, char *);
- static int utf8(const char *);
- static char *getproto(char *, struct prototype *);
- size_t (*ofiles)(char **, size_t *) = ofiles_cpio;
- void (*prtime)(time_t) = prtime_cpio;
- int
- main(int argc, char **argv)
- {
- myuid = getuid();
- mygid = getgid();
- umask(umsk = umask(0));
- progname = basename(argv[0]);
- setlocale(LC_CTYPE, "");
- setlocale(LC_TIME, "");
- inull = scalloc(1, sizeof *inull);
- inull->i_lln = inull->i_rln = inull;
- flags(argc, argv);
- switch (action) {
- case 'i':
- if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
- sigset(SIGINT, onint);
- doinp();
- break;
- case 'o':
- dooutp();
- break;
- case 'p':
- if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
- sigset(SIGINT, onint);
- dopass(argv[optind]);
- break;
- }
- if (tflag)
- fflush(stdout);
- else if (Vflag)
- prdot(1);
- if (pax != PAX_TYPE_CPIO)
- pax_onexit();
- //fprintf(stderr, "%llu blocks\n", blocks + ((bytes + 0777) >> 9));
- mclose();
- if (errcnt && sysv3 == 0)
- fprintf(stderr, "%llu error(s)\n", errcnt);
- return errcnt ? sysv3 ? 1 : 2 : 0;
- }
- static size_t
- ofiles_cpio(char **name, size_t *namsiz)
- {
- static struct iblok *ip;
- if (ip == NULL)
- ip = ib_alloc(0, 0);
- return ib_getlin(ip, name, namsiz, srealloc);
- }
- /*
- * Read the file name list for -o and -p and do initial processing
- * for each name.
- */
- static void
- copyout(int (*copyfn)(const char *, struct stat *))
- {
- char *name = NULL, *np;
- size_t namsiz = 0, namlen;
- struct stat st;
- struct prototype pt;
- while ((namlen = ofiles(&name, &namsiz)) != 0) {
- lineno++;
- if (name[namlen-1] == '\n')
- name[--namlen] = '\0';
- if (Pflag)
- np = getproto(name, &pt);
- else
- np = name;
- while (np[0] == '.' && np[1] == '/') {
- np += 2;
- while (*np == '/')
- np++;
- if (*np == '\0') {
- np = name;
- break;
- }
- }
- if (lstat(np, &st) < 0) {
- if (Pflag && *np && ((pt.pt_spec &
- (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE|PT_RDEV) &&
- ((pt.pt_mode&S_IFMT) == S_IFBLK ||
- (pt.pt_mode&S_IFMT) == S_IFCHR)) ||
- (pt.pt_spec &
- (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE) &&
- ((pt.pt_mode&S_IFMT) == S_IFDIR ||
- (pt.pt_mode&S_IFMT) == S_IFIFO ||
- (pt.pt_mode&S_IFMT) == S_IFREG)))) {
- memset(&st, 0, sizeof st);
- st.st_mode = pt.pt_mode;
- st.st_blksize = 4096;
- st.st_nlink = 1;
- goto missingok;
- }
- else if (sysv3 < 0)
- msg(2, 0, "< %s > ?\n", np);
- else if (sysv3 > 0)
- msg(2, 0, "Cannot obtain information "
- "about file: \"%s\".\n",
- np);
- else
- emsg(2, "Error with lstat of \"%s\"", np);
- errcnt++;
- continue;
- }
- missingok:
- if (Lflag && (st.st_mode&S_IFMT) == S_IFLNK) {
- if (stat(np, &st) < 0) {
- emsg(2, "Cannot follow \"%s\"", np);
- errcnt++;
- continue;
- }
- }
- /*
- * These file types are essentially useless in an archive
- * since they are recreated by any process that needs them.
- * We thus ignore them and do not even issue a warning,
- * because that would only displace more important messages
- * on a terminal and confuse people who just want to copy
- * directory hierarchies.--But for pax, POSIX.1-2001 requires
- * us to fail!
- */
- if ((st.st_mode&S_IFMT) == S_IFSOCK ||
- (st.st_mode&S_IFMT) == S_IFDOOR) {
- if (pax >= PAX_TYPE_PAX2001) {
- msg(2, 0, "Cannot handle %s \"%s\".\n",
- (st.st_mode&S_IFMT) == S_IFSOCK ?
- "socket" : "door", np);
- errcnt++;
- }
- continue;
- }
- if (Pflag) {
- if (pt.pt_spec & PT_TYPE)
- if ((st.st_mode&S_IFMT) != (pt.pt_mode&S_IFMT))
- msg(4, 0, "line %lld: types "
- "do not match\n", lineno);
- if (pt.pt_spec & PT_OWNER)
- st.st_uid = pt.pt_uid;
- if (pt.pt_spec & PT_GROUP)
- st.st_gid = pt.pt_gid;
- if (pt.pt_spec & PT_MODE) {
- st.st_mode &= ~(mode_t)07777;
- st.st_mode |= pt.pt_mode;
- }
- if (pt.pt_spec & PT_ATIME)
- st.st_atime = pt.pt_atime;
- if (pt.pt_spec & PT_MTIME)
- st.st_mtime = pt.pt_mtime;
- if (pt.pt_spec & PT_RDEV) {
- if ((st.st_mode&S_IFMT) != S_IFBLK &&
- (st.st_mode&S_IFMT) != S_IFCHR)
- msg(4, 0, "line %lld: device type "
- "specified for non-device "
- "file\n", lineno);
- st.st_rdev = pt.pt_rdev;
- }
- }
- if (pax_track(np, st.st_mtime) == 0)
- continue;
- if ((fmttype == FMT_ZIP ||
- fmttype & TYP_BAR ||
- fmttype == FMT_GNUTAR)
- && (st.st_mode&S_IFMT) == S_IFDIR &&
- name[namlen-1] != '/') {
- if (namlen+2 >= namsiz) {
- size_t diff = np - name;
- name = srealloc(name, namsiz = namlen+2);
- np = &name[diff];
- }
- name[namlen++] = '/';
- name[namlen] = '\0';
- }
- errcnt += copyfn(np, &st);
- }
- }
- /*
- * Execution for -o.
- */
- static void
- dooutp(void)
- {
- if (Oflag) {
- if ((mt = Aflag ? open(Oflag, O_RDWR, 0666) :
- creat(Oflag, 0666)) < 0) {
- if (sysv3) {
- emsg(013, "Cannot open <%s> for %s.", Oflag,
- Aflag ? "append" : "output");
- done(1);
- } else
- msg(3, -2, "Cannot open \"%s\" for %s\n", Oflag,
- Aflag ? "append" : "output");
- }
- } else
- mt = dup(1);
- mstat();
- blkbuf = svalloc(blksiz, 1);
- if (Aflag) {
- if (totrailer() != 0)
- return;
- } else if (fmttype == FMT_NONE)
- fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
- if (fmttype & TYP_BINARY) {
- maxino = 0177777;
- fakeino = 0177777;
- maxpath = 256;
- if (fmttype & TYP_SGI) {
- maxsize = 0x7FFFFFFFFFFFFFFFLL;
- maxmajor = 037777;
- maxminor = 0777777;
- } else {
- maxsize = 0x7FFFFFFFLL;
- maxrdev = 0177777;
- }
- maxuid = 0177777;
- maxgid = 0177777;
- maxnlink = 0177777;
- } else if (fmttype == FMT_ODC) {
- maxino = 0777777;
- fakeino = 0777777;
- maxpath = 256;
- maxsize = 077777777777LL;
- maxrdev = 0777777;
- maxuid = 0777777;
- maxgid = 0777777;
- maxnlink = 0777777;
- } else if (fmttype == FMT_DEC) {
- maxino = 0777777;
- fakeino = 0777777;
- maxpath = 256;
- maxsize = 077777777777LL;
- maxmajor = 077777777;
- maxminor = 077777777;
- maxuid = 0777777;
- maxgid = 0777777;
- maxnlink = 0777777;
- } else if (fmttype & TYP_NCPIO) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = 1024;
- maxsize = fmttype&TYP_SCO ? 0x7FFFFFFFFFFFFFFFLL : 0xFFFFFFFFUL;
- maxmajor = 0xFFFFFFFFUL;
- maxminor = 0xFFFFFFFFUL;
- maxuid = 0xFFFFFFFFUL;
- maxgid = 0xFFFFFFFFUL;
- maxnlink = 0xFFFFFFFFUL;
- } else if (fmttype & TYP_CRAY) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = SANELIMIT;
- maxsize = 0x7FFFFFFFFFFFFFFFLL;
- maxrdev = 0x7FFFFFFFFFFFFFFFLL;
- maxuid = 0x7FFFFFFFFFFFFFFFLL;
- maxgid = 0x7FFFFFFFFFFFFFFFLL;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- } else if (fmttype == FMT_GNUTAR) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = SANELIMIT;
- maxsize = 0x7FFFFFFFFFFFFFFFLL;
- maxmajor = 0x7FFFFFFFFFFFFFFFLL;
- maxminor = 0x7FFFFFFFFFFFFFFFLL;
- maxuid = 0x7FFFFFFFFFFFFFFFLL;
- maxgid = 0x7FFFFFFFFFFFFFFFLL;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- } else if (fmttype & TYP_PAX) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = SANELIMIT;
- maxsize = 0x7FFFFFFFFFFFFFFFLL;
- maxmajor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
- maxminor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
- maxuid = 0x7FFFFFFFFFFFFFFFLL;
- maxgid = 0x7FFFFFFFFFFFFFFFLL;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- if (pax_oflag & PO_TIMES)
- globrec |= PR_ATIME|PR_MTIME;
- } else if (fmttype & TYP_BAR) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = 512 - SIZEOF_bar_header - 1;
- maxsize = 077777777777LL;
- maxrdev = 07777777;
- maxuid = 07777777;
- maxgid = 07777777;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- if (nwritten == 0)
- dump_barhdr();
- } else if (fmttype & TYP_USTAR) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = 256;
- maxsize = 077777777777LL;
- maxmajor = 07777777;
- maxminor = 07777777;
- maxuid = 07777777;
- maxgid = 07777777;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- } else if (fmttype & TYP_OTAR) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = 99;
- maxsize = 077777777777LL;
- maxuid = 07777777;
- maxgid = 07777777;
- maxnlink = 0x7FFFFFFFFFFFFFFFLL;
- } else if (fmttype == FMT_ZIP) {
- maxino = 0xFFFFFFFFUL;
- fakeino = 0xFFFFFFFFUL;
- maxpath = 60000;
- maxsize = 0x7FFFFFFFFFFFFFFFLL;
- maxrdev = 0xFFFFFFFFUL;
- maxuid = 0xFFFFFFFFUL;
- maxgid = 0xFFFFFFFFUL;
- maxnlink = 0xFFFFFFFFUL;
- } else
- abort();
- fakedev = 0177777;
- harddev = 1;
- copyout(outfile);
- if (fmttype & TYP_NCPIO)
- lflush();
- if (fmttype & TYP_CPIO) {
- struct stat st;
- memset(&st, 0, sizeof st);
- st.st_nlink = 1;
- outfile(trailer, &st);
- }
- if (fmttype & TYP_TAR) {
- char b[512];
- memset(b, 0, sizeof b);
- bwrite(b, sizeof b);
- bwrite(b, sizeof b);
- }
- if (fmttype == FMT_ZIP)
- ziptrailer();
- bflush();
- }
- /*
- * Handle a single file for -o, do sanity checks and detect hard links.
- */
- static int
- outfile(const char *file, struct stat *st)
- {
- uint32_t dev, ino;
- size_t pathsz;
- if ((st->st_mode&S_IFMT) == S_IFREG)
- if (mtst.st_dev == st->st_dev && mtst.st_ino == st->st_ino)
- return 0;
- if (st->st_size > maxsize) {
- msg(2, 0, "Size of %c%s%c >%lluGB. Not dumped\n",
- sysv3 ? '<' : '"',
- file,
- sysv3 ? '>' : '"',
- (maxsize+1) / (1024*1024*1024));
- return 1;
- }
- if (((st->st_mode&S_IFMT)==S_IFBLK||(st->st_mode&S_IFMT)==S_IFCHR) &&
- (maxrdev &&
- (unsigned long long)st->st_rdev > maxrdev ||
- maxmajor &&
- (unsigned long long)major(st->st_rdev) > maxmajor ||
- maxminor &&
- (unsigned long long)minor(st->st_rdev) > maxminor)) {
- cantsup(1, file);
- return 1;
- }
- if ((unsigned long long)st->st_uid > maxuid) {
- if (cray_eflag) {
- cantsup(1, file);
- return 1;
- }
- cantsup(0, file);
- st->st_uid = 60001;
- if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0111)
- st->st_mode &= ~(mode_t)S_ISUID;
- if ((unsigned long long)st->st_gid > maxgid) {
- st->st_gid = 60001;
- if ((st->st_mode&S_IFMT)==S_IFREG && st->st_mode&0010)
- st->st_mode &= ~(mode_t)S_ISGID;
- }
- } else if ((unsigned long long)st->st_gid > maxgid) {
- if (cray_eflag) {
- cantsup(1, file);
- return 1;
- }
- cantsup(0, file);
- st->st_gid = 60001;
- if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0010)
- st->st_mode &= ~(mode_t)S_ISGID;
- }
- if ((pathsz = strlen(file)) > maxpath) {
- msg(2, 0, "%s: file name too long\n", file);
- return 1;
- }
- /*
- * Detect hard links and compute fake inode counts. The mechanism
- * is as follows: If a file has more than one link, a fake device
- * number starting at one is used for its device, and a fake inode
- * number is used starting at one too.
- *
- * The information on links of directories is useless, so it is
- * dropped and handled like a file with a single link only: Fake
- * devices are allocated just below the format's limit, fake
- * i-nodes the same.
- *
- * This way even the binary cpio format can have up to ~4G files.
- */
- if (maxino && st->st_nlink > 1 && (st->st_mode&S_IFMT) != S_IFDIR) {
- struct dslot *ds, *dp;
- struct islot *ip;
- dev = 1;
- ds = devices;
- dp = NULL;
- nextdev:
- for (; ds; dp = ds, ds = ds->d_nxt, dev++ /* see below! */)
- if (ds->d_dev == st->st_dev)
- break;
- if (markeddevs && marked(dev)) {
- dev++;
- goto nextdev;
- }
- if (dev >= fakedev)
- msg(4, 1, "Too many devices in archive, exiting\n");
- if (ds == NULL) {
- ds = scalloc(1, sizeof *ds);
- ds->d_dev = st->st_dev;
- ds->d_fake = dev;
- if (devices == NULL)
- devices = ds;
- else
- dp->d_nxt = ds;
- }
- harddev = dev;
- if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL) {
- if (ds->d_cnt >= maxino) {
- /* corresponds to for loop above */
- dev++, dp = ds, ds = ds->d_nxt;
- goto nextdev;
- }
- ip = scalloc(1, sizeof *ip);
- ip->i_ino = st->st_ino;
- ip->i_fino = ++ds->d_cnt;
- ip->i_nlk = st->st_nlink;
- if (fmttype & TYP_TAR)
- ip->i_name = sstrdup(file);
- if (fmttype & TYP_NCPIO) {
- ip->i_st = smalloc(sizeof *ip->i_st);
- *ip->i_st = *st;
- }
- iput(ip, &ds->d_isl);
- }
- ino = ip->i_fino;
- if (fmttype & TYP_NCPIO) {
- /*
- * In SVR4 ascii cpio format, files with multiple
- * links are stored with a zero size except for the
- * last link, which contains the actual file content.
- * As one cannot know which is the last link in
- * advance since some links may be outside the
- * archive content, all links have to be collected
- * and written out at once.
- */
- struct ilink *il, *ik;
- switch (ip->i_nlk) {
- case 1:
- /*
- * This was the last link to a file. Write
- * all previous links and break to write
- * the actual file content. Free the pointers
- * in islot; islot remains within the tree
- * with a remaining link count of zero.
- */
- ip->i_nlk--;
- free(ip->i_st);
- ip->i_st = NULL;
- for (il = ip->i_lnk, ik = NULL; il;
- ik = il, il = il->l_nxt,
- ik ? free(ik), 0 : 0) {
- errcnt += addfile(il->l_nam, st,
- dev, ino, 1, 0);
- free(il->l_nam);
- }
- break;
- case 0:
- /*
- * This file got a link during operation, or
- * -L was specified and we encountered a link
- * more than once. Start with a fresh link
- * count again.
- */
- ip->i_nlk = st->st_nlink;
- ip->i_lnk = NULL;
- ip->i_st = smalloc(sizeof *ip->i_st);
- *ip->i_st = *st;
- /*FALLTHRU*/
- default:
- /*
- * There are more links to this file. Store
- * only the name and return.
- */
- ip->i_nlk--;
- if (ip->i_lnk) {
- for (il = ip->i_lnk; il->l_nxt;
- il = il->l_nxt);
- il->l_nxt = scalloc(1,sizeof*il->l_nxt);
- il = il->l_nxt;
- } else {
- ip->i_lnk = scalloc(1,sizeof*ip->i_lnk);
- il = ip->i_lnk;
- }
- il->l_nam = smalloc(pathsz + 1);
- strcpy(il->l_nam, file);
- return 0;
- }
- } else if (fmttype & TYP_TAR) {
- if (strcmp(ip->i_name, file))
- return addfile(file, st, dev, ino, 1,
- ip->i_name);
- }
- } else { /* single-linked or directory */
- dev = fakedev;
- while (markeddevs && marked(dev))
- dev--;
- if ((ino = fakeino--) == 0) {
- if (--dev <= harddev)
- msg(4, 1, "Too many devices in archive, "
- "exiting\n");
- fakedev = dev;
- ino = maxino;
- fakeino = ino - 1;
- }
- }
- return addfile(file, st, dev, ino, 0, 0);
- }
- /*
- * Add a single file to the archive with -o.
- */
- static int
- addfile(const char *realfile, struct stat *st,
- uint32_t dev, uint32_t ino, int zerolink, const char *linkname)
- {
- union bincpio bc;
- int fd = -1;
- long long size;
- int pad, i;
- ssize_t rsz = 0, wsz = 0, hsz, fsz, psz;
- long long remsz, relative, nlink;
- long long Kbase = 0, Krest = 0, Ksize = 0;
- struct hdr_cpio K2hdr;
- uint32_t crc = 0;
- long long csize = 0;
- char *file;
- char *symblink = NULL;
- int failure = 1;
- file = sstrdup(realfile);
- if (pax != PAX_TYPE_CPIO && strcmp(file, trailer)) {
- size_t junk = 0;
- if (pax_sflag && pax_sname(&file, &junk) == 0)
- goto cleanup;
- if (rflag && rname(&file, &junk) == 0)
- goto cleanup;
- }
- fsz = strlen(file);
- relative = nwritten;
- memset(bc.data, 0, sizeof bc.data);
- if (fmttype == FMT_PAX && pax_oflag & PO_LINKDATA &&
- (st->st_mode&S_IFMT) == S_IFREG)
- size = st->st_size;
- else if (zerolink)
- size = 0;
- else if ((st->st_mode&S_IFMT) == S_IFREG)
- size = st->st_size;
- else if ((st->st_mode&S_IFMT) == S_IFLNK) {
- i = st->st_size ? st->st_size : PATH_MAX;
- symblink = smalloc(i+1);
- if ((size = readlink(realfile, symblink, i)) < 0) {
- emsg(3, "Cannot read symbolic link \"%s\"", realfile);
- goto cleanup;
- }
- symblink[size] = '\0';
- } else
- size = 0;
- nlink = ((unsigned long long)st->st_nlink>maxnlink ?
- maxnlink : st->st_nlink);
- if (fmttype & TYP_NCPIO) {
- long size1;
- if (fmttype & TYP_SCO && size >= mag_sco) {
- char *ofile = file;
- size1 = mag_sco;
- fsz += 22;
- file = smalloc(fsz + 1);
- snprintf(file, fsz + 1, "%s%csize=%016llx",
- ofile, 0, size);
- free(ofile);
- } else
- size1 = size;
- pad = 4;
- sprintf(bc.data, "%*.*s%08lx%08lx%08lx%08lx%08lx%08lx"
- "%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
- (int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
- (int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
- fmttype & TYP_CRC ? mag_crc : mag_asc,
- (long)ino & ECUT,
- (long)st->st_mode & ECUT,
- (long)st->st_uid & ECUT,
- (long)st->st_gid & ECUT,
- (long)nlink & ECUT,
- (long)st->st_mtime & ECUT,
- (long)size1 & ECUT,
- (long)major(dev) & ECUT,
- (long)minor(dev) & ECUT,
- (long)major(st->st_rdev) & ECUT,
- (long)minor(st->st_rdev) & ECUT,
- (long)++fsz,
- 0L);
- hsz = SIZEOF_Exp_cpio_hdr;
- if ((psz = (hsz + fsz) % pad) != 0)
- psz = pad - psz;
- } else if (fmttype == FMT_ODC) {
- pad = 1;
- sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo%06lo"
- "%011lo%06lo%011lo",
- (int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
- (long)dev & OCUT,
- (long)ino & OCUT,
- (long)st->st_mode & OCUT,
- (long)st->st_uid & OCUT,
- (long)st->st_gid & OCUT,
- (long)nlink & OCUT,
- (long)st->st_rdev & OCUT,
- (long)st->st_mtime,
- (long)++fsz,
- (long)size);
- hsz = SIZEOF_c_hdr;
- if ((psz = (hsz + fsz) % pad) != 0)
- psz = pad - psz;
- } else if (fmttype == FMT_DEC) {
- pad = 1;
- sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo"
- "%08lo%08lo%011lo%06lo%011lo",
- (int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
- (long)dev & OCUT,
- (long)ino & OCUT,
- (long)st->st_mode & OCUT,
- (long)st->st_uid & OCUT,
- (long)st->st_gid & OCUT,
- (long)nlink & OCUT,
- (long)major(st->st_rdev) & 077777777,
- (long)minor(st->st_rdev) & 077777777,
- (long)st->st_mtime,
- (long)++fsz,
- (long)size);
- hsz = SIZEOF_d_hdr;
- if ((psz = (hsz + fsz) % pad) != 0)
- psz = pad - psz;
- } else if (fmttype & TYP_BINARY) {
- /*
- * To avoid gcc's stupid 'comparison is always false due to
- * limited range of data type' warning.
- */
- unsigned long long gcccrap;
- pad = 2;
- if (fmttype & TYP_BE) {
- be16p(mag_bin, bc.Hdr.c_magic);
- be16p(dev, bc.Hdr.c_dev);
- be16p(ino, bc.Hdr.c_ino);
- be16p(st->st_mode, bc.Hdr.c_mode);
- be16p(st->st_uid, bc.Hdr.c_uid);
- be16p(st->st_gid, bc.Hdr.c_gid);
- be16p(nlink, bc.Hdr.c_nlink);
- be16p(st->st_rdev, bc.Hdr.c_rdev);
- be32p(st->st_mtime, bc.Hdr.c_mtime);
- be16p(++fsz, bc.Hdr.c_namesize);
- } else {
- le16p(mag_bin, bc.Hdr.c_magic);
- le16p(dev, bc.Hdr.c_dev);
- le16p(ino, bc.Hdr.c_ino);
- le16p(st->st_mode, bc.Hdr.c_mode);
- le16p(st->st_uid, bc.Hdr.c_uid);
- le16p(st->st_gid, bc.Hdr.c_gid);
- le16p(nlink, bc.Hdr.c_nlink);
- le16p(st->st_rdev, bc.Hdr.c_rdev);
- me32p(st->st_mtime, bc.Hdr.c_mtime);
- le16p(++fsz, bc.Hdr.c_namesize);
- }
- if (fmttype & TYP_SGI && size > 0x7FFFFFFFLL) {
- Krest = size & 0x7FFFFFFFLL;
- Kbase = size - Krest;
- Ksize = 0x100000000LL - (Kbase >> 31);
- if (fmttype & TYP_BE)
- be32p(Ksize, bc.Hdr.c_filesize);
- else
- me32p(Ksize, bc.Hdr.c_filesize);
- K2hdr = bc.Hdr;
- if (fmttype & TYP_BE)
- be32p(Krest, K2hdr.c_filesize);
- else
- me32p(Krest, K2hdr.c_filesize);
- } else {
- if (fmttype & TYP_BE)
- be32p(size, bc.Hdr.c_filesize);
- else
- me32p(size, bc.Hdr.c_filesize);
- }
- if (fmttype & TYP_SGI &&
- (((st->st_mode&S_IFMT) == S_IFBLK ||
- (st->st_mode&S_IFMT) == S_IFCHR) &&
- ((unsigned long long)major(st->st_rdev)>0xFF ||
- (unsigned long long)minor(st->st_rdev)>0xFF) ||
- (gcccrap = st->st_rdev) > 0177777)) {
- uint32_t rdev;
- rdev = (minor(st->st_rdev) & 0x0003FFFF) +
- ((major(st->st_rdev)<<18) & 0xFFFC0000);
- if (fmttype & TYP_BE) {
- be16p(0xFFFF, bc.Hdr.c_rdev);
- be32p(rdev, bc.Hdr.c_filesize);
- } else {
- le16p(0xFFFF, bc.Hdr.c_rdev);
- me32p(rdev, bc.Hdr.c_filesize);
- }
- }
- hsz = SIZEOF_hdr_cpio;
- psz = (hsz + fsz) % 2;
- } else if (fmttype & TYP_CRAY) {
- int diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
- mode_t mo;
- pad = 1;
- be64p(mag_bin, bc.Crayhdr.C_magic);
- be64p(dev, bc.Crayhdr.C_dev);
- be64p(ino, bc.Crayhdr.C_ino);
- if ((st->st_mode&S_IFMT) == S_IFLNK) /* non-standard */
- mo = st->st_mode&07777|0130000; /* S_IFLNK on Cray */
- else
- mo = st->st_mode;
- be64p(mo, bc.Crayhdr.C_mode);
- be64p(st->st_uid, bc.Crayhdr.C_uid);
- be64p(st->st_gid, bc.Crayhdr.C_gid);
- be64p(nlink, bc.Crayhdr.C_nlink);
- be64p(st->st_rdev, bc.Crayhdr.C_rdev);
- be64p(st->st_mtime, bc.Crayhdr.C_mtime - diff5);
- be64p(++fsz, bc.Crayhdr.C_namesize - diff5);
- be64p(size, bc.Crayhdr.C_filesize - diff5);
- hsz = SIZEOF_cray_hdr - diff5;
- psz = 0;
- } else if (fmttype & TYP_BAR) {
- int c, n = 0;
- pad = 512;
- sprintf(bc.Bdr.b_mode, "%7.7o",(int)st->st_mode&(07777|S_IFMT));
- sprintf(bc.Bdr.b_uid, "%7.7lo", (long)st->st_uid);
- sprintf(bc.Bdr.b_gid, "%7.7lo", (long)st->st_gid);
- sprintf(bc.Bdr.b_size, "%11.11llo",
- (st->st_mode&S_IFMT) == S_IFREG && !zerolink ?
- (long long)st->st_size&077777777777LL : 0LL);
- sprintf(bc.Bdr.b_mtime, "%11.11lo", (long)st->st_mtime);
- sprintf(bc.Bdr.b_rdev, "%7.7lo", (long)st->st_rdev);
- strcpy(&bc.data[SIZEOF_bar_header], file);
- c = tlflag(st);
- if (zerolink == 0) {
- bc.Bdr.b_linkflag = c;
- if (c == '2') {
- strncpy(&bc.data[SIZEOF_bar_header+fsz+1],
- symblink,
- 512-SIZEOF_bar_header-fsz);
- n = size;
- }
- } else {
- bc.Bdr.b_linkflag = '1';
- strncpy(&bc.data[SIZEOF_bar_header+fsz+1], linkname,
- 512-SIZEOF_bar_header-fsz-1);
- n = strlen(linkname);
- }
- if (n > 512-SIZEOF_bar_header-fsz-1) {
- msg(3, 0, "%s: linked name too long\n", realfile);
- goto cleanup;
- }
- bchksum(&bc);
- hsz = 512;
- psz = 0;
- fsz = 0;
- } else if (fmttype & TYP_TAR) {
- const char *cp;
- int c;
- /*
- * Many SVR4 cpio derivatives expect the mode field
- * to contain S_IFMT bits. The meaning of these bits
- * in the mode field of the ustar header is left
- * unspecified by IEEE Std 1003.1, 1996, 10.1.1.
- */
- int mmask = fmttype == FMT_USTAR || fmttype == FMT_PAX ?
- 07777 : 07777|S_IFMT;
- paxrec = globrec;
- pad = 512;
- if (tmkname(&bc.Tdr, file) != 0)
- goto cleanup;
- sprintf(bc.Tdr.t_mode, "%7.7o", (int)st->st_mode & mmask);
- if (fmttype == FMT_GNUTAR && st->st_uid > 07777777) {
- be64p(st->st_uid, bc.Tdr.t_uid);
- bc.Tdr.t_uid[0] |= 0200;
- } else {
- sprintf(bc.Tdr.t_uid, "%7.7lo",
- (long)st->st_uid&07777777);
- if (fmttype & TYP_PAX && st->st_uid > 07777777)
- paxrec |= PR_UID;
- }
- if (fmttype == FMT_GNUTAR && st->st_gid > 07777777) {
- be64p(st->st_gid, bc.Tdr.t_gid);
- bc.Tdr.t_gid[0] |= 0200;
- } else {
- sprintf(bc.Tdr.t_gid, "%7.7lo",
- (long)st->st_gid&07777777);
- if (fmttype & TYP_PAX && st->st_gid > 07777777)
- paxrec |= PR_GID;
- }
- if (fmttype == FMT_GNUTAR && (st->st_mode&S_IFMT) == S_IFREG &&
- st->st_size > 077777777777LL && !zerolink) {
- bc.Tdr.t_size[0] = '\200';
- be64p(st->st_size, &bc.Tdr.t_size[4]);
- } else {
- sprintf(bc.Tdr.t_size, "%11.11llo",
- (st->st_mode&S_IFMT) == S_IFREG &&
- (!zerolink || fmttype == FMT_PAX &&
- pax_oflag & PO_LINKDATA) ?
- (long long)st->st_size&077777777777LL : 0LL);
- if (fmttype & TYP_PAX &&
- (st->st_mode&S_IFMT) == S_IFREG &&
- st->st_size > 077777777777LL &&
- (!zerolink || fmttype == FMT_PAX &&
- pax_oflag & PO_LINKDATA))
- paxrec |= PR_SIZE;
- }
- sprintf(bc.Tdr.t_mtime, "%11.11lo", (long)st->st_mtime);
- if ((c = tlflag(st)) < 0) {
- if ((st->st_mode&S_IFMT) != S_IFDIR) {
- msg(2, 0, "%s is not a file. Not dumped\n",
- realfile);
- errcnt++;
- } else
- failure = 0;
- goto cleanup;
- }
- if (zerolink == 0) {
- bc.Tdr.t_linkflag = c;
- if (c == '2') {
- if (tmklink(&bc.Tdr, symblink) != 0)
- goto cleanup;
- }
- } else {
- bc.Tdr.t_linkflag = '1';
- if (tmklink(&bc.Tdr, linkname) != 0)
- goto cleanup;
- }
- if (fmttype & TYP_USTAR) {
- if (fmttype == FMT_GNUTAR)
- strcpy(bc.Tdr.t_magic, mag_gnutar);
- else {
- strcpy(bc.Tdr.t_magic, mag_ustar);
- bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
- }
- if ((cp = getuser(st->st_uid)) != NULL)
- sprintf(bc.Tdr.t_uname, "%.31s", cp);
- else
- msg(1, 0, "could not get passwd information "
- "for %s\n", realfile);
- if ((cp = getgroup(st->st_gid)) != NULL)
- sprintf(bc.Tdr.t_gname, "%.31s", cp);
- else
- msg(1, 0, "could not get group information "
- "for %s\n", realfile);
- if (fmttype == FMT_GNUTAR &&
- (unsigned long long)major(st->st_rdev)
- > 077777777) {
- be64p(major(st->st_rdev), bc.Tdr.t_devmajor);
- bc.Tdr.t_devmajor[0] |= 0200;
- } else {
- if (fmttype == FMT_SUN &&
- (unsigned long long)major(st->st_rdev)
- > 077777777 &&
- ((st->st_mode&S_IFMT)==S_IFBLK||
- (st->st_mode&S_IFMT)==S_IFCHR))
- paxrec |= PR_SUN_DEVMAJOR;
- sprintf(bc.Tdr.t_devmajor, "%7.7o",
- (int)major(st->st_rdev)&07777777);
- }
- if (fmttype == FMT_GNUTAR &&
- (unsigned long long)minor(st->st_rdev)
- > 077777777) {
- be64p(minor(st->st_rdev), bc.Tdr.t_devminor);
- bc.Tdr.t_devminor[0] |= 0200;
- } else {
- if (fmttype == FMT_SUN &&
- (unsigned long long)minor(st->st_rdev)
- > 077777777 &&
- ((st->st_mode&S_IFMT)==S_IFBLK||
- (st->st_mode&S_IFMT)==S_IFCHR))
- paxrec |= PR_SUN_DEVMINOR;
- sprintf(bc.Tdr.t_devminor, "%7.7o",
- (int)minor(st->st_rdev)&07777777);
- }
- }
- tchksum(&bc);
- hsz = 512;
- psz = 0;
- fsz = 0;
- } else if (fmttype == FMT_ZIP) {
- pad = 1;
- memcpy(bc.Zdr.z_signature, mag_zipsig, sizeof mag_zipsig);
- bc.Zdr.z_version[0] = 10;
- mkdostime(st->st_mtime, bc.Zdr.z_modtime, bc.Zdr.z_moddate);
- if ((st->st_mode&S_IFMT) == S_IFREG ||
- (st->st_mode&S_IFMT) == S_IFLNK) {
- le32p(size, bc.Zdr.z_csize);
- le32p(size, bc.Zdr.z_nsize);
- csize = size;
- }
- le16p(fsz, bc.Zdr.z_namelen);
- le16p(SIZEOF_zextra_cp, bc.Zdr.z_extralen);
- hsz = SIZEOF_zip_header;
- psz = 0;
- } else
- abort();
- /*
- * Start writing the file to the archive.
- */
- if ((st->st_mode&S_IFMT) == S_IFREG && st->st_size != 0 &&
- (zerolink == 0 || fmttype == FMT_PAX &&
- pax_oflag & PO_LINKDATA)) {
- char *buf;
- size_t bufsize;
- int readerr = 0;
- if ((fd = open(realfile, O_RDONLY)) < 0) {
- if (sysv3 < 0)
- msg(0, 0, "< %s > ?\n", realfile);
- else if (sysv3 > 0)
- fprintf(stderr, "<%s> ?\n", realfile);
- else
- msg(0, 0, "\"%s\" ?\n", realfile);
- goto cleanup;
- }
- if (fmttype == FMT_ZIP) {
- if (zipwrite(fd, file, st, &bc, fsz, dev, ino,
- &crc, &csize) < 0)
- goto cleanup2;
- goto done;
- }
- if (fmttype & TYP_CRC)
- if (sum(fd, realfile, st, bc.Edr.E_chksum) < 0)
- goto cleanup2;
- if (fmttype & TYP_PAX && paxrec != PR_NONE)
- wrpax(file, symblink?symblink:linkname, st);
- bwrite(bc.data, hsz);
- if (fsz)
- bwrite(file, fsz);
- if (psz)
- bwrite(&bc.data[hsz], psz);
- if (Kbase)
- remsz = Kbase;
- else
- remsz = st->st_size;
- getbuf(&buf, &bufsize, st->st_blksize);
- again: while (remsz > 0) {
- if (fd < 0 || (rsz = read(fd, &buf[wsz],
- bufsize - wsz)) < 0) {
- if (readerr == 0) {
- emsg(3, "Cannot read \"%s\"", realfile);
- if (fd >= 0)
- errcnt++;
- readerr = 1;
- }
- if (fd >= 0 && lseek(fd, bufsize - wsz,
- SEEK_CUR) < 0) {
- close(fd);
- fd = -1;
- }
- rsz = bufsize - wsz;
- if (rsz > remsz)
- rsz = remsz;
- memset(&buf[wsz], 0, rsz);
- }
- if (rsz > remsz)
- rsz = remsz;
- wsz += rsz;
- remsz -= rsz;
- bwrite(buf, wsz);
- memset(buf, 0, wsz);
- size = wsz;
- wsz = 0;
- }
- wsz = size;
- if (Kbase) {
- if ((i = Ksize % pad) != 0)
- bwrite(&bc.data[hsz], i);
- bwrite((char *)&K2hdr, hsz);
- if (fsz)
- bwrite(file, fsz);
- if (psz)
- bwrite(&bc.data[hsz], psz);
- remsz = Krest;
- Kbase = 0;
- wsz = 0;
- goto again;
- } else if (Ksize)
- wsz = Krest;
- } else if ((fmttype == FMT_ZIP || fmttype & TYP_CPIO) &&
- (st->st_mode&S_IFMT) == S_IFLNK) {
- wsz = size;
- if (fmttype == FMT_ZIP) {
- crc = zipcrc(0, (unsigned char *)symblink, wsz);
- le32p(crc, bc.Zdr.z_crc32);
- bwrite(bc.data, SIZEOF_zip_header);
- bwrite(file, fsz);
- zipwxtra(file, st, dev, ino);
- bwrite(symblink, wsz);
- } else {
- bwrite(bc.data, hsz);
- if (fsz)
- bwrite(file, fsz);
- if (psz)
- bwrite(&bc.data[hsz], psz);
- bwrite(symblink, wsz);
- }
- } else {
- if (fmttype & TYP_PAX && paxrec != PR_NONE)
- wrpax(file, symblink?symblink:linkname, st);
- bwrite(bc.data, hsz);
- if (fsz)
- bwrite(file, fsz);
- if (psz)
- bwrite(&bc.data[hsz], psz);
- if (fmttype == FMT_ZIP)
- zipwxtra(file, st, dev, ino);
- }
- done: if (fmttype == FMT_ZIP) {
- zipdefer(file, st, relative, crc, csize, &bc.Zdr);
- }
- if ((i = wsz % pad) != 0)
- bwrite(&bc.data[hsz], pad - i);
- if (vflag && strcmp(file, trailer))
- fprintf(stderr, "%s\n", file);
- else if (Vflag)
- prdot(0);
- failure = 0;
- cleanup2:
- if ((st->st_mode&S_IFMT) == S_IFREG) {
- if (fd >= 0)
- close(fd);
- if (aflag)
- errcnt += rstime(realfile, st, "access");
- }
- cleanup:
- free(file);
- free(symblink);
- return failure;
- }
- /*
- * Flush a SVR4 cpio format inode tree for -o.
- */
- static void
- iflush(struct islot *ip, uint32_t dev)
- {
- if (ip == inull)
- return;
- iflush(ip->i_lln, dev);
- iflush(ip->i_rln, dev);
- if (ip->i_nlk > 0 && ip->i_st) {
- struct ilink *il;
- for (il = ip->i_lnk; il->l_nxt; il = il->l_nxt)
- errcnt += addfile(il->l_nam, ip->i_st,
- dev, ip->i_fino, 1, 0);
- errcnt += addfile(il->l_nam, ip->i_st, dev, ip->i_fino, 0, 0);
- }
- }
- /*
- * Flush the SVR4 cpio link forest for -o.
- */
- static void
- lflush(void)
- {
- struct dslot *ds;
- for (ds = devices; ds; ds = ds->d_nxt)
- iflush(ds->d_isl, ds->d_fake);
- }
- int
- setfmt(char *s)
- {
- int i, j;
- struct {
- const char *ucs;
- const char *lcs;
- int type;
- int bits;
- } fs[] = {
- { "NEWC", "newc", FMT_ASC, 00 },
- { "SCO", "sco", FMT_SCOASC, 00 },
- { "CRC", "crc", FMT_CRC, 00 },
- { "SCOCRC", "scocrc", FMT_SCOCRC, 00 },
- { "ODC", "odc", FMT_ODC, 00 },
- { "DEC", "dec", FMT_DEC, 00 },
- { "BIN", "bin", FMT_NONE, 00 },
- { "BBS", "bbs", TYP_BE, 00 },
- { "SGI", "sgi", FMT_SGIBE, 00 },
- { "CRAY", "cray", FMT_CRAY, 00 },
- { "CRAY5", "cray5", FMT_CRAY5, 00 },
- { "TAR", "tar", FMT_TAR, 00 },
- { "USTAR", "ustar", FMT_USTAR, 00 },
- { "PAX:", "pax:", FMT_PAX, 00 },
- { "SUN", "sun", FMT_SUN, 00 },
- { "GNU", "gnu", FMT_GNUTAR, 00 },
- { "OTAR", "otar", FMT_OTAR, 00 },
- { "BAR", "bar", FMT_BAR, 00 },
- { "ZIP:", "zip:", FMT_ZIP, 00 },
- { NULL, NULL, 0, 00 }
- };
- for (i = 0; fs[i].ucs; i++) {
- for (j = 0; s[j] &&
- (s[j] == fs[i].ucs[j] || s[j] == fs[i].lcs[j]);
- j++)
- if (fs[i].ucs[j] == ':')
- break;
- if (s[j] == '\0' &&
- (fs[i].ucs[j] == '\0' || fs[i].ucs[j] == ':') ||
- s[j] == ':' && fs[i].ucs[j] == ':') {
- fmttype = fs[i].type;
- if (fmttype == FMT_ZIP && s[j] == ':') {
- #if USE_ZLIB
- if (strcmp(&s[j+1], "en") == 0)
- zipclevel = 00;
- else if (strcmp(&s[j+1], "ex") == 0)
- zipclevel = 01;
- else if (strcmp(&s[j+1], "ef") == 0)
- zipclevel = 02;
- else if (strcmp(&s[j+1], "es") == 0)
- zipclevel = 03;
- else
- #endif /* USE_ZLIB */
- if (strcmp(&s[j+1], "e0") == 0)
- zipclevel = 04;
- else
- #if USE_BZLIB
- if (strcmp(&s[j+1], "bz2") == 0)
- zipclevel = 07;
- else
- #endif /* USE_BZLIB */
- continue;
- } else if (fmttype == FMT_NONE)
- fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
- else if (fmttype == TYP_BE)
- fmttype = bigendian() ? FMT_BINLE : FMT_BINBE;
- else if (fmttype == FMT_PAX && s[j] == ':') {
- if (pax_options(&s[j+1], 0) < 0)
- continue;
- }
- return 0;
- }
- }
- msg(3, 0, "Invalid header \"%s\" specified.\n", s);
- return -1;
- }
- static int
- bigendian(void)
- {
- union {
- char u_c[2];
- int16_t u_i;
- } u;
- u.u_i = 1;
- return u.u_c[1] == 1;
- }
- int
- setreassign(const char *s)
- {
- struct passwd *pwd;
- int val = 0;
- if (myuid != 0) {
- msg(3, 0, "R option only valid for super-user.\n");
- val = -1;
- }
- if ((pwd = getpwnam(s)) == NULL) {
- msg(3, 0, "\"%s\" is not a valid user id\n", s);
- val = -1;
- } else {
- Ruid = pwd->pw_uid;
- Rgid = pwd->pw_gid;
- }
- return val;
- }
- void *
- srealloc(void *m, size_t n)
- {
- if ((m = realloc(m, n)) == NULL) {
- write(2, "Out of memory.\n", 15);
- _exit(sysv3 ? 2 : 3);
- }
- return m;
- }
- void *
- smalloc(size_t n)
- {
- return srealloc(NULL, n);
- }
- void *
- scalloc(size_t nmemb, size_t size)
- {
- void *vp;
- if ((vp = calloc(nmemb, size)) == NULL) {
- write(2, "Out of memory.\n", 15);
- _exit(sysv3 ? 2 : 3);
- }
- return vp;
- }
- void *
- svalloc(size_t n, int force)
- {
- static long pagesize;
- void *vp;
- if (pagesize == 0)
- if ((pagesize = sysconf(_SC_PAGESIZE)) < 0)
- pagesize = 4096;
- if ((vp = memalign(pagesize, n)) == NULL && force) {
- write(2, "Out of memory.\n", 15);
- _exit(sysv3 ? 2 : 3);
- }
- return vp;
- }
- /*
- * A single static buffer is used for intermediate copying from file
- * data to the tape buffer and vice versa, for creating checksums, and
- * for data transfer with -p.
- */
- static void
- getbuf(char **bufp, size_t *sizep, size_t best)
- {
- static char *buf;
- static size_t size;
- if (size != best) {
- if (buf)
- free(buf);
- size = best;
- if (size == 0 || (buf = svalloc(size, 0)) == NULL)
- buf = svalloc(size = 512, 1);
- }
- *bufp = buf;
- *sizep = size;
- }
- static void
- sevprnt(int sev)
- {
- if (printsev) switch (sev) {
- case 1:
- fprintf(stderr, "INFORM: ");
- break;
- case 2:
- fprintf(stderr, "WARNING: ");
- break;
- case 3:
- fprintf(stderr, "ERROR: ");
- break;
- case 4:
- fprintf(stderr, "HALT: ");
- break;
- }
- }
- void
- msg(int sev, int err, const char *fmt, ...)
- {
- va_list ap;
- /*
- * The error message should appear near the file it refers to.
- */
- if (tflag)
- fflush(stdout);
- else if (Vflag)
- prdot(1);
- if (sysv3 >= 0 && sev >= (printsev ? 0 : -1))
- fprintf(stderr, "%s: ", progname);
- sevprnt(sev);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- if (err > 0)
- done(err);
- else if (err == -2) {
- if (sysv3)
- done(1);
- usage();
- }
- }
- void
- emsg(int sev, const char *fmt, ...)
- {
- char _fmt[60];
- int i, fl = sev & 030, n;
- va_list ap;
- sev &= ~030;
- i = errno;
- if (tflag)
- fflush(stdout);
- else if (Vflag)
- prdot(1);
- fprintf(stderr, "%s: ", progname);
- sevprnt(sev);
- va_start(ap, fmt);
- if (sysv3) {
- if (fmt[(n=strlen(fmt))-1] == '"' && fmt[n-2] == 's' &&
- fmt[n-3] == '%' && fmt[n-4] == '"' &&
- n < sizeof _fmt) {
- strcpy(_fmt, fmt);
- _fmt[n-1] = '>';
- _fmt[n-4] = '<';
- fmt = _fmt;
- }
- }
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- if (sysv3 > 0 && sev >= 0 && fl & 010)
- fprintf(stderr, "\n\t%s\n", strerror(i));
- else if (sysv3 < 0) {
- if (fl & 020)
- putc('\n', stderr);
- else
- fprintf(stderr, " (errno:%d)\n", i);
- } else
- fprintf(stderr, ", errno %d, %s\n", i, strerror(i));
- }
- static void
- prdot(int flush)
- {
- static int column;
- if (flush && column != 0 || column >= 50) {
- write(action == 'o' && !Oflag ? 2 : 1, "\n", 1);
- column = 0;
- }
- if (!flush) {
- write(action == 'o' && !Oflag ? 2 : 1, ".", 1);
- column++;
- }
- }
- /*
- * Ask the user for new media if applicable, or exit.
- */
- static void
- newmedia(int err)
- {
- static int mediacnt = 1;
- static char answer[PATH_MAX+1];
- const char *mesf = action == 'i' ?
- (Iflag && !sysv3 ? Iflag : "input") :
- (Oflag && !sysv3 ? Oflag : "output");
- char c;
- int i, j;
- if (mfl == 0 && close(mt) < 0) {
- emsg(3, "Close error on \"%s\"", mesf);
- errcnt++;
- }
- mfl = -1;
- if ((mtst.st_mode&S_IFMT)!=S_IFCHR && (mtst.st_mode&S_IFMT)!=S_IFBLK ||
- Dflag) {
- if (action == 'o') {
- switch (err) {
- case 0:
- break;
- case EFBIG:
- msg(3, 0, "ulimit reached for output file.\n");
- break;
- case ENOSPC:
- msg(3, 0, "No space left for output file.\n");
- break;
- default:
- msg(3, 0, "I/O error - cannot continue, "
- "errno %d, %s\n",
- err, strerror(err));
- }
- }
- return;
- }
- if (err == ENOSPC || err == ENXIO || err == 0)
- msg(-2, 0, sysv3 ? "Reached end of medium on %s.\a\n" :
- "End of medium on \"%s\".\a\n", mesf);
- else
- msg(3, 0, "I/O error on \"%s\", errno %d, %s\a\n", mesf,
- err, strerror(err));
- mediacnt++;
- while (mfl < 0) {
- if (Iflag || Oflag)
- msg(-2, 0, Mflag ? Mflag :
- "Change to part %d and press "
- "RETURN key. [q] ", mediacnt);
- else
- msg(-2, 0, sysv3 ? "If you want to go on, "
- "type device/file name when ready.\n" :
- "To continue, type device/file name "
- "when ready.\n");
- if (tty == 0)
- if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
- fcntl(tty, F_SETFD, FD_CLOEXEC) < 0) {
- cantrt: errcnt++;
- msg(4, 1, "Cannot read tty.\n");
- }
- i = 0;
- while ((j = read(tty, &c, 1)) == 1 && c != '\n')
- if (i < sizeof answer - 1)
- answer[i++] = c;
- if (j != 1)
- goto cantrt;
- answer[i] = 0;
- if (Iflag || Oflag) {
- if (answer[0] == '\0')
- snprintf(answer, sizeof answer, Iflag ? Iflag :
- Oflag);
- else if (answer[0] == 'q')
- exit(errcnt != 0 ? sysv3 ? 1 : 2 : 0);
- else if (Iflag)
- Iflag = sstrdup(answer);
- else if (Oflag)
- Oflag = sstrdup(answer);
- } else if (answer[0] == '\0')
- return;
- if ((mt = action == 'i' ? open(answer, O_RDONLY) :
- creat(answer, 0666)) < 0) {
- if (sysv3)
- msg(-2, 0, "That didn't work, "
- "cannot open \"%s\"\n%s\n",
- answer, strerror(errno));
- else
- emsg(3, "Cannot open \"%s\"", answer);
- }
- else
- mfl = 0;
- }
- mstat();
- }
- static void
- mclose(void)
- {
- if (action == 'o' && mt >= 0 && close(mt) < 0) {
- emsg(3, "Close error on \"%s\"",
- Oflag && !sysv3 ? Oflag : "output");
- errcnt++;
- }
- }
- /*
- * Write the archive buffer to tape.
- */
- static ssize_t
- mwrite(int max)
- {
- ssize_t wo, wt = 0;
- do {
- if ((wo = write(mt, blkbuf + wt, (max?max:blksiz) - wt)) < 0) {
- if (errno == EINTR) {
- continue;
- } else {
- newmedia(errno);
- if (mfl == 0) {
- if (fmttype & TYP_BAR)
- dump_barhdr();
- continue;
- }
- else
- done(1);
- }
- }
- poffs += wo;
- wt += wo;
- } while (wt < (max?max:blksiz));
- blocks += wt >> 9;
- bytes += wt & 0777;
- return wt;
- }
- /*
- * Buffered writes to tape.
- */
- static void
- bwrite(const char *data, size_t sz)
- {
- size_t di;
- nwritten += sz;
- while (curpos + sz > blksiz) {
- di = blksiz - curpos;
- sz -= di;
- memcpy(&blkbuf[curpos], data, di);
- mwrite(0);
- data += di;
- curpos = 0;
- }
- memcpy(&blkbuf[curpos], data, sz);
- curpos += sz;
- }
- /*
- * Flush the tape write buffer.
- */
- static void
- bflush(void)
- {
- if (curpos) {
- memset(&blkbuf[curpos], 0, blksiz - curpos);
- mwrite(fmttype==FMT_ZIP && (mtst.st_mode&S_IFMT) == S_IFREG ?
- curpos : 0);
- }
- curpos = 0;
- }
- /*
- * CRC format checksum calculation with -i.
- */
- static int
- sum(int fd, const char *fn, struct stat *sp, char *tg)
- {
- char *buf;
- size_t bufsize;
- uint32_t size = sp->st_size, sum = 0;
- ssize_t rd;
- char c;
- getbuf(&buf, &bufsize, sp->st_blksize);
- /*
- * Unfortunately, SVR4 cpio derivatives (as on Solaris 8 and
- * UnixWare 2.1) compute the checksum of signed char values,
- * whereas GNU cpio and the pax implementations of AT&T and
- * BSD use unsigned chars. Since there is no 'open' standard
- * for the SVR4 CRC format, the SVR4 implementation should be
- * taken as a de facto reference and we thus create SVR4 type
- * checksums.
- */
- while ((rd = read(fd, buf, size>bufsize ? bufsize : size )) > 0) {
- size -= rd;
- do
- sum += ((signed char *)buf)[--rd];
- while (rd);
- }
- if (rd < 0) {
- msg(3, 0, "Error computing checksum\n");
- return 1;
- }
- if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
- emsg(3, "Cannot reset file after checksum");
- return 1;
- }
- c = tg[8];
- sprintf(tg, "%08lx", (long)sum);
- tg[8] = c;
- return 0;
- }
- static int
- rstime(const char *fn, struct stat *st, const char *which)
- {
- struct utimbuf utb;
- utb.actime = st->st_atime;
- utb.modtime = st->st_mtime;
- if (pax != PAX_TYPE_CPIO &&
- (pax_preserve&(PAX_P_ATIME|PAX_P_MTIME)) != 0 &&
- (pax_preserve&PAX_P_EVERY) == 0) {
- struct stat xst;
- if (stat(fn, &xst) < 0)
- goto fail;
- if (pax_preserve&PAX_P_ATIME)
- utb.actime = xst.st_atime;
- if (pax_preserve&PAX_P_MTIME)
- utb.modtime = xst.st_mtime;
- }
- if (utime(fn, &utb) < 0) {
- fail: emsg(2, "Unable to reset %s time for \"%s\"", which, fn);
- return 1;
- }
- return 0;
- }
- /*
- * Top-down splay function for inode tree.
- */
- static struct islot *
- isplay(ino_t ino, struct islot *x)
- {
- struct islot hdr;
- struct islot *leftmax, *rightmin;
- struct islot *y;
- hdr.i_lln = hdr.i_rln = inull;
- leftmax = rightmin = &hdr;
- inull->i_ino = ino;
- while (ino != x->i_ino) {
- if (ino < x->i_ino) {
- if (ino < x->i_lln->i_ino) {
- y = x->i_lln;
- x->i_lln = y->i_rln;
- y->i_rln = x;
- x = y;
- }
- if (x->i_lln == inull)
- break;
- rightmin->i_lln = x;
- rightmin = x;
- x = x->i_lln;
- } else {
- if (ino > x->i_rln->i_ino) {
- y = x->i_rln;
- x->i_rln = y->i_lln;
- y->i_lln = x;
- x = y;
- }
- if (x->i_rln == inull)
- break;
- leftmax->i_rln = x;
- leftmax = x;
- x = x->i_rln;
- }
- }
- leftmax->i_rln = x->i_lln;
- rightmin->i_lln = x->i_rln;
- x->i_lln = hdr.i_rln;
- x->i_rln = hdr.i_lln;
- inull->i_ino = !ino;
- return x;
- }
- /*
- * Find the inode number ino.
- */
- static struct islot *
- ifind(ino_t ino, struct islot **it)
- {
- if (*it == NULL)
- return NULL;
- *it = isplay(ino, *it);
- return (*it)->i_ino == ino ? *it : NULL;
- }
- /*
- * Put ik into the tree.
- */
- static void
- iput(struct islot *ik, struct islot **it)
- {
- if ((*it) == NULL) {
- ik->i_lln = ik->i_rln = inull;
- (*it) = ik;
- } else {
- /* ifind() is always called before */
- /*(*it) = isplay(ik->i_ino, (*it));*/
- if (ik->i_ino < (*it)->i_ino) {
- ik->i_lln = (*it)->i_lln;
- ik->i_rln = (*it);
- (*it)->i_lln = inull;
- (*it) = ik;
- } else if ((*it)->i_ino < ik->i_ino) {
- ik->i_rln = (*it)->i_rln;
- ik->i_lln = (*it);
- (*it)->i_rln = inull;
- (*it) = ik;
- }
- }
- }
- /*
- * Find the device dev or add it to the device/inode forest if not
- * already present.
- */
- static struct dslot *
- dfind(struct dslot **root, dev_t dev)
- {
- struct dslot *ds, *dp;
- for (ds = *root, dp = NULL; ds; dp = ds, ds = ds->d_nxt)
- if (ds->d_dev == dev)
- break;
- if (ds == NULL) {
- ds = scalloc(1, sizeof *ds);
- ds->d_dev = dev;
- if (*root == NULL)
- *root = ds;
- else
- dp->d_nxt = ds;
- }
- return ds;
- }
- /*
- * Exit on fatal error conditions.
- */
- static void
- done(int i)
- {
- if (tflag)
- fflush(stdout);
- errcnt += i;
- mclose();
- if (errcnt && !sysv3)
- fprintf(stderr, "%llu errors\n", errcnt);
- exit(sysv3 ? 2 : 3);
- }
- static char *pcopy, *pcend;
- static size_t psz, pslen, pss;
- /*
- * Execution for -p.
- */
- static void
- dopass(const char *target)
- {
- struct stat st;
- if (access(target, W_OK) < 0) {
- emsg(033, sysv3 ? "cannot write in <%s>" :
- "Error during access() of \"%s\"", target);
- if (sysv3)
- done(1);
- usage();
- }
- if (stat(target, &st) < 0) {
- emsg(023, "Error during stat() of \"%s\"", target);
- done(1);
- }
- if ((st.st_mode&S_IFMT) != S_IFDIR)
- msg(3, 1, sysv3 ? "<%s> not a directory.\n" :
- "\"%s\" is not a directory\n", target);
- getpath(target, &pcopy, &pcend, &psz, &pslen);
- copyout(passfile);
- }
- /*
- * Callback for sfile().
- */
- /*ARGSUSED*/
- void
- writerr(void *vp, int count, int written)
- {
- }
- /*
- * Copy file data of regular files with -p.
- */
- static int
- passdata(struct file *f, const char *tgt, int tfd)
- {
- char *buf;
- size_t bufsize;
- ssize_t rd = 0;
- if (f->f_fd < 0) /* is a zero-sized unreadable file */
- return 0;
- #ifdef __linux__
- if (f->f_st.st_size > 0) {
- long long sent;
- sent = sfile(tfd, f->f_fd, f->f_st.st_mode, f->f_st.st_size);
- blocks += (sent + 0777) >> 9;
- if (sent == f->f_st.st_size)
- return 0;
- if (sent < 0)
- goto rerr;
- }
- #endif /* __linux__ */
- getbuf(&buf, &bufsize, f->f_st.st_blksize);
- while ((rd = read(f->f_fd, buf, bufsize)) > 0) {
- blocks += (rd + 0777) >> 9;
- if (write(tfd, buf, rd) != rd) {
- emsg(3, "Cannot write \"%s\"", tgt);
- return -1;
- }
- }
- if (rd < 0) {
- #ifdef __linux__
- rerr:
- #endif /* __linux__ */
- emsg(3, "Cannot read \"%s\"", f->f_name);
- return -1;
- }
- return 0;
- }
- /*
- * Handle a single file for -p.
- */
- static int
- passfile(const char *fn, struct stat *st)
- {
- struct file f;
- ssize_t sz;
- int val;
- char *newfn;
- size_t newsz = 0;
- newfn = sstrdup(fn);
- if (pax != PAX_TYPE_CPIO) {
- if (pax_sflag && pax_sname(&newfn, &newsz) == 0)
- return 0;
- if (rflag && rname(&newfn, &newsz) == 0)
- return 0;
- }
- setpath(newfn, &pcopy, &pcend, pslen, &psz, &pss);
- free(newfn);
- memset(&f, 0, sizeof f);
- f.f_name = sstrdup(fn);
- f.f_nsiz = strlen(fn) + 1;
- f.f_st = *st;
- f.f_fd = -1;
- if ((st->st_mode&S_IFMT) == S_IFLNK) {
- sz = st->st_size ? st->st_size : PATH_MAX;
- f.f_lnam = smalloc(sz+1);
- if ((sz = readlink(fn, f.f_lnam, sz+1)) < 0) {
- emsg(3, "Cannot read symbolic link \"%s\"", fn);
- free(f.f_lnam);
- return 1;
- }
- f.f_lnam[sz] = '\0';
- } else if ((st->st_mode&S_IFMT) == S_IFREG) {
- if ((f.f_fd = open(fn, O_RDONLY)) < 0) {
- if (sysv3)
- msg(2, 0, "Cannot open file \"%s\".\n", fn);
- else
- emsg(2, "Cannot open \"%s\", skipped", fn);
- if (st->st_size != 0)
- return 1;
- }
- }
- val = filein(&f, passdata, pcopy);
- if (f.f_lnam)
- free(f.f_lnam);
- free(f.f_name);
- if (f.f_fd >= 0)
- close(f.f_fd);
- if (val <= 1 && aflag && (st->st_mode&S_IFMT) == S_IFREG)
- errcnt += rstime(fn, st, "access");
- return val != 0;
- }
- /*
- * Processing of a single file common to -i and -p. Return value: 0 if
- * successful, 1 at failure if data was read, 2 at failure if no data
- * was read.
- */
- static int
- filein(struct file *f, int (*copydata)(struct file *, const char *, int),
- char *tgt)
- {
- struct stat nst;
- char *temp = NULL;
- size_t len;
- int fd, i, j, new;
- int failure = 2;
- if (fmttype == FMT_ZIP && (f->f_st.st_mode&S_IFMT) != S_IFREG &&
- (f->f_st.st_mode&S_IFMT) != S_IFLNK &&
- (f->f_csize > 0 || f->f_gflag & FG_DESC))
- skipfile(f);
- if ((new = lstat(tgt, &nst)) == 0) {
- if (action == 'p' && f->f_st.st_dev == nst.st_dev &&
- f->f_st.st_ino == nst.st_ino) {
- msg(3, 0, sysv3 ?
- "Attempt to pass file to self!\n" :
- "Attempt to pass a file to itself.\n");
- return 1;
- }
- if ((f->f_st.st_mode&S_IFMT) == S_IFDIR) {
- if ((nst.st_mode&S_IFMT) == S_IFDIR)
- return setattr(tgt, &f->f_st);
- rmdir(tgt);
- } else {
- if (pax_kflag) {
- failure = 0;
- goto skip;
- }
- if (uflag == 0 && f->f_st.st_mtime <= nst.st_mtime) {
- if (pax == PAX_TYPE_CPIO)
- msg(-1, 0, sysv3 ?
- "current <%s> newer or same age\n" :
- "Existing \"%s\" same age or newer\n",
- tgt);
- else
- failure = 0;
- goto skip;
- }
- }
- } else {
- if (imdir(tgt) < 0)
- goto skip;
- }
- if (Vflag && !vflag)
- prdot(0);
- if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && lflag) {
- if (Lflag) {
- char *symblink, *name;
- struct stat xst;
- name = f->f_name;
- for (;;) {
- if (lstat(name, &xst) < 0) {
- emsg(3, "Cannot lstat \"%s\"", name);
- if (name != f->f_name)
- free(name);
- goto cantlink;
- }
- if ((xst.st_mode&S_IFMT) != S_IFLNK)
- break;
- i = xst.st_size ? xst.st_size : PATH_MAX;
- symblink = smalloc(i+1);
- if ((j = readlink(name, symblink, i)) < 0) {
- emsg(3, "Cannot read symbolic link "
- "\"%s\"", name);
- free(symblink);
- if (name != f->f_name)
- free(name);
- goto cantlink;
- }
- symblink[j] = '\0';
- symblink = joinpath(name, symblink);
- if (name != f->f_name)
- free(name);
- name = symblink;
- }
- if (linkunlink(name, tgt) == 0) {
- if (vflag)
- fprintf(stderr, "%s\n", tgt);
- if (name != f->f_name)
- free(name);
- return 0;
- }
- if (name != f->f_name)
- free(name);
- } else if (linkunlink(f->f_name, tgt) == 0) {
- if (vflag)
- fprintf(stderr, "%s\n", tgt);
- return 0;
- }
- cantlink: errcnt += 1;
- }
- if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && f->f_st.st_nlink > 1 &&
- (fmttype & TYP_CPIO || fmttype == FMT_ZIP
- || action == 'p') &&
- (i = canlink(tgt, &f->f_st, 1)) != 0) {
- if (i < 0)
- goto skip;
- /*
- * At this point, hard links in SVR4 cpio format have
- * been reordered and data is associated with the first
- * link; remaining links have st_size == 0 so don't
- * overwrite the data here.
- */
- if (fmttype & TYP_NCPIO && f->f_st.st_size == 0 ||
- (f->f_st.st_mode&S_IFMT) != S_IFREG) {
- if (vflag)
- fprintf(stderr, "%s\n", f->f_name);
- return 0;
- }
- /*
- * Make sure we can creat() this file later.
- */
- chmod(tgt, 0600);
- } else if (fmttype & TYP_TAR && f->f_st.st_nlink > 1) {
- if (linkunlink(f->f_lnam, f->f_name) == 0) {
- if (fmttype & TYP_USTAR && f->f_st.st_size > 0)
- chmod(tgt, 0600);
- else {
- if (vflag)
- fprintf(stderr, "%s\n", f->f_name);
- return 0;
- }
- } else {
- goto restore;
- }
- } else if (new == 0 && (f->f_st.st_mode&S_IFMT) != S_IFDIR) {
- len = strlen(tgt);
- temp = smalloc(len + 7);
- strcpy(temp, tgt);
- strcpy(&temp[len], "XXXXXX");
- if ((fd = mkstemp(temp)) < 0 || close(fd) < 0) {
- emsg(3, "Cannot create temporary file");
- if (fd < 0) {
- free(temp);
- temp = NULL;
- }
- goto skip;
- }
- cur_ofile = tgt;
- cur_tfile = temp;
- if (rename(tgt, temp) < 0) {
- emsg(3, "Cannot rename current \"%s\"", tgt);
- tunlink(&temp);
- goto skip;
- }
- }
- switch (f->f_st.st_mode & S_IFMT) {
- case S_IFDIR:
- if (!dflag) {
- if (action == 'p')
- msg(-1, 0, "Use -d option to copy \"%s\"\n",
- f->f_name);
- goto restore;
- }
- if (mkdir(tgt, 0777) < 0 && errno != EEXIST) {
- emsg(-1, "Cannot create directory \"%s\"", tgt);
- goto restore;
- }
- break;
- case S_IFLNK:
- if (symlink(f->f_lnam, tgt) < 0) {
- emsg(3, "Cannot create \"%s\"", tgt);
- goto restore;
- }
- break;
- case S_IFREG:
- if (temp && f->f_fd < 0)
- goto restore;
- cur_ofile = tgt;
- if ((fd = (compressed_bar ? zcreat : creat)(tgt,
- f->f_st.st_mode & 0777)) < 0) {
- emsg(3, "Cannot create \"%s\"", tgt);
- goto skip;
- }
- failure = 1;
- if (copydata(f, tgt, fd) != 0) {
- close(fd);
- goto restore;
- }
- if ((compressed_bar ? zclose : close)(fd) < 0) {
- emsg(3, "Close error on \"%s\"", tgt);
- goto restore;
- }
- break;
- case S_IFBLK:
- case S_IFCHR:
- case S_IFIFO:
- case S_IFNAM:
- case S_IFNWK:
- if (mknod(tgt, f->f_st.st_mode&(S_IFMT|0777),
- f->f_st.st_rdev) < 0) {
- emsg(3, "Cannot mknod() \"%s\"", tgt);
- goto restore;
- }
- break;
- default:
- msg(-1, 0, "Impossible file type\n");
- goto skip;
- }
- if (vflag)
- fprintf(stderr, "%s\n", f->f_name);
- tunlink(&temp);
- return setattr(tgt, &f->f_st);
- skip: if (copydata == indata)
- skipdata(f, copydata);
- restore:
- if (temp) {
- if (rename(temp, tgt) < 0) {
- emsg(3, "Cannot recover original version of \"%s\"",
- tgt);
- tunlink(&temp);
- } else
- fprintf(stderr, "Restoring existing \"%s\"\n", tgt);
- cur_tfile = cur_ofile = NULL;
- }
- return failure;
- }
- static int
- linkunlink(const char *path1, const char *path2)
- {
- int twice = 0;
- do {
- if (link(path1, path2) == 0) {
- if (vflag && pax == PAX_TYPE_CPIO)
- printf("%s linked to %s\n", path1, path2);
- return 0;
- }
- if (errno == EEXIST && unlink(path2) < 0)
- emsg(3, sysv3 ? "cannot unlink <%s>" :
- "Error cannot unlink \"%s\"", path2);
- } while (twice++ == 0);
- emsg(023, sysv3 ? "Cannot link <%s> & <%s>" :
- "Cannot link \"%s\" and \"%s\"", path1, path2);
- return -1;
- }
- static void
- tunlink(char **fn)
- {
- cur_tfile = cur_ofile = NULL;
- if (*fn) {
- if (unlink(*fn) < 0)
- emsg(3, "Cannot unlink() temp file \"%s\"", *fn);
- free(*fn);
- *fn = NULL;
- }
- }
- /*
- * For -it[v] option.
- */
- static int
- filet(struct file *f, int (*copydata)(struct file *, const char *, int))
- {
- if (vflag && (
- (fmttype == FMT_ZIP && f->f_gflag & FG_DESC &&
- (f->f_cmethod == C_DEFLATED || f->f_cmethod == C_ENHDEFLD) &&
- (f->f_gflag & FG_CRYPT) == 0) ||
- f->f_Kbase)) {
- /*
- * Need to read zip data descriptor located after the
- * file contents in order to know the file size. Or
- * need to read second header of -K archive.
- */
- int i;
- i = skipdata(f, copydata);
- filev(f);
- return i;
- }
- if (vflag)
- filev(f);
- else
- puts(f->f_name);
- return skipdata(f, copydata);
- }
- static void
- filev(struct file *f)
- {
- const char *cp;
- long c;
- if (pax == PAX_TYPE_CPIO && fmttype & TYP_TAR && f->f_st.st_nlink > 1)
- printf("%s linked to %s\n", f->f_lnam, f->f_name);
- if (sysv3)
- printf("%-6o", (int)f->f_st.st_mode&(07777|S_IFMT));
- else {
- c = typec(&f->f_st);
- putchar(c);
- permbits(f->f_st.st_mode);
- }
- if (fmttype == FMT_ZIP && vflag > 1)
- zipinfo(f);
- else {
- if (sysv3 == 0)
- printf(" %3d", fmttype&TYP_TAR &&
- (f->f_st.st_mode&S_IFMT) == S_IFLNK ?
- 2 : (int)f->f_st.st_nlink);
- if ((cp = getuser(f->f_st.st_uid)) != NULL)
- printf(sysv3 ? " %-6s" : " %-8s", cp);
- else
- printf(sysv3 ? " %-6lu" : " %-8lu",
- (long)f->f_st.st_uid);
- if (sysv3 == 0) {
- if ((cp = getgroup(f->f_st.st_gid)) != NULL)
- printf(" %-8s", cp);
- else
- printf(" %-8lu", (long)f->f_st.st_gid);
- }
- }
- if (sysv3 || (f->f_st.st_mode&S_IFMT)!=S_IFCHR &&
- (f->f_st.st_mode&S_IFMT)!=S_IFBLK &&
- (f->f_st.st_mode&S_IFMT)!=S_IFNAM &&
- (f->f_st.st_mode&S_IFMT)!=S_IFNWK)
- printf(pax != PAX_TYPE_CPIO ? "%8llu" :
- sysv3 ? "%7llu" : " %-7llu", f->f_dsize);
- else
- printf(" %3lu,%3lu", (long)f->f_rmajor, (long)f->f_rminor);
- prtime(f->f_st.st_mtime);
- printf("%s", f->f_name);
- if ((f->f_st.st_mode&S_IFMT) == S_IFLNK)
- printf(" -> %s", f->f_lnam);
- if (pax != PAX_TYPE_CPIO && (f->f_st.st_mode&S_IFMT) != S_IFDIR &&
- f->f_st.st_nlink>1) {
- if (fmttype & TYP_TAR)
- printf(" == %s", f->f_lnam);
- else
- canlink(f->f_name, &f->f_st, 0);
- }
- putchar('\n');
- }
- static int
- typec(struct stat *st)
- {
- switch (st->st_mode&S_IFMT) {
- case S_IFREG:
- return '-';
- case S_IFDIR:
- return 'd';
- case S_IFLNK:
- return 'l';
- case S_IFCHR:
- return 'c';
- case S_IFBLK:
- return 'b';
- case S_IFIFO:
- return 'p';
- case S_IFNWK:
- return 'n';
- case S_IFNAM:
- switch (st->st_rdev) {
- case S_INSEM:
- return 's';
- case S_INSHD:
- return 'm';
- }
- /*FALLTHRU*/
- default:
- msg(-1, 0, "Impossible file type\n");
- errcnt++;
- return '-';
- }
- }
- static void
- permbits(mode_t mode)
- {
- mode_t mask = 0700, shft = 6;
- while (mask) {
- if (((mode & mask) >> shft) & 04)
- putchar('r');
- else
- putchar('-');
- if (((mode & mask) >> shft) & 02)
- putchar('w');
- else
- putchar('-');
- if (mask == 0700 && mode & 04000)
- putchar('s');
- else if (mask == 0070 && (mode & 02010) == 02010)
- putchar('s');
- else if (mask == 0070 && mode & 02000)
- putchar('l');
- else if (mask == 0007 && mode & 01000)
- putchar('t');
- else if (((mode & mask) >> shft) & 01)
- putchar('x');
- else
- putchar('-');
- mask >>= 3;
- shft -= 3;
- }
- }
- static void
- prtime_cpio(time_t t)
- {
- char b[30];
- strftime(b, sizeof b, sysv3 ? "%b %e %H:%M:%S %Y" : "%b %e %H:%M %Y",
- localtime(&t));
- printf(sysv3 ? " %s " : " %s, ", b);
- }
- /*
- * Prepare path to add names of files below it later.
- */
- static void
- getpath(const char *path, char **file, char **filend, size_t *sz, size_t *slen)
- {
- *sz = 14 + strlen(path) + 2;
- *file = smalloc(*sz);
- *filend = *file;
- if (path[0] == '/' && path[1] == '\0')
- *(*filend)++ = '/';
- else {
- const char *cp = path;
- while ((*(*filend)++ = *cp++) != '\0');
- (*filend)[-1] = '/';
- }
- *slen = *filend - *file;
- }
- /*
- * Concatenate base (prepared with getpath()) and file.
- */
- static void
- setpath(const char *base, char **file, char **filend,
- size_t slen, size_t *sz, size_t *ss)
- {
- if (slen + (*ss = strlen(base)) >= *sz) {
- *sz += slen + *ss + 15;
- *file = srealloc(*file, *sz);
- *filend = &(*file)[slen];
- }
- strcpy(*filend, base);
- }
- /*
- * Create intermediate directories with -m.
- */
- static int
- imdir(char *fn)
- {
- struct stat st;
- char *cp;
- int dfl = dflag != 0;
- for (cp = fn; *cp == '/'; cp++);
- do {
- while (*cp && *cp != '/')
- cp++;
- if (*cp == '/') {
- *cp = '\0';
- if (stat(fn, &st) < 0) {
- if (dfl) {
- if (mkdir(fn, 0777) < 0) {
- *cp = '/';
- emsg(-1, "Cannot create "
- "directory for \"%s\"",
- fn);
- return -1;
- }
- } else {
- msg(-1, 0, sysv3 ?
- "missing 'd' option\n" :
- "Missing -d option\n");
- dfl = 2;
- }
- }
- *cp = '/';
- while (*cp == '/')
- cp++;
- }
- } while (*cp);
- return 0;
- }
- /*
- * Set file attributes and make sure that suid/sgid bits are only restored
- * if the owner is the same as on the tape.
- */
- static int
- setattr(const char *fn, struct stat *st)
- {
- mode_t mode = st->st_mode & 07777;
- uid_t uid = Rflag ? Ruid : myuid;
- gid_t gid = Rflag ? Rgid : mygid;
- if ((pax != PAX_TYPE_CPIO || myuid == 0) &&
- (pax == PAX_TYPE_CPIO ||
- pax_preserve&(PAX_P_OWNER|PAX_P_EVERY))) {
- if (setowner(fn, st) != 0)
- return 1;
- }
- if (pax != PAX_TYPE_CPIO &&
- (pax_preserve&(PAX_P_OWNER|PAX_P_EVERY)) == 0)
- mode &= ~(mode_t)(S_ISUID|S_ISGID);
- if (myuid != 0 || Rflag) {
- if (st->st_uid != uid || st->st_gid != gid) {
- mode &= ~(mode_t)S_ISUID;
- if ((st->st_mode&S_IFMT) != S_IFDIR && mode & 0010)
- mode &= ~(mode_t)S_ISGID;
- if ((st->st_mode&S_IFMT) == S_IFDIR && st->st_gid!=gid)
- mode &= ~(mode_t)S_ISGID;
- }
- }
- if ((st->st_mode&S_IFMT) == S_IFLNK)
- return 0;
- if (hp_Uflag)
- mode &= ~umsk|S_IFMT;
- if (pax != PAX_TYPE_CPIO &&
- (pax_preserve&(PAX_P_MODE|PAX_P_OWNER|PAX_P_EVERY))==0)
- mode &= 01777|S_IFMT;
- if ((pax == PAX_TYPE_CPIO || pax_preserve&(PAX_P_MODE|PAX_P_EVERY)) &&
- chmod(fn, mode) < 0) {
- emsg(2, "Cannot chmod() \"%s\"", fn);
- return 1;
- }
- if (pax != PAX_TYPE_CPIO ?
- (pax_preserve&(PAX_P_ATIME|PAX_P_MTIME|PAX_P_EVERY)) !=
- (PAX_P_ATIME|PAX_P_MTIME) : mflag)
- return rstime(fn, st, "modification");
- else
- return 0;
- }
- static int
- setowner(const char *fn, struct stat *st)
- {
- uid_t uid = Rflag ? Ruid : myuid ? myuid : st->st_uid;
- gid_t gid = Rflag ? Rgid : st->st_gid;
- if (((st->st_mode&S_IFMT)==S_IFLNK?lchown:chown)(fn, uid, gid) < 0) {
- emsg(2, "Cannot chown() \"%s\"", fn);
- return 1;
- }
- if (pax >= PAX_TYPE_PAX2001 && myuid && myuid != st->st_uid &&
- pax_preserve & (PAX_P_OWNER|PAX_P_EVERY)) {
- /*
- * Do not even try to preserve user ownership in this case.
- * It would either fail, or, without _POSIX_CHOWN_RESTRICTED,
- * leave us with a file we do not own and which we thus could
- * not chmod() later.
- */
- errno = EPERM;
- emsg(2, "Cannot chown() \"%s\"", fn);
- return 1;
- }
- return 0;
- }
- /*
- * For -i and -p: Check if device/inode have been created already and
- * if so, link path (or print the link name).
- */
- static int
- canlink(const char *path, struct stat *st, int really)
- {
- struct dslot *ds;
- struct islot *ip;
- ds = dfind(&devices, st->st_dev);
- if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL ||
- ip->i_name == NULL) {
- if (ip == NULL) {
- ip = scalloc(1, sizeof *ip);
- ip->i_ino = st->st_ino;
- if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0)
- ip->i_nlk = st->st_nlink;
- iput(ip, &ds->d_isl);
- }
- ip->i_name = sstrdup(path);
- } else {
- if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0) {
- /*
- * If an archive inode has more links than given in
- * st_nlink, write a new disk inode. See the comments
- * to storelink() for the rationale.
- */
- if (--ip->i_nlk == 0) {
- free(ip->i_name);
- ip->i_name = sstrdup(path);
- ip->i_nlk = st->st_nlink;
- return 0;
- }
- }
- if (really) {
- /*
- * If there was file data before the last link with
- * SVR4 cpio formats, do not create a hard link.
- */
- if (fmttype & TYP_NCPIO && ip->i_nlk == 0)
- return 0;
- if (linkunlink(ip->i_name, path) == 0)
- return 1;
- else
- return -1;
- } else {
- printf(" == %s", ip->i_name);
- return 1;
- }
- }
- return 0;
- }
- /*
- * Execution for -i.
- */
- static void
- doinp(void)
- {
- union bincpio bc;
- struct file f;
- int n;
- memset(&f, 0, sizeof f);
- if (Eflag)
- patfile();
- if (Iflag) {
- if ((mt = open(Iflag, O_RDONLY)) < 0) {
- if (sysv3) {
- emsg(3, "Cannot open <%s> for input.", Iflag);
- done(1);
- }
- else
- msg(3, -2, "Cannot open \"%s\" for input\n",
- Iflag);
- }
- } else
- mt = dup(0);
- mstat();
- blkbuf = svalloc(blksiz, 1);
- if ((n = mread()) == 0)
- unexeoa();
- else if (n < 0) {
- emsg(3, "Read error on \"%s\"",
- Iflag && !sysv3 ? Iflag : "input");
- done(1);
- }
- if (kflag == 0 && sixflag == 0)
- fmttype = FMT_NONE;
- if (fmttype == FMT_NONE)
- whathdr();
- else
- formatforced = 1;
- while (readhdr(&f, &bc) == 0) {
- if (fmttype & TYP_NCPIO && f.f_st.st_nlink > 1 &&
- (f.f_st.st_mode&S_IFMT) != S_IFDIR) {
- if (f.f_st.st_size != 0)
- flushlinks(&f);
- else
- storelink(&f);
- } else
- inpone(&f, 1);
- f.f_Kbase = f.f_Krest = f.f_Ksize = 0;
- f.f_oflag = 0;
- }
- if (fmttype & TYP_NCPIO)
- flushrest(f.f_pad);
- }
- /*
- * SVR4 cpio link handling with -i; called if we assume that more links
- * to this file will follow.
- */
- static void
- storelink(struct file *f)
- {
- struct dslot *ds;
- struct islot *ip;
- struct ilink *il, *iz;
- ds = dfind(&devices, f->f_st.st_dev);
- if ((ip = ifind(f->f_st.st_ino, &ds->d_isl)) == NULL) {
- ip = scalloc(1, sizeof *ip);
- ip->i_ino = f->f_st.st_ino;
- ip->i_nlk = f->f_st.st_nlink;
- ip->i_st = smalloc(sizeof *ip->i_st);
- *ip->i_st = f->f_st;
- iput(ip, &ds->d_isl);
- } else {
- if (ip->i_nlk == 0) {
- /*
- * More links to an inode than given in the first
- * st_nlink occurence are found within this archive.
- * This happens if -L was specified and soft links
- * point to a file with multiple hard links. We do
- * the same as SVR4 cpio implementations here and
- * associate these links with a new inode, since it
- * is related to the way a file with a single hard
- * link but referenced by soft links is unpacked.
- */
- ip->i_nlk = f->f_st.st_nlink;
- if (ip->i_name)
- free(ip->i_name);
- ip->i_name = NULL;
- ip->i_st = smalloc(sizeof *ip->i_st);
- *ip->i_st = f->f_st;
- } else if (ip->i_nlk <= 2) {
- /*
- * We get here if
- * - a file with multiple links is empty;
- * - a broken implementation has stored file data
- * before the last link;
- * - a linked file has been added to the archive later
- * again (does not happen with our implementation).
- */
- flushnode(ip, f);
- return;
- } else
- ip->i_nlk--;
- }
- for (il = ip->i_lnk, iz = NULL; il; il = il->l_nxt)
- iz = il;
- il = scalloc(1, sizeof *il);
- il->l_siz = strlen(f->f_name) + 1;
- il->l_nam = smalloc(il->l_siz);
- strcpy(il->l_nam, f->f_name);
- if (iz)
- iz->l_nxt = il;
- else
- ip->i_lnk = il;
- }
- /*
- * Flush all links of a file with SVR4 cpio format and -i.
- */
- static void
- flushlinks(struct file *f)
- {
- struct dslot *ds;
- struct islot *ip;
- ds = dfind(&devices, f->f_st.st_dev);
- ip = ifind(f->f_st.st_ino, &ds->d_isl);
- flushnode(ip, f);
- }
- /*
- * Data of a multi-linked file shall be transferred now for SVR4 cpio
- * format and -i.
- */
- static void
- flushnode(struct islot *ip, struct file *f)
- {
- struct file nf;
- struct ilink *il, *iz;
- f->f_dsize = f->f_st.st_size;
- if (ip && ip->i_nlk > 0) {
- /*
- * Write out the file data with the first link the user
- * wants to be extracted, but show the same display size
- * for all links.
- */
- for (il = ip->i_lnk, iz = NULL; il;
- iz = il, il = il->l_nxt, iz ? free(iz), 0 : 0) {
- memset(&nf, 0, sizeof nf);
- nf.f_name = il->l_nam;
- nf.f_nsiz = il->l_siz;
- nf.f_st = f->f_st;
- nf.f_chksum = f->f_chksum;
- nf.f_pad = f->f_pad;
- nf.f_dsize = f->f_dsize;
- if (inpone(&nf, 0) == 0) {
- f->f_chksum = 0;
- f->f_st.st_size = 0;
- }
- free(il->l_nam);
- }
- ip->i_lnk = NULL;
- if (ip->i_st)
- free(ip->i_st);
- ip->i_st = NULL;
- }
- if (f->f_name)
- inpone(f, 1);
- if (ip) {
- if (ip->i_nlk <= 2 && ip->i_name) {
- free(ip->i_name);
- ip->i_name = NULL;
- }
- ip->i_nlk = 0;
- }
- }
- /*
- * This writes all remaining multiply linked files, i. e. those with
- * st_size == 0 and st_nlink > number of links within the archive.
- */
- static void
- flushrest(int pad)
- {
- struct dslot *ds;
- for (ds = devices; ds; ds = ds->d_nxt)
- if (ds->d_isl != NULL)
- flushtree(ds->d_isl, pad);
- }
- static void
- flushtree(struct islot *ip, int pad)
- {
- struct file f;
- if (ip == inull)
- return;
- flushtree(ip->i_lln, pad);
- flushtree(ip->i_rln, pad);
- if (ip->i_nlk > 0 && ip->i_st) {
- memset(&f, 0, sizeof f);
- f.f_st = *ip->i_st;
- f.f_pad = pad;
- flushnode(ip, &f);
- }
- }
- /*
- * See if the user wants to have this file with -i, and extract it or
- * skip its data on the tape.
- */
- static int
- inpone(struct file *f, int shallskip)
- {
- struct glist *gp = NULL, *gb = NULL;
- int val = -1, selected = 0;
- if ((patterns == NULL || (gp = want(f, &gb)) != NULL ^ fflag) &&
- pax_track(f->f_name, f->f_st.st_mtime)) {
- selected = 1;
- if ((pax_sflag == 0 || pax_sname(&f->f_name, &f->f_nsiz)) &&
- (rflag == 0 || rname(&f->f_name, &f->f_nsiz))) {
- errcnt += infile(f);
- val = 0;
- }
- } else if (shallskip)
- errcnt += skipfile(f);
- if (gp != NULL && selected) {
- gp->g_gotcha = 1;
- if (gp->g_nxt && gp->g_nxt->g_art)
- gp->g_nxt->g_gotcha = 1;
- else if (gp->g_art && gb)
- gb->g_gotcha = 1;
- }
- return val;
- }
- /*
- * Read the header for a file with -i. Return values: 0 okay, 1 end of
- * archive, -1 failure.
- */
- static int
- readhdr(struct file *f, union bincpio *bp)
- {
- long namlen, l1, l2, rd, hsz;
- static long attempts;
- long long skipped = 0;
- if (fmttype & TYP_TAR) {
- if (f->f_name)
- f->f_name[0] = '\0';
- if (f->f_lnam)
- f->f_lnam[0] = '\0';
- }
- paxrec = globrec;
- retry: f->f_st = globst;
- retry2: if (fmttype & TYP_BINARY) {
- hsz = SIZEOF_hdr_cpio;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if ((fmttype&TYP_BE ? pbe16(bp->Hdr.c_magic) :
- ple16(bp->Hdr.c_magic)) != mag_bin)
- goto badhdr;
- if (fmttype & TYP_BE) {
- f->f_st.st_dev = pbe16(bp->Hdr.c_dev)&0177777;
- f->f_st.st_ino = pbe16(bp->Hdr.c_ino)&0177777;
- f->f_st.st_mode = pbe16(bp->Hdr.c_mode);
- } else {
- f->f_st.st_dev = ple16(bp->Hdr.c_dev)&0177777;
- f->f_st.st_ino = ple16(bp->Hdr.c_ino)&0177777;
- f->f_st.st_mode = ple16(bp->Hdr.c_mode);
- }
- if (sixflag) {
- /*
- * relevant Unix 6th Edition style mode bits
- * (according to /usr/sys/inode.h, untested:)
- *
- * 060000 IFMT type of file
- * 040000 IFDIR directory
- * 020000 IFCHR character special
- * 060000 IFBLK block special, 0 is regular
- * 007777 permission bits as today
- */
- f->f_st.st_mode &= 067777;
- if ((f->f_st.st_mode & 060000) == 0)
- f->f_st.st_mode |= S_IFREG;
- }
- if (fmttype & TYP_BE) {
- f->f_st.st_uid = pbe16(bp->Hdr.c_uid)&0177777;
- f->f_st.st_gid = pbe16(bp->Hdr.c_gid)&0177777;
- f->f_st.st_nlink = pbe16(bp->Hdr.c_nlink)&0177777;
- f->f_st.st_mtime = pbe32(bp->Hdr.c_mtime);
- f->f_st.st_rdev = pbe16(bp->Hdr.c_rdev);
- namlen = pbe16(bp->Hdr.c_namesize);
- f->f_st.st_size = pbe32(bp->Hdr.c_filesize)&0xFFFFFFFF;
- } else {
- f->f_st.st_uid = ple16(bp->Hdr.c_uid)&0177777;
- f->f_st.st_gid = ple16(bp->Hdr.c_gid)&0177777;
- f->f_st.st_nlink = ple16(bp->Hdr.c_nlink)&0177777;
- f->f_st.st_mtime = pme32(bp->Hdr.c_mtime);
- f->f_st.st_rdev = ple16(bp->Hdr.c_rdev);
- namlen = ple16(bp->Hdr.c_namesize);
- f->f_st.st_size = pme32(bp->Hdr.c_filesize)&0xFFFFFFFF;
- }
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- if ((f->f_st.st_rdev&0xFFFF) == 0xFFFF &&
- f->f_st.st_size > 0 &&
- ((f->f_st.st_mode&S_IFMT) == S_IFBLK ||
- (f->f_st.st_mode&S_IFMT) == S_IFCHR &&
- (!formatforced || fmttype & TYP_SGI))) {
- fmttype |= TYP_SGI;
- f->f_rmajor = (f->f_st.st_size&0xFFFC0000)>>18;
- f->f_rminor = f->f_st.st_size&0x0003FFFF;
- f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
- f->f_st.st_size = 0;
- }
- if ((f->f_st.st_mode&S_IFMT) == S_IFREG &&
- f->f_st.st_size&0x80000000 &&
- (!formatforced || fmttype & TYP_SGI)) {
- fmttype |= TYP_SGI;
- f->f_Ksize = f->f_st.st_size&0xFFFFFFFF;
- f->f_Kbase = (0x100000000LL-f->f_st.st_size) *
- 0x80000000;
- f->f_st.st_size = 0;
- }
- f->f_pad = 2;
- rd = SIZEOF_hdr_cpio;
- } else if (fmttype == FMT_ODC) {
- hsz = SIZEOF_c_hdr;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if(memcmp(bp->Cdr.c_magic, mag_odc, sizeof mag_odc))
- goto badhdr;
- f->f_st.st_dev = rdoct(bp->Cdr.c_dev, 6);
- f->f_st.st_ino = rdoct(bp->Cdr.c_ino, 6);
- f->f_st.st_mode = rdoct(bp->Cdr.c_mode, 6);
- f->f_st.st_uid = rdoct(bp->Cdr.c_uid, 6);
- f->f_st.st_gid = rdoct(bp->Cdr.c_gid, 6);
- f->f_st.st_nlink = rdoct(bp->Cdr.c_nlink, 6);
- f->f_st.st_rdev = rdoct(bp->Cdr.c_rdev, 6);
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- f->f_st.st_mtime = rdoct(bp->Cdr.c_mtime, 11);
- namlen = rdoct(bp->Cdr.c_namesz, 6);
- f->f_st.st_size = rdoct(bp->Cdr.c_filesz, 11);
- f->f_pad = 1;
- rd = SIZEOF_c_hdr;
- } else if (fmttype == FMT_DEC) {
- hsz = SIZEOF_d_hdr;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if(memcmp(bp->Ddr.d_magic, mag_odc, sizeof mag_odc))
- goto badhdr;
- f->f_st.st_dev = rdoct(bp->Ddr.d_dev, 6);
- f->f_st.st_ino = rdoct(bp->Ddr.d_ino, 6);
- f->f_st.st_mode = rdoct(bp->Ddr.d_mode, 6);
- f->f_st.st_uid = rdoct(bp->Ddr.d_uid, 6);
- f->f_st.st_gid = rdoct(bp->Ddr.d_gid, 6);
- f->f_st.st_nlink = rdoct(bp->Ddr.d_nlink, 6);
- f->f_rmajor = rdoct(bp->Ddr.d_rmaj, 8);
- f->f_rminor = rdoct(bp->Ddr.d_rmin, 8);
- f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
- f->f_st.st_mtime = rdoct(bp->Ddr.d_mtime, 11);
- namlen = rdoct(bp->Ddr.d_namesz, 6);
- f->f_st.st_size = rdoct(bp->Ddr.d_filesz, 11);
- f->f_pad = 1;
- rd = SIZEOF_d_hdr;
- } else if (fmttype & TYP_NCPIO) {
- hsz = SIZEOF_Exp_cpio_hdr;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if (memcmp(bp->Edr.E_magic, mag_asc, sizeof mag_asc) &&
- memcmp(bp->Edr.E_magic, mag_crc, sizeof mag_crc))
- goto badhdr;
- f->f_st.st_ino = rdhex(bp->Edr.E_ino, 8);
- f->f_st.st_mode = rdhex(bp->Edr.E_mode, 8);
- f->f_st.st_uid = rdhex(bp->Edr.E_uid, 8);
- f->f_st.st_gid = rdhex(bp->Edr.E_gid, 8);
- f->f_st.st_nlink = rdhex(bp->Edr.E_nlink, 8);
- f->f_st.st_mtime = rdhex(bp->Edr.E_mtime, 8);
- f->f_st.st_size = rdhex(bp->Edr.E_filesize, 8);
- l1 = rdhex(bp->Edr.E_maj, 8);
- l2 = rdhex(bp->Edr.E_min, 8);
- f->f_st.st_dev = makedev(l1, l2);
- f->f_rmajor = rdhex(bp->Edr.E_rmaj, 8);
- f->f_rminor = rdhex(bp->Edr.E_rmin, 8);
- f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
- namlen = rdhex(bp->Edr.E_namesize, 8);
- f->f_chksum = rdhex(bp->Edr.E_chksum, 8);
- f->f_pad = 4;
- rd = SIZEOF_Exp_cpio_hdr;
- } else if (fmttype & TYP_CRAY) {
- int diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
- hsz = SIZEOF_cray_hdr - diff5;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if (pbe64(bp->Crayhdr.C_magic) != mag_bin)
- goto badhdr;
- f->f_st.st_dev = pbe64(bp->Crayhdr.C_dev);
- f->f_st.st_ino = pbe64(bp->Crayhdr.C_ino);
- f->f_st.st_mode = pbe64(bp->Crayhdr.C_mode);
- /*
- * Some of the S_IFMT bits on Cray UNICOS 9 differ from
- * the common Unix values, namely:
- *
- * S_IFLNK 0130000 symbolic link
- * S_IFOFD 0110000 off line, with data
- * S_IFOFL 0120000 off line, with no data
- *
- * We treat the latter ones as regular files.
- */
- if ((f->f_st.st_mode&S_IFMT) == 0130000)
- f->f_st.st_mode = f->f_st.st_mode&07777|S_IFLNK;
- else if ((f->f_st.st_mode&S_IFMT) == 0110000 ||
- (f->f_st.st_mode&S_IFMT) == 0120000)
- f->f_st.st_mode = f->f_st.st_mode&07777|S_IFREG;
- f->f_st.st_uid = pbe64(bp->Crayhdr.C_uid);
- f->f_st.st_gid = pbe64(bp->Crayhdr.C_gid);
- f->f_st.st_nlink = pbe64(bp->Crayhdr.C_nlink);
- f->f_st.st_mtime = pbe64(bp->Crayhdr.C_mtime - diff5);
- f->f_st.st_rdev = pbe64(bp->Crayhdr.C_rdev);
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- f->f_st.st_size = pbe64(bp->Crayhdr.C_filesize - diff5);
- namlen = pbe64(bp->Crayhdr.C_namesize - diff5);
- f->f_pad = 1;
- rd = SIZEOF_cray_hdr - diff5;
- } else if (fmttype & TYP_BAR) {
- hsz = 512;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if (bp->data[SIZEOF_bar_header] == '\0') {
- if (kflag && !allzero(bp->data, hsz))
- goto badhdr;
- return 1;
- }
- if (trdsum(bp) != 0 && kflag == 0)
- goto badhdr;
- bp->data[512] = bp->data[513] = '\0';
- if (f->f_name == NULL || f->f_name[0] == '\0') {
- if (f->f_nsiz < 512) {
- free(f->f_name);
- f->f_name = smalloc(f->f_nsiz = 512);
- }
- strcpy(f->f_name, &bp->data[SIZEOF_bar_header]);
- }
- namlen = strlen(f->f_name) + 1;
- f->f_st.st_mode = rdoct(bp->Bdr.b_mode, 8);
- f->f_st.st_uid = rdoct(bp->Bdr.b_uid, 8);
- f->f_st.st_gid = rdoct(bp->Bdr.b_gid, 8);
- f->f_st.st_size = rdoct(bp->Bdr.b_size, 12);
- f->f_st.st_mtime = rdoct(bp->Bdr.b_mtime, 12);
- f->f_st.st_rdev = rdoct(bp->Bdr.b_rdev, 8);
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- switch (bp->Bdr.b_linkflag) {
- case '0':
- f->f_st.st_mode &= 07777;
- if (f->f_name[namlen-2] == '/')
- f->f_st.st_mode |= S_IFDIR;
- else
- f->f_st.st_mode |= S_IFREG;
- break;
- case '1':
- if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
- blinkof(bp->data, f, namlen);
- f->f_st.st_nlink = 2;
- if ((f->f_st.st_mode&S_IFMT) == 0)
- f->f_st.st_mode |= S_IFREG;
- break;
- case '2':
- if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
- blinkof(bp->data, f, namlen);
- f->f_st.st_mode &= 07777;
- f->f_st.st_mode |= S_IFLNK;
- }
- f->f_pad = 512;
- rd = 512;
- } else if (fmttype & TYP_TAR) {
- hsz = 512;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if (bp->Tdr.t_name[0] == '\0') {
- if (kflag && !allzero(bp->data, hsz))
- goto badhdr;
- return 1;
- }
- if (fmttype == FMT_GNUTAR) {
- if (memcmp(bp->Tdr.t_magic, mag_gnutar, 8))
- goto badhdr;
- } else if (fmttype & TYP_USTAR) {
- if (memcmp(bp->Tdr.t_magic, mag_ustar, 6))
- goto badhdr;
- }
- if (trdsum(bp) != 0 && kflag == 0)
- goto badhdr;
- if ((fmttype & TYP_PAX) == 0 && (fmttype == FMT_GNUTAR ||
- strcmp(bp->Tdr.t_name, "././@LongLink") == 0)) {
- switch (bp->Tdr.t_linkflag) {
- case 'L':
- if (readgnuname(&f->f_name, &f->f_nsiz,
- rdoct(bp->Tdr.t_size, 12)) < 0)
- goto badhdr;
- goto retry;
- case 'K':
- if (readgnuname(&f->f_lnam, &f->f_lsiz,
- rdoct(bp->Tdr.t_size, 12)) < 0)
- goto badhdr;
- goto retry;
- }
- }
- if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR) {
- switch (bp->Tdr.t_linkflag) {
- case 'g':
- case 'x':
- if (formatforced && fmttype != FMT_PAX)
- break;
- fmttype = FMT_PAX;
- tgetpax(&bp->Tdr, f);
- goto retry2;
- case 'X':
- if (formatforced && fmttype != FMT_SUN)
- break;
- fmttype = FMT_SUN;
- tgetpax(&bp->Tdr, f);
- goto retry2;
- }
- }
- if (f->f_name == NULL || f->f_name[0] == '\0') {
- if (f->f_nsiz < TNAMSIZ+TPFXSIZ+2) {
- free(f->f_name);
- f->f_name= smalloc(f->f_nsiz=TNAMSIZ+TPFXSIZ+2);
- }
- tnameof(&bp->Tdr, f->f_name);
- }
- namlen = strlen(f->f_name) + 1;
- f->f_st.st_mode = rdoct(bp->Tdr.t_mode, 8) & 07777;
- if (paxrec & PR_UID)
- /*EMPTY*/;
- else if (fmttype == FMT_GNUTAR && bp->Tdr.t_uid[0] & 0200)
- f->f_st.st_uid = pbe64(bp->Tdr.t_uid) &
- 0x7FFFFFFFFFFFFFFFLL;
- else
- f->f_st.st_uid = rdoct(bp->Tdr.t_uid, 8);
- if (paxrec & PR_GID)
- /*EMPTY*/;
- else if (fmttype == FMT_GNUTAR && bp->Tdr.t_gid[0] & 0200)
- f->f_st.st_gid = pbe64(bp->Tdr.t_gid) &
- 0x7FFFFFFFFFFFFFFFLL;
- else
- f->f_st.st_gid = rdoct(bp->Tdr.t_gid, 8);
- if (paxrec & PR_SIZE)
- /*EMPTY*/;
- else if (fmttype == FMT_GNUTAR && bp->Tdr.t_size[0] == '\200')
- f->f_st.st_size = pbe64(&bp->Tdr.t_size[4]);
- else
- f->f_st.st_size = rdoct(bp->Tdr.t_size, 12);
- if ((paxrec & PR_MTIME) == 0)
- f->f_st.st_mtime = rdoct(bp->Tdr.t_mtime, TTIMSIZ);
- if (bp->Tdr.t_linkflag == '1') {
- if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
- tlinkof(&bp->Tdr, f);
- f->f_st.st_mode |= S_IFREG; /* for -tv */
- f->f_st.st_nlink = 2;
- } else if (bp->Tdr.t_linkflag == '2') {
- if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
- tlinkof(&bp->Tdr, f);
- f->f_st.st_mode |= S_IFLNK;
- } else if (tflag == 0 && (f->f_name)[namlen-2] == '/')
- f->f_st.st_mode |= S_IFDIR;
- else
- f->f_st.st_mode |= tifmt(bp->Tdr.t_linkflag);
- if (paxrec & PR_SUN_DEVMAJOR)
- /*EMPTY*/;
- else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devmajor[0] & 0200)
- f->f_rmajor = pbe64(bp->Tdr.t_devmajor) &
- 0x7FFFFFFFFFFFFFFFLL;
- else
- f->f_rmajor = rdoct(bp->Tdr.t_devmajor, 8);
- if (paxrec & PR_SUN_DEVMINOR)
- /*EMPTY*/;
- else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devminor[0] & 0200)
- f->f_rminor = pbe64(bp->Tdr.t_devminor) &
- 0x7FFFFFFFFFFFFFFFLL;
- else
- f->f_rminor = rdoct(bp->Tdr.t_devminor, 8);
- f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
- f->f_pad = 512;
- rd = 512;
- } else if (fmttype == FMT_ZIP) {
- hsz = SIZEOF_zip_header;
- if (attempts == 0 && bread(bp->data, hsz) != hsz)
- unexeoa();
- if (memcmp(bp->Zdr.z_signature, mag_zipctr,
- sizeof mag_zipctr) == 0 ||
- memcmp(bp->Zdr.z_signature, mag_zipend,
- sizeof mag_zipend) == 0 ||
- memcmp(bp->Zdr.z_signature, mag_zip64e,
- sizeof mag_zip64e) == 0 ||
- memcmp(bp->Zdr.z_signature, mag_zip64l,
- sizeof mag_zip64l) == 0)
- return 1;
- if (memcmp(bp->Zdr.z_signature, mag_zipsig, sizeof mag_zipsig))
- goto badhdr;
- f->f_st.st_uid = myuid;
- f->f_st.st_gid = mygid;
- f->f_st.st_nlink = 1;
- f->f_st.st_mtime =
- gdostime(bp->Zdr.z_modtime, bp->Zdr.z_moddate);
- f->f_st.st_size = ple32(bp->Zdr.z_nsize);
- f->f_csize = ple32(bp->Zdr.z_csize);
- f->f_cmethod = ple16(bp->Zdr.z_cmethod);
- f->f_chksum = ple32(bp->Zdr.z_crc32);
- f->f_gflag = ple16(bp->Zdr.z_gflag);
- rd = SIZEOF_zip_header;
- namlen = ple16(bp->Zdr.z_namelen)&0177777;
- if (f->f_nsiz < namlen+1) {
- free(f->f_name);
- f->f_name = smalloc(f->f_nsiz = namlen+1);
- }
- if (bread(f->f_name, namlen) != namlen)
- goto corrupt;
- (f->f_name)[namlen] = '\0';
- if (f->f_name[namlen-1] == '/')
- f->f_st.st_mode = 0777&~(mode_t)umsk|S_IFDIR;
- else
- f->f_st.st_mode = 0666&~(mode_t)umsk|S_IFREG;
- rd += namlen;
- if ((l1 = ziprxtra(f, &bp->Zdr)) < 0)
- goto corrupt;
- rd += l1;
- f->f_pad = 1;
- } else
- abort();
- if (fmttype & TYP_CPIO) {
- if (f->f_st.st_nlink <= 0 || namlen <= 0 || namlen >= SANELIMIT)
- goto corrupt;
- if (namlen > f->f_nsiz) {
- if (f->f_name)
- free(f->f_name);
- f->f_name = smalloc(f->f_nsiz = namlen);
- }
- if (bread(f->f_name, namlen) != namlen)
- unexeoa();
- if (f->f_name[namlen-1] != '\0')
- goto corrupt;
- rd += namlen;
- skippad(rd, f->f_pad);
- if (fmttype & TYP_NCPIO &&
- (!formatforced || fmttype & TYP_SCO)) {
- /*
- * The UnixWare format is a hack, but not a bad
- * one. It is backwards compatible as far as
- * possible; old implementations can use the -k
- * option and will then get only the beginning
- * of a large file, but all other files in the
- * archive.
- */
- if (namlen >= 24 && memcmp(&f->f_name[namlen-23],
- "\0size=", 6) == 0) {
- fmttype |= TYP_SCO;
- f->f_st.st_size = rdhex(&f->f_name[namlen-17],
- 16);
- }
- }
- }
- if (attempts) {
- if (tflag)
- fflush(stdout);
- msg(0, 0, sysv3 < 0 ?
- "Re-synced after skipping %lld bytes.\n" :
- "Re-synchronized on magic number/header.\n",
- skipped);
- }
- attempts = 0;
- if (f->f_st.st_atime == 0 || (pax_preserve&(PAX_P_ATIME|PAX_P_EVERY)) ==
- PAX_P_ATIME)
- f->f_st.st_atime = f->f_st.st_mtime;
- if ((f->f_dsize = f->f_st.st_size) < 0)
- goto badhdr;
- if (fmttype & TYP_CPIO && strcmp(f->f_name, trailer) == 0)
- return 1;
- return 0;
- corrupt:
- if (kflag) {
- if (tflag)
- fflush(stdout);
- msg(3, 0, "Corrupt header, file(s) may be lost.\n");
- }
- badhdr:
- if (kflag) {
- int next = fmttype & TYP_TAR ? 512 : 1;
- if (attempts++ == 0) {
- if (tflag)
- fflush(stdout);
- msg(1, 0, sysv3 < 0 ? "Out of phase; resyncing.\n" :
- "Searching for magic number/header.\n");
- errcnt++;
- }
- for (l1 = next; l1 < hsz; l1++)
- bp->data[l1-next] = bp->data[l1];
- if (bread(&bp->data[l1-next], next) != next)
- unexeoa();
- skipped++;
- goto retry;
- }
- if (tflag)
- fflush(stdout);
- msg(3, 1, sysv3 < 0 ? "Out of phase--get help\n" :
- sysv3 > 0 ? "Out of sync, bad magic number/header.\n" :
- "Bad magic number/header.\n");
- /*NOTREACHED*/
- return -1;
- }
- /*
- * Determine the kind of archive on tape.
- */
- static void
- whathdr(void)
- {
- again: if (blktop >= SIZEOF_hdr_cpio &&
- ple16(blkbuf) == mag_bin) {
- fmttype = FMT_BINLE;
- } else if (blktop >= SIZEOF_hdr_cpio &&
- pbe16(blkbuf) == mag_bin) {
- fmttype = FMT_BINBE;
- } else if (blktop >= SIZEOF_c_hdr &&
- memcmp(blkbuf, mag_odc, sizeof mag_odc) == 0) {
- /*
- * The DEC format is rubbish. Instead of introducing a new
- * archive magic, its engineers reused the POSIX/odc magic
- * and changed the fields. But there's a workaround: For a
- * real odc archive, the byte following the file name is a
- * \0 byte (unless the archive is damaged, of course). If
- * it is not a \0 byte, but a \0 byte appears at the
- * corresponding location for the DEC format, this is
- * treated as a DEC archive. It must be noted that the
- * Tru64 UNIX 5.1 cpio command is too stupid even for
- * doing that - it will spill out a list of complaints
- * if an extended archive is read but -e is not given.
- */
- int ns1, ns2;
- ns1 = rdoct(((struct c_hdr *)blkbuf)->c_namesz, 6);
- ns2 = rdoct(((struct d_hdr *)blkbuf)->d_namesz, 6);
- if (blktop >= SIZEOF_d_hdr &&
- (ns1 >= blktop - SIZEOF_c_hdr ||
- blkbuf[SIZEOF_c_hdr+ns1-1] != '\0') &&
- ns2 <= blktop - SIZEOF_d_hdr &&
- blkbuf[SIZEOF_d_hdr+ns2-1] == '\0')
- fmttype = FMT_DEC;
- else
- fmttype = FMT_ODC;
- } else if (blktop >= SIZEOF_Exp_cpio_hdr &&
- memcmp(blkbuf, mag_asc, sizeof mag_asc) == 0) {
- fmttype = FMT_ASC;
- } else if (blktop >= SIZEOF_Exp_cpio_hdr &&
- memcmp(blkbuf, mag_crc, sizeof mag_crc) == 0) {
- fmttype = FMT_CRC;
- } else if (blktop >= SIZEOF_cray_hdr &&
- pbe64(blkbuf) == mag_bin) {
- /*
- * Old and new Cray headers are identical in the first
- * 64 header bytes (including the magic). cpio(5) on
- * UNICOS does not describe what the new param field
- * is for.
- *
- * An archive is treated as old format if the mtime
- * and namesize fields make sense and all characters
- * of the name are non-null.
- */
- struct cray_hdr *Cp = (struct cray_hdr *)blkbuf;
- long long mtime, namesize;
- fmttype = FMT_CRAY;
- mtime = pbe64(Cp->C_mtime - CRAY_PARAMSZ);
- namesize = pbe64(Cp->C_namesize - CRAY_PARAMSZ);
- if (mtime > 0 && mtime < (1LL<<31) &&
- namesize > 0 && namesize < 2048 &&
- blktop > SIZEOF_cray_hdr-
- CRAY_PARAMSZ+namesize+1) {
- int i;
- for (i = 0; i < namesize; i++)
- if (blkbuf[SIZEOF_cray_hdr-
- CRAY_PARAMSZ+i] == '\0')
- break;
- if (i == namesize-1)
- fmttype = FMT_CRAY5;
- }
- } else if (blktop >= 512 &&
- memcmp(&blkbuf[257], mag_ustar, 6) == 0 &&
- tcssum((union bincpio *)blkbuf, 1) == 0) {
- fmttype = FMT_USTAR;
- } else if (blktop >= 512 &&
- memcmp(&blkbuf[257], mag_gnutar, 8) == 0 &&
- tcssum((union bincpio *)blkbuf, 1) == 0) {
- fmttype = FMT_GNUTAR;
- } else if (blktop >= 512 && blkbuf[0] && /* require filename
- to avoid match on
- /dev/zero etc. */
- memcmp(&blkbuf[257], "\0\0\0\0\0", 5) == 0 &&
- tcssum((union bincpio *)blkbuf, 0) == 0) {
- fmttype = FMT_OTAR;
- } else if (blktop >= SIZEOF_zip_header &&
- (memcmp(blkbuf, mag_zipctr, sizeof mag_zipctr) == 0 ||
- memcmp(blkbuf, mag_zipsig, sizeof mag_zipsig) == 0 ||
- memcmp(blkbuf, mag_zipend, sizeof mag_zipend) == 0 ||
- memcmp(blkbuf, mag_zip64e, sizeof mag_zip64e) == 0 ||
- memcmp(blkbuf, mag_zip64l, sizeof mag_zip64l) == 0)) {
- fmttype = FMT_ZIP;
- } else if (blktop >= 512 && memcmp(blkbuf,"\0\0\0\0\0\0\0\0",8) == 0 &&
- memcmp(&blkbuf[65], mag_bar, sizeof mag_bar) == 0 &&
- bcssum((union bincpio *)blkbuf) == 0) {
- fmttype = FMT_BAR;
- compressed_bar = blkbuf[71] == '1';
- curpos = 512;
- } else if (!Aflag && blktop > 3 && memcmp(blkbuf, "BZh", 3) == 0 &&
- redirect("bzip2", "-cd") == 0) {
- goto zip;
- } else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\235", 2) == 0 &&
- redirect("zcat", NULL) == 0) {
- goto zip;
- } else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\213", 2) == 0 &&
- redirect("gzip", "-cd") == 0) {
- goto zip;
- } else if (!Aflag && blktop > 4 &&
- memcmp(blkbuf, "\355\253\356\333", 4) == 0 &&
- redirect("rpm2cpio", "-") == 0) {
- goto zip;
- } else {
- msg(3, 0, sysv3 ? "This is not a cpio file, bad header.\n" :
- "Not a cpio file, bad header.\n");
- done(1);
- }
- return;
- zip:
- blktop = curpos = 0;
- blocks = 0;
- bytes = 0;
- mfl = 0;
- mstat();
- if (mread() == 0)
- unexeoa();
- goto again;
- }
- /*
- * Processing of a single file with -i.
- */
- static int
- infile(struct file *f)
- {
- int val;
- if ((fmttype & TYP_CPIO || fmttype == FMT_ZIP && f->f_st.st_size) &&
- (f->f_st.st_mode&S_IFMT) == S_IFLNK) {
- if (f->f_lsiz < f->f_st.st_size+1)
- f->f_lnam = srealloc(f->f_lnam,
- f->f_lsiz = f->f_st.st_size+1);
- if (bread(f->f_lnam, f->f_st.st_size) != f->f_st.st_size)
- unexeoa();
- f->f_lnam[f->f_st.st_size] = '\0';
- skippad(f->f_st.st_size, f->f_pad);
- }
- if (tflag)
- val = filet(f, indata);
- else
- val = filein(f, indata, f->f_name);
- return val != 0;
- }
- /*
- * Fetch the data of regular files from tape and write it to tfd or, if
- * tfd < 0, discard it.
- */
- static int
- indata(struct file *f, const char *tgt, int tfd)
- {
- char *buf;
- size_t bufsize;
- struct stat ts;
- long long size;
- ssize_t rd;
- uint32_t ssum = 0, usum = 0;
- int val = 0;
- int doswap = 0;
- size = fmttype == FMT_ZIP ? f->f_csize :
- f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
- doswap = ((bflag|sflag) == 0 ||
- ckodd(size, 2, "bytes", f->f_name) == 0) &
- ((bflag|Sflag) == 0 ||
- ckodd(size, 4, "halfwords", f->f_name) == 0) &
- (bflag|sflag|Sflag);
- if (fmttype == FMT_ZIP && f->f_cmethod != C_STORED)
- return zipread(f, tgt, tfd, doswap);
- if (tfd < 0 || fstat(tfd, &ts) < 0)
- ts.st_blksize = 4096;
- getbuf(&buf, &bufsize, ts.st_blksize);
- again: while (size) {
- if ((rd = bread(buf, size > bufsize ? bufsize : size)) <= 0)
- unexeoa();
- if (doswap)
- swap(buf, rd, bflag || sflag, bflag || Sflag);
- if (tfd >= 0 && write(tfd, buf, rd) != rd) {
- emsg(3, "Cannot write \"%s\"", tgt);
- tfd = -1;
- val = -1;
- }
- size -= rd;
- if (fmttype & TYP_CRC)
- do {
- rd--;
- ssum += ((signed char *)buf)[rd];
- usum += ((unsigned char *)buf)[rd];
- } while (rd);
- }
- if (f->f_Kbase) {
- skippad(f->f_Ksize, f->f_pad);
- readK2hdr(f);
- size = f->f_Krest;
- goto again;
- }
- skippad(fmttype==FMT_ZIP ? f->f_csize :
- f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
- if (fmttype & TYP_CRC) {
- if (f->f_chksum != ssum && f->f_chksum != usum) {
- msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
- if (kflag)
- errcnt++;
- else
- val = -1;
- }
- }
- return val;
- }
- /*
- * Skip the data for a file on tape.
- */
- static int
- skipfile(struct file *f)
- {
- char b[4096];
- long long size;
- ssize_t rd;
- if (fmttype & TYP_TAR && ((f->f_st.st_mode&S_IFMT) == S_IFLNK ||
- f->f_st.st_nlink > 1)) {
- if (fmttype & TYP_USTAR && (!formatforced ||
- fmttype == FMT_PAX) &&
- f->f_st.st_nlink > 1 &&
- f->f_st.st_size > 0)
- /*EMPTY*/;
- else
- return 0;
- }
- /*
- * SVR4 cpio derivatives always ignore the size of these,
- * even if not reading tar formats. We do the same for now,
- * although POSIX (for -H odc format) says the contrary.
- */
- if (fmttype != FMT_ZIP && ((f->f_st.st_mode&S_IFMT) == S_IFDIR ||
- (f->f_st.st_mode&S_IFMT) == S_IFCHR ||
- (f->f_st.st_mode&S_IFMT) == S_IFBLK ||
- (f->f_st.st_mode&S_IFMT) == S_IFIFO ||
- (f->f_st.st_mode&S_IFMT) == S_IFNAM ||
- (f->f_st.st_mode&S_IFMT) == S_IFNWK))
- return 0;
- if (fmttype == FMT_ZIP && f->f_gflag & FG_DESC)
- return zipread(f, f->f_name, -1, 0);
- size = fmttype == FMT_ZIP ? f->f_csize :
- f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
- again: while (size) {
- if ((rd = bread(b, size > sizeof b ? sizeof b : size)) <= 0)
- unexeoa();
- size -= rd;
- }
- if (f->f_Kbase) {
- skippad(f->f_Ksize, f->f_pad);
- readK2hdr(f);
- size = f->f_Krest;
- goto again;
- }
- skippad(fmttype==FMT_ZIP ? f->f_csize :
- f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
- return 0;
- }
- /*
- * Skip data also, but perform checks as if copying.
- */
- static int
- skipdata(struct file *f, int (*copydata)(struct file *, const char *, int))
- {
- switch (f->f_st.st_mode&S_IFMT) {
- case S_IFLNK:
- break;
- case S_IFDIR:
- case S_IFBLK:
- case S_IFCHR:
- case S_IFIFO:
- case S_IFNAM:
- case S_IFNWK:
- if (fmttype != FMT_ZIP)
- break;
- /*FALLTHRU*/
- case S_IFREG:
- default:
- if (fmttype & TYP_TAR && f->f_st.st_nlink > 1 &&
- ((fmttype & TYP_USTAR) == 0 ||
- formatforced && fmttype != FMT_PAX))
- break;
- if (copydata(f, f->f_name, -1) != 0)
- return 1;
- }
- return 0;
- }
- /*
- * Seek to a position in the archive measured from the begin of
- * operation. Handle media-dependent seeks. n must be a multiple
- * of the tape device's physical block size.
- */
- static int
- tseek(off_t n)
- {
- int fault;
- if (tapeblock > 0) {
- int i = (n - poffs) / tapeblock;
- #if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
- defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
- defined (__OpenBSD__) || defined (__DragonFly__) || \
- defined (__APPLE__) || defined (__CYGWIN__)
- struct mtop mo;
- mo.mt_op = i > 0 ? MTFSR : MTBSR;
- mo.mt_count = i > 0 ? i : -i;
- fault = ioctl(mt, MTIOCTOP, &mo);
- #else /* SVR4.2MP */
- int t, a;
- t = i > 0 ? T_SBF : T_SBB;
- a = i > 0 ? i : -i;
- fault = ioctl(mt, t, a);
- #endif /* SVR4.2MP */
- } else
- fault = lseek(mt, n - poffs, SEEK_CUR) == (off_t)-1 ? -1 : 0;
- if (fault == 0)
- poffs = n;
- return fault;
- }
- /*
- * Advance to the trailer on the tape (for -A).
- */
- static int
- totrailer(void)
- {
- union bincpio bc;
- struct file f;
- off_t ooffs, noffs, diff;
- int i;
- if (mread() == 0)
- unexeoa();
- if (fmttype == FMT_NONE)
- whathdr();
- memset(&f, 0, sizeof f);
- for (;;) {
- ooffs = aoffs;
- if ((i = readhdr(&f, &bc)) < 0)
- goto fail;
- if (i > 0)
- break;
- markdev(f.f_st.st_dev);
- if (skipfile(&f) != 0)
- goto fail;
- if (fmttype == FMT_ZIP)
- zipdefer(f.f_name, &f.f_st, ooffs,
- f.f_chksum, f.f_csize, &bc.Zdr);
- pax_track(f.f_name, f.f_st.st_mtime);
- }
- /*
- * Now seek to the position of the trailer, but retain the
- * block alignment.
- */
- diff = ooffs % blksiz;
- noffs = ooffs - diff;
- if (diff ? tseek(noffs) == 0 && mread() >= diff && tseek(noffs) == 0
- : tseek(ooffs) == 0) {
- free(f.f_name);
- free(f.f_lnam);
- nwritten = ooffs;
- curpos = diff;
- return 0;
- }
- fail: msg(4, 1, "Unable to append to this archive\n");
- /*NOTREACHED*/
- return 1;
- }
- static long long
- rdoct(const char *data, int len)
- {
- int i;
- long long val = 0;
- for (i = 0; i < len && data[i] == ' '; i++);
- for ( ; i < len && data[i] && data[i] != ' '; i++) {
- val <<= 3;
- val += data[i] - '0';
- }
- return val;
- }
- static long long
- rdhex(const char *data, int len)
- {
- int i;
- long long val = 0;
- for (i = 0; i < len && data[i] == ' '; i++);
- for ( ; i < len && data[i] && data[i] != ' '; i++) {
- val <<= 4;
- val += data[i] > '9' ? data[i] > 'F' ? data[i] - 'a' + 10 :
- data[i] - 'A' + 10 : data[i] - '0';
- }
- return val;
- }
- void
- unexeoa(void)
- {
- if (sysv3) {
- fprintf(stderr,
- "Can't read input: end of file encountered "
- "prior to expected end of archive.\n");
- done(1);
- }
- else
- msg(3, 1, "Unexpected end-of-archive encountered.\n");
- }
- static char *peekdata;
- static size_t peekbot, peektop, peeksize;
- void
- bunread(const char *data, size_t sz)
- {
- peekdata = srealloc(peekdata, peeksize += sz);
- memcpy(&peekdata[peekbot], data, sz);
- peektop += sz;
- aoffs -= sz;
- }
- /*
- * Buffered read of data from tape. sz is the amount of data required
- * by the archive format; if it cannot be retrieved, processing fails.
- */
- ssize_t
- bread(char *data, size_t sz)
- {
- ssize_t rd = 0;
- if (peekdata) {
- rd = sz>peektop-peekbot ? peektop-peekbot : sz;
- memcpy(&data[0], &peekdata[peekbot], rd);
- sz -= rd;
- peekbot += rd;
- if (peekbot == peektop) {
- free(peekdata);
- peekdata = 0;
- peeksize = 0;
- peekbot = peektop = 0;
- }
- }
- while (sz) {
- if (blktop - curpos >= sz) {
- memcpy(&data[rd], &blkbuf[curpos], sz);
- curpos += sz;
- rd += sz;
- aoffs += rd;
- return rd;
- }
- if (blktop > curpos) {
- memcpy(&data[rd], &blkbuf[curpos], blktop - curpos);
- rd += blktop - curpos;
- sz -= blktop - curpos;
- curpos = blktop;
- }
- if (mfl < 0) {
- if (mfl == -1) {
- emsg(3, "I/O error on \"%s\"",
- Iflag ? Iflag : "input");
- if (kflag == 0)
- break;
- if (tapeblock < 0 && (
- (mtst.st_mode&S_IFMT)==S_IFBLK||
- (mtst.st_mode&S_IFMT)==S_IFCHR||
- (mtst.st_mode&S_IFMT)==S_IFREG
- ) && lseek(mt, blksiz, SEEK_CUR)
- == (off_t)-1) {
- emsg(3, "Cannot lseek()");
- done(1);
- }
- }
- if (kflag == 0 || mfl == -2) {
- if ((mtst.st_mode&S_IFMT)!=S_IFCHR &&
- (mtst.st_mode&S_IFMT)!=S_IFBLK)
- break;
- newmedia(mfl == -1 ? errno : 0);
- if (mfl == -1) {
- mfl = -2;
- break;
- }
- if (fmttype & TYP_BAR)
- curpos = 512;
- }
- }
- mread();
- }
- aoffs += rd;
- return rd;
- }
- /*
- * Read a block of data from tape.
- */
- static ssize_t
- mread(void)
- {
- ssize_t ro, rt = 0;
- do {
- if ((ro = read(mt, blkbuf + rt, blksiz - rt)) <= 0) {
- if (ro < 0) {
- if (errno == EINTR)
- continue;
- mfl = -1;
- } else
- mfl = -2;
- if (rt > 0) {
- rt += ro;
- break;
- }
- curpos = blktop = 0;
- return ro;
- }
- rt += ro;
- poffs += ro;
- if (tapeblock == 0) {
- tapeblock = ro;
- if (!Bflag && !Cflag)
- blksiz = ro;
- }
- } while (rt < blksiz);
- curpos = 0;
- blocks += rt >> 9;
- bytes += rt & 0777;
- blktop = rt;
- return rt;
- }
- /*
- * Look what kind of tape or other archive media we are working on and
- * set the buffer size appropriately (if not specified by the user).
- */
- static void
- mstat(void)
- {
- if (fstat(mt, &mtst) < 0) {
- emsg(3, "Error during stat() of archive");
- done(1);
- }
- #if defined (__linux__)
- if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
- struct mtget mg;
- if (ioctl(mt, MTIOCGET, &mg) == 0)
- tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
- MT_ST_BLKSIZE_SHIFT;
- } else if ((mtst.st_mode&S_IFMT) == S_IFBLK) {
- /*
- * If using a block device, write blocks of the floppy
- * disk sector with direct i/o. This enables signals
- * after each block is written instead of being ~40
- * seconds in uninterruptible sleep when calling close()
- * later. For block devices other than floppies, use the
- * kernel defined i/o block size. For floppies, use direct
- * i/o even when reading since it is faster.
- */
- struct floppy_struct fs;
- int floppy = -1;
- int blkbsz;
- if (blksiz == 0) {
- if ((floppy = ioctl(mt, FDGETPRM, &fs)) == 0)
- blksiz = fs.sect * FD_SECTSIZE(&fs);
- #ifdef BLKBSZGET
- else if (ioctl(mt, BLKBSZGET, &blkbsz) == 0)
- blksiz = blkbsz;
- #endif /* BLKBSZGET */
- }
- #ifdef O_DIRECT
- if ((action == 'o' || floppy == 0) && blksiz != 0) {
- int flags;
- if ((flags = fcntl(mt, F_GETFL)) != -1)
- fcntl(mt, F_SETFL, flags | O_DIRECT);
- }
- }
- #endif /* O_DIRECT */
- #elif defined (__sun)
- if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
- struct mtdrivetype_request mr;
- static struct mtdrivetype md;
- mr.size = sizeof md;
- mr.mtdtp = &md;
- if (ioctl(mt, MTIOCGETDRIVETYPE, &mr) == 0)
- tapeblock = md.bsize;
- }
- #elif defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
- || defined (__DragonFly__) || defined (__APPLE__)
- if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
- struct mtget mg;
- if (ioctl(mt, MTIOCGET, &mg) == 0)
- tapeblock = mg.mt_blksiz;
- }
- #elif defined (__CYGWIN__)
- if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
- struct mtget mg;
- if (ioctl(mt, MTIOCGET, &mg) == 0)
- tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
- MT_ST_BLKSIZE_SHIFT;
- }
- #elif defined (__hpux) || defined (_AIX)
- #else /* SVR4.2MP */
- if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
- struct blklen bl;
- if (ioctl(mt, T_RDBLKLEN, &bl) == 0)
- /*
- * These are not the values we're interested in
- * (always 1 and 16M-1 for DAT/DDS tape drives).
- */
- tapeblock = 0;
- }
- #endif /* SVR4.2MP */
- if (blksiz == 0)
- switch (mtst.st_mode&S_IFMT) {
- case S_IFREG:
- case S_IFBLK:
- blksiz = 4096;
- break;
- case S_IFCHR:
- if (action == 'o' && !Aflag) {
- if (pax != PAX_TYPE_CPIO) {
- if (fmttype & TYP_PAX)
- blksiz = 5120;
- else if (fmttype & TYP_TAR)
- blksiz = 10240;
- else if (fmttype & TYP_CPIO)
- blksiz = 5120;
- else
- blksiz = 512;
- } else
- blksiz = tapeblock>0 ? tapeblock : 512;
- } else
- blksiz = tapeblock > 0 && tapeblock % 1024 ?
- tapeblock : tapeblock > 10240 ?
- tapeblock : 10240;
- break;
- default:
- blksiz = 512;
- }
- }
- /*
- * Skip tape data such that size becomes aligned to pad.
- */
- static int
- skippad(unsigned long long size, int pad)
- {
- char b[512];
- int to;
- if ((to = size % pad) != 0) {
- if (bread(b, pad - to) != pad - to)
- unexeoa();
- }
- return 0;
- }
- static int
- allzero(const char *bp, int n)
- {
- int i;
- for (i = 0; i < n; i++)
- if (bp[i] != '\0')
- return 0;
- return 1;
- }
- #define CACHESIZE 16
- static const char *
- getuser(uid_t uid)
- {
- static struct {
- char *name;
- uid_t uid;
- } cache[CACHESIZE];
- static int last;
- int i;
- struct passwd *pwd;
- const char *name;
- for (i = 0; i < CACHESIZE && cache[i].name; i++)
- if (cache[i].uid == uid)
- goto found;
- if ((pwd = getpwuid(uid)) != NULL)
- name = pwd->pw_name;
- else
- name = "";
- if (i >= CACHESIZE) {
- if (last >= CACHESIZE)
- last = 0;
- i = last++;
- }
- if (cache[i].name)
- free(cache[i].name);
- cache[i].name = sstrdup(name);
- cache[i].uid = uid;
- found: return cache[i].name[0] ? cache[i].name : NULL;
- }
- static const char *
- getgroup(gid_t gid)
- {
- static struct {
- char *name;
- gid_t gid;
- } cache[CACHESIZE];
- static int last;
- int i;
- struct group *grp;
- const char *name;
- for (i = 0; i < CACHESIZE && cache[i].name; i++)
- if (cache[i].gid == gid)
- goto found;
- if ((grp = getgrgid(gid)) != NULL)
- name = grp->gr_name;
- else
- name = "";
- if (i >= CACHESIZE) {
- if (last >= CACHESIZE)
- last = 0;
- i = last++;
- }
- if (cache[i].name)
- free(cache[i].name);
- cache[i].name = sstrdup(name);
- cache[i].gid = gid;
- found: return cache[i].name[0] ? cache[i].name : NULL;
- }
- /*
- * Return a version of the passed string that contains at most one '%d'
- * and no other printf formats.
- */
- char *
- oneintfmt(const char *op)
- {
- char *new, *np;
- int no = 0;
- np = new = smalloc(2 * strlen(op) + 1);
- do {
- if (*op == '%') {
- *np++ = *op++;
- if (*op != '%')
- if (*op != 'd' || no++)
- *np++ = '%';
- }
- *np++ = *op;
- } while (*op++);
- return new;
- }
- char *
- sstrdup(const char *op)
- {
- char *np;
- np = smalloc(strlen(op) + 1);
- strcpy(np, op);
- return np;
- }
- /*
- * Add this pattern to the extraction list with -i.
- */
- void
- addg(const char *pattern, int art)
- {
- struct glist *gp;
- gp = scalloc(1, sizeof *gp);
- if (pax == PAX_TYPE_CPIO && pattern[0] == '!') {
- gp->g_not = 1;
- pattern++;
- }
- gp->g_pat = sstrdup(pattern);
- gp->g_art = art;
- if (pax != PAX_TYPE_CPIO) {
- struct glist *gb = NULL, *gc;
- for (gc = patterns; gc; gc = gc->g_nxt)
- gb = gc;
- if (gb)
- gb->g_nxt = gp;
- else
- patterns = gp;
- } else {
- gp->g_nxt = patterns;
- patterns = gp;
- }
- }
- /*
- * Check if the file name s matches any of the given patterns.
- */
- static struct glist *
- want(struct file *f, struct glist **gb)
- {
- extern int gmatch(const char *, const char *);
- struct glist *gp;
- for (gp = patterns; gp; gp = gp->g_nxt) {
- if ((gmatch(f->f_name, gp->g_pat) != 0) ^ gp->g_not &&
- (pax_nflag == 0 || gp->g_gotcha == 0)) {
- return gp;
- }
- *gb = gp;
- }
- return NULL;
- }
- static void
- patfile(void)
- {
- struct iblok *ip;
- char *name = NULL;
- size_t namsiz = 0, namlen;
- if ((ip = ib_open(Eflag, 0)) == NULL)
- msg(3, -2, "Cannot open \"%s\" to read patterns\n", Eflag);
- while ((namlen = ib_getlin(ip, &name, &namsiz, srealloc)) != 0) {
- if (name[namlen-1] == '\n')
- name[--namlen] = '\0';
- addg(name, 0);
- }
- ib_close(ip);
- }
- void
- swap(char *b, size_t sz, int s8, int s16)
- {
- uint8_t u8;
- uint16_t u16;
- union types2 *t2;
- union types4 *t4;
- int i;
- if (s8) {
- for (i = 0; i < (sz >> 1); i++) {
- t2 = &((union types2 *)b)[i];
- u8 = t2->byte[0];
- t2->byte[0] = t2->byte[1];
- t2->byte[1] = u8;
- }
- }
- if (s16) {
- for (i = 0; i < (sz >> 2); i++) {
- t4 = &((union types4 *)b)[i];
- u16 = t4->sword[0];
- t4->sword[0] = t4->sword[1];
- t4->sword[1] = u16;
- }
- }
- }
- static int
- ckodd(long long size, int mod, const char *str, const char *fn)
- {
- if (size % mod) {
- msg(3, 0, "Cannot swap %s of \"%s\", odd number of %s\n",
- str, fn, str);
- errcnt++;
- return 1;
- }
- return 0;
- }
- /*
- * Interactive rename (-r option).
- */
- static int
- rname(char **oldp, size_t *olds)
- {
- char *new = NULL;
- size_t newsize = 0;
- int i, r;
- char c;
- fprintf(stderr, "Rename \"%s\"? ", *oldp);
- if (tty == 0)
- if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
- fcntl(tty, F_SETFD, FD_CLOEXEC) < 0)
- err: msg(3, 1, "Cannot read tty.\n");
- i = 0;
- while ((r = read(tty, &c, 1)) == 1 && c != '\n') {
- if (i+1 >= newsize)
- new = srealloc(new, newsize += 32);
- new[i++] = c;
- }
- if (r <= 0)
- goto err;
- if (new == NULL)
- return 0;
- new[i] = '\0';
- if (new[0] == '.' && new[1] == '\0') {
- free(new);
- } else {
- free(*oldp);
- *oldp = new;
- *olds = newsize;
- }
- return 1;
- }
- /*
- * Filter data from tape through the commands given in arg?.
- */
- static int
- redirect(const char *arg0, const char *arg1)
- {
- int pd[2];
- if (pipe(pd) < 0)
- return -1;
- switch (fork()) {
- case 0:
- if (tapeblock>=0 || lseek(mt, -blktop, SEEK_CUR) == (off_t)-1) {
- int xpd[2];
- if (pipe(xpd) == 0 && fork() == 0) {
- ssize_t rd, wo, wt;
- close(xpd[0]);
- do {
- wo = wt = 0;
- do {
- if ((wo = write(xpd[1],
- blkbuf + wt,
- blktop - wt))
- <= 0) {
- if (errno == EINTR)
- continue;
- _exit(0);
- }
- wt += wo;
- } while (wt < blktop);
- } while ((rd = mread()) >= 0);
- if (rd < 0) {
- emsg(3, "Read error on \"%s\"",
- Iflag && !sysv3 ?
- Iflag : "input");
- }
- _exit(0);
- } else {
- close(xpd[1]);
- dup2(xpd[0], 0);
- close(xpd[0]);
- }
- } else {
- dup2(mt, 0);
- }
- close(mt);
- dup2(pd[1], 1);
- close(pd[0]);
- close(pd[1]);
- execlp(arg0, arg0, arg1, NULL);
- fprintf(stderr, "%s: could not exec %s: %s\n",
- progname, arg0, strerror(errno));
- _exit(0177);
- /*NOTREACHED*/
- default:
- dup2(pd[0], mt);
- close(pd[0]);
- close(pd[1]);
- tapeblock = -1;
- break;
- case -1:
- return -1;
- }
- return 0;
- }
- /*
- * Get the name stored in a tar header. buf is expected to be at least
- * TPFXSIZ+TNAMSIZ+2 bytes.
- */
- static char *
- tnameof(struct tar_header *hp, char *buf)
- {
- const char *cp;
- char *bp = buf;
- if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR &&
- hp->t_prefix[0] != '\0') {
- cp = hp->t_prefix;
- while (cp < &hp->t_prefix[TPFXSIZ] && *cp)
- *bp++ = *cp++;
- if (bp > buf)
- *bp++ = '/';
- }
- cp = hp->t_name;
- while (cp < &hp->t_name[TNAMSIZ] && *cp)
- *bp++ = *cp++;
- *bp = '\0';
- return buf;
- }
- /*
- * Store fn as file name in a tar header.
- */
- static int
- tmkname(struct tar_header *hp, const char *fn)
- {
- const char *cp, *cs = NULL;
- for (cp = fn; *cp; cp++) {
- if (fmttype & TYP_USTAR && *cp == '/' && cp[1] != '\0' &&
- cp > fn && cp-fn <= TPFXSIZ)
- cs = cp;
- }
- if (fmttype == FMT_GNUTAR && cp - fn > 99) {
- writegnuname(fn, cp - fn + 1, 'L');
- cp = &fn[99];
- } else if (cp - (cs ? &cs[1] : fn) > TNAMSIZ) {
- if (fmttype & TYP_PAX && utf8(fn)) {
- paxrec |= PR_PATH;
- strcpy(hp->t_name, sequence());
- return 0;
- }
- msg(3, 0, "%s: file name too long\n", fn);
- return -1;
- }
- if (cs && cp - fn > TNAMSIZ) {
- memcpy(hp->t_prefix, fn, cs - fn);
- if (cs - fn < TPFXSIZ)
- hp->t_prefix[cs - fn] = '\0';
- memcpy(hp->t_name, &cs[1], cp - &cs[1]);
- if (cp - &cs[1] < TNAMSIZ)
- hp->t_name[cp - &cs[1]] = '\0';
- } else {
- memcpy(hp->t_name, fn, cp - fn);
- if (cp - fn < TNAMSIZ)
- hp->t_name[cp - fn] = '\0';
- }
- return 0;
- }
- /*
- * Get the link name of a tar header.
- */
- static void
- tlinkof(struct tar_header *hp, struct file *f)
- {
- const char *cp;
- char *bp;
- if (f->f_lsiz < TNAMSIZ+1)
- f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = TNAMSIZ+1);
- cp = hp->t_linkname;
- bp = f->f_lnam;
- while (cp < &hp->t_linkname[TNAMSIZ] && *cp)
- *bp++ = *cp++;
- *bp = '\0';
- }
- /*
- * Create the link name in a tar header.
- */
- static int
- tmklink(struct tar_header *hp, const char *fn)
- {
- const char *cp;
- for (cp = fn; *cp; cp++);
- if (fmttype == FMT_GNUTAR && cp - fn > 99) {
- writegnuname(fn, cp - fn + 1, 'K');
- cp = &fn[99];
- } else if (cp - fn > TNAMSIZ) {
- if (fmttype & TYP_PAX && utf8(fn)) {
- paxrec |= PR_LINKPATH;
- strcpy(hp->t_linkname, sequence());
- return 0;
- }
- msg(3, 0, "%s: linked name too long\n", fn);
- return -1;
- }
- memcpy(hp->t_linkname, fn, cp - fn);
- if (cp - fn < TNAMSIZ)
- hp->t_linkname[cp - fn] = '\0';
- return 0;
- }
- static int
- tlflag(struct stat *st)
- {
- if (fmttype & TYP_BAR) {
- switch (st->st_mode & S_IFMT) {
- case S_IFREG:
- case S_IFDIR:
- return '0';
- case S_IFLNK:
- return '2';
- default:
- return '3';
- }
- } else if (fmttype & TYP_USTAR) {
- switch (st->st_mode & S_IFMT) {
- case S_IFREG:
- return '0';
- case S_IFLNK:
- return '2';
- case S_IFCHR:
- return '3';
- case S_IFBLK:
- return '4';
- case S_IFDIR:
- return '5';
- case S_IFIFO:
- return '6';
- default:
- return -1;
- }
- } else {
- switch (st->st_mode & S_IFMT) {
- case S_IFREG:
- return '\0';
- case S_IFLNK:
- return '2';
- default:
- return -1;
- }
- }
- }
- /*
- * Ustar checksums are created using unsigned chars, as specified by
- * POSIX. Traditional tar implementations use signed chars. Some
- * implementations (notably SVR4 cpio derivatives) use signed chars
- * even for ustar archives, but this is clearly an implementation bug.
- */
- static void
- tchksum(union bincpio *bp)
- {
- uint32_t sum;
- char *cp;
- memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
- sum = 0;
- if (fmttype & TYP_USTAR)
- for (cp = bp->data; cp < &bp->data[512]; cp++)
- sum += *((unsigned char *)cp);
- else
- for (cp = bp->data; cp < &bp->data[512]; cp++)
- sum += *((signed char *)cp);
- sprintf(bp->Tdr.t_chksum, "%7.7o", sum);
- }
- static int
- tcssum(union bincpio *bp, int ustar)
- {
- uint32_t ssum = 0, usum = 0, osum;
- char ochk[sizeof bp->Tdr.t_chksum];
- char *cp;
- osum = rdoct(bp->Tdr.t_chksum, 8);
- memcpy(ochk, bp->Tdr.t_chksum, sizeof ochk);
- memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
- for (cp = bp->data; cp < &bp->data[512]; cp++) {
- ssum += *((signed char *)cp);
- usum += *((unsigned char *)cp);
- }
- memcpy(bp->Tdr.t_chksum, ochk, sizeof bp->Tdr.t_chksum);
- return ssum != osum && usum != osum;
- }
- static int
- trdsum(union bincpio *bp)
- {
- int i;
- if (fmttype & TYP_BAR)
- i = bcssum(bp);
- else
- i = tcssum(bp, fmttype & TYP_USTAR);
- if (i)
- msg(3, 0, "Bad header - checksum error.\n");
- return i;
- }
- static mode_t
- tifmt(int c)
- {
- switch (c) {
- default:
- case '\0':
- case '0':
- return S_IFREG;
- case '2':
- return S_IFLNK;
- case '3':
- return S_IFCHR;
- case '4':
- return S_IFBLK;
- case '5':
- return S_IFDIR;
- case '6':
- return S_IFIFO;
- }
- }
- /*
- * bar format support functions.
- */
- static void
- bchksum(union bincpio *bp)
- {
- uint32_t sum;
- char *cp;
- memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
- sum = 0;
- for (cp = bp->data; cp < &bp->data[512]; cp++)
- sum += *((signed char *)cp);
- sprintf(bp->Bdr.b_chksum, "%7.7o", sum);
- }
- static int
- bcssum(union bincpio *bp)
- {
- uint32_t sum, osum;
- char ochk[sizeof bp->Bdr.b_chksum];
- char *cp;
- osum = rdoct(bp->Bdr.b_chksum, 8);
- memcpy(ochk, bp->Bdr.b_chksum, sizeof ochk);
- memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
- sum = 0;
- for (cp = bp->data; cp < &bp->data[512]; cp++)
- sum += *((signed char *)cp);
- memcpy(bp->Bdr.b_chksum, ochk, sizeof bp->Bdr.b_chksum);
- return sum != osum;
- }
- static void
- blinkof(const char *cp, struct file *f, int namlen)
- {
- if (f->f_lsiz < 512)
- f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = 512);
- strcpy(f->f_lnam, &cp[SIZEOF_bar_header + namlen]);
- }
- static void
- dump_barhdr(void)
- {
- union bincpio bc;
- static int volno;
- static time_t now = -1;
- memset(&bc, 0, 512);
- sprintf(bc.Bdr.b_uid, "%d", myuid & 07777777);
- sprintf(bc.Bdr.b_gid, "%d", mygid & 07777777);
- bc.Bdr.b_size[0] = '0';
- memcpy(bc.Bdr.b_bar_magic, mag_bar, sizeof bc.Bdr.b_bar_magic);
- sprintf(bc.Bdr.b_volume_num, "%d", ++volno & 0777);
- bc.Bdr.b_compressed = '0';
- if (now == (time_t)-1)
- time(&now);
- sprintf(bc.Bdr.b_date, "%llo", now & 077777777777LL);
- bchksum(&bc);
- bwrite(bc.data, 512);
- }
- /*
- * Support for compressed bar format (any regular file is piped through zcat).
- */
- static pid_t zpid;
- static int
- zcreat(const char *name, mode_t mode)
- {
- int pd[2];
- int fd;
- if (pipe(pd) < 0)
- return -1;
- if ((fd = creat(name, mode)) < 0) {
- fd = errno;
- close(pd[0]);
- close(pd[1]);
- errno = fd;
- return -1;
- }
- switch (zpid = fork()) {
- case -1:
- return -1;
- case 0:
- dup2(pd[0], 0);
- dup2(fd, 1);
- close(pd[0]);
- close(pd[1]);
- close(fd);
- execlp("zcat", "zcat", NULL);
- _exit(0177);
- /*NOTREACHED*/
- }
- close(pd[0]);
- close(fd);
- sigset(SIGPIPE, SIG_IGN);
- return pd[1];
- }
- static int
- zclose(int fd)
- {
- int c, s;
- c = close(fd);
- while (waitpid(zpid, &s, 0) != zpid);
- return c != 0 || s != 0 ? -1 : 0;
- }
- /*
- * If using the -A option, device numbers that appear in the archive
- * are not reused for appended files. This avoids wrong hardlink
- * connections on extraction.
- *
- * In fact, this should be done even if we did not fake device and
- * inode numbers, since it is not guaranteed that the archive was
- * created on the same machine, or even on the same machine, inode
- * number could have changed after a file was unlinked.
- */
- static void
- markdev(dev_t dev)
- {
- struct dslot *dp, *dq = NULL;;
- for (dp = markeddevs; dp; dp = dp->d_nxt) {
- if (dp->d_dev == dev)
- return;
- dq = dp;
- }
- dp = scalloc(1, sizeof *dp);
- dp->d_dev = dev;
- if (markeddevs == NULL)
- markeddevs = dp;
- else
- dq->d_nxt = dp;
- }
- static int
- marked(dev_t dev)
- {
- struct dslot *dp;
- for (dp = markeddevs; dp; dp = dp->d_nxt)
- if (dp->d_dev == dev)
- return 1;
- return 0;
- }
- static void
- cantsup(int err, const char *file)
- {
- if (sysv3)
- msg(err ? 3 : 2, 0,
- "format can't support expanded types on %s\n",
- file);
- else
- msg(0, 0, "%s format can't support expanded types on %s\n",
- err ? "Error" : "Warning", file);
- errcnt++;
- }
- static void
- onint(int signo)
- {
- if (cur_ofile && cur_tfile && rename(cur_tfile, cur_ofile) < 0)
- emsg(3, "Cannot recover original \"%s\"", cur_ofile);
- if (cur_ofile && cur_tfile == NULL && unlink(cur_ofile) < 0)
- emsg(3, "Cannot remove incomplete \"%s\"", cur_ofile);
- exit(signo | 0200);
- }
- /*
- * Read the compressed zip data part for a file.
- */
- static int
- zipread(struct file *f, const char *tgt, int tfd, int doswap)
- {
- int val = 0;
- uint32_t crc = 0;
- if (f->f_gflag & FG_DESC) {
- if (f->f_cmethod != C_DEFLATED && f->f_cmethod != C_ENHDEFLD ||
- f->f_gflag & FG_CRYPT)
- msg(4, 1, "Cannot handle zip data descriptor\n");
- f->f_csize = 0x7FFFFFFFFFFFFFFFLL;
- f->f_st.st_size = 0x7FFFFFFFFFFFFFFFLL;
- } else if (tfd < 0)
- return skipfile(f);
- if (f->f_gflag & FG_CRYPT)
- return cantunzip(f, "encrypted");
- switch (f->f_cmethod) {
- case C_DEFLATED:
- case C_ENHDEFLD:
- val = zipinflate(f, tgt, tfd, doswap, &crc);
- break;
- case C_SHRUNK:
- val = zipunshrink(f, tgt, tfd, doswap, &crc);
- break;
- case C_IMPLODED:
- val = zipexplode(f, tgt, tfd, doswap, &crc);
- break;
- case C_REDUCED1:
- case C_REDUCED2:
- case C_REDUCED3:
- case C_REDUCED4:
- val = zipexpand(f, tgt, tfd, doswap, &crc);
- break;
- case C_TOKENIZED:
- return cantunzip(f, "tokenized");
- case C_DCLIMPLODED:
- val = zipblast(f, tgt, tfd, doswap, &crc);
- break;
- case C_BZIP2:
- #if USE_BZLIB
- val = zipunbz2(f, tgt, tfd, doswap, &crc);
- break;
- #else /* !USE_BZLIB */
- return cantunzip(f, "bzip2 compressed");
- #endif /* !USE_BZLIB */
- default:
- return cantunzip(f, "compressed");
- }
- if (f->f_gflag & FG_DESC)
- zipreaddesc(f);
- if (val == 0 && crc != f->f_chksum) {
- msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
- return -1;
- }
- return val;
- }
- /*
- * Read a zip data descriptor (i. e. a field after the compressed data
- * that contains the actual values for sizes and crc).
- */
- static void
- zipreaddesc(struct file *f)
- {
- if (f->f_oflag & OF_ZIP64) {
- struct zipddesc64 zd64;
- bread((char *)&zd64, SIZEOF_zipddesc64);
- if (memcmp(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds))
- msg(4, 1, "Invalid zip data descriptor\n");
- f->f_chksum = ple32(zd64.zd_crc32);
- f->f_dsize = f->f_st.st_size = ple64(zd64.zd_nsize) &
- 0xFFFFFFFFFFFFFFFFULL;
- f->f_csize = ple64(zd64.zd_csize) &
- 0xFFFFFFFFFFFFFFFFULL;
- } else {
- struct zipddesc zd;
- bread((char *)&zd, SIZEOF_zipddesc);
- if (memcmp(zd.zd_signature, mag_zipdds, sizeof mag_zipdds))
- msg(4, 1, "Invalid zip data descriptor\n");
- f->f_chksum = ple32(zd.zd_crc32);
- f->f_dsize = f->f_st.st_size = ple32(zd.zd_nsize)&0xFFFFFFFFUL;
- f->f_csize = ple32(zd.zd_csize)&0xFFFFFFFFUL;
- }
- }
- static int
- cantunzip(struct file *f, const char *method)
- {
- msg(3, 0, "Cannot unzip %s file \"%s\"\n", method, f->f_name);
- errcnt++;
- return skipfile(f);
- }
- /*
- * PC-DOS time format:
- *
- * 31 24 20 15 10 4 0
- * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
- * | | | | |
- * |year |months |days |hours |minutes |biseconds|
- *
- * refers to local time on the machine it was created on.
- */
- static time_t
- gdostime(const char *tp, const char *dp)
- {
- uint32_t v;
- struct tm tm;
- v = (int)(tp[0]&0377) +
- ((int)(tp[1]&0377) << 8) +
- ((int)(dp[0]&0377) << 16) +
- ((int)(dp[1]&0377) << 24);
- memset(&tm, 0, sizeof tm);
- tm.tm_sec = (v&0x1F) << 1;
- tm.tm_min = (v&0x7E0) >> 5;
- tm.tm_hour = (v&0xF800) >> 11;
- tm.tm_mday = ((v&0x1F0000) >> 16);
- tm.tm_mon = ((v&0x1E00000) >> 21) - 1;
- tm.tm_year = ((v&0xFE000000) >> 25) + 80;
- tm.tm_isdst = -1;
- return mktime(&tm);
- }
- static void
- mkdostime(time_t t, char *tp, char *dp)
- {
- uint32_t v;
- struct tm *tm;
- tm = localtime(&t);
- v = (tm->tm_sec >> 1) + (tm->tm_sec&1) + (tm->tm_min << 5) +
- (tm->tm_hour << 11) + (tm->tm_mday << 16) +
- ((tm->tm_mon+1) << 21) + ((tm->tm_year - 80) << 25);
- le16p(v&0x0000ffff, tp);
- le16p((v&0xffff0000) >> 16, dp);
- }
- /*
- * Read and interpret the zip extra field for a file.
- */
- static ssize_t
- ziprxtra(struct file *f, struct zip_header *z)
- {
- union zextra *x, *xp;
- short tag, size;
- ssize_t len;
- len = ple16(z->z_extralen)&0177777;
- if (len > 0) {
- x = smalloc(len);
- if (bread((char *)x, len) != len)
- return -1;
- if (len < 4)
- return len;
- xp = x;
- while (len > 0) {
- if (len < 4)
- return -1;
- tag = ple16(xp->Ze_gn.ze_gn_tag);
- size = (ple16(xp->Ze_gn.ze_gn_tsize)&0177777) + 4;
- switch (tag) {
- case mag_zip64f: /* ZIP64 extended information */
- if (size != SIZEOF_zextra_64 &&
- size != SIZEOF_zextra_64_a &&
- size != SIZEOF_zextra_64_b)
- break;
- if (f->f_st.st_size == 0xffffffff)
- f->f_st.st_size =
- ple64(xp->Ze_64.ze_64_nsize);
- if (f->f_csize == 0xffffffff)
- f->f_csize =
- ple64(xp->Ze_64.ze_64_csize);
- f->f_oflag |= OF_ZIP64;
- break;
- case 0x000d: /* PKWARE Unix Extra Field */
- if (size != SIZEOF_zextra_pk)
- break;
- f->f_st.st_atime = ple32(xp->Ze_pk.ze_pk_atime);
- f->f_st.st_mtime = ple32(xp->Ze_pk.ze_pk_mtime);
- f->f_st.st_uid = ple16(xp->Ze_pk.ze_pk_uid) &
- 0177777;
- f->f_st.st_gid = ple16(xp->Ze_pk.ze_pk_gid) &
- 0177777;
- break;
- case 0x5455: /* Extended Timestamp Extra Field */
- if (xp->Ze_et.ze_et_flags[0] & 1)
- f->f_st.st_atime =
- ple32(xp->Ze_et.ze_et_atime);
- if (xp->Ze_et.ze_et_flags[0] & 2)
- f->f_st.st_mtime =
- ple32(xp->Ze_et.ze_et_mtime);
- if (xp->Ze_et.ze_et_flags[0] & 3)
- f->f_st.st_ctime =
- ple32(xp->Ze_et.ze_et_ctime);
- break;
- case 0x5855: /* Info-ZIP Unix Extra Field #1 */
- if (size != SIZEOF_zextra_i1)
- break;
- f->f_st.st_atime = ple32(xp->Ze_i1.ze_i1_atime);
- f->f_st.st_mtime = ple32(xp->Ze_i1.ze_i1_mtime);
- f->f_st.st_uid = ple16(xp->Ze_i1.ze_i1_uid) &
- 0177777;
- f->f_st.st_gid = ple16(xp->Ze_i1.ze_i1_gid) &
- 0177777;
- break;
- case 0x7855: /* Info-ZIP Unix Extra Field #2 */
- if (size != SIZEOF_zextra_i2)
- break;
- f->f_st.st_uid = ple16(xp->Ze_i2.ze_i2_uid) &
- 0177777;
- f->f_st.st_gid = ple16(xp->Ze_i2.ze_i2_gid) &
- 0177777;
- break;
- case 0x756e: /* ASi Unix Extra Field */
- if (size < SIZEOF_zextra_as)
- break;
- f->f_st.st_mode = ple16(xp->Ze_as.ze_as_mode);
- f->f_st.st_uid = ple16(xp->Ze_as.ze_as_uid) &
- 0177777;
- f->f_st.st_gid = ple16(xp->Ze_as.ze_as_gid) &
- 0177777;
- if ((f->f_st.st_mode&S_IFMT) == S_IFLNK) {
- if (f->f_lsiz < size-14+1)
- f->f_lnam = srealloc(f->f_lnam,
- f->f_lsiz =
- size-18+1);
- memcpy(f->f_lnam, &((char *)xp)[18],
- size-18);
- f->f_lnam[size-18] = '\0';
- f->f_st.st_size = size-18;
- } else {
- f->f_st.st_rdev =
- ple32(xp->Ze_as.ze_as_sizdev);
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- }
- break;
- case mag_zipcpio:
- if (size != SIZEOF_zextra_cp)
- break;
- f->f_st.st_dev = ple32(xp->Ze_cp.ze_cp_dev);
- f->f_st.st_ino = ple32(xp->Ze_cp.ze_cp_ino);
- f->f_st.st_mode = ple32(xp->Ze_cp.ze_cp_mode);
- f->f_st.st_uid = ple32(xp->Ze_cp.ze_cp_uid) &
- 0xFFFFFFFFUL;
- f->f_st.st_gid = ple32(xp->Ze_cp.ze_cp_gid) &
- 0xFFFFFFFFUL;
- f->f_st.st_nlink = ple32(xp->Ze_cp.ze_cp_nlink)&
- 0xFFFFFFFFUL;
- f->f_st.st_rdev = ple32(xp->Ze_cp.ze_cp_rdev);
- f->f_rmajor = major(f->f_st.st_rdev);
- f->f_rminor = minor(f->f_st.st_rdev);
- f->f_st.st_mtime = ple32(xp->Ze_cp.ze_cp_mtime);
- f->f_st.st_atime = ple32(xp->Ze_cp.ze_cp_atime);
- break;
- }
- xp = (union zextra *)&((char *)xp)[size];
- len -= size;
- }
- free(x);
- }
- return len;
- }
- /*
- * Write the central directory and the end of a zip file.
- */
- static void
- ziptrailer(void)
- {
- struct zipstuff *zs;
- struct zipcentral zc;
- struct zipend ze;
- long long cpstart, cpend, entries = 0;
- size_t sz;
- cpstart = nwritten;
- for (zs = zipbulk; zs; zs = zs->zs_next) {
- entries++;
- memset(&zc, 0, SIZEOF_zipcentral);
- memcpy(zc.zc_signature, mag_zipctr, 4);
- zc.zc_versionmade[0] = 20;
- zc.zc_versionextr[0] = zs->zs_cmethod == 8 ? 20 : 10;
- mkdostime(zs->zs_mtime, zc.zc_modtime, zc.zc_moddate);
- le32p(zs->zs_crc32, zc.zc_crc32);
- le16p(zs->zs_cmethod, zc.zc_cmethod);
- le16p(zs->zs_gflag, zc.zc_gflag);
- /*
- * We flag files as created on PC-DOS / FAT filesystem
- * and thus set PC-DOS attributes here.
- */
- if ((zs->zs_mode&0222) == 0)
- zc.zc_external[0] |= 0x01; /* readonly attribute */
- if ((zs->zs_mode&S_IFMT) == S_IFDIR)
- zc.zc_external[0] |= 0x10; /* directory attr. */
- sz = strlen(zs->zs_name);
- le16p(sz, zc.zc_namelen);
- if (zs->zs_size >= 0xffffffff || zs->zs_csize >= 0xffffffff ||
- zs->zs_relative >= 0xffffffff) {
- struct zextra_64 zf;
- memset(&zf, 0, SIZEOF_zextra_64);
- le16p(mag_zip64f, zf.ze_64_tag);
- le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
- if ((zs->zs_mode&S_IFMT) == S_IFREG ||
- (zs->zs_mode&S_IFMT) == S_IFLNK) {
- le32p(0xffffffff, zc.zc_csize);
- le32p(0xffffffff, zc.zc_nsize);
- le64p(zs->zs_csize, zf.ze_64_csize);
- le64p(zs->zs_size, zf.ze_64_nsize);
- }
- le64p(zs->zs_relative, zf.ze_64_reloff);
- le32p(0xffffffff, zc.zc_relative);
- le16p(SIZEOF_zextra_64, zc.zc_extralen);
- bwrite((char *)&zc, SIZEOF_zipcentral);
- bwrite(zs->zs_name, sz);
- bwrite((char *)&zf, SIZEOF_zextra_64);
- } else {
- if ((zs->zs_mode&S_IFMT) == S_IFREG ||
- (zs->zs_mode&S_IFMT) == S_IFLNK) {
- le32p(zs->zs_csize, zc.zc_csize);
- le32p(zs->zs_size, zc.zc_nsize);
- }
- le32p(zs->zs_relative, zc.zc_relative);
- bwrite((char *)&zc, SIZEOF_zipcentral);
- bwrite(zs->zs_name, sz);
- }
- }
- cpend = nwritten;
- memset(&ze, 0, SIZEOF_zipend);
- memcpy(ze.ze_signature, mag_zipend, 4);
- if (cpend >= 0xffffffff || entries >= 0xffff) {
- struct zip64end z6;
- struct zip64loc z4;
- memset(&z6, 0, SIZEOF_zip64end);
- memcpy(z6.z6_signature, mag_zip64e, 4);
- le64p(SIZEOF_zip64end - 12, z6.z6_recsize);
- z6.z6_versionmade[0] = 20;
- z6.z6_versionextr[0] = 20;
- le64p(entries, z6.z6_thisentries);
- le64p(entries, z6.z6_allentries);
- le64p(cpend - cpstart, z6.z6_dirsize);
- le64p(cpstart, z6.z6_startsize);
- bwrite((char *)&z6, SIZEOF_zip64end);
- memset(&z4, 0, SIZEOF_zip64loc);
- memcpy(z4.z4_signature, mag_zip64l, 4);
- le64p(cpend, z4.z4_reloff);
- le32p(1, z4.z4_alldiskn);
- bwrite((char *)&z4, SIZEOF_zip64loc);
- le16p(0xffff, ze.ze_thisentries);
- le16p(0xffff, ze.ze_allentries);
- le32p(0xffffffff, ze.ze_dirsize);
- le32p(0xffffffff, ze.ze_startsize);
- } else {
- le16p(entries, ze.ze_thisentries);
- le16p(entries, ze.ze_allentries);
- le32p(cpend - cpstart, ze.ze_dirsize);
- le32p(cpstart, ze.ze_startsize);
- }
- bwrite((char *)&ze, SIZEOF_zipend);
- }
- /*
- * Store the data later needed for the central directory.
- */
- static void
- zipdefer(const char *fn, struct stat *st, long long relative,
- uint32_t crc, long long csize, const struct zip_header *zh)
- {
- struct zipstuff *zp;
- zp = scalloc(1, sizeof *zp);
- zp->zs_name = sstrdup(fn);
- zp->zs_size = st->st_size;
- zp->zs_mtime = st->st_mtime;
- zp->zs_mode = st->st_mode;
- zp->zs_relative = relative;
- zp->zs_cmethod = ple16(zh->z_cmethod);
- zp->zs_gflag = ple16(zh->z_gflag);
- zp->zs_csize = csize;
- zp->zs_crc32 = crc;
- zp->zs_next = zipbulk;
- zipbulk = zp;
- }
- #define ziptrlevel() ( \
- zipclevel == 01 ? 9 : /* maximum */ \
- zipclevel == 02 ? 3 : /* fast */ \
- zipclevel == 03 ? 1 : /* super fast */\
- /*zipclevel==00*/ 6 /* normal */ \
- )
- /*
- * Write (and compress) data for a regular file to a zip archive.
- */
- static int
- zipwrite(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
- uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
- {
- #if USE_ZLIB
- struct z_stream_s z;
- int i;
- size_t osize = 0;
- #endif /* USE_ZLIB */
- char *ibuf, *obuf = 0;
- if (st->st_size > 196608 || (ibuf = malloc(st->st_size)) == 0) {
- #if USE_ZLIB
- if (zipclevel < 04)
- return zipwdesc(fd, fn, st, bp, sz, dev, ino,
- crc, csize);
- #endif /* USE_ZLIB */
- return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
- }
- *csize = 0;
- if (read(fd, ibuf, st->st_size) != st->st_size) {
- free(ibuf);
- emsg(3, "Cannot read \"%s\"", fn);
- close(fd);
- return -1;
- }
- *crc = zipcrc(0, (unsigned char *)ibuf, st->st_size);
- #if USE_BZLIB
- if (zipclevel == 07) {
- unsigned int sb;
- if ((obuf = malloc(sb = st->st_size)) == 0)
- goto store;
- if (BZ2_bzBuffToBuffCompress(obuf, &sb, ibuf, st->st_size,
- 9, 0, 0) != BZ_OK)
- goto store;
- *csize = sb;
- bp->Zdr.z_cmethod[0] = C_BZIP2;
- bp->Zdr.z_version[0] = 0x2e;
- goto out;
- }
- #endif /* USE_BZLIB */
- if (zipclevel > 03)
- goto store;
- #if USE_ZLIB
- memset(&z, 0, sizeof z);
- if (deflateInit2(&z, ziptrlevel(), Z_DEFLATED, -15,
- 8, Z_DEFAULT_STRATEGY) < 0)
- goto store;
- z.next_in = (unsigned char *)ibuf;
- z.avail_in = z.total_in = st->st_size;
- do {
- if (z.avail_out == 0) {
- if ((obuf = realloc(obuf, osize += 4096)) == 0) {
- deflateEnd(&z);
- goto store;
- }
- z.next_out = (unsigned char *)&obuf[*csize];
- z.avail_out = osize - *csize;
- }
- if ((i = deflate(&z, z.avail_in ? Z_NO_FLUSH : Z_FINISH)) < 0) {
- deflateEnd(&z);
- goto store;
- }
- *csize = osize - z.avail_out;
- } while (z.avail_in || i != Z_STREAM_END);
- deflateEnd(&z);
- if (*csize < st->st_size) {
- bp->Zdr.z_cmethod[0] = C_DEFLATED;
- bp->Zdr.z_gflag[0] |= zipclevel << 1;
- bp->Zdr.z_version[0] = 20;
- } else
- #endif /* USE_ZLIB */
- store: *csize = st->st_size;
- #if USE_BZLIB
- out:
- #endif /* USE_BZLIB */
- le32p(*crc, bp->Zdr.z_crc32);
- le32p(*csize, bp->Zdr.z_csize);
- bwrite((char *)bp, SIZEOF_zip_header);
- bwrite(fn, sz);
- zipwxtra(fn, st, dev, ino);
- switch (bp->Zdr.z_cmethod[0]) {
- case C_DEFLATED:
- case C_BZIP2:
- bwrite(obuf, *csize);
- break;
- default:
- bwrite(ibuf, *csize);
- }
- free(ibuf);
- free(obuf);
- close(fd);
- return 0;
- }
- /*
- * Write and compress data to a zip archive for a file that is to large
- * too be kept in memory. If there is an error with the temporary file
- * (e. g. no space left on device), the file is stored in uncompressed
- * form.
- */
- static int
- zipwtemp(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
- uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
- {
- static int tf = -1;
- static char tlate[] = "/var/tmp/cpioXXXXXX";
- char ibuf[32768];
- #if USE_ZLIB || USE_BZLIB
- char obuf[32768];
- #endif /* USE_ZLIB || USE_BZLIB */
- struct zextra_64 zf;
- struct zextra_64 *zfp = 0;
- long long size = st->st_size;
- const char *sname;
- int cuse, sf;
- ssize_t rd;
- *csize = 0;
- *crc = 0;
- #if USE_ZLIB || USE_BZLIB
- if (tf < 0) {
- if ((tf = mkstemp(tlate)) >= 0)
- unlink(tlate);
- } else if (lseek(tf, 0, SEEK_SET) != 0) {
- close(tf);
- tf = -1;
- }
- #endif /* USE_ZLIB || USE_BZLIB */
- #if USE_ZLIB
- if (zipclevel < 04) {
- struct z_stream_s z;
- memset(&z, 0, sizeof z);
- if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
- -15, 8, Z_DEFAULT_STRATEGY)) < 0)
- goto store;
- do {
- if (z.avail_in == 0 && size > 0) {
- if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
- emsg(3, "Cannot read \"%s\"", fn);
- close(fd);
- ftruncate(tf, 0);
- return -1;
- }
- z.next_in = (unsigned char *)ibuf;
- z.avail_in = z.total_in = rd;
- size -= rd;
- *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
- }
- if (z.next_out == NULL || (char *)z.next_out > obuf) {
- if (z.next_out && tf >= 0) {
- if (write(tf, obuf,
- (char *)z.next_out-obuf) !=
- (char *)z.next_out-obuf) {
- close(tf);
- tf = -1;
- }
- *csize += (char *)z.next_out - obuf;
- }
- z.next_out = (unsigned char *)obuf;
- z.avail_out = sizeof obuf;
- }
- if (cuse >= 0 && cuse != Z_STREAM_END)
- cuse = deflate(&z,
- z.avail_in?Z_NO_FLUSH:Z_FINISH);
- else
- z.avail_in = 0;
- } while (size>0 || (char *)z.next_out>obuf ||
- cuse>=0 && cuse!=Z_STREAM_END);
- deflateEnd(&z);
- goto out;
- }
- #endif /* USE_ZLIB */
- #if USE_BZLIB
- if (zipclevel == 07) {
- bz_stream bs;
- int ok, on;
- memset(&bs, sizeof bs, 0);
- if ((ok = BZ2_bzCompressInit(&bs, 9, 0, 0)) != BZ_OK)
- goto store;
- cuse = 1;
- do {
- if (bs.avail_in == 0 && size > 0) {
- if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
- emsg(3, "Cannot read \"%s\"", fn);
- close(fd);
- ftruncate(tf, 0);
- return -1;
- }
- bs.next_in = ibuf;
- bs.avail_in = rd;
- size -= rd;
- *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
- }
- if (bs.next_out == NULL || bs.next_out > obuf) {
- if (bs.next_out && tf >= 0) {
- on = bs.next_out - obuf;
- if (write(tf, obuf, on) != on) {
- close(tf);
- tf = -1;
- }
- *csize += on;
- }
- bs.next_out = obuf;
- bs.avail_out = sizeof obuf;
- }
- if (ok != BZ_STREAM_END) {
- switch (ok = BZ2_bzCompress(&bs,
- bs.avail_in?BZ_RUN:BZ_FINISH)) {
- case BZ_RUN_OK:
- case BZ_FINISH_OK:
- case BZ_STREAM_END:
- break;
- default:
- msg(3, 1, "Compression error %d "
- "on \"%s\"\n", ok, fn);
- close(fd);
- return -1;
- }
- }
- } while (size > 0 || bs.next_out > obuf || ok != BZ_STREAM_END);
- BZ2_bzCompressEnd(&bs);
- goto out;
- }
- #endif /* USE_BZLIB */
- store: cuse = -1;
- while (size > 0) {
- if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
- emsg(3, "Cannot read \"%s\"", fn);
- close(fd);
- return -1;
- }
- size -= rd;
- *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
- }
- out: if (tf >= 0 && cuse >= 0 && *csize < st->st_size) {
- if (zipclevel == 07) {
- bp->Zdr.z_cmethod[0] = C_BZIP2;
- bp->Zdr.z_version[0] = 0x2e;
- } else {
- bp->Zdr.z_cmethod[0] = C_DEFLATED;
- bp->Zdr.z_gflag[0] |= zipclevel << 1;
- bp->Zdr.z_version[0] = 20;
- }
- sf = tf;
- sname = tlate;
- } else {
- *csize = st->st_size;
- sf = fd;
- sname = fn;
- }
- if ((lseek(sf, 0, SEEK_SET)) != 0) {
- emsg(3, "Cannot rewind \"%s\"", sname);
- errcnt++;
- close(fd);
- ftruncate(tf, 0);
- return -1;
- }
- le32p(*crc, bp->Zdr.z_crc32);
- if (st->st_size >= 0xffffffff || *csize >= 0xffffffff) {
- int n;
- zfp = &zf;
- memset(&zf, 0, SIZEOF_zextra_64);
- le16p(mag_zip64f, zf.ze_64_tag);
- le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
- le64p(st->st_size, zf.ze_64_nsize);
- le64p(*csize, zf.ze_64_csize);
- le32p(0xffffffff, bp->Zdr.z_csize);
- le32p(0xffffffff, bp->Zdr.z_nsize);
- n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
- le16p(n, bp->Zdr.z_extralen);
- } else
- le32p(*csize, bp->Zdr.z_csize);
- bwrite((char *)bp, SIZEOF_zip_header);
- bwrite(fn, sz);
- if (zfp)
- bwrite((char *)zfp, SIZEOF_zextra_64);
- zipwxtra(fn, st, dev, ino);
- size = *csize;
- while (size) {
- if ((rd=read(sf, ibuf, size>sizeof ibuf?sizeof ibuf:size)) <= 0)
- msg(3, 1, "Cannot read \"%s\"\n", sname);
- bwrite(ibuf, rd);
- size -= rd;
- }
- ftruncate(tf, 0);
- close(fd);
- return 0;
- }
- #if USE_ZLIB
- /*
- * Write a zip archive entry using the data descriptor structure.
- */
- static int
- zipwdesc(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
- uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
- {
- struct zextra_64 zf;
- struct zextra_64 *zfp = 0;
- char ibuf[32768], obuf[32768];
- long long size = st->st_size;
- ssize_t rd;
- struct z_stream_s z;
- int cuse;
- *csize = 0;
- *crc = 0;
- memset(&z, 0, sizeof z);
- if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
- -15, 8, Z_DEFAULT_STRATEGY)) < 0)
- return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
- bp->Zdr.z_cmethod[0] = C_DEFLATED;
- bp->Zdr.z_gflag[0] |= zipclevel << 1 | FG_DESC;
- bp->Zdr.z_version[0] = 20;
- /*
- * RFC 1951 states that deflate compression needs 5 bytes additional
- * space per 32k block in the worst case. Thus a compressed size
- * greater than 4G-1 can be reached if at least 131052 blocks are
- * used.
- */
- if (st->st_size >= 131052LL*32768) {
- int n;
- zfp = &zf;
- memset(&zf, 0, SIZEOF_zextra_64);
- le16p(mag_zip64f, zf.ze_64_tag);
- le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
- le32p(0xffffffff, bp->Zdr.z_csize);
- le32p(0xffffffff, bp->Zdr.z_nsize);
- n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
- le16p(n, bp->Zdr.z_extralen);
- }
- bwrite((char *)bp, SIZEOF_zip_header);
- bwrite(fn, sz);
- if (zfp)
- bwrite((char *)zfp, SIZEOF_zextra_64);
- zipwxtra(fn, st, dev, ino);
- do {
- if (z.avail_in == 0 && size > 0) {
- if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
- emsg(3, "Cannot read \"%s\"", fn);
- st->st_size -= size;
- size = 0; /* can't simply stop here */
- rd = 0; /* no data */
- }
- z.next_in = (unsigned char *)ibuf;
- z.avail_in = z.total_in = rd;
- size -= rd;
- *crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
- }
- if (z.next_out == NULL || (char *)z.next_out > obuf) {
- if (z.next_out) {
- bwrite(obuf, (char *)z.next_out - obuf);
- *csize += (char *)z.next_out - obuf;
- }
- z.next_out = (unsigned char *)obuf;
- z.avail_out = sizeof obuf;
- }
- if (cuse >= 0 && cuse != Z_STREAM_END)
- cuse = deflate(&z, z.avail_in?Z_NO_FLUSH:Z_FINISH);
- else
- z.avail_in = 0;
- } while (size > 0 || (char *)z.next_out > obuf ||
- cuse >= 0 && cuse != Z_STREAM_END);
- deflateEnd(&z);
- if (zfp) {
- struct zipddesc64 zd64;
- memcpy(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds);
- le32p(*crc, zd64.zd_crc32);
- le64p(st->st_size, zd64.zd_nsize);
- le64p(*csize, zd64.zd_csize);
- bwrite((char *)&zd64, SIZEOF_zipddesc64);
- } else {
- struct zipddesc zd;
- memcpy(zd.zd_signature, mag_zipdds, sizeof mag_zipdds);
- le32p(*crc, zd.zd_crc32);
- le32p(st->st_size, zd.zd_nsize);
- le32p(*csize, zd.zd_csize);
- bwrite((char *)&zd, SIZEOF_zipddesc);
- }
- close(fd);
- return 0;
- }
- #endif /* USE_ZLIB */
- /*
- * Write the extra fields for a file to a zip archive (currently
- * our own field type). Note that the z_extralen field in the file
- * header must correspond to the size of the data written here.
- */
- static int
- zipwxtra(const char *fn, struct stat *st, uint32_t dev, uint32_t ino)
- {
- struct zextra_cp z;
- memset(&z, 0, SIZEOF_zextra_cp);
- le16p(mag_zipcpio, z.ze_cp_tag);
- le16p(SIZEOF_zextra_cp - 4, z.ze_cp_tsize);
- le32p(dev, z.ze_cp_mode);
- le32p(ino, z.ze_cp_ino);
- le32p(st->st_mode&(S_IFMT|07777), z.ze_cp_mode);
- le32p(st->st_uid, z.ze_cp_uid);
- le32p(st->st_gid, z.ze_cp_gid);
- le32p(st->st_nlink, z.ze_cp_nlink);
- le32p(st->st_rdev, z.ze_cp_rdev);
- le32p(st->st_mtime, z.ze_cp_mtime);
- le32p(st->st_atime, z.ze_cp_atime);
- bwrite((char *)&z, SIZEOF_zextra_cp);
- return SIZEOF_zextra_cp;
- }
- static void
- zipinfo(struct file *f)
- {
- const char *cp;
- char b[5];
- int i;
- printf(" %7llu", f->f_csize);
- if (f->f_dsize) {
- i = f->f_csize*100 / f->f_dsize;
- i += f->f_csize*200 / f->f_dsize & 1;
- i = 100 - i;
- } else
- i = 0;
- printf(" %3d%%", i);
- switch (f->f_cmethod) {
- case C_STORED:
- cp = "stor";
- break;
- case C_SHRUNK:
- cp = "shrk";
- break;
- case C_REDUCED1:
- cp = "re:1";
- break;
- case C_REDUCED2:
- cp = "re:2";
- break;
- case C_REDUCED3:
- cp = "re:3";
- break;
- case C_REDUCED4:
- cp = "re:4";
- break;
- case C_IMPLODED:
- b[0] = 'i';
- b[1] = f->f_gflag & FG_BIT1 ? '8' : '4';
- b[2] = ':';
- b[3] = f->f_gflag & FG_BIT2 ? '3' : '2';
- b[4] = '\0';
- cp = b;
- break;
- case C_TOKENIZED:
- cp = "tokn";
- break;
- case C_DEFLATED:
- b[0] = 'd', b[1] = 'e', b[2] = 'f', b[4] = '\0';
- if (f->f_gflag & FG_BIT2)
- b[3] = f->f_gflag & FG_BIT1 ? 'S' : 'F';
- else
- b[3] = f->f_gflag & FG_BIT1 ? 'X' : 'N';
- cp = b;
- break;
- case C_ENHDEFLD:
- cp = "edef";
- break;
- case C_DCLIMPLODED:
- cp = "dcli";
- break;
- case C_BZIP2:
- cp = "bz2 ";
- break;
- default:
- snprintf(b, sizeof b, "%4.4X", f->f_cmethod);
- cp = b;
- }
- printf(" %s", cp);
- }
- #if USE_BZLIB
- int
- zipunbz2(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
- {
- bz_stream bs;
- long long isize = f->f_csize;
- char ibuf[4096], obuf[8192];
- int in, on, val = 0, ok;
- memset(&bs, 0, sizeof bs);
- if ((ok = BZ2_bzDecompressInit(&bs, 0, 0)) != BZ_OK) {
- msg(3, 0, "bzip2 initialization error %d on \"%s\"\n",
- ok, f->f_name);
- errcnt++;
- return skipfile(f);
- }
- while (isize > 0 || ok == BZ_OK) {
- if (bs.avail_in == 0 && isize > 0) {
- in = sizeof ibuf < isize ? sizeof ibuf : isize;
- isize -= in;
- if (bread(ibuf, in) != in)
- unexeoa();
- if (doswap)
- swap(ibuf, in, bflag || sflag, bflag || Sflag);
- bs.next_in = ibuf;
- bs.avail_in = in;
- }
- if (ok == BZ_OK) {
- bs.next_out = obuf;
- bs.avail_out = sizeof obuf;
- switch (ok = BZ2_bzDecompress(&bs)) {
- case BZ_OK:
- case BZ_STREAM_END:
- on = sizeof obuf - bs.avail_out;
- if (tfd >= 0 && write(tfd, obuf, on) != on) {
- emsg(3, "Cannot write \"%s\"", tgt);
- tfd = -1;
- val = 1;
- }
- *crc = zipcrc(*crc, (unsigned char *)obuf, on);
- break;
- default:
- msg(3, 0, "compression error %d on \"%s\"\n",
- ok, f->f_name);
- errcnt++;
- val = 1;
- }
- }
- }
- BZ2_bzDecompressEnd(&bs);
- return val;
- }
- #endif /* USE_BZLIB */
- struct blasthow {
- struct file *bh_f;
- const char *bh_tgt;
- long long bh_isize;
- uint32_t *bh_crc;
- int bh_tfd;
- int bh_doswap;
- int bh_val;
- };
- static unsigned
- blastin(void *how, unsigned char **buf)
- {
- const int chunk = 16384;
- static unsigned char *hold;
- struct blasthow *bp = how;
- unsigned sz;
- if (bp->bh_isize <= 0)
- return 0;
- if (hold == NULL)
- hold = smalloc(chunk);
- sz = bp->bh_isize > chunk ? chunk : bp->bh_isize;
- bp->bh_isize -= sz;
- if (bread((char *)hold, sz) != sz)
- unexeoa();
- if (bp->bh_doswap)
- swap((char *)hold, sz, bflag || sflag, bflag || Sflag);
- *buf = hold;
- return sz;
- }
- static int
- blastout(void *how, unsigned char *buf, unsigned len)
- {
- struct blasthow *bp = how;
- if (bp->bh_tfd >= 0 && write(bp->bh_tfd, buf, len) != len) {
- emsg(3, "Cannot write \"%s\"", bp->bh_tgt);
- bp->bh_tfd = -1;
- bp->bh_val = 1;
- }
- *bp->bh_crc = zipcrc(*bp->bh_crc, buf, len);
- return 0;
- }
- int
- zipblast(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
- {
- struct blasthow bh;
- int n;
- bh.bh_f = f;
- bh.bh_tgt = tgt;
- bh.bh_isize = f->f_csize;
- bh.bh_crc = crc;
- bh.bh_tfd = tfd;
- bh.bh_doswap = doswap;
- bh.bh_val = 0;
- switch (n = blast(blastin, &bh, blastout, &bh)) {
- case 0:
- break;
- default:
- msg(3, 0, "compression error %d on \"%s\"\n", n, f->f_name);
- errcnt++;
- bh.bh_val = 1;
- }
- while (bh.bh_isize) {
- char buf[4096];
- unsigned n;
- n = bh.bh_isize > sizeof buf ? sizeof buf : bh.bh_isize;
- if (bread(buf, n) != n)
- unexeoa();
- bh.bh_isize -= n;
- }
- return bh.bh_val;
- }
- /*
- * The SGI -K format was introduced with SGI's IRIX 5.X. It is essentially
- * a slightly extended binary format. The known additions are:
- *
- * - If the major or minor st_rdev device number exceeds the limit of
- * 255 imposed by the 16-bit c_rdev field, this field is set to 0xFFFF
- * and st_rdev is stored in the c_filesize 32-bit field. The first 14
- * bits of this field compose the major device number, the minor is
- * stored in the remaining 18 bits. This enables implementations to
- * read the modified format without special support if they ignore
- * the size of device files; otherwise they will try to read a lot
- * of archive data and fail.
- *
- * - If the file is larger than 2 GB - 1 byte, two nearly identical
- * archive headers are stored for it. The only difference is in
- * the c_filesize field:
- *
- * [first header: file size X times 2 GB]
- * [first data part: X times 2 GB]
- * [second header: file size modulo 2 GB]
- * [second data part: rest of data]
- *
- * The first header can be recognized by a negative c_filesize. A
- * value of 0xFFFFFFFF means that 2 GB follow, 0xFFFFFFFE -> 4 GB
- * 0xFFFFFFFD -> 6 GB, 0xFFFFFFFC -> 8 GB, and so forth. The second
- * is a standard binary cpio header, although the following data is
- * meant to be appended to the preceding file.
- *
- * It is important to note that padding follows the number in
- * c_filesize, not the amount of data written; thus all data parts
- * with odd c_filesize fields (0xFFFFFFFF = 2+ GB, 0xFFFFFFFD = 6+ GB
- * etc.) cause the following archive entries to be aligned on an odd
- * offset. This seems to be an implementation artifact but has to be
- * followed for compatibility.
- *
- * This extension seems a bit weird since no known cpio implementation
- * is able to read these archive entries without special support. Thus
- * a more straightforward extension (such as storing the size just past
- * the file name) would well have had the same effect. Nevertheless,
- * the cpio -K format is useful, so it is implemented here.
- *
- * --Note that IRIX 6.X tar also has a -K option. This option extends
- * the tar format in essentially the same way as the second extension
- * to cpio described above. Unfortunately, the result is definitively
- * broken. Contrasting to the binary cpio format, the standard POSIX
- * tar format is well able to hold files of size 0xFFFFFFFF and below
- * in a regular manner. Thus, a tar -K archive entry is _exactly_ the
- * same as two regular POSIX tar entries for the same file. And this
- * situation even occurs on a regular basis with tar -r! For this
- * reason, we do not implement the IRIX tar -K format and will probably
- * never do so unless it is changed (the tar format really has a lot
- * of options to indicate extensions that might be used for this).
- *
- * Many thanks to Sven Mascheck who made archiving tests on the IRIX
- * machine.
- */
- /*
- * This function reads the second header of a SGI -K format archive.
- */
- static void
- readK2hdr(struct file *f)
- {
- struct file n;
- union bincpio bc;
- n.f_name = n.f_lnam = NULL;
- n.f_nsiz = n.f_lsiz = 0;
- readhdr(&n, &bc);
- f->f_Krest = n.f_st.st_size;
- f->f_dsize = f->f_st.st_size = n.f_st.st_size + f->f_Kbase;
- f->f_Kbase = 0;
- free(n.f_name);
- free(n.f_lnam);
- }
- /*
- * Read the data of a GNU filename extra header.
- */
- static int
- readgnuname(char **np, size_t *sp, long length)
- {
- if (length > SANELIMIT)
- return -1;
- if (*sp == 0 || *sp <= length)
- *np = srealloc(*np, *sp = length+1);
- bread(*np, length);
- (*np)[length] = '\0';
- skippad(length, 512);
- return 0;
- }
- /*
- * Write a GNU filename extra header and its data.
- */
- static void
- writegnuname(const char *fn, long length, int flag)
- {
- union bincpio bc;
- memset(bc.data, 0, 512);
- strcpy(bc.Tdr.t_name, "././@LongLink");
- sprintf(bc.Tdr.t_mode, "%7.7o", 0);
- sprintf(bc.Tdr.t_uid, "%7.7o", 0);
- sprintf(bc.Tdr.t_gid, "%7.7o", 0);
- sprintf(bc.Tdr.t_size, "%11.11lo", length);
- sprintf(bc.Tdr.t_mtime, "%11.11lo", 0L);
- bc.Tdr.t_linkflag = flag;
- memcpy(bc.Tdr.t_magic, mag_gnutar, 8);
- strcpy(bc.Tdr.t_uname, "root");
- strcpy(bc.Tdr.t_gname, "root");
- tchksum(&bc);
- bwrite(bc.data, 512);
- bwrite(fn, length);
- length %= 512;
- memset(bc.data, 0, 512 - length);
- bwrite(bc.data, 512 - length);
- }
- /*
- * POSIX.1-2001 pax format support.
- */
- static void
- tgetpax(struct tar_header *tp, struct file *f)
- {
- char *keyword, *value;
- char *block, *bp;
- long long n;
- enum paxrec pr;
- n = rdoct(tp->t_size, 12);
- bp = block = smalloc(n+1);
- bread(block, n);
- skippad(n, 512);
- block[n] = '\0';
- while (bp < &block[n]) {
- int c;
- pr = tgetrec(&bp, &keyword, &value);
- switch (pr) {
- case PR_ATIME:
- f->f_st.st_atime = strtoll(value, NULL, 10);
- break;
- case PR_GID:
- f->f_st.st_gid = strtoll(value, NULL, 10);
- break;
- case PR_LINKPATH:
- c = strlen(value);
- if (f->f_lnam == NULL || f->f_lsiz < c+1) {
- f->f_lsiz = c+1;
- f->f_lnam = srealloc(f->f_lnam, c+1);
- }
- strcpy(f->f_lnam, value);
- break;
- case PR_MTIME:
- f->f_st.st_mtime = strtoll(value, NULL, 10);
- break;
- case PR_PATH:
- c = strlen(value);
- if (f->f_name == NULL || f->f_nsiz < c+1) {
- f->f_nsiz = c+1;
- f->f_name = srealloc(f->f_name, c+1);
- }
- strcpy(f->f_name, value);
- break;
- case PR_SIZE:
- f->f_st.st_size = strtoll(value, NULL, 10);
- break;
- case PR_UID:
- f->f_st.st_uid = strtoll(value, NULL, 10);
- break;
- case PR_SUN_DEVMAJOR:
- f->f_rmajor = strtoll(value, NULL, 10);
- break;
- case PR_SUN_DEVMINOR:
- f->f_rminor = strtoll(value, NULL, 10);
- break;
- }
- paxrec |= pr;
- }
- if (tp->t_linkflag == 'g') {
- globrec = paxrec & ~(PR_LINKPATH|PR_PATH|PR_SIZE);
- globst = f->f_st;
- }
- free(block);
- }
- static enum paxrec
- tgetrec(char **bp, char **keyword, char **value)
- {
- char *x;
- long n = 0;
- enum paxrec pr;
- *keyword = "";
- *value = "";
- while (**bp && (n = strtol(*bp, &x, 10)) <= 0 && (*x!=' ' || *x!='\t'))
- do
- (*bp)++;
- while (**bp && **bp != '\n');
- if (*x == '\0' || **bp == '\0') {
- (*bp)++;
- return PR_NONE;
- }
- while (x < &(*bp)[n] && (*x == ' ' || *x == '\t'))
- x++;
- if (x == &(*bp)[n] || *x == '=')
- goto out;
- *keyword = x;
- while (x < &(*bp)[n] && *x != '=')
- x++;
- if (x == &(*bp)[n])
- goto out;
- *x = '\0';
- if (&x[1] < &(*bp)[n])
- *value = &x[1];
- (*bp)[n-1] = '\0';
- out: *bp = &(*bp)[n];
- if (strcmp(*keyword, "atime") == 0)
- pr = PR_ATIME;
- else if (strcmp(*keyword, "gid") == 0)
- pr = PR_GID;
- else if (strcmp(*keyword, "linkpath") == 0)
- pr = PR_LINKPATH;
- else if (strcmp(*keyword, "mtime") == 0)
- pr = PR_MTIME;
- else if (strcmp(*keyword, "path") == 0)
- pr = PR_PATH;
- else if (strcmp(*keyword, "size") == 0)
- pr = PR_SIZE;
- else if (strcmp(*keyword, "uid") == 0)
- pr = PR_UID;
- else if (strcmp(*keyword, "SUN.devmajor") == 0)
- pr = PR_SUN_DEVMAJOR;
- else if (strcmp(*keyword, "SUN.devminor") == 0)
- pr = PR_SUN_DEVMINOR;
- else
- pr = PR_NONE;
- return pr;
- }
- static void
- wrpax(const char *longname, const char *linkname, struct stat *sp)
- {
- union bincpio bc;
- char *pdata = NULL;
- long psize = 0, pcur = 0;
- long long blocks;
- memset(bc.data, 0, 512);
- if (paxrec & PR_ATIME)
- addrec(&pdata, &psize, &pcur, "atime", NULL, sp->st_atime);
- if (paxrec & PR_GID)
- addrec(&pdata, &psize, &pcur, "gid", NULL, sp->st_gid);
- if (paxrec & PR_LINKPATH)
- addrec(&pdata, &psize, &pcur, "linkpath", linkname, 0);
- if (paxrec & PR_MTIME)
- addrec(&pdata, &psize, &pcur, "mtime", NULL, sp->st_mtime);
- if (paxrec & PR_PATH)
- addrec(&pdata, &psize, &pcur, "path", longname, 0);
- if (paxrec & PR_SIZE)
- addrec(&pdata, &psize, &pcur, "size", NULL, sp->st_size);
- if (paxrec & PR_UID)
- addrec(&pdata, &psize, &pcur, "uid", NULL, sp->st_uid);
- if (paxrec & PR_SUN_DEVMAJOR)
- addrec(&pdata, &psize, &pcur, "SUN.devmajor", NULL,
- major(sp->st_rdev));
- if (paxrec & PR_SUN_DEVMINOR)
- addrec(&pdata, &psize, &pcur, "SUN.devminor", NULL,
- minor(sp->st_rdev));
- paxnam(&bc.Tdr, longname);
- sprintf(bc.Tdr.t_mode, "%7.7o", fmttype==FMT_SUN ? 0444|S_IFREG : 0444);
- sprintf(bc.Tdr.t_uid, "%7.7o", 0);
- sprintf(bc.Tdr.t_gid, "%7.7o", 0);
- sprintf(bc.Tdr.t_size, "%11.11lo", pcur);
- sprintf(bc.Tdr.t_mtime, "%11.11o", 0);
- strcpy(bc.Tdr.t_magic, "ustar");
- bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
- strcpy(bc.Tdr.t_uname, "root");
- strcpy(bc.Tdr.t_gname, "root");
- bc.Tdr.t_linkflag = fmttype==FMT_SUN ? 'X' : 'x';
- tchksum(&bc);
- bwrite(bc.data, 512);
- memset(&pdata[pcur], 0, psize - pcur);
- blocks = (pcur + (512-1)) / 512;
- bwrite(pdata, blocks * 512);
- free(pdata);
- }
- static void
- addrec(char **pdata, long *psize, long *pcur,
- const char *keyword, const char *sval, long long lval)
- {
- char dval[25], xval[25];
- long od, d, r;
- if (sval == 0) {
- sprintf(xval, "%lld", lval);
- sval = xval;
- }
- r = strlen(keyword) + strlen(sval) + 3;
- d = 0;
- do {
- od = d;
- d = sprintf(dval, "%ld", od + r);
- } while (d != od);
- *psize += d + r + 1 + 512;
- *pdata = srealloc(*pdata, *psize);
- sprintf(&(*pdata)[*pcur], "%s %s=%s\n", dval, keyword, sval);
- *pcur += d + r;
- }
- static void
- paxnam(struct tar_header *hp, const char *name)
- {
- char buf[257], *bp;
- const char *cp, *np;
- int bl = 0;
- static int pid;
- if (pid == 0)
- pid = getpid();
- for (np = name; *np; np++);
- while (np > name && *np != '/') {
- np--;
- bl++;
- }
- if ((np > name || *name == '/') && np-name <= 120)
- for (bp = buf, cp = name; cp < np; bp++, cp++)
- *bp = *cp;
- else {
- *buf = '.';
- bp = &buf[1];
- }
- snprintf(bp, sizeof buf - (bp - buf), "/PaxHeaders.%d/%s",
- pid, bl < 100 ? np>name?&np[1]:name : sequence());
- tmkname(hp, buf);
- }
- static char *
- sequence(void)
- {
- static char buf[25];
- static long long d;
- sprintf(buf, "%10.10lld", ++d);
- return buf;
- }
- static int
- pax_oneopt(const char *s, int warn)
- {
- if (strcmp(s, "linkdata") == 0)
- pax_oflag |= PO_LINKDATA;
- else if (strcmp(s, "times") == 0)
- pax_oflag |= PO_TIMES;
- else {
- if (warn)
- msg(2, 0, "Unknown flag \"-o %s\"\n", s);
- return -1;
- }
- return 0;
- }
- int
- pax_options(char *s, int warn)
- {
- char *o = s, c;
- int val = 0, word = 0;
- do {
- if (word == 0) {
- if (isspace(*s&0377))
- o = &s[1];
- else
- word = 1;
- }
- if (*s == ',' || *s == '\0') {
- c = *s;
- *s = '\0';
- val |= pax_oneopt(o, warn);
- *s = c;
- o = &s[1];
- word = 0;
- }
- } while (*s++);
- return val;
- }
- /*
- * Given a symbolic link "base" and the result of readlink "name", form
- * a valid path name for the link target.
- */
- static char *
- joinpath(const char *base, char *name)
- {
- const char *bp = NULL, *cp;
- char *new, *np;
- if (*name == '/')
- return name;
- for (cp = base; *cp; cp++)
- if (*cp == '/')
- bp = cp;
- if (bp == NULL)
- return name;
- np = new = smalloc(bp - base + strlen(name) + 2);
- for (cp = base; cp < bp; cp++)
- *np++ = *cp;
- *np++ = '/';
- for (cp = name; *cp; cp++)
- *np++ = *cp;
- *np = '\0';
- free(name);
- return new;
- }
- static int
- utf8(const char *cp)
- {
- int c, n;
- while (*cp) if ((c = *cp++ & 0377) & 0200) {
- if (c == (c & 037 | 0300))
- n = 1;
- else if (c == (c & 017 | 0340))
- n = 2;
- else if (c == (c & 07 | 0360))
- n = 3;
- else if (c == (c & 03 | 0370))
- n = 4;
- else if (c == (c & 01 | 0374))
- n = 5;
- else
- return 0;
- while (n--) {
- c = *cp++ & 0377;
- if (c != (c & 077 | 0200))
- return 0;
- }
- }
- return 1;
- }
- static time_t
- fetchtime(const char *cp)
- {
- struct tm tm;
- time_t t;
- char *xp;
- int n;
- t = strtoll(cp, &xp, 10);
- if (*xp == '\0')
- return t;
- memset(&tm, 0, sizeof tm);
- n = sscanf(cp, "%4d%2d%2dT%2d%2d%2d",
- &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
- &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
- tm.tm_year -= 1900;
- tm.tm_mon--;
- tm.tm_isdst = -1;
- t = mktime(&tm);
- if (n < 3 || t == (time_t)-1)
- msg(4, 1, "line %lld: illegal time \"%s\"\n",
- lineno, cp);
- return t;
- }
- static char *
- nextfield(char *cp, const char *fieldname)
- {
- while (*cp && *cp != ':')
- cp++;
- if (*cp == 0)
- msg(4, 1, "line %lld: unterminated \"%s\" field\n",
- lineno, fieldname);
- *cp++ = 0;
- return cp;
- }
- static char *
- getproto(char *np, struct prototype *pp)
- {
- char *tp, *xp;
- long long t, u;
- memset(pp, 0, sizeof *pp);
- if (*np == ':')
- np++;
- else {
- tp = nextfield(np, "type");
- if (np[1])
- goto notype;
- switch (np[0]) {
- case 'b':
- pp->pt_mode |= S_IFBLK;
- break;
- case 'c':
- pp->pt_mode |= S_IFCHR;
- break;
- case 'd':
- pp->pt_mode |= S_IFDIR;
- break;
- case 'f':
- pp->pt_mode |= S_IFREG;
- break;
- case 'p':
- pp->pt_mode |= S_IFIFO;
- break;
- case 's':
- pp->pt_mode |= S_IFLNK;
- break;
- default:
- notype:
- msg(4, 1, "line %lld: unknown type \"%s\"\n",
- lineno, np);
- }
- pp->pt_spec |= PT_TYPE;
- np = tp;
- }
- if (*np == ':')
- np++;
- else {
- struct passwd *pwd;
- tp = nextfield(np, "owner");
- t = strtoll(np, &xp, 10);
- if (*xp == '\0')
- pp->pt_uid = t;
- else {
- if ((pwd = getpwnam(np)) == NULL)
- msg(4, 1, "line %lld: unknown user \"%s\"\n",
- lineno, np);
- pp->pt_uid = pwd->pw_uid;
- }
- pp->pt_spec |= PT_OWNER;
- np = tp;
- }
- if (*np == ':')
- np++;
- else {
- struct group *grp;
- tp = nextfield(np, "group");
- t = strtoll(np, &xp, 10);
- if (*xp == '\0')
- pp->pt_gid = t;
- else {
- if ((grp = getgrnam(np)) == NULL)
- msg(4, 1, "line %lld: unknown group \"%s\"\n",
- lineno, np);
- pp->pt_gid = grp->gr_gid;
- }
- pp->pt_spec |= PT_GROUP;
- np = tp;
- }
- if (*np == ':')
- np++;
- else {
- tp = nextfield(np, "mode");
- t = strtol(np, &xp, 8);
- if (t & ~07777 || *xp)
- msg(4, 1, "line %lld: illegal mode \"%s\"\n",
- lineno, np);
- pp->pt_mode |= t;
- pp->pt_spec |= PT_MODE;
- np = tp;
- }
- if (*np == ':')
- np++;
- else {
- tp = nextfield(np, "access time");
- pp->pt_atime = fetchtime(np);
- pp->pt_spec |= PT_ATIME;
- np = tp;
- }
- if (*np == ':')
- np++;
- else {
- tp = nextfield(np, "modification time");
- pp->pt_mtime = fetchtime(np);
- pp->pt_spec |= PT_MTIME;
- np = tp;
- }
- if (*np == ':') {
- np++;
- if (*np++ != ':')
- majmin: msg(4, 1, "line %lld: need either both major and "
- "minor or none\n",
- lineno);
- } else {
- tp = nextfield(np, "major");
- t = strtoll(np, &xp, 10);
- if (*xp)
- msg(4, 1, "line %lld: illegal major \"%s\"\n",
- lineno, np);
- np = tp;
- if (*np == ':')
- goto majmin;
- tp = nextfield(np, "minor");
- u = strtoll(np, &xp, 10);
- if (*xp)
- msg(4, 1, "line %lld: illegal minor \"%s\"\n",
- lineno, np);
- np = tp;
- pp->pt_rdev = makedev(t, u);
- pp->pt_spec |= PT_RDEV;
- }
- return np;
- }
|