a —+«biã@sVdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZddlmZddlmZmZmZmZm Z mZdd lmZmZmZd d „Zgd ¢Zd gZe dej¡Z dd„Z!dd„Z"dd„Z#Gdd„de$ƒZ%dd„Z&dd„Z'Gdd„de$ƒZ(gd¢Z)Gdd„dej*ƒZ+d d!„Z,d"d#„Z-Gd$d%„d%e$ƒZ.d&d'„Z/dS)(aRcommand server extension for cHg 'S' channel (read/write) propagate ui.system() request to client 'attachio' command attach client's stdio passed by sendmsg() 'chdir' command change current directory 'setenv' command replace os.environ completely 'setumask' command (DEPRECATED) 'setumask2' command set umask 'validate' command reload the config and check if the server is up to date Config ------ :: [chgserver] # how long (in seconds) should an idle chg server exit idletimeout = 3600 # whether to skip config or env change checks skiphash = False é)Úabsolute_importNé)Ú_)ÚgetattrÚsetattr)Úhex)Ú commandserverÚencodingÚerrorÚ extensionsÚpycompatÚutil)ÚhashutilÚprocutilÚ stringutilcCstt t |¡¡ ¡ƒS)z return sha1 hexdigest for a list)rrZsha1rÚpprintÚdigest)Úitems©rú9/usr/lib64/python3.9/site-packages/mercurial/chgserver.pyÚ _hashlistKsr)saliass diff-toolsseolsextdiffs extensionss fastannotates merge-toolssschemes)scommandssshow.aliasprefixs~\A(?: CHGHG |HG(?:DEMANDIMPORT|EMITWARNINGS|MODULEPOLICY|PROF|RCPATH)? |HG(?:ENCODING|PLAIN).* |LANG(?:UAGE)? |LC_.* |LD_.* |PATH |PYTHON.* |TERM(?:INFO)? |TZ )\Zcsœg}tD]}| | |¡¡qtD]\}}| | ||¡¡q"t|ƒ}dtjvrXdh‰ntƒ‰‡fdd„t   tj¡Dƒ}tt |ƒƒ}|dd…|dd…S)aÎreturn a quick hash for detecting config/env changes confighash is the hash of sensitive config items and environment variables. for chgserver, it is designed that once confighash changes, the server is not qualified to serve its client and should redirect the client to a new server. different from mtimehash, confighash change will not mark the server outdated and exit since the user can have different configs at the same time. sCHGHGsHGcs*g|]"\}}t |¡r|ˆvr||f‘qSr)Ú_envreÚmatch©Ú.0ÚkÚv©ZignoredrrÚ ˆsþz_confighash..Né) Ú_configsectionsÚappendZ configitemsÚ_configsectionitemsZconfigrr ÚenvironÚsetr Ú iteritemsÚsorted)ÚuiZ sectionitemsÚsectionÚitemZ sectionhashZenvitemsZenvhashrrrÚ _confighashrs     þ r*c Csœdd„t |¡Dƒ}zddlm}| |¡Wnty@Yn0g}tjrX| tj¡|D]2}z| t t  |¡¡¡Wq\t yŒYq\0q\t t |ƒƒS)zÒget a list of paths that should be checked to detect change The list will include: - extensions (will not cover all files for complex extensions) - mercurial/__version__.py - python binary cSsg|] \}}|‘qSrr)rÚnÚmrrrr™óz"_getmtimepaths..r)Ú __version__) r Úr.r!Ú ImportErrorr Z sysexecutableÚfsencodeÚinspectZ getabsfileÚ TypeErrorr&r$)r'Úmodulesr.Úfilesr,rrrÚ_getmtimepaths‘s    r6cCs dd„}tt ||¡ƒdd…S)aWreturn a quick hash for detecting file changes mtimehash calls stat on given paths and calculate a hash based on size and mtime of each file. mtimehash does not read file content because reading is expensive. therefore it's not 100% reliable for detecting content changes. it's possible to return different hashes for same file contents. it's also possible to return a same hash for different file contents for some carefully crafted situation. for chgserver, it is designed that once mtimehash changes, the server is considered outdated immediately and should no longer provide service. mtimehash is not included in confighash because we only know the paths of extensions after importing them (there is imp.find_module but that faces race conditions). We need to calculate confighash without importing. cSs4zt |¡}|tj|jfWSty.Yn0dS©N)ÚosÚstatÚST_MTIMEÚst_sizeÚOSError)ÚpathÚstrrrÚtrystat½s   z_mtimehash..trystatNé )rr Zmaplist)Úpathsr?rrrÚ _mtimehash«srBc@s&eZdZdZdd„Zeddd„ƒZdS)Ú hashstatezCa structure storing confighash, mtimehash, paths used for mtimehashcCs||_||_||_dSr7)Ú confighashÚ mtimehashÚ mtimepaths)ÚselfrDrErFrrrÚ__init__Ëszhashstate.__init__NcCs<|durt|ƒ}t|ƒ}t|ƒ}| dd||¡t|||ƒS)Ns cmdserversconfighash = %s mtimehash = %s )r6r*rBÚlogrC)r'rFrDrErrrÚfromuiÐsüzhashstate.fromui)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rHÚ staticmethodrJrrrrrCÈsrCcs"G‡‡‡fdd„d|jƒ‰ˆ|ƒS)Ncs8eZdZd‡‡‡fdd„ Zdd„Zd ‡fdd„ Z‡ZS) z_newchgui..chguiNcs.tˆ|ƒ |¡|r$t|dˆƒ|_nˆ|_dS)NÚ_csystem)ÚsuperrHrrP)rGÚsrc)Ú __class__ÚchguiÚcsystemrrrHász!_newchgui..chgui.__init__cSs`||jus2t |jd¡r2|j ¡tj ¡ks2|jrDtj||||dS| ¡|  |t  |¡|¡S)Nsfileno)r#ÚcwdÚout) Úfoutr Ú safehasattrÚfilenorÚstdoutZ_finoutredirectedÚsystemÚflushrPÚ shellenviron)rGÚcmdr#rVrWrrrÚ _runsystemèsÿ þýüz#_newchgui..chgui._runsystemcs |j|t |¡ddˆiddS)Nópageróattachio)ÚtypeÚcmdtableT)rPrr^)rGr_Úenv)ÚattachiorrÚ _runpagerøsüz"_newchgui..chgui._runpager)N)N)rKrLrMrHr`rgÚ __classcell__r©rfrTrU©rSrrTàsrTrj)ÚsrcuirUrfrrirÚ _newchguißs!rlcCs(ddlm}|j ¡}dD]}t||t||ƒƒqt |d¡rF|j|_|  ||¡}|  ||d¡|  ¡D]B\}}} |  ||¡} d| vsj| dksj|   d¡rœqj| ||| | ¡qj|d } | rÆtj | ¡pÈd} |d } |j|| | d \} }t |¡tj||d ||ur t |¡tj||d ||fS) Nr)Údispatch)ófinófoutóferrsenvirons_csystemsconfigó:s--configó$scwds repository)Zwd)Úfp)r/rmrSÚloadrrr rYrPZ_earlyparseoptsZ _parseconfigZ walkconfigZ configsourceÚ startswithÚ setconfigr8r=ÚrealpathZ _getlocalr Z populateuirZ setuplogging)rkÚargsÚcdebugrmZnewuiÚaÚoptionsr(ÚnameÚvalueÚsourcerVZrpathr=ZnewluirrrÚ _loadnewuis.        rc@s"eZdZdZdd„Zddd„ZdS) ÚchanneledsystemaËPropagate ui.system() request in the following format: payload length (unsigned int), type, '', cmd, '', cwd, '', envkey, '=', val, '', ... envkey, '=', val if type == 'system', waits for: exitcode length (unsigned int), exitcode (int) if type == 'pager', repetitively waits for a command name ending with ' ' and executes it defined by cmdtable, or exits the loop if the command name is empty. cCs||_||_||_dSr7)Úin_rWÚchannel)rGrrWr‚rrrrH=szchanneledsystem.__init__Nósystemc Cs ||t |pd¡g}| dd„t |¡Dƒ¡d |¡}|j t  d|j t |ƒ¡¡|j |¡|j  ¡|dkr¼|j  d¡}t d|¡\}|dkr¢t td ƒ¡‚t d |j  d¡¡\} | S|d kr|j  ¡dd …}|sàq|rø||vrø||ƒqÆt td ƒ|¡‚qÆnt d|¡‚dS)Nó.css|]\}}d||fVqdS)s%s=%sNrrrrrÚ Dr-z+channeledsystem.__call__..óó>cIrƒéó>Isinvalid responseó>iraéÿÿÿÿsunexpected command: %ssinvalid S channel type: %s)r ÚabspathÚextendr r%ÚjoinrWÚwriteÚstructÚpackr‚Úlenr]rÚreadÚunpackr ZAbortrÚreadlineZProgrammingError) rGr_r#rVrcrdrxÚdataÚlengthÚrcrrrÚ__call__Bs*       zchanneledsystem.__call__)NrƒN)rKrLrMrNrHr™rrrrr€(sr€))scinrnÚrb)scoutroÚwb)scerrrpr›c s¼eZdZ‡fdd„Z‡fdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z ‡fdd„Z dd„Zejj ¡Ze ee e ee e dœ¡e ed¡r´dd„Zeed<‡ZS)Ú chgcmdserverc sltt|ƒ t|t||dƒ|jƒ||||¡||_d|_g|_||_ ||_ |durh|j   ¡|_ tj |j d<dS)NóSFsvalidate)rQrœrHrlr€rfÚ clientsockÚ _ioattachedÚ_oldiosrCÚ baseaddressÚ capabilitiesÚcopyÚvalidate) rGr'ÚrepoÚfinrXÚsockÚ prereposetupsrCr¡rjrrrHgs û zchgcmdserver.__init__cs*tt|ƒ ¡|j ¡| ¡d|_dS©NF)rQrœÚcleanupr'r]Ú _restoreiorŸ©rGrjrrrªzs zchgcmdserver.cleanupc Cs:|j t ddd¡¡t |j ¡¡}|j dd|¡|j}|  ¡|  ¡t |t ƒD]À\}\}}}|dkspJ‚t ||ƒ}t || ¡¡t |¡|jrœqV|dkrª|}nJtjrÐ| ¡rÄt |¡}qôt |¡}n$| ¡rÞd} nd} t | ¡|| ¡}||ur t|||ƒt|||ƒqVd |_|j t d t|ƒ¡¡d S) z{Attach to client's stdio passed via unix domain socket; all channels except cresult will no longer be used r‡óIró chgserversreceived fds: %r rrpr‹TrŠN)ržZsendallrr‘r ZrecvfdsrZr'rIr]Ú_saveioÚzipÚ _iochannelsrr8Údup2ÚcloserŸr Úispy3ÚisattyrZmake_line_bufferedZunwrap_line_bufferedÚfdopenrÚcresultrr’) rGZ clientfdsr'ÚfdÚcnÚfnÚmodersÚnewfpÚbufsizerrrrf‚s8       zchgcmdserver.attachiocCsX|jr dS|j}tD]>\}}}t||ƒ}t||ƒ}t | ¡¡}|j |||f¡qdSr7)r r'r±rr8ÚduprZr!)rGr'r¹rºÚ_modeÚchrsr¸rrrr¯³s  zchgcmdserver._saveioc Cs|js dSt tjtj¡}|j}t|jtƒD]Ö\\}}}\}}}t||ƒ} t j s`| |us`|   ¡zLt j sp| |urd|vrt  ||  ¡¡| ¡t  ||  ¡¡t  |¡Wn<tyè} z$|j ddt | ¡|¡WYd} ~ n d} ~ 00t|||ƒt|||ƒq,t  |¡|jdd…=dS)NÚwr®sgot %s while duplicating %s )r r8ÚopenÚdevnullÚO_WRONLYr'r°r±rr r´r³r²rZr]r<rIrÚ forcebytestrr) rGZnullfdr'rÀrsr¸r¹rºr»r¼Úerrrrrr«½s2  ü  zchgcmdserver._restoreioc CsÂ| ¡}d}d}zt|j||jƒ\|_}Wn¨tjyŠ}zF|j tdƒt |¡¡|j rr|j tdƒ|j ¡d}WYd}~nRd}~0tj yÒ}z0|j durª|j }|j |  ¡¡d}WYd}~n d}~00|r|j  ¡d}|j dd¡rú|}|j d |¡dSt ||jj¡}g}|j|jjkrdt|j|jjƒ} | d | ¡|jjrd| d ¡|j|jjkrt|j|jƒ} | d | ¡|j d dt |¡¡|j d |¡pºd¡dS)a»Reload the config and check if the server is up to date Read a list of '' separated arguments. Write a non-empty list of '' separated instruction strings or '' if the list is empty. An instruction string could be either: - "unlink $path", the client should unlink the path to stop the outdated server. - "redirect $path", the client should attempt to connect to $path first. If it does not work, start a new server. It implies "reconnect". - "exit $n", the client should exit directly with code n. This may happen if we cannot parse the config. - "reconnect", the client should close the connection and reconnect. If neither "reconnect" nor "redirect" is included in the instruction list, the client can continue with this server after completing all the instructions. Féÿs abort: %s s(%s) TNsuisdetailed-exit-codesexit %ds unlink %ss reconnects redirect %sr®s validate: %s r†)Ú _readlistrr'ryr Z RepoErrorrrrÅÚhintÚErrorÚdetailed_exit_codeÚformatr]Ú configboolr·rrCrJrFrEÚ _hashaddressr¡rDr!rIrrŽ) rGrxZ errorraisedrËZluiÚinstZ exit_codeZnewhashZinstsZaddrrrrr¤æsD    zchgcmdserver.validatecCs.| ¡}|sdS|j dd|¡t |¡dS)zœChange current directory Note that the behavior of --cwd option is bit different from this. It does not affect --config parameter. Nr®schdir to '%s' )Ú_readstrr'rIr8Úchdir)rGr=rrrrÑ"s zchgcmdserver.chdircCs| | d¡¡dS)zChange umask (DEPRECATED)rˆN)Ú _setumaskZ_readr¬rrrÚsetumask.szchgcmdserver.setumaskcCs*| ¡}t|ƒdkrtdƒ‚| |¡dS)z Change umaskrˆs(invalid mask length in setumask2 requestN)rÐr’Ú ValueErrorrÒ)rGr–rrrÚ setumask24s zchgcmdserver.setumask2cCs.t d|¡d}|j dd|¡t |¡dS)Nr‰rr®s setumask %r )rr”r'rIr rÓ)rGr–ÚmaskrrrrÒ;szchgcmdserver._setumaskcs@|j}g|_ztt|ƒ ¡W| ¡||_S| ¡||_0dSr7)r rQrœÚ runcommandr«)rGZ globaloldiosrjrrr×@sýÿzchgcmdserver.runcommandcCsl| ¡}ztdd„|Dƒƒ}Wnty8tdƒ‚Yn0|j ddt| ¡ƒ¡tj  ¡tj  |¡dS)ztClear and update os.environ Note that not all variables can make an effect on the running process. css|]}| dd¡VqdS)ó=rN)Úsplit)rÚsrrrr…Sr-z&chgcmdserver.setenv..s"unexpected value in setenv requestr®s setenv: %r N) rÈÚdictrÔr'rIr&Úkeysr r#ÚclearÚupdate)rGÚlZnewenvrrrÚsetenvLs  zchgcmdserver.setenv)rbschdirs runcommandssetenvssetumasks setumask2s setprocnamecCs&| ¡}|j dd|¡t |¡dS)zChange process titler®ssetprocname: %r N)rÐr'rIrÚ setprocname)rGr|rrrráiszchgcmdserver.setprocname)rKrLrMrHrªrfr¯r«r¤rÑrÓrÕrÒr×ràrÚserverr¢r£rÞr rYrrárhrrrjrrœfs2  1 )<   úÿ rœcCsd|t ¡fS)Ns %s.%d.tmp)r8Úgetpid)ÚaddressrrrÚ _tempaddressrsråcCs6tj |¡\}}| dd¡d}dtj ||¡|fS)Nr„rrs%s-%s)r8r=rÙrŽ)räZhashstrÚdirnameÚbasenamerrrrÎvsrÎc@sleZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z dd„Zdd„ZdS)Úchgunixservicehandlerz"Set of operations for chg servicesrcCs4||_d|_d|_d|_| dd¡|_t ¡|_dS)Nr®s idletimeout)r'Ú _hashstateÚ _baseaddressÚ _realaddressZ configintÚ _idletimeoutÚtimeÚ _lastactive)rGr'rrrrH„s zchgunixservicehandler.__init__cCs(| |¡| ¡| |¡| ¡dSr7)Ú_inithashstateÚ_checkextensionsÚ_bindÚ_createsymlink)rGr§rärrrÚ bindsockets  z chgunixservicehandler.bindsocketcCsF||_|j dd¡r$d|_||_dSt |j¡|_t||jjƒ|_dS)Nr®sskiphash) rêr'rÍrérërCrJrÎrD©rGrärrrrï—sz$chgunixservicehandler._inithashstatecCs|js dSt ¡rd|j_dS)Nr-)rér Z notloadedrEr¬rrrrð sz&chgunixservicehandler._checkextensionscCs@t|jƒ}t ||¡t |¡|_| tj ¡t  ||j¡dSr7) rårër Zbindunixsocketr8r9Ú _socketstatZlistenÚsocketZ SOMAXCONNÚrename)rGr§Ú tempaddressrrrrñ©s     zchgunixservicehandler._bindcCsB|j|jkrdSt|jƒ}t tj |j¡|¡t ||j¡dSr7) rêrërår8Úsymlinkr=rçr r÷)rGrørrrrò´s   z$chgunixservicehandler._createsymlinkcCsLz2t |j¡}|j|jjko0|tj|jtjkWStyFYdS0dSr©)r8r9rëÚst_inorõr:r<)rGr>rrrÚ_issocketowner»s þ z$chgunixservicehandler._issocketownercCs| ¡s dSt |j¡dSr7)rûr Z tryunlinkrërôrrrÚ unlinksocketÅsz"chgunixservicehandler.unlinksocketcCsH| ¡s|j dd|j¡dSt ¡|j|jkrD|j dd¡dSdS)Nr®s%s is not owned, exiting. Tsbeing idle too long. exiting. F)rûr'rIrërírîrìr¬rrrÚ shouldexitÎsÿz chgunixservicehandler.shouldexitcCst ¡|_dSr7)rírîr¬rrrÚ newconnectionÙsz#chgunixservicehandler.newconnectionc Cst|j||||||j|jƒSr7)rœr'rérê)rGr¥Zconnr¦rXr¨rrrÚcreatecmdserverÜsøz%chgunixservicehandler.createcmdserverN)rKrLrMrNZ pollintervalrHrórïrðrñròrûrürýrþrÿrrrrrès       rècCsˆdtjvrtjd=dtjvr6tjdtjd<tjd=n$dtjvrZdtjvrRtjd=tjd=|rn| dddd¡t|ƒ}tj|d||d S) NsCHGINTERNALMARKsCHGORIG_LC_CTYPEsLC_CTYPEsCHG_CLEAR_LC_CTYPEsbundles mainreporootr-srepo)r¥ÚoptsZhandler)r r#rvrèrZunixforkingservice)r'r¥rÚhrrrÚchgunixserviceés     r)0rNZ __future__rr2r8Úrerör9rríZi18nrr rrZnoderr/rr r r r Zutilsrrrrr r"ÚcompileÚXrr*r6rBÚobjectrCrlrr€r±rârœrårÎrèrrrrrÚsH"     ÿ ó%$6 j