SmbFile.cs 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.Net;
  21. using System.Text;
  22. using SharpCifs.Dcerpc;
  23. using SharpCifs.Dcerpc.Msrpc;
  24. using SharpCifs.Netbios;
  25. using SharpCifs.Util;
  26. using SharpCifs.Util.Sharpen;
  27. namespace SharpCifs.Smb
  28. {
  29. /// <summary>This class represents a resource on an SMB network.</summary>
  30. /// <remarks>
  31. /// This class represents a resource on an SMB network. Mainly these
  32. /// resources are files and directories however an <code>SmbFile</code>
  33. /// may also refer to servers and workgroups. If the resource is a file or
  34. /// directory the methods of <code>SmbFile</code> follow the behavior of
  35. /// the well known
  36. /// <see cref="FilePath">Sharpen.FilePath</see>
  37. /// class. One fundamental difference
  38. /// is the usage of a URL scheme [1] to specify the target file or
  39. /// directory. SmbFile URLs have the following syntax:
  40. /// <blockquote><pre>
  41. /// smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?[param=value[param2=value2[...]]]
  42. /// </pre></blockquote>
  43. /// This example:
  44. /// <blockquote><pre>
  45. /// smb://storage15/public/foo.txt
  46. /// </pre></blockquote>
  47. /// would reference the file <code>foo.txt</code> in the share
  48. /// <code>public</code> on the server <code>storage15</code>. In addition
  49. /// to referencing files and directories, jCIFS can also address servers,
  50. /// and workgroups.
  51. /// <p>
  52. /// <font color="#800000"><i>Important: all SMB URLs that represent
  53. /// workgroups, servers, shares, or directories require a trailing slash '/'.
  54. /// </i></font>
  55. /// <p>
  56. /// When using the <tt>java.net.URL</tt> class with
  57. /// 'smb://' URLs it is necessary to first call the static
  58. /// <tt>jcifs.Config.registerSmbURLHandler();</tt> method. This is required
  59. /// to register the SMB protocol handler.
  60. /// <p>
  61. /// The userinfo component of the SMB URL (<tt>domain;user:pass</tt>) must
  62. /// be URL encoded if it contains reserved characters. According to RFC 2396
  63. /// these characters are non US-ASCII characters and most meta characters
  64. /// however jCIFS will work correctly with anything but '@' which is used
  65. /// to delimit the userinfo component from the server and '%' which is the
  66. /// URL escape character itself.
  67. /// <p>
  68. /// The server
  69. /// component may a traditional NetBIOS name, a DNS name, or IP
  70. /// address. These name resolution mechanisms and their resolution order
  71. /// can be changed (See <a href="../../../resolver.html">Setting Name
  72. /// Resolution Properties</a>). The servername and path components are
  73. /// not case sensitive but the domain, username, and password components
  74. /// are. It is also likely that properties must be specified for jcifs
  75. /// to function (See <a href="../../overview-summary.html#scp">Setting
  76. /// JCIFS Properties</a>). Here are some examples of SMB URLs with brief
  77. /// descriptions of what they do:
  78. /// <p>[1] This URL scheme is based largely on the <i>SMB
  79. /// Filesharing URL Scheme</i> IETF draft.
  80. /// <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
  81. /// <tr bgcolor="#ccccff">
  82. /// <td colspan="2"><b>SMB URL Examples</b></td>
  83. /// <tr><td width="20%"><b>URL</b></td><td><b>Description</b></td></tr>
  84. /// <tr><td width="20%"><code>smb://users-nyc;miallen:mypass@angus/tmp/</code></td><td>
  85. /// This URL references a share called <code>tmp</code> on the server
  86. /// <code>angus</code> as user <code>miallen</code> who's password is
  87. /// <code>mypass</code>.
  88. /// </td></tr>
  89. /// <tr><td width="20%">
  90. /// <code>smb://Administrator:P%40ss@msmith1/c/WINDOWS/Desktop/foo.txt</code></td><td>
  91. /// A relativly sophisticated example that references a file
  92. /// <code>msmith1</code>'s desktop as user <code>Administrator</code>. Notice the '@' is URL encoded with the '%40' hexcode escape.
  93. /// </td></tr>
  94. /// <tr><td width="20%"><code>smb://angus/</code></td><td>
  95. /// This references only a server. The behavior of some methods is different
  96. /// in this context(e.g. you cannot <code>delete</code> a server) however
  97. /// as you might expect the <code>list</code> method will list the available
  98. /// shares on this server.
  99. /// </td></tr>
  100. /// <tr><td width="20%"><code>smb://myworkgroup/</code></td><td>
  101. /// This syntactically is identical to the above example. However if
  102. /// <code>myworkgroup</code> happends to be a workgroup(which is indeed
  103. /// suggested by the name) the <code>list</code> method will return
  104. /// a list of servers that have registered themselves as members of
  105. /// <code>myworkgroup</code>.
  106. /// </td></tr>
  107. /// <tr><td width="20%"><code>smb://</code></td><td>
  108. /// Just as <code>smb://server/</code> lists shares and
  109. /// <code>smb://workgroup/</code> lists servers, the <code>smb://</code>
  110. /// URL lists all available workgroups on a netbios LAN. Again,
  111. /// in this context many methods are not valid and return default
  112. /// values(e.g. <code>isHidden</code> will always return false).
  113. /// </td></tr>
  114. /// <tr><td width="20%"><code>smb://angus.foo.net/d/jcifs/pipes.doc</code></td><td>
  115. /// The server name may also be a DNS name as it is in this example. See
  116. /// <a href="../../../resolver.html">Setting Name Resolution Properties</a>
  117. /// for details.
  118. /// </td></tr>
  119. /// <tr><td width="20%"><code>smb://192.168.1.15/ADMIN$/</code></td><td>
  120. /// The server name may also be an IP address. See &lt;a
  121. /// href="../../../resolver.html"&gt;Setting Name Resolution Properties</a>
  122. /// for details.
  123. /// </td></tr>
  124. /// <tr><td width="20%">
  125. /// <code>smb://domain;username:password@server/share/path/to/file.txt</code></td><td>
  126. /// A prototypical example that uses all the fields.
  127. /// </td></tr>
  128. /// <tr><td width="20%"><code>smb://myworkgroup/angus/ &lt;-- ILLEGAL </code></td><td>
  129. /// Despite the hierarchial relationship between workgroups, servers, and
  130. /// filesystems this example is not valid.
  131. /// </td></tr>
  132. /// <tr><td width="20%">
  133. /// <code>smb://server/share/path/to/dir &lt;-- ILLEGAL </code></td><td>
  134. /// URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.
  135. /// </td></tr>
  136. /// <tr><td width="20%">
  137. /// <code>smb://MYGROUP/?SERVER=192.168.10.15</code></td><td>
  138. /// SMB URLs support some query string parameters. In this example
  139. /// the <code>SERVER</code> parameter is used to override the
  140. /// server name service lookup to contact the server 192.168.10.15
  141. /// (presumably known to be a master
  142. /// browser) for the server list in workgroup <code>MYGROUP</code>.
  143. /// </td></tr>
  144. /// </table>
  145. /// <p>A second constructor argument may be specified to augment the URL
  146. /// for better programmatic control when processing many files under
  147. /// a common base. This is slightly different from the corresponding
  148. /// <code>java.io.File</code> usage; a '/' at the beginning of the second
  149. /// parameter will still use the server component of the first parameter. The
  150. /// examples below illustrate the resulting URLs when this second contructor
  151. /// argument is used.
  152. /// <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
  153. /// <tr bgcolor="#ccccff">
  154. /// <td colspan="3">
  155. /// <b>Examples Of SMB URLs When Augmented With A Second Constructor Parameter</b></td>
  156. /// <tr><td width="20%">
  157. /// <b>First Parameter</b></td><td><b>Second Parameter</b></td><td><b>Result</b></td></tr>
  158. /// <tr><td width="20%"><code>
  159. /// smb://host/share/a/b/
  160. /// </code></td><td width="20%"><code>
  161. /// c/d/
  162. /// </code></td><td><code>
  163. /// smb://host/share/a/b/c/d/
  164. /// </code></td></tr>
  165. /// <tr><td width="20%"><code>
  166. /// smb://host/share/foo/bar/
  167. /// </code></td><td width="20%"><code>
  168. /// /share2/zig/zag
  169. /// </code></td><td><code>
  170. /// smb://host/share2/zig/zag
  171. /// </code></td></tr>
  172. /// <tr><td width="20%"><code>
  173. /// smb://host/share/foo/bar/
  174. /// </code></td><td width="20%"><code>
  175. /// ../zip/
  176. /// </code></td><td><code>
  177. /// smb://host/share/foo/zip/
  178. /// </code></td></tr>
  179. /// <tr><td width="20%"><code>
  180. /// smb://host/share/zig/zag
  181. /// </code></td><td width="20%"><code>
  182. /// smb://foo/bar/
  183. /// </code></td><td><code>
  184. /// smb://foo/bar/
  185. /// </code></td></tr>
  186. /// <tr><td width="20%"><code>
  187. /// smb://host/share/foo/
  188. /// </code></td><td width="20%"><code>
  189. /// ../.././.././../foo/
  190. /// </code></td><td><code>
  191. /// smb://host/foo/
  192. /// </code></td></tr>
  193. /// <tr><td width="20%"><code>
  194. /// smb://host/share/zig/zag
  195. /// </code></td><td width="20%"><code>
  196. /// /
  197. /// </code></td><td><code>
  198. /// smb://host/
  199. /// </code></td></tr>
  200. /// <tr><td width="20%"><code>
  201. /// smb://server/
  202. /// </code></td><td width="20%"><code>
  203. /// ../
  204. /// </code></td><td><code>
  205. /// smb://server/
  206. /// </code></td></tr>
  207. /// <tr><td width="20%"><code>
  208. /// smb://
  209. /// </code></td><td width="20%"><code>
  210. /// myworkgroup/
  211. /// </code></td><td><code>
  212. /// smb://myworkgroup/
  213. /// </code></td></tr>
  214. /// <tr><td width="20%"><code>
  215. /// smb://myworkgroup/
  216. /// </code></td><td width="20%"><code>
  217. /// angus/
  218. /// </code></td><td><code>
  219. /// smb://myworkgroup/angus/ &lt;-- ILLEGAL<br />(But if you first create an <tt>SmbFile</tt> with 'smb://workgroup/' and use and use it as the first parameter to a constructor that accepts it with a second <tt>String</tt> parameter jCIFS will factor out the 'workgroup'.)
  220. /// </code></td></tr>
  221. /// </table>
  222. /// <p>Instances of the <code>SmbFile</code> class are immutable; that is,
  223. /// once created, the abstract pathname represented by an SmbFile object
  224. /// will never change.
  225. /// </remarks>
  226. /// <seealso cref="FilePath">Sharpen.FilePath</seealso>
  227. public class SmbFile : UrlConnection
  228. {
  229. internal const int ORdonly = 0x01;
  230. internal const int OWronly = 0x02;
  231. internal const int ORdwr = 0x03;
  232. internal const int OAppend = 0x04;
  233. internal const int OCreat = 0x0010;
  234. internal const int OExcl = 0x0020;
  235. internal const int OTrunc = 0x0040;
  236. /// <summary>
  237. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  238. /// other SMB clients (including other threads making calls into jCIFS)
  239. /// will not be permitted to access the target file and will receive "The
  240. /// file is being accessed by another process" message.
  241. /// </summary>
  242. /// <remarks>
  243. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  244. /// other SMB clients (including other threads making calls into jCIFS)
  245. /// will not be permitted to access the target file and will receive "The
  246. /// file is being accessed by another process" message.
  247. /// </remarks>
  248. public const int FileNoShare = 0x00;
  249. /// <summary>
  250. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  251. /// other SMB clients will be permitted to read from the target file while
  252. /// this file is open.
  253. /// </summary>
  254. /// <remarks>
  255. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  256. /// other SMB clients will be permitted to read from the target file while
  257. /// this file is open. This constant may be logically OR'd with other share
  258. /// access flags.
  259. /// </remarks>
  260. public const int FileShareRead = 0x01;
  261. /// <summary>
  262. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  263. /// other SMB clients will be permitted to write to the target file while
  264. /// this file is open.
  265. /// </summary>
  266. /// <remarks>
  267. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  268. /// other SMB clients will be permitted to write to the target file while
  269. /// this file is open. This constant may be logically OR'd with other share
  270. /// access flags.
  271. /// </remarks>
  272. public const int FileShareWrite = 0x02;
  273. /// <summary>
  274. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  275. /// other SMB clients will be permitted to delete the target file while
  276. /// this file is open.
  277. /// </summary>
  278. /// <remarks>
  279. /// When specified as the <tt>shareAccess</tt> constructor parameter,
  280. /// other SMB clients will be permitted to delete the target file while
  281. /// this file is open. This constant may be logically OR'd with other share
  282. /// access flags.
  283. /// </remarks>
  284. public const int FileShareDelete = 0x04;
  285. /// <summary>
  286. /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
  287. /// with <tt>setAttributes()</tt> will be read-only
  288. /// </summary>
  289. public const int AttrReadonly = 0x01;
  290. /// <summary>
  291. /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
  292. /// with <tt>setAttributes()</tt> will be hidden
  293. /// </summary>
  294. public const int AttrHidden = 0x02;
  295. /// <summary>
  296. /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
  297. /// with <tt>setAttributes()</tt> will be a system file
  298. /// </summary>
  299. public const int AttrSystem = 0x04;
  300. /// <summary>
  301. /// A file with this bit on as returned by <tt>getAttributes()</tt> is
  302. /// a volume
  303. /// </summary>
  304. public const int AttrVolume = 0x08;
  305. /// <summary>
  306. /// A file with this bit on as returned by <tt>getAttributes()</tt> is
  307. /// a directory
  308. /// </summary>
  309. public const int AttrDirectory = 0x10;
  310. /// <summary>
  311. /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
  312. /// with <tt>setAttributes()</tt> is an archived file
  313. /// </summary>
  314. public const int AttrArchive = 0x20;
  315. internal const int AttrCompressed = 0x800;
  316. internal const int AttrNormal = 0x080;
  317. internal const int AttrTemporary = 0x100;
  318. internal const int AttrGetMask = 0x7FFF;
  319. internal const int AttrSetMask = 0x30A7;
  320. internal const int DefaultAttrExpirationPeriod = 5000;
  321. internal static readonly int HashDot = ".".GetHashCode();
  322. internal static readonly int HashDotDot = "..".GetHashCode();
  323. //internal static LogStream log = LogStream.GetInstance();
  324. public LogStream Log
  325. {
  326. get { return LogStream.GetInstance(); }
  327. }
  328. internal static long AttrExpirationPeriod;
  329. internal static bool IgnoreCopyToException;
  330. static SmbFile()
  331. {
  332. // Open Function Encoding
  333. // create if the file does not exist
  334. // fail if the file exists
  335. // truncate if the file exists
  336. // share access
  337. // file attribute encoding
  338. // extended file attribute encoding(others same as above)
  339. /*try
  340. {
  341. Sharpen.Runtime.GetType("jcifs.Config");
  342. }
  343. catch (TypeLoadException cnfe)
  344. {
  345. Sharpen.Runtime.PrintStackTrace(cnfe);
  346. }*/
  347. AttrExpirationPeriod = Config.GetLong("jcifs.smb.client.attrExpirationPeriod", DefaultAttrExpirationPeriod
  348. );
  349. IgnoreCopyToException = Config.GetBoolean("jcifs.smb.client.ignoreCopyToException"
  350. , true);
  351. Dfs = new Dfs();
  352. }
  353. /// <summary>
  354. /// Returned by
  355. /// <see cref="GetType()">GetType()</see>
  356. /// if the resource this <tt>SmbFile</tt>
  357. /// represents is a regular file or directory.
  358. /// </summary>
  359. public const int TypeFilesystem = 0x01;
  360. /// <summary>
  361. /// Returned by
  362. /// <see cref="GetType()">GetType()</see>
  363. /// if the resource this <tt>SmbFile</tt>
  364. /// represents is a workgroup.
  365. /// </summary>
  366. public const int TypeWorkgroup = 0x02;
  367. /// <summary>
  368. /// Returned by
  369. /// <see cref="GetType()">GetType()</see>
  370. /// if the resource this <tt>SmbFile</tt>
  371. /// represents is a server.
  372. /// </summary>
  373. public const int TypeServer = 0x04;
  374. /// <summary>
  375. /// Returned by
  376. /// <see cref="GetType()">GetType()</see>
  377. /// if the resource this <tt>SmbFile</tt>
  378. /// represents is a share.
  379. /// </summary>
  380. public const int TypeShare = 0x08;
  381. /// <summary>
  382. /// Returned by
  383. /// <see cref="GetType()">GetType()</see>
  384. /// if the resource this <tt>SmbFile</tt>
  385. /// represents is a named pipe.
  386. /// </summary>
  387. public const int TypeNamedPipe = 0x10;
  388. /// <summary>
  389. /// Returned by
  390. /// <see cref="GetType()">GetType()</see>
  391. /// if the resource this <tt>SmbFile</tt>
  392. /// represents is a printer.
  393. /// </summary>
  394. public const int TypePrinter = 0x20;
  395. /// <summary>
  396. /// Returned by
  397. /// <see cref="GetType()">GetType()</see>
  398. /// if the resource this <tt>SmbFile</tt>
  399. /// represents is a communications device.
  400. /// </summary>
  401. public const int TypeComm = 0x40;
  402. private string _canon;
  403. private string _share;
  404. private long _createTime;
  405. private long _lastModified;
  406. private int _attributes;
  407. private long _attrExpiration;
  408. private long _size;
  409. private long _sizeExpiration;
  410. private bool _isExists;
  411. private int _shareAccess = FileShareRead | FileShareWrite | FileShareDelete;
  412. private bool _enableDfs = Config.GetBoolean("jcifs.smb.client.enabledfs", false);
  413. private SmbComBlankResponse _blankResp;
  414. private DfsReferral _dfsReferral;
  415. protected internal static Dfs Dfs;
  416. internal NtlmPasswordAuthentication Auth;
  417. internal SmbTree Tree;
  418. internal string Unc;
  419. internal int Fid;
  420. internal int Type;
  421. internal bool Opened;
  422. internal int TreeNum;
  423. public bool EnableDfs
  424. {
  425. get { return _enableDfs; }
  426. set { _enableDfs = value; }
  427. }
  428. /// <summary>
  429. /// Constructs an SmbFile representing a resource on an SMB network such as
  430. /// a file or directory.
  431. /// </summary>
  432. /// <remarks>
  433. /// Constructs an SmbFile representing a resource on an SMB network such as
  434. /// a file or directory. See the description and examples of smb URLs above.
  435. /// </remarks>
  436. /// <param name="url">A URL string</param>
  437. /// <exception cref="System.UriFormatException">
  438. /// If the <code>parent</code> and <code>child</code> parameters
  439. /// do not follow the prescribed syntax
  440. /// </exception>
  441. public SmbFile(string url)
  442. : this(new Uri(url))
  443. {
  444. }
  445. /// <summary>
  446. /// Constructs an SmbFile representing a resource on an SMB network such
  447. /// as a file or directory.
  448. /// </summary>
  449. /// <remarks>
  450. /// Constructs an SmbFile representing a resource on an SMB network such
  451. /// as a file or directory. The second parameter is a relative path from
  452. /// the <code>parent SmbFile</code>. See the description above for examples
  453. /// of using the second <code>name</code> parameter.
  454. /// </remarks>
  455. /// <param name="context">A base <code>SmbFile</code></param>
  456. /// <param name="name">A path string relative to the <code>parent</code> paremeter</param>
  457. /// <exception cref="System.UriFormatException">
  458. /// If the <code>parent</code> and <code>child</code> parameters
  459. /// do not follow the prescribed syntax
  460. /// </exception>
  461. /// <exception cref="UnknownHostException">If the server or workgroup of the <tt>context</tt> file cannot be determined
  462. /// </exception>
  463. public SmbFile(SmbFile context, string name)
  464. : this(context.IsWorkgroup0
  465. () ? new Uri("smb://" + name) : new Uri(context.Url.AbsoluteUri + name),
  466. context.Auth)
  467. {
  468. this._enableDfs = context.EnableDfs;
  469. if (!context.IsWorkgroup0())
  470. {
  471. Addresses = context.Addresses;
  472. if (context._share != null)
  473. {
  474. Tree = context.Tree;
  475. _dfsReferral = context._dfsReferral;
  476. }
  477. }
  478. }
  479. /// <summary>
  480. /// Constructs an SmbFile representing a resource on an SMB network such
  481. /// as a file or directory.
  482. /// </summary>
  483. /// <remarks>
  484. /// Constructs an SmbFile representing a resource on an SMB network such
  485. /// as a file or directory. The second parameter is a relative path from
  486. /// the <code>parent</code>. See the description above for examples of
  487. /// using the second <code>chile</code> parameter.
  488. /// </remarks>
  489. /// <param name="context">A URL string</param>
  490. /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
  491. /// <exception cref="System.UriFormatException">
  492. /// If the <code>context</code> and <code>name</code> parameters
  493. /// do not follow the prescribed syntax
  494. /// </exception>
  495. /*public SmbFile(string context, string name)
  496. : this(new Uri(new Uri(null, context), name))
  497. {
  498. }*/
  499. public SmbFile(string context, string name)
  500. : this(new Uri(context + name))
  501. {
  502. }
  503. /// <summary>
  504. /// Constructs an SmbFile representing a resource on an SMB network such
  505. /// as a file or directory.
  506. /// </summary>
  507. /// <remarks>
  508. /// Constructs an SmbFile representing a resource on an SMB network such
  509. /// as a file or directory.
  510. /// </remarks>
  511. /// <param name="url">A URL string</param>
  512. /// <param name="auth">The credentials the client should use for authentication</param>
  513. /// <exception cref="System.UriFormatException">If the <code>url</code> parameter does not follow the prescribed syntax
  514. /// </exception>
  515. public SmbFile(string url, NtlmPasswordAuthentication auth)
  516. : this(new Uri(url, UriKind.RelativeOrAbsolute),
  517. auth)
  518. {
  519. }
  520. /// <summary>Constructs an SmbFile representing a file on an SMB network.</summary>
  521. /// <remarks>
  522. /// Constructs an SmbFile representing a file on an SMB network. The
  523. /// <tt>shareAccess</tt> parameter controls what permissions other
  524. /// clients have when trying to access the same file while this instance
  525. /// is still open. This value is either <tt>FILE_NO_SHARE</tt> or any
  526. /// combination of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>,
  527. /// and <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
  528. /// </remarks>
  529. /// <param name="url">A URL string</param>
  530. /// <param name="auth">The credentials the client should use for authentication</param>
  531. /// <param name="shareAccess">Specifies what access other clients have while this file is open.
  532. /// </param>
  533. /// <exception cref="System.UriFormatException">If the <code>url</code> parameter does not follow the prescribed syntax
  534. /// </exception>
  535. public SmbFile(string url, NtlmPasswordAuthentication auth, int shareAccess)
  536. : this
  537. (new Uri(url), auth)
  538. {
  539. // Initially null; set by getUncPath; dir must end with '/'
  540. // Can be null
  541. // For getDfsPath() and getServerWithDfs()
  542. // Cannot be null
  543. // Initially null
  544. // Initially null; set by getUncPath; never ends with '/'
  545. // Initially 0; set by open()
  546. if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
  547. 0)
  548. {
  549. throw new RuntimeException("Illegal shareAccess parameter");
  550. }
  551. this._shareAccess = shareAccess;
  552. }
  553. /// <summary>
  554. /// Constructs an SmbFile representing a resource on an SMB network such
  555. /// as a file or directory.
  556. /// </summary>
  557. /// <remarks>
  558. /// Constructs an SmbFile representing a resource on an SMB network such
  559. /// as a file or directory. The second parameter is a relative path from
  560. /// the <code>context</code>. See the description above for examples of
  561. /// using the second <code>name</code> parameter.
  562. /// </remarks>
  563. /// <param name="context">A URL string</param>
  564. /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
  565. /// <param name="auth">The credentials the client should use for authentication</param>
  566. /// <exception cref="System.UriFormatException">
  567. /// If the <code>context</code> and <code>name</code> parameters
  568. /// do not follow the prescribed syntax
  569. /// </exception>
  570. public SmbFile(string context, string name, NtlmPasswordAuthentication auth)
  571. : this
  572. (new Uri(context + name)
  573. , auth)
  574. {
  575. }
  576. /// <summary>
  577. /// Constructs an SmbFile representing a resource on an SMB network such
  578. /// as a file or directory.
  579. /// </summary>
  580. /// <remarks>
  581. /// Constructs an SmbFile representing a resource on an SMB network such
  582. /// as a file or directory. The second parameter is a relative path from
  583. /// the <code>context</code>. See the description above for examples of
  584. /// using the second <code>name</code> parameter. The <tt>shareAccess</tt>
  585. /// parameter controls what permissions other clients have when trying
  586. /// to access the same file while this instance is still open. This
  587. /// value is either <tt>FILE_NO_SHARE</tt> or any combination
  588. /// of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
  589. /// <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
  590. /// </remarks>
  591. /// <param name="context">A URL string</param>
  592. /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
  593. /// <param name="auth">The credentials the client should use for authentication</param>
  594. /// <param name="shareAccess">Specifies what access other clients have while this file is open.
  595. /// </param>
  596. /// <exception cref="System.UriFormatException">
  597. /// If the <code>context</code> and <code>name</code> parameters
  598. /// do not follow the prescribed syntax
  599. /// </exception>
  600. public SmbFile(string context, string name, NtlmPasswordAuthentication auth, int
  601. shareAccess)
  602. : this(new Uri(context + name), auth)
  603. {
  604. if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
  605. 0)
  606. {
  607. throw new RuntimeException("Illegal shareAccess parameter");
  608. }
  609. this._shareAccess = shareAccess;
  610. }
  611. /// <summary>
  612. /// Constructs an SmbFile representing a resource on an SMB network such
  613. /// as a file or directory.
  614. /// </summary>
  615. /// <remarks>
  616. /// Constructs an SmbFile representing a resource on an SMB network such
  617. /// as a file or directory. The second parameter is a relative path from
  618. /// the <code>context</code>. See the description above for examples of
  619. /// using the second <code>name</code> parameter. The <tt>shareAccess</tt>
  620. /// parameter controls what permissions other clients have when trying
  621. /// to access the same file while this instance is still open. This
  622. /// value is either <tt>FILE_NO_SHARE</tt> or any combination
  623. /// of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
  624. /// <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
  625. /// </remarks>
  626. /// <param name="context">A base <code>SmbFile</code></param>
  627. /// <param name="name">A path string relative to the <code>context</code> file path</param>
  628. /// <param name="shareAccess">Specifies what access other clients have while this file is open.
  629. /// </param>
  630. /// <exception cref="System.UriFormatException">
  631. /// If the <code>context</code> and <code>name</code> parameters
  632. /// do not follow the prescribed syntax
  633. /// </exception>
  634. /// <exception cref="UnknownHostException"></exception>
  635. public SmbFile(SmbFile context, string name, int shareAccess)
  636. : this(context.IsWorkgroup0() ? new Uri("smb://" + name) : new Uri(
  637. context.Url.AbsoluteUri + name), context.Auth)
  638. {
  639. if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
  640. 0)
  641. {
  642. throw new RuntimeException("Illegal shareAccess parameter");
  643. }
  644. if (!context.IsWorkgroup0())
  645. {
  646. this.Addresses = context.Addresses;
  647. if (context._share != null || context.Tree != null)
  648. {
  649. Tree = context.Tree;
  650. _dfsReferral = context._dfsReferral;
  651. }
  652. }
  653. this._shareAccess = shareAccess;
  654. this._enableDfs = context.EnableDfs;
  655. }
  656. /// <summary>
  657. /// Constructs an SmbFile representing a resource on an SMB network such
  658. /// as a file or directory from a <tt>URL</tt> object.
  659. /// </summary>
  660. /// <remarks>
  661. /// Constructs an SmbFile representing a resource on an SMB network such
  662. /// as a file or directory from a <tt>URL</tt> object.
  663. /// </remarks>
  664. /// <param name="url">The URL of the target resource</param>
  665. protected SmbFile(Uri url)
  666. : this(url, new NtlmPasswordAuthentication(url.GetUserInfo
  667. ()))
  668. {
  669. }
  670. /// <summary>
  671. /// Constructs an SmbFile representing a resource on an SMB network such
  672. /// as a file or directory from a <tt>URL</tt> object and an
  673. /// <tt>NtlmPasswordAuthentication</tt> object.
  674. /// </summary>
  675. /// <remarks>
  676. /// Constructs an SmbFile representing a resource on an SMB network such
  677. /// as a file or directory from a <tt>URL</tt> object and an
  678. /// <tt>NtlmPasswordAuthentication</tt> object.
  679. /// </remarks>
  680. /// <param name="url">The URL of the target resource</param>
  681. /// <param name="auth">The credentials the client should use for authentication</param>
  682. public SmbFile(Uri url, NtlmPasswordAuthentication auth)
  683. {
  684. this.Auth = auth ?? new NtlmPasswordAuthentication(url.GetUserInfo());
  685. Url = url;
  686. GetUncPath0();
  687. }
  688. /// <exception cref="System.UriFormatException"></exception>
  689. /// <exception cref="UnknownHostException"></exception>
  690. /*internal SmbFile(Jcifs.Smb.SmbFile context, string name, int type, int attributes
  691. , long createTime, long lastModified, long size)
  692. : this(context.IsWorkgroup0() ?
  693. new Uri(null, "smb://" + name + "/") : new Uri(context.url,
  694. name + ((attributes & ATTR_DIRECTORY) > 0 ? "/" : string.Empty)))*/
  695. internal SmbFile(SmbFile context, string name, int type, int attributes
  696. , long createTime, long lastModified, long size)
  697. : this(context.IsWorkgroup0() ?
  698. new Uri("smb://" + name + "/") : new Uri(context.Url.AbsoluteUri +
  699. name + ((attributes & AttrDirectory) > 0 ? "/" : string.Empty)))
  700. {
  701. Auth = context.Auth;
  702. if (context._share != null)
  703. {
  704. Tree = context.Tree;
  705. _dfsReferral = context._dfsReferral;
  706. }
  707. int last = name.Length - 1;
  708. if (name[last] == '/')
  709. {
  710. name = Runtime.Substring(name, 0, last);
  711. }
  712. if (context._share == null)
  713. {
  714. Unc = "\\";
  715. }
  716. else
  717. {
  718. if (context.Unc.Equals("\\"))
  719. {
  720. Unc = '\\' + name;
  721. }
  722. else
  723. {
  724. Unc = context.Unc + '\\' + name;
  725. }
  726. }
  727. if (!context.IsWorkgroup0())
  728. {
  729. Addresses = context.Addresses;
  730. }
  731. this._enableDfs = context.EnableDfs;
  732. this.Type = type;
  733. this._attributes = attributes;
  734. this._createTime = createTime;
  735. this._lastModified = lastModified;
  736. this._size = size;
  737. _isExists = true;
  738. _attrExpiration = _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  739. }
  740. private SmbComBlankResponse Blank_resp()
  741. {
  742. if (_blankResp == null)
  743. {
  744. _blankResp = new SmbComBlankResponse();
  745. }
  746. return _blankResp;
  747. }
  748. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  749. internal virtual void ResolveDfs(ServerMessageBlock request)
  750. {
  751. if (!_enableDfs)
  752. {
  753. Connect0();
  754. return;
  755. }
  756. if (request is SmbComClose)
  757. {
  758. return;
  759. }
  760. Connect0();
  761. DfsReferral dr = Dfs.Resolve(Tree.Session.transport.TconHostName, Tree.Share, Unc
  762. , Auth);
  763. if (dr != null)
  764. {
  765. string service = null;
  766. if (request != null)
  767. {
  768. switch (request.Command)
  769. {
  770. case ServerMessageBlock.SmbComTransaction:
  771. case ServerMessageBlock.SmbComTransaction2:
  772. {
  773. switch (((SmbComTransaction)request).SubCommand & 0xFF)
  774. {
  775. case SmbComTransaction.Trans2GetDfsReferral:
  776. {
  777. break;
  778. }
  779. default:
  780. {
  781. service = "A:";
  782. break;
  783. }
  784. }
  785. break;
  786. }
  787. default:
  788. {
  789. service = "A:";
  790. break;
  791. }
  792. }
  793. }
  794. DfsReferral start = dr;
  795. SmbException se = null;
  796. do
  797. {
  798. try
  799. {
  800. if (Log.Level >= 2)
  801. {
  802. Log.WriteLine("DFS redirect: " + dr);
  803. }
  804. UniAddress addr = UniAddress.GetByName(dr.Server);
  805. SmbTransport trans = SmbTransport.GetSmbTransport(addr, Url.Port);
  806. trans.Connect();
  807. Tree = trans.GetSmbSession(Auth).GetSmbTree(dr.Share, service);
  808. if (dr != start && dr.Key != null)
  809. {
  810. dr.Map.Put(dr.Key, dr);
  811. }
  812. se = null;
  813. break;
  814. }
  815. catch (IOException ioe)
  816. {
  817. if (ioe is SmbException)
  818. {
  819. se = (SmbException)ioe;
  820. }
  821. else
  822. {
  823. se = new SmbException(dr.Server, ioe);
  824. }
  825. }
  826. dr = dr.Next;
  827. }
  828. while (dr != start);
  829. if (se != null)
  830. {
  831. throw se;
  832. }
  833. if (Log.Level >= 3)
  834. {
  835. Log.WriteLine(dr);
  836. }
  837. _dfsReferral = dr;
  838. if (dr.PathConsumed < 0)
  839. {
  840. dr.PathConsumed = 0;
  841. }
  842. else
  843. {
  844. if (dr.PathConsumed > Unc.Length)
  845. {
  846. dr.PathConsumed = Unc.Length;
  847. }
  848. }
  849. string dunc = Runtime.Substring(Unc, dr.PathConsumed);
  850. if (dunc.Equals(string.Empty))
  851. {
  852. dunc = "\\";
  853. }
  854. if (!dr.Path.Equals(string.Empty))
  855. {
  856. dunc = "\\" + dr.Path + dunc;
  857. }
  858. Unc = dunc;
  859. if (request != null && request.Path != null && request.Path.EndsWith("\\") && dunc
  860. .EndsWith("\\") == false)
  861. {
  862. dunc += "\\";
  863. }
  864. if (request != null)
  865. {
  866. request.Path = dunc;
  867. request.Flags2 |= SmbConstants.Flags2ResolvePathsInDfs;
  868. }
  869. }
  870. else
  871. {
  872. if (Tree.InDomainDfs && !(request is NtTransQuerySecurityDesc) && !(request is SmbComClose
  873. ) && !(request is SmbComFindClose2))
  874. {
  875. throw new SmbException(NtStatus.NtStatusNotFound, false);
  876. }
  877. if (request != null)
  878. {
  879. request.Flags2 &= ~SmbConstants.Flags2ResolvePathsInDfs;
  880. }
  881. }
  882. }
  883. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  884. internal virtual void Send(ServerMessageBlock request, ServerMessageBlock response
  885. )
  886. {
  887. for (; ; )
  888. {
  889. ResolveDfs(request);
  890. try
  891. {
  892. Tree.Send(request, response);
  893. break;
  894. }
  895. catch (DfsReferral dre)
  896. {
  897. if (dre.ResolveHashes)
  898. {
  899. throw;
  900. }
  901. request.Reset();
  902. }
  903. }
  904. }
  905. internal static string QueryLookup(string query, string param)
  906. {
  907. char[] instr = query.ToCharArray();
  908. int i;
  909. int ch;
  910. int st;
  911. int eq;
  912. st = eq = 0;
  913. for (i = 0; i < instr.Length; i++)
  914. {
  915. ch = instr[i];
  916. if (ch == '&')
  917. {
  918. if (eq > st)
  919. {
  920. string p = new string(instr, st, eq - st);
  921. if (Runtime.EqualsIgnoreCase(p, param))
  922. {
  923. eq++;
  924. return new string(instr, eq, i - eq);
  925. }
  926. }
  927. st = i + 1;
  928. }
  929. else
  930. {
  931. if (ch == '=')
  932. {
  933. eq = i;
  934. }
  935. }
  936. }
  937. if (eq > st)
  938. {
  939. string p = new string(instr, st, eq - st);
  940. if (Runtime.EqualsIgnoreCase(p, param))
  941. {
  942. eq++;
  943. return new string(instr, eq, instr.Length - eq);
  944. }
  945. }
  946. return null;
  947. }
  948. internal UniAddress[] Addresses;
  949. internal int AddressIndex;
  950. /// <exception cref="UnknownHostException"></exception>
  951. internal virtual UniAddress GetAddress()
  952. {
  953. if (AddressIndex == 0)
  954. {
  955. return GetFirstAddress();
  956. }
  957. return Addresses[AddressIndex - 1];
  958. }
  959. /// <exception cref="UnknownHostException"></exception>
  960. internal virtual UniAddress GetFirstAddress()
  961. {
  962. AddressIndex = 0;
  963. string host = Url.GetHost();
  964. string path = Url.AbsolutePath;
  965. string query = Url.GetQuery();
  966. if (Addresses != null && Addresses.Length > 0)
  967. {
  968. return GetNextAddress();
  969. }
  970. if (query != null)
  971. {
  972. string server = QueryLookup(query, "server");
  973. if (!string.IsNullOrEmpty(server))
  974. {
  975. Addresses = new UniAddress[1];
  976. Addresses[0] = UniAddress.GetByName(server);
  977. return GetNextAddress();
  978. }
  979. string address = QueryLookup(query, "address");
  980. if (!string.IsNullOrEmpty(address))
  981. {
  982. byte[] ip = Extensions.GetAddressByName(address).GetAddressBytes();
  983. Addresses = new UniAddress[1];
  984. //addresses[0] = new UniAddress(IPAddress.Parse(host, ip));
  985. Addresses[0] = new UniAddress(IPAddress.Parse(host));
  986. return GetNextAddress();
  987. }
  988. }
  989. if (host.Length == 0)
  990. {
  991. try
  992. {
  993. NbtAddress addr = NbtAddress.GetByName(NbtAddress.MasterBrowserName, 0x01, null);
  994. Addresses = new UniAddress[1];
  995. Addresses[0] = UniAddress.GetByName(addr.GetHostAddress());
  996. }
  997. catch (UnknownHostException uhe)
  998. {
  999. NtlmPasswordAuthentication.InitDefaults();
  1000. if (NtlmPasswordAuthentication.DefaultDomain.Equals("?"))
  1001. {
  1002. throw;
  1003. }
  1004. Addresses = UniAddress.GetAllByName(NtlmPasswordAuthentication.DefaultDomain, true
  1005. );
  1006. }
  1007. }
  1008. else
  1009. {
  1010. if (path.Length == 0 || path.Equals("/"))
  1011. {
  1012. Addresses = UniAddress.GetAllByName(host, true);
  1013. }
  1014. else
  1015. {
  1016. Addresses = UniAddress.GetAllByName(host, false);
  1017. }
  1018. }
  1019. return GetNextAddress();
  1020. }
  1021. internal virtual UniAddress GetNextAddress()
  1022. {
  1023. UniAddress addr = null;
  1024. if (AddressIndex < Addresses.Length)
  1025. {
  1026. addr = Addresses[AddressIndex++];
  1027. }
  1028. return addr;
  1029. }
  1030. internal virtual bool HasNextAddress()
  1031. {
  1032. return AddressIndex < Addresses.Length;
  1033. }
  1034. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1035. internal virtual void Connect0()
  1036. {
  1037. try
  1038. {
  1039. Connect();
  1040. }
  1041. catch (UnknownHostException uhe)
  1042. {
  1043. throw new SmbException("Failed to connect to server", uhe);
  1044. }
  1045. catch (SmbException se)
  1046. {
  1047. throw;
  1048. }
  1049. catch (IOException ioe)
  1050. {
  1051. throw new SmbException("Failed to connect to server", ioe);
  1052. }
  1053. }
  1054. /// <exception cref="System.IO.IOException"></exception>
  1055. internal virtual void DoConnect()
  1056. {
  1057. SmbTransport trans;
  1058. UniAddress addr;
  1059. addr = GetAddress();
  1060. if (Tree != null && Tree.Session.transport.Address.Equals(addr))
  1061. {
  1062. trans = Tree.Session.transport;
  1063. }
  1064. else
  1065. {
  1066. trans = SmbTransport.GetSmbTransport(addr, Url.Port);
  1067. Tree = trans.GetSmbSession(Auth).GetSmbTree(_share, null);
  1068. }
  1069. string hostName = GetServerWithDfs();
  1070. if (_enableDfs)
  1071. {
  1072. Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
  1073. }
  1074. if (Tree.InDomainDfs)
  1075. {
  1076. Tree.ConnectionState = 2;
  1077. }
  1078. try
  1079. {
  1080. if (Log.Level >= 3)
  1081. {
  1082. Log.WriteLine("doConnect: " + addr);
  1083. }
  1084. Tree.TreeConnect(null, null);
  1085. }
  1086. catch (SmbAuthException sae)
  1087. {
  1088. NtlmPasswordAuthentication a;
  1089. SmbSession ssn;
  1090. if (_share == null)
  1091. {
  1092. // IPC$ - try "anonymous" credentials
  1093. ssn = trans.GetSmbSession(NtlmPasswordAuthentication.Null);
  1094. Tree = ssn.GetSmbTree(null, null);
  1095. Tree.TreeConnect(null, null);
  1096. }
  1097. else
  1098. {
  1099. if ((a = NtlmAuthenticator.RequestNtlmPasswordAuthentication(Url.ToString(), sae)
  1100. ) != null)
  1101. {
  1102. Auth = a;
  1103. ssn = trans.GetSmbSession(Auth);
  1104. Tree = ssn.GetSmbTree(_share, null);
  1105. Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
  1106. if (Tree.InDomainDfs)
  1107. {
  1108. Tree.ConnectionState = 2;
  1109. }
  1110. Tree.TreeConnect(null, null);
  1111. }
  1112. else
  1113. {
  1114. if (Log.Level >= 1 && HasNextAddress())
  1115. {
  1116. Runtime.PrintStackTrace(sae, Log);
  1117. }
  1118. throw;
  1119. }
  1120. }
  1121. }
  1122. }
  1123. /// <summary>It is not necessary to call this method directly.</summary>
  1124. /// <remarks>
  1125. /// It is not necessary to call this method directly. This is the
  1126. /// <tt>URLConnection</tt> implementation of <tt>connect()</tt>.
  1127. /// </remarks>
  1128. /// <exception cref="System.IO.IOException"></exception>
  1129. public void Connect()
  1130. {
  1131. SmbTransport trans;
  1132. SmbSession ssn;
  1133. UniAddress addr;
  1134. if (IsConnected())
  1135. {
  1136. return;
  1137. }
  1138. GetUncPath0();
  1139. GetFirstAddress();
  1140. for (; ; )
  1141. {
  1142. try
  1143. {
  1144. DoConnect();
  1145. return;
  1146. }
  1147. catch (SmbAuthException sae)
  1148. {
  1149. throw;
  1150. }
  1151. catch (SmbException se)
  1152. {
  1153. // Prevents account lockout on servers with multiple IPs
  1154. if (GetNextAddress() == null)
  1155. {
  1156. throw;
  1157. }
  1158. else
  1159. {
  1160. RemoveCurrentAddress();
  1161. }
  1162. if (Log.Level >= 3)
  1163. {
  1164. Runtime.PrintStackTrace(se, Log);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. internal virtual bool IsConnected()
  1170. {
  1171. return Tree != null && Tree.ConnectionState == 2;
  1172. }
  1173. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1174. internal virtual int Open0(int flags, int access, int attrs, int options)
  1175. {
  1176. int f;
  1177. Connect0();
  1178. if (Log.Level >= 3)
  1179. {
  1180. Log.WriteLine("open0: " + Unc);
  1181. }
  1182. if (Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs))
  1183. {
  1184. SmbComNtCreateAndXResponse response = new SmbComNtCreateAndXResponse();
  1185. SmbComNtCreateAndX request = new SmbComNtCreateAndX(Unc, flags, access, _shareAccess
  1186. , attrs, options, null);
  1187. if (this is SmbNamedPipe)
  1188. {
  1189. request.Flags0 |= 0x16;
  1190. request.DesiredAccess |= 0x20000;
  1191. response.IsExtended = true;
  1192. }
  1193. Send(request, response);
  1194. f = response.Fid;
  1195. _attributes = response.ExtFileAttributes & AttrGetMask;
  1196. _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  1197. _isExists = true;
  1198. }
  1199. else
  1200. {
  1201. SmbComOpenAndXResponse response = new SmbComOpenAndXResponse();
  1202. Send(new SmbComOpenAndX(Unc, access, flags, null), response);
  1203. f = response.Fid;
  1204. }
  1205. return f;
  1206. }
  1207. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1208. internal virtual void Open(int flags, int access, int attrs, int options)
  1209. {
  1210. if (IsOpen())
  1211. {
  1212. return;
  1213. }
  1214. Fid = Open0(flags, access, attrs, options);
  1215. Opened = true;
  1216. TreeNum = Tree.TreeNum;
  1217. }
  1218. internal virtual bool IsOpen()
  1219. {
  1220. bool ans = Opened && IsConnected() && TreeNum == Tree.TreeNum;
  1221. return ans;
  1222. }
  1223. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1224. internal virtual void Close(int f, long lastWriteTime)
  1225. {
  1226. if (Log.Level >= 3)
  1227. {
  1228. Log.WriteLine("close: " + f);
  1229. }
  1230. Send(new SmbComClose(f, lastWriteTime), Blank_resp());
  1231. }
  1232. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1233. internal virtual void Close(long lastWriteTime)
  1234. {
  1235. if (IsOpen() == false)
  1236. {
  1237. return;
  1238. }
  1239. Close(Fid, lastWriteTime);
  1240. Opened = false;
  1241. }
  1242. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1243. internal virtual void Close()
  1244. {
  1245. Close(0L);
  1246. }
  1247. /// <summary>
  1248. /// Returns the <tt>NtlmPasswordAuthentication</tt> object used as
  1249. /// credentials with this file or pipe.
  1250. /// </summary>
  1251. /// <remarks>
  1252. /// Returns the <tt>NtlmPasswordAuthentication</tt> object used as
  1253. /// credentials with this file or pipe. This can be used to retrieve the
  1254. /// username for example:
  1255. /// <tt>
  1256. /// String username = f.getPrincipal().getName();
  1257. /// </tt>
  1258. /// The <tt>Principal</tt> object returned will never be <tt>null</tt>
  1259. /// however the username can be <tt>null</tt> indication anonymous
  1260. /// credentials were used (e.g. some IPC$ services).
  1261. /// </remarks>
  1262. public virtual Principal GetPrincipal()
  1263. {
  1264. return Auth;
  1265. }
  1266. /// <summary>Returns the last component of the target URL.</summary>
  1267. /// <remarks>
  1268. /// Returns the last component of the target URL. This will
  1269. /// effectively be the name of the file or directory represented by this
  1270. /// <code>SmbFile</code> or in the case of URLs that only specify a server
  1271. /// or workgroup, the server or workgroup will be returned. The name of
  1272. /// the root URL <code>smb://</code> is also <code>smb://</code>. If this
  1273. /// <tt>SmbFile</tt> refers to a workgroup, server, share, or directory,
  1274. /// the name will include a trailing slash '/' so that composing new
  1275. /// <tt>SmbFile</tt>s will maintain the trailing slash requirement.
  1276. /// </remarks>
  1277. /// <returns>
  1278. /// The last component of the URL associated with this SMB
  1279. /// resource or <code>smb://</code> if the resource is <code>smb://</code>
  1280. /// itself.
  1281. /// </returns>
  1282. public virtual string GetName()
  1283. {
  1284. GetUncPath0();
  1285. if (_canon.Length > 1)
  1286. {
  1287. int i = _canon.Length - 2;
  1288. while (_canon[i] != '/')
  1289. {
  1290. i--;
  1291. }
  1292. return Runtime.Substring(_canon, i + 1);
  1293. }
  1294. if (_share != null)
  1295. {
  1296. return _share + '/';
  1297. }
  1298. if (Url.GetHost().Length > 0)
  1299. {
  1300. return Url.GetHost() + '/';
  1301. }
  1302. return "smb://";
  1303. }
  1304. /// <summary>
  1305. /// Everything but the last component of the URL representing this SMB
  1306. /// resource is effectivly it's parent.
  1307. /// </summary>
  1308. /// <remarks>
  1309. /// Everything but the last component of the URL representing this SMB
  1310. /// resource is effectivly it's parent. The root URL <code>smb://</code>
  1311. /// does not have a parent. In this case <code>smb://</code> is returned.
  1312. /// </remarks>
  1313. /// <returns>
  1314. /// The parent directory of this SMB resource or
  1315. /// <code>smb://</code> if the resource refers to the root of the URL
  1316. /// hierarchy which incedentally is also <code>smb://</code>.
  1317. /// </returns>
  1318. public virtual string GetParent()
  1319. {
  1320. string str = Url.Authority;
  1321. if (str.Length > 0)
  1322. {
  1323. StringBuilder sb = new StringBuilder("smb://");
  1324. sb.Append(str);
  1325. GetUncPath0();
  1326. if (_canon.Length > 1)
  1327. {
  1328. sb.Append(_canon);
  1329. }
  1330. else
  1331. {
  1332. sb.Append('/');
  1333. }
  1334. str = sb.ToString();
  1335. int i = str.Length - 2;
  1336. while (str[i] != '/')
  1337. {
  1338. i--;
  1339. }
  1340. return Runtime.Substring(str, 0, i + 1);
  1341. }
  1342. return "smb://";
  1343. }
  1344. /// <summary>Returns the full uncanonicalized URL of this SMB resource.</summary>
  1345. /// <remarks>
  1346. /// Returns the full uncanonicalized URL of this SMB resource. An
  1347. /// <code>SmbFile</code> constructed with the result of this method will
  1348. /// result in an <code>SmbFile</code> that is equal to the original.
  1349. /// </remarks>
  1350. /// <returns>The uncanonicalized full URL of this SMB resource.</returns>
  1351. public virtual string GetPath()
  1352. {
  1353. return Url.ToString();
  1354. }
  1355. internal virtual string GetUncPath0()
  1356. {
  1357. if (Unc == null)
  1358. {
  1359. char[] instr = Url.LocalPath.ToCharArray();
  1360. char[] outstr = new char[instr.Length];
  1361. int length = instr.Length;
  1362. int i;
  1363. int o;
  1364. int state;
  1365. state = 0;
  1366. o = 0;
  1367. for (i = 0; i < length; i++)
  1368. {
  1369. switch (state)
  1370. {
  1371. case 0:
  1372. {
  1373. if (instr[i] != '/')
  1374. {
  1375. return null;
  1376. }
  1377. outstr[o++] = instr[i];
  1378. state = 1;
  1379. break;
  1380. }
  1381. case 1:
  1382. {
  1383. if (instr[i] == '/')
  1384. {
  1385. break;
  1386. }
  1387. if (instr[i] == '.' && ((i + 1) >= length || instr[i + 1] == '/'))
  1388. {
  1389. i++;
  1390. break;
  1391. }
  1392. if ((i + 1) < length && instr[i] == '.' && instr[i + 1] == '.' && ((i + 2) >= length
  1393. || instr[i + 2] == '/'))
  1394. {
  1395. i += 2;
  1396. if (o == 1)
  1397. {
  1398. break;
  1399. }
  1400. do
  1401. {
  1402. o--;
  1403. }
  1404. while (o > 1 && outstr[o - 1] != '/');
  1405. break;
  1406. }
  1407. state = 2;
  1408. goto case 2;
  1409. }
  1410. case 2:
  1411. {
  1412. if (instr[i] == '/')
  1413. {
  1414. state = 1;
  1415. }
  1416. outstr[o++] = instr[i];
  1417. break;
  1418. }
  1419. }
  1420. }
  1421. _canon = new string(outstr, 0, o);
  1422. if (o > 1)
  1423. {
  1424. o--;
  1425. i = _canon.IndexOf('/', 1);
  1426. if (i < 0)
  1427. {
  1428. _share = Runtime.Substring(_canon, 1);
  1429. Unc = "\\";
  1430. }
  1431. else
  1432. {
  1433. if (i == o)
  1434. {
  1435. _share = Runtime.Substring(_canon, 1, i);
  1436. Unc = "\\";
  1437. }
  1438. else
  1439. {
  1440. _share = Runtime.Substring(_canon, 1, i);
  1441. Unc = Runtime.Substring(_canon, i, outstr[o] == '/' ? o : o + 1);
  1442. Unc = Unc.Replace('/', '\\');
  1443. }
  1444. }
  1445. }
  1446. else
  1447. {
  1448. _share = null;
  1449. Unc = "\\";
  1450. }
  1451. }
  1452. return Unc;
  1453. }
  1454. /// <summary>Retuns the Windows UNC style path with backslashs intead of forward slashes.
  1455. /// </summary>
  1456. /// <remarks>Retuns the Windows UNC style path with backslashs intead of forward slashes.
  1457. /// </remarks>
  1458. /// <returns>The UNC path.</returns>
  1459. public virtual string GetUncPath()
  1460. {
  1461. GetUncPath0();
  1462. if (_share == null)
  1463. {
  1464. return "\\\\" + Url.GetHost();
  1465. }
  1466. return "\\\\" + Url.GetHost() + _canon.Replace('/', '\\');
  1467. }
  1468. /// <summary>
  1469. /// Returns the full URL of this SMB resource with '.' and '..' components
  1470. /// factored out.
  1471. /// </summary>
  1472. /// <remarks>
  1473. /// Returns the full URL of this SMB resource with '.' and '..' components
  1474. /// factored out. An <code>SmbFile</code> constructed with the result of
  1475. /// this method will result in an <code>SmbFile</code> that is equal to
  1476. /// the original.
  1477. /// </remarks>
  1478. /// <returns>The canonicalized URL of this SMB resource.</returns>
  1479. public virtual string GetCanonicalPath()
  1480. {
  1481. string str = Url.Authority;
  1482. GetUncPath0();
  1483. if (str.Length > 0)
  1484. {
  1485. return "smb://" + Url.Authority + _canon;
  1486. }
  1487. return "smb://";
  1488. }
  1489. /// <summary>Retrieves the share associated with this SMB resource.</summary>
  1490. /// <remarks>
  1491. /// Retrieves the share associated with this SMB resource. In
  1492. /// the case of <code>smb://</code>, <code>smb://workgroup/</code>,
  1493. /// and <code>smb://server/</code> URLs which do not specify a share,
  1494. /// <code>null</code> will be returned.
  1495. /// </remarks>
  1496. /// <returns>The share component or <code>null</code> if there is no share</returns>
  1497. public virtual string GetShare()
  1498. {
  1499. return _share;
  1500. }
  1501. internal virtual string GetServerWithDfs()
  1502. {
  1503. if (_dfsReferral != null)
  1504. {
  1505. return _dfsReferral.Server;
  1506. }
  1507. return GetServer();
  1508. }
  1509. /// <summary>Retrieve the hostname of the server for this SMB resource.</summary>
  1510. /// <remarks>
  1511. /// Retrieve the hostname of the server for this SMB resource. If this
  1512. /// <code>SmbFile</code> references a workgroup, the name of the workgroup
  1513. /// is returned. If this <code>SmbFile</code> refers to the root of this
  1514. /// SMB network hierarchy, <code>null</code> is returned.
  1515. /// </remarks>
  1516. /// <returns>
  1517. /// The server or workgroup name or <code>null</code> if this
  1518. /// <code>SmbFile</code> refers to the root <code>smb://</code> resource.
  1519. /// </returns>
  1520. public virtual string GetServer()
  1521. {
  1522. string str = Url.GetHost();
  1523. if (str.Length == 0)
  1524. {
  1525. return null;
  1526. }
  1527. return str;
  1528. }
  1529. /// <summary>Returns type of of object this <tt>SmbFile</tt> represents.</summary>
  1530. /// <remarks>Returns type of of object this <tt>SmbFile</tt> represents.</remarks>
  1531. /// <returns>
  1532. /// <tt>TYPE_FILESYSTEM, TYPE_WORKGROUP, TYPE_SERVER, TYPE_SHARE,
  1533. /// TYPE_PRINTER, TYPE_NAMED_PIPE</tt>, or <tt>TYPE_COMM</tt>.
  1534. /// </returns>
  1535. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1536. public new virtual int GetType()
  1537. {
  1538. if (Type == 0)
  1539. {
  1540. if (GetUncPath0().Length > 1)
  1541. {
  1542. Type = TypeFilesystem;
  1543. }
  1544. else
  1545. {
  1546. if (_share != null)
  1547. {
  1548. // treeConnect good enough to test service type
  1549. Connect0();
  1550. if (_share.Equals("IPC$"))
  1551. {
  1552. Type = TypeNamedPipe;
  1553. }
  1554. else
  1555. {
  1556. if (Tree.Service.Equals("LPT1:"))
  1557. {
  1558. Type = TypePrinter;
  1559. }
  1560. else
  1561. {
  1562. if (Tree.Service.Equals("COMM"))
  1563. {
  1564. Type = TypeComm;
  1565. }
  1566. else
  1567. {
  1568. Type = TypeShare;
  1569. }
  1570. }
  1571. }
  1572. }
  1573. else
  1574. {
  1575. if (string.IsNullOrEmpty(Url.Authority))
  1576. {
  1577. Type = TypeWorkgroup;
  1578. }
  1579. else
  1580. {
  1581. UniAddress addr;
  1582. try
  1583. {
  1584. addr = GetAddress();
  1585. }
  1586. catch (UnknownHostException uhe)
  1587. {
  1588. throw new SmbException(Url.ToString(), uhe);
  1589. }
  1590. if (addr.GetAddress() is NbtAddress)
  1591. {
  1592. int code = ((NbtAddress)addr.GetAddress()).GetNameType();
  1593. if (code == 0x1d || code == 0x1b)
  1594. {
  1595. Type = TypeWorkgroup;
  1596. return Type;
  1597. }
  1598. }
  1599. Type = TypeServer;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. return Type;
  1605. }
  1606. /// <exception cref="UnknownHostException"></exception>
  1607. internal virtual bool IsWorkgroup0()
  1608. {
  1609. if (Type == TypeWorkgroup || Url.GetHost().Length == 0)
  1610. {
  1611. Type = TypeWorkgroup;
  1612. return true;
  1613. }
  1614. GetUncPath0();
  1615. if (_share == null)
  1616. {
  1617. UniAddress addr = GetAddress();
  1618. if (addr.GetAddress() is NbtAddress)
  1619. {
  1620. int code = ((NbtAddress)addr.GetAddress()).GetNameType();
  1621. if (code == 0x1d || code == 0x1b)
  1622. {
  1623. Type = TypeWorkgroup;
  1624. return true;
  1625. }
  1626. }
  1627. Type = TypeServer;
  1628. }
  1629. return false;
  1630. }
  1631. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1632. internal virtual IInfo QueryPath(string path, int infoLevel)
  1633. {
  1634. Connect0();
  1635. if (Log.Level >= 3)
  1636. {
  1637. Log.WriteLine("queryPath: " + path);
  1638. }
  1639. if (Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs))
  1640. {
  1641. Trans2QueryPathInformationResponse response = new Trans2QueryPathInformationResponse
  1642. (infoLevel);
  1643. Send(new Trans2QueryPathInformation(path, infoLevel), response);
  1644. return response.Info;
  1645. }
  1646. else
  1647. {
  1648. SmbComQueryInformationResponse response = new SmbComQueryInformationResponse(Tree
  1649. .Session.transport.Server.ServerTimeZone * 1000 * 60L);
  1650. Send(new SmbComQueryInformation(path), response);
  1651. return response;
  1652. }
  1653. }
  1654. /// <summary>Tests to see if the SMB resource exists.</summary>
  1655. /// <remarks>
  1656. /// Tests to see if the SMB resource exists. If the resource refers
  1657. /// only to a server, this method determines if the server exists on the
  1658. /// network and is advertising SMB services. If this resource refers to
  1659. /// a workgroup, this method determines if the workgroup name is valid on
  1660. /// the local SMB network. If this <code>SmbFile</code> refers to the root
  1661. /// <code>smb://</code> resource <code>true</code> is always returned. If
  1662. /// this <code>SmbFile</code> is a traditional file or directory, it will
  1663. /// be queried for on the specified server as expected.
  1664. /// </remarks>
  1665. /// <returns>
  1666. /// <code>true</code> if the resource exists or is alive or
  1667. /// <code>false</code> otherwise
  1668. /// </returns>
  1669. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1670. public virtual bool Exists()
  1671. {
  1672. if (_attrExpiration > Runtime.CurrentTimeMillis())
  1673. {
  1674. return _isExists;
  1675. }
  1676. _attributes = AttrReadonly | AttrDirectory;
  1677. _createTime = 0L;
  1678. _lastModified = 0L;
  1679. _isExists = false;
  1680. try
  1681. {
  1682. if (Url.GetHost().Length == 0)
  1683. {
  1684. }
  1685. else
  1686. {
  1687. if (_share == null)
  1688. {
  1689. if (GetType() == TypeWorkgroup)
  1690. {
  1691. UniAddress.GetByName(Url.GetHost(), true);
  1692. }
  1693. else
  1694. {
  1695. UniAddress.GetByName(Url.GetHost()).GetHostName();
  1696. }
  1697. }
  1698. else
  1699. {
  1700. if (GetUncPath0().Length == 1 || Runtime.EqualsIgnoreCase(_share, "IPC$"))
  1701. {
  1702. Connect0();
  1703. }
  1704. else
  1705. {
  1706. // treeConnect is good enough
  1707. IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
  1708. );
  1709. _attributes = info.GetAttributes();
  1710. _createTime = info.GetCreateTime();
  1711. _lastModified = info.GetLastWriteTime();
  1712. }
  1713. }
  1714. }
  1715. _isExists = true;
  1716. }
  1717. catch (UnknownHostException)
  1718. {
  1719. }
  1720. catch (SmbException se)
  1721. {
  1722. switch (se.GetNtStatus())
  1723. {
  1724. case NtStatus.NtStatusNoSuchFile:
  1725. case NtStatus.NtStatusObjectNameInvalid:
  1726. case NtStatus.NtStatusObjectNameNotFound:
  1727. case NtStatus.NtStatusObjectPathNotFound:
  1728. {
  1729. break;
  1730. }
  1731. default:
  1732. {
  1733. throw;
  1734. }
  1735. }
  1736. }
  1737. _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  1738. return _isExists;
  1739. }
  1740. /// <summary>
  1741. /// Tests to see if the file this <code>SmbFile</code> represents can be
  1742. /// read.
  1743. /// </summary>
  1744. /// <remarks>
  1745. /// Tests to see if the file this <code>SmbFile</code> represents can be
  1746. /// read. Because any file, directory, or other resource can be read if it
  1747. /// exists, this method simply calls the <code>exists</code> method.
  1748. /// </remarks>
  1749. /// <returns><code>true</code> if the file is read-only</returns>
  1750. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1751. public virtual bool CanRead()
  1752. {
  1753. if (GetType() == TypeNamedPipe)
  1754. {
  1755. // try opening the pipe for reading?
  1756. return true;
  1757. }
  1758. return Exists();
  1759. }
  1760. // try opening and catch sharing violation?
  1761. /// <summary>
  1762. /// Tests to see if the file this <code>SmbFile</code> represents
  1763. /// exists and is not marked read-only.
  1764. /// </summary>
  1765. /// <remarks>
  1766. /// Tests to see if the file this <code>SmbFile</code> represents
  1767. /// exists and is not marked read-only. By default, resources are
  1768. /// considered to be read-only and therefore for <code>smb://</code>,
  1769. /// <code>smb://workgroup/</code>, and <code>smb://server/</code> resources
  1770. /// will be read-only.
  1771. /// </remarks>
  1772. /// <returns>
  1773. /// <code>true</code> if the resource exists is not marked
  1774. /// read-only
  1775. /// </returns>
  1776. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1777. public virtual bool CanWrite()
  1778. {
  1779. if (GetType() == TypeNamedPipe)
  1780. {
  1781. // try opening the pipe for writing?
  1782. return true;
  1783. }
  1784. return Exists() && (_attributes & AttrReadonly) == 0;
  1785. }
  1786. /// <summary>Tests to see if the file this <code>SmbFile</code> represents is a directory.
  1787. /// </summary>
  1788. /// <remarks>Tests to see if the file this <code>SmbFile</code> represents is a directory.
  1789. /// </remarks>
  1790. /// <returns><code>true</code> if this <code>SmbFile</code> is a directory</returns>
  1791. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1792. public virtual bool IsDirectory()
  1793. {
  1794. if (GetUncPath0().Length == 1)
  1795. {
  1796. return true;
  1797. }
  1798. if (!Exists())
  1799. {
  1800. return false;
  1801. }
  1802. return (_attributes & AttrDirectory) == AttrDirectory;
  1803. }
  1804. /// <summary>Tests to see if the file this <code>SmbFile</code> represents is not a directory.
  1805. /// </summary>
  1806. /// <remarks>Tests to see if the file this <code>SmbFile</code> represents is not a directory.
  1807. /// </remarks>
  1808. /// <returns><code>true</code> if this <code>SmbFile</code> is not a directory</returns>
  1809. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1810. public virtual bool IsFile()
  1811. {
  1812. if (GetUncPath0().Length == 1)
  1813. {
  1814. return false;
  1815. }
  1816. Exists();
  1817. return (_attributes & AttrDirectory) == 0;
  1818. }
  1819. /// <summary>
  1820. /// Tests to see if the file this SmbFile represents is marked as
  1821. /// hidden.
  1822. /// </summary>
  1823. /// <remarks>
  1824. /// Tests to see if the file this SmbFile represents is marked as
  1825. /// hidden. This method will also return true for shares with names that
  1826. /// end with '$' such as <code>IPC$</code> or <code>C$</code>.
  1827. /// </remarks>
  1828. /// <returns><code>true</code> if the <code>SmbFile</code> is marked as being hidden</returns>
  1829. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1830. public virtual bool IsHidden()
  1831. {
  1832. if (_share == null)
  1833. {
  1834. return false;
  1835. }
  1836. if (GetUncPath0().Length == 1)
  1837. {
  1838. if (_share.EndsWith("$"))
  1839. {
  1840. return true;
  1841. }
  1842. return false;
  1843. }
  1844. Exists();
  1845. return (_attributes & AttrHidden) == AttrHidden;
  1846. }
  1847. /// <summary>
  1848. /// If the path of this <code>SmbFile</code> falls within a DFS volume,
  1849. /// this method will return the referral path to which it maps.
  1850. /// </summary>
  1851. /// <remarks>
  1852. /// If the path of this <code>SmbFile</code> falls within a DFS volume,
  1853. /// this method will return the referral path to which it maps. Otherwise
  1854. /// <code>null</code> is returned.
  1855. /// </remarks>
  1856. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1857. public virtual string GetDfsPath()
  1858. {
  1859. ResolveDfs(null);
  1860. if (_dfsReferral == null)
  1861. {
  1862. return null;
  1863. }
  1864. string path = "smb:/" + _dfsReferral.Server + "/" + _dfsReferral.Share + Unc;
  1865. path = path.Replace('\\', '/');
  1866. if (IsDirectory())
  1867. {
  1868. path += '/';
  1869. }
  1870. return path;
  1871. }
  1872. /// <summary>Retrieve the time this <code>SmbFile</code> was created.</summary>
  1873. /// <remarks>
  1874. /// Retrieve the time this <code>SmbFile</code> was created. The value
  1875. /// returned is suitable for constructing a
  1876. /// <see cref="System.DateTime">System.DateTime</see>
  1877. /// object
  1878. /// (i.e. seconds since Epoch 1970). Times should be the same as those
  1879. /// reported using the properties dialog of the Windows Explorer program.
  1880. /// For Win95/98/Me this is actually the last write time. It is currently
  1881. /// not possible to retrieve the create time from files on these systems.
  1882. /// </remarks>
  1883. /// <returns>
  1884. /// The number of milliseconds since the 00:00:00 GMT, January 1,
  1885. /// 1970 as a <code>long</code> value
  1886. /// </returns>
  1887. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1888. public virtual long CreateTime()
  1889. {
  1890. if (GetUncPath0().Length > 1)
  1891. {
  1892. Exists();
  1893. return _createTime;
  1894. }
  1895. return 0L;
  1896. }
  1897. /// <summary>
  1898. /// Retrieve the last time the file represented by this
  1899. /// <code>SmbFile</code> was modified.
  1900. /// </summary>
  1901. /// <remarks>
  1902. /// Retrieve the last time the file represented by this
  1903. /// <code>SmbFile</code> was modified. The value returned is suitable for
  1904. /// constructing a
  1905. /// <see cref="System.DateTime">System.DateTime</see>
  1906. /// object (i.e. seconds since Epoch
  1907. /// 1970). Times should be the same as those reported using the properties
  1908. /// dialog of the Windows Explorer program.
  1909. /// </remarks>
  1910. /// <returns>
  1911. /// The number of milliseconds since the 00:00:00 GMT, January 1,
  1912. /// 1970 as a <code>long</code> value
  1913. /// </returns>
  1914. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1915. public virtual long LastModified()
  1916. {
  1917. if (GetUncPath0().Length > 1)
  1918. {
  1919. Exists();
  1920. return _lastModified;
  1921. }
  1922. return 0L;
  1923. }
  1924. /// <summary>List the contents of this SMB resource.</summary>
  1925. /// <remarks>
  1926. /// List the contents of this SMB resource. The list returned by this
  1927. /// method will be;
  1928. /// <ul>
  1929. /// <li> files and directories contained within this resource if the
  1930. /// resource is a normal disk file directory,
  1931. /// <li> all available NetBIOS workgroups or domains if this resource is
  1932. /// the top level URL <code>smb://</code>,
  1933. /// <li> all servers registered as members of a NetBIOS workgroup if this
  1934. /// resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
  1935. /// <li> all browseable shares of a server including printers, IPC
  1936. /// services, or disk volumes if this resource is a server URL in the form
  1937. /// <code>smb://server/</code>,
  1938. /// <li> or <code>null</code> if the resource cannot be resolved.
  1939. /// </ul>
  1940. /// </remarks>
  1941. /// <returns>
  1942. /// A <code>String[]</code> array of files and directories,
  1943. /// workgroups, servers, or shares depending on the context of the
  1944. /// resource URL
  1945. /// </returns>
  1946. /// <exception cref="SmbException"></exception>
  1947. public virtual string[] List()
  1948. {
  1949. return List("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
  1950. }
  1951. /// <summary>List the contents of this SMB resource.</summary>
  1952. /// <remarks>
  1953. /// List the contents of this SMB resource. The list returned will be
  1954. /// identical to the list returned by the parameterless <code>list()</code>
  1955. /// method minus filenames filtered by the specified filter.
  1956. /// </remarks>
  1957. /// <param name="filter">a filename filter to exclude filenames from the results</param>
  1958. /// <exception cref="SmbException"># @return An array of filenames</exception>
  1959. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  1960. public virtual string[] List(ISmbFilenameFilter filter)
  1961. {
  1962. return List("*", AttrDirectory | AttrHidden | AttrSystem, filter, null);
  1963. }
  1964. /// <summary>
  1965. /// List the contents of this SMB resource as an array of
  1966. /// <code>SmbFile</code> objects.
  1967. /// </summary>
  1968. /// <remarks>
  1969. /// List the contents of this SMB resource as an array of
  1970. /// <code>SmbFile</code> objects. This method is much more efficient than
  1971. /// the regular <code>list</code> method when querying attributes of each
  1972. /// file in the result set.
  1973. /// <p>
  1974. /// The list of <code>SmbFile</code>s returned by this method will be;
  1975. /// <ul>
  1976. /// <li> files and directories contained within this resource if the
  1977. /// resource is a normal disk file directory,
  1978. /// <li> all available NetBIOS workgroups or domains if this resource is
  1979. /// the top level URL <code>smb://</code>,
  1980. /// <li> all servers registered as members of a NetBIOS workgroup if this
  1981. /// resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
  1982. /// <li> all browseable shares of a server including printers, IPC
  1983. /// services, or disk volumes if this resource is a server URL in the form
  1984. /// <code>smb://server/</code>,
  1985. /// <li> or <code>null</code> if the resource cannot be resolved.
  1986. /// </ul>
  1987. /// </remarks>
  1988. /// <returns>
  1989. /// An array of <code>SmbFile</code> objects representing file
  1990. /// and directories, workgroups, servers, or shares depending on the context
  1991. /// of the resource URL
  1992. /// </returns>
  1993. /// <exception cref="SmbException"></exception>
  1994. public virtual SmbFile[] ListFiles()
  1995. {
  1996. return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
  1997. }
  1998. /// <summary>
  1999. /// The CIFS protocol provides for DOS "wildcards" to be used as
  2000. /// a performance enhancement.
  2001. /// </summary>
  2002. /// <remarks>
  2003. /// The CIFS protocol provides for DOS "wildcards" to be used as
  2004. /// a performance enhancement. The client does not have to filter
  2005. /// the names and the server does not have to return all directory
  2006. /// entries.
  2007. /// <p>
  2008. /// The wildcard expression may consist of two special meta
  2009. /// characters in addition to the normal filename characters. The '*'
  2010. /// character matches any number of characters in part of a name. If
  2011. /// the expression begins with one or more '?'s then exactly that
  2012. /// many characters will be matched whereas if it ends with '?'s
  2013. /// it will match that many characters <i>or less</i>.
  2014. /// <p>
  2015. /// Wildcard expressions will not filter workgroup names or server names.
  2016. /// <blockquote><pre>
  2017. /// winnt&gt; ls c?o
  2018. /// clock.avi -rw-- 82944 Mon Oct 14 1996 1:38 AM
  2019. /// Cookies drw-- 0 Fri Nov 13 1998 9:42 PM
  2020. /// 2 items in 5ms
  2021. /// </pre></blockquote>
  2022. /// </remarks>
  2023. /// <param name="wildcard">a wildcard expression</param>
  2024. /// <exception cref="SmbException">SmbException</exception>
  2025. /// <returns>
  2026. /// An array of <code>SmbFile</code> objects representing file
  2027. /// and directories, workgroups, servers, or shares depending on the context
  2028. /// of the resource URL
  2029. /// </returns>
  2030. /// <exception cref="SmbException"></exception>
  2031. public virtual SmbFile[] ListFiles(string wildcard)
  2032. {
  2033. return ListFiles(wildcard, AttrDirectory | AttrHidden | AttrSystem, null, null
  2034. );
  2035. }
  2036. /// <summary>List the contents of this SMB resource.</summary>
  2037. /// <remarks>
  2038. /// List the contents of this SMB resource. The list returned will be
  2039. /// identical to the list returned by the parameterless <code>listFiles()</code>
  2040. /// method minus files filtered by the specified filename filter.
  2041. /// </remarks>
  2042. /// <param name="filter">a filter to exclude files from the results</param>
  2043. /// <returns>An array of <tt>SmbFile</tt> objects</returns>
  2044. /// <exception cref="SmbException">SmbException</exception>
  2045. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2046. public virtual SmbFile[] ListFiles(ISmbFilenameFilter filter)
  2047. {
  2048. return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, filter, null);
  2049. }
  2050. /// <summary>List the contents of this SMB resource.</summary>
  2051. /// <remarks>
  2052. /// List the contents of this SMB resource. The list returned will be
  2053. /// identical to the list returned by the parameterless <code>listFiles()</code>
  2054. /// method minus filenames filtered by the specified filter.
  2055. /// </remarks>
  2056. /// <param name="filter">a file filter to exclude files from the results</param>
  2057. /// <returns>An array of <tt>SmbFile</tt> objects</returns>
  2058. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2059. public virtual SmbFile[] ListFiles(ISmbFileFilter filter)
  2060. {
  2061. return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, filter);
  2062. }
  2063. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2064. internal virtual string[] List(string wildcard, int searchAttributes, ISmbFilenameFilter
  2065. fnf, ISmbFileFilter ff)
  2066. {
  2067. List<object> list = new List<object>();
  2068. DoEnum(list, false, wildcard, searchAttributes, fnf, ff);
  2069. return Collections.ToArray<string>(list); //Collections.ToArray<string>(list);
  2070. }
  2071. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2072. internal virtual SmbFile[] ListFiles(string wildcard, int searchAttributes
  2073. , ISmbFilenameFilter fnf, ISmbFileFilter ff)
  2074. {
  2075. List<object> list = new List<object>();
  2076. DoEnum(list, true, wildcard, searchAttributes, fnf, ff);
  2077. return Collections.ToArray<SmbFile>(list); //Collections.ToArray<SmbFile>(list);
  2078. }
  2079. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2080. internal virtual void DoEnum(List<object> list, bool files, string wildcard, int searchAttributes
  2081. , ISmbFilenameFilter fnf, ISmbFileFilter ff)
  2082. {
  2083. if (ff != null && ff is DosFileFilter)
  2084. {
  2085. DosFileFilter dff = (DosFileFilter)ff;
  2086. if (dff.Wildcard != null)
  2087. {
  2088. wildcard = dff.Wildcard;
  2089. }
  2090. searchAttributes = dff.Attributes;
  2091. }
  2092. try
  2093. {
  2094. int hostlen = Url.GetHost() != null ? Url.GetHost().Length : 0;
  2095. if (hostlen == 0 || GetType() == TypeWorkgroup)
  2096. {
  2097. DoNetServerEnum(list, files, wildcard, searchAttributes, fnf, ff);
  2098. }
  2099. else
  2100. {
  2101. if (_share == null)
  2102. {
  2103. DoShareEnum(list, files, wildcard, searchAttributes, fnf, ff);
  2104. }
  2105. else
  2106. {
  2107. DoFindFirstNext(list, files, wildcard, searchAttributes, fnf, ff);
  2108. }
  2109. }
  2110. }
  2111. catch (UnknownHostException uhe)
  2112. {
  2113. throw new SmbException(Url.ToString(), uhe);
  2114. }
  2115. catch (UriFormatException mue)
  2116. {
  2117. throw new SmbException(Url.ToString(), mue);
  2118. }
  2119. }
  2120. private void RemoveCurrentAddress()
  2121. {
  2122. if (AddressIndex >= 1)
  2123. {
  2124. UniAddress[] aux = new UniAddress[Addresses.Length - 1];
  2125. Array.Copy(Addresses, 1, aux, 0, Addresses.Length - 1);
  2126. Addresses = aux;
  2127. AddressIndex--;
  2128. }
  2129. }
  2130. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2131. /// <exception cref="UnknownHostException"></exception>
  2132. /// <exception cref="System.UriFormatException"></exception>
  2133. internal virtual void DoShareEnum(List<object> list, bool files, string wildcard, int
  2134. searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
  2135. {
  2136. string p = Url.AbsolutePath;
  2137. IOException last = null;
  2138. IFileEntry[] entries;
  2139. UniAddress addr;
  2140. IFileEntry e;
  2141. Hashtable map;
  2142. if (p.LastIndexOf('/') != (p.Length - 1))
  2143. {
  2144. throw new SmbException(Url + " directory must end with '/'");
  2145. }
  2146. if (GetType() != TypeServer)
  2147. {
  2148. throw new SmbException("The requested list operations is invalid: " + Url);
  2149. }
  2150. map = new Hashtable();
  2151. if (_enableDfs && Dfs.IsTrustedDomain(GetServer(), Auth))
  2152. {
  2153. try
  2154. {
  2155. entries = DoDfsRootEnum();
  2156. for (int ei = 0; ei < entries.Length; ei++)
  2157. {
  2158. e = entries[ei];
  2159. if (map.ContainsKey(e) == false)
  2160. {
  2161. map.Put(e, e);
  2162. }
  2163. }
  2164. }
  2165. catch (IOException ioe)
  2166. {
  2167. if (Log.Level >= 4)
  2168. {
  2169. Runtime.PrintStackTrace(ioe, Log);
  2170. }
  2171. }
  2172. }
  2173. addr = GetFirstAddress();
  2174. while (addr != null)
  2175. {
  2176. try
  2177. {
  2178. last = null;
  2179. DoConnect();
  2180. try
  2181. {
  2182. entries = DoMsrpcShareEnum();
  2183. }
  2184. catch (IOException ioe)
  2185. {
  2186. if (Log.Level >= 3)
  2187. {
  2188. Runtime.PrintStackTrace(ioe, Log);
  2189. }
  2190. entries = DoNetShareEnum();
  2191. }
  2192. for (int ei = 0; ei < entries.Length; ei++)
  2193. {
  2194. e = entries[ei];
  2195. if (map.ContainsKey(e) == false)
  2196. {
  2197. map.Put(e, e);
  2198. }
  2199. }
  2200. break;
  2201. }
  2202. catch (IOException ioe)
  2203. {
  2204. if (Log.Level >= 3)
  2205. {
  2206. Runtime.PrintStackTrace(ioe, Log);
  2207. }
  2208. last = ioe;
  2209. if (!(ioe is SmbAuthException))
  2210. {
  2211. RemoveCurrentAddress();
  2212. addr = GetNextAddress();
  2213. }
  2214. else
  2215. {
  2216. break;
  2217. }
  2218. }
  2219. }
  2220. if (last != null && map.Count == 0)
  2221. {
  2222. if (last is SmbException == false)
  2223. {
  2224. throw new SmbException(Url.ToString(), last);
  2225. }
  2226. throw (SmbException)last;
  2227. }
  2228. //Iterator iter = map.Keys.Iterator();
  2229. //while (iter.HasNext())
  2230. foreach (var item in map.Keys)
  2231. {
  2232. e = (IFileEntry)item;
  2233. string name = e.GetName();
  2234. if (fnf != null && fnf.Accept(this, name) == false)
  2235. {
  2236. continue;
  2237. }
  2238. if (name.Length > 0)
  2239. {
  2240. // if !files we don't need to create SmbFiles here
  2241. SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
  2242. | AttrDirectory, 0L, 0L, 0L);
  2243. if (ff != null && ff.Accept(f) == false)
  2244. {
  2245. continue;
  2246. }
  2247. if (files)
  2248. {
  2249. list.Add(f);
  2250. }
  2251. else
  2252. {
  2253. list.Add(name);
  2254. }
  2255. }
  2256. }
  2257. }
  2258. /// <exception cref="System.IO.IOException"></exception>
  2259. internal virtual IFileEntry[] DoDfsRootEnum()
  2260. {
  2261. MsrpcDfsRootEnum rpc;
  2262. DcerpcHandle handle = null;
  2263. IFileEntry[] entries;
  2264. handle = DcerpcHandle.GetHandle("ncacn_np:" + GetAddress().GetHostAddress() + "[\\PIPE\\netdfs]"
  2265. , Auth);
  2266. try
  2267. {
  2268. rpc = new MsrpcDfsRootEnum(GetServer());
  2269. handle.Sendrecv(rpc);
  2270. if (rpc.Retval != 0)
  2271. {
  2272. throw new SmbException(rpc.Retval, true);
  2273. }
  2274. return rpc.GetEntries();
  2275. }
  2276. finally
  2277. {
  2278. try
  2279. {
  2280. handle.Close();
  2281. }
  2282. catch (IOException ioe)
  2283. {
  2284. if (Log.Level >= 4)
  2285. {
  2286. Runtime.PrintStackTrace(ioe, Log);
  2287. }
  2288. }
  2289. }
  2290. }
  2291. /// <exception cref="System.IO.IOException"></exception>
  2292. internal virtual IFileEntry[] DoMsrpcShareEnum()
  2293. {
  2294. MsrpcShareEnum rpc;
  2295. DcerpcHandle handle;
  2296. rpc = new MsrpcShareEnum(Url.GetHost());
  2297. handle = DcerpcHandle.GetHandle("ncacn_np:" + GetAddress().GetHostAddress() + "[\\PIPE\\srvsvc]"
  2298. , Auth);
  2299. try
  2300. {
  2301. handle.Sendrecv(rpc);
  2302. if (rpc.Retval != 0)
  2303. {
  2304. throw new SmbException(rpc.Retval, true);
  2305. }
  2306. return rpc.GetEntries();
  2307. }
  2308. finally
  2309. {
  2310. try
  2311. {
  2312. handle.Close();
  2313. }
  2314. catch (IOException ioe)
  2315. {
  2316. if (Log.Level >= 4)
  2317. {
  2318. Runtime.PrintStackTrace(ioe, Log);
  2319. }
  2320. }
  2321. }
  2322. }
  2323. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2324. internal virtual IFileEntry[] DoNetShareEnum()
  2325. {
  2326. SmbComTransaction req = new NetShareEnum();
  2327. SmbComTransactionResponse resp = new NetShareEnumResponse();
  2328. Send(req, resp);
  2329. if (resp.Status != WinError.ErrorSuccess)
  2330. {
  2331. throw new SmbException(resp.Status, true);
  2332. }
  2333. return resp.Results;
  2334. }
  2335. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2336. /// <exception cref="UnknownHostException"></exception>
  2337. /// <exception cref="System.UriFormatException"></exception>
  2338. internal virtual void DoNetServerEnum(List<object> list, bool files, string wildcard
  2339. , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
  2340. {
  2341. int listType = Url.GetHost().Length == 0 ? 0 : GetType();
  2342. SmbComTransaction req;
  2343. SmbComTransactionResponse resp;
  2344. if (listType == 0)
  2345. {
  2346. Connect0();
  2347. req = new NetServerEnum2(Tree.Session.transport.Server.OemDomainName, NetServerEnum2
  2348. .SvTypeDomainEnum);
  2349. resp = new NetServerEnum2Response();
  2350. }
  2351. else
  2352. {
  2353. if (listType == TypeWorkgroup)
  2354. {
  2355. req = new NetServerEnum2(Url.GetHost(), NetServerEnum2.SvTypeAll);
  2356. resp = new NetServerEnum2Response();
  2357. }
  2358. else
  2359. {
  2360. throw new SmbException("The requested list operations is invalid: " + Url);
  2361. }
  2362. }
  2363. bool more;
  2364. do
  2365. {
  2366. int n;
  2367. Send(req, resp);
  2368. if (resp.Status != WinError.ErrorSuccess && resp.Status != WinError.ErrorMoreData)
  2369. {
  2370. throw new SmbException(resp.Status, true);
  2371. }
  2372. more = resp.Status == WinError.ErrorMoreData;
  2373. n = more ? resp.NumEntries - 1 : resp.NumEntries;
  2374. for (int i = 0; i < n; i++)
  2375. {
  2376. IFileEntry e = resp.Results[i];
  2377. string name = e.GetName();
  2378. if (fnf != null && fnf.Accept(this, name) == false)
  2379. {
  2380. continue;
  2381. }
  2382. if (name.Length > 0)
  2383. {
  2384. // if !files we don't need to create SmbFiles here
  2385. SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
  2386. | AttrDirectory, 0L, 0L, 0L);
  2387. if (ff != null && ff.Accept(f) == false)
  2388. {
  2389. continue;
  2390. }
  2391. if (files)
  2392. {
  2393. list.Add(f);
  2394. }
  2395. else
  2396. {
  2397. list.Add(name);
  2398. }
  2399. }
  2400. }
  2401. if (GetType() != TypeWorkgroup)
  2402. {
  2403. break;
  2404. }
  2405. req.SubCommand = unchecked(SmbComTransaction.NetServerEnum3);
  2406. req.Reset(0, ((NetServerEnum2Response)resp).LastName);
  2407. resp.Reset();
  2408. }
  2409. while (more);
  2410. }
  2411. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2412. /// <exception cref="UnknownHostException"></exception>
  2413. /// <exception cref="System.UriFormatException"></exception>
  2414. internal virtual void DoFindFirstNext(List<object> list, bool files, string wildcard
  2415. , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
  2416. {
  2417. SmbComTransaction req;
  2418. Trans2FindFirst2Response resp;
  2419. int sid;
  2420. string path = GetUncPath0();
  2421. string p = Url.AbsolutePath;
  2422. if (p.LastIndexOf('/') != (p.Length - 1))
  2423. {
  2424. throw new SmbException(Url + " directory must end with '/'");
  2425. }
  2426. req = new Trans2FindFirst2(path, wildcard, searchAttributes);
  2427. resp = new Trans2FindFirst2Response();
  2428. if (Log.Level >= 3)
  2429. {
  2430. Log.WriteLine("doFindFirstNext: " + req.Path);
  2431. }
  2432. Send(req, resp);
  2433. sid = resp.Sid;
  2434. req = new Trans2FindNext2(sid, resp.ResumeKey, resp.LastName);
  2435. resp.SubCommand = SmbComTransaction.Trans2FindNext2;
  2436. for (; ; )
  2437. {
  2438. for (int i = 0; i < resp.NumEntries; i++)
  2439. {
  2440. IFileEntry e = resp.Results[i];
  2441. string name = e.GetName();
  2442. if (name.Length < 3)
  2443. {
  2444. int h = name.GetHashCode();
  2445. if (h == HashDot || h == HashDotDot)
  2446. {
  2447. if (name.Equals(".") || name.Equals(".."))
  2448. {
  2449. continue;
  2450. }
  2451. }
  2452. }
  2453. if (fnf != null && fnf.Accept(this, name) == false)
  2454. {
  2455. continue;
  2456. }
  2457. if (name.Length > 0)
  2458. {
  2459. SmbFile f = new SmbFile(this, name, TypeFilesystem, e.GetAttributes
  2460. (), e.CreateTime(), e.LastModified(), e.Length());
  2461. if (ff != null && ff.Accept(f) == false)
  2462. {
  2463. continue;
  2464. }
  2465. if (files)
  2466. {
  2467. list.Add(f);
  2468. }
  2469. else
  2470. {
  2471. list.Add(name);
  2472. }
  2473. }
  2474. }
  2475. if (resp.IsEndOfSearch || resp.NumEntries == 0)
  2476. {
  2477. break;
  2478. }
  2479. req.Reset(resp.ResumeKey, resp.LastName);
  2480. resp.Reset();
  2481. Send(req, resp);
  2482. }
  2483. try
  2484. {
  2485. Send(new SmbComFindClose2(sid), Blank_resp());
  2486. }
  2487. catch (SmbException se)
  2488. {
  2489. if (Log.Level >= 4)
  2490. {
  2491. Runtime.PrintStackTrace(se, Log);
  2492. }
  2493. }
  2494. }
  2495. /// <summary>
  2496. /// Changes the name of the file this <code>SmbFile</code> represents to the name
  2497. /// designated by the <code>SmbFile</code> argument.
  2498. /// </summary>
  2499. /// <remarks>
  2500. /// Changes the name of the file this <code>SmbFile</code> represents to the name
  2501. /// designated by the <code>SmbFile</code> argument.
  2502. /// <p/>
  2503. /// <i>Remember: <code>SmbFile</code>s are immutible and therefore
  2504. /// the path associated with this <code>SmbFile</code> object will not
  2505. /// change). To access the renamed file it is necessary to construct a
  2506. /// new <tt>SmbFile</tt></i>.
  2507. /// </remarks>
  2508. /// <param name="dest">An <code>SmbFile</code> that represents the new pathname</param>
  2509. /// <exception cref="System.ArgumentNullException">If the <code>dest</code> argument is <code>null</code>
  2510. /// </exception>
  2511. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2512. public virtual void RenameTo(SmbFile dest)
  2513. {
  2514. if (GetUncPath0().Length == 1 || dest.GetUncPath0().Length == 1)
  2515. {
  2516. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  2517. }
  2518. ResolveDfs(null);
  2519. dest.ResolveDfs(null);
  2520. if (!Tree.Equals(dest.Tree))
  2521. {
  2522. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  2523. }
  2524. if (Log.Level >= 3)
  2525. {
  2526. Log.WriteLine("renameTo: " + Unc + " -> " + dest.Unc);
  2527. }
  2528. _attrExpiration = _sizeExpiration = 0;
  2529. dest._attrExpiration = 0;
  2530. Send(new SmbComRename(Unc, dest.Unc), Blank_resp());
  2531. }
  2532. internal class WriterThread : Thread
  2533. {
  2534. internal byte[] B;
  2535. internal int N;
  2536. internal long Off;
  2537. internal bool Ready;
  2538. internal SmbFile Dest;
  2539. internal SmbException E;
  2540. internal bool UseNtSmbs;
  2541. internal SmbComWriteAndX Reqx;
  2542. internal SmbComWrite Req;
  2543. internal ServerMessageBlock Resp;
  2544. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2545. public WriterThread(SmbFile enclosing)
  2546. : base("JCIFS-WriterThread")
  2547. {
  2548. this._enclosing = enclosing;
  2549. UseNtSmbs = this._enclosing.Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs);
  2550. if (UseNtSmbs)
  2551. {
  2552. Reqx = new SmbComWriteAndX();
  2553. Resp = new SmbComWriteAndXResponse();
  2554. }
  2555. else
  2556. {
  2557. Req = new SmbComWrite();
  2558. Resp = new SmbComWriteResponse();
  2559. }
  2560. Ready = false;
  2561. }
  2562. internal virtual void Write(byte[] b, int n, SmbFile dest, long off)
  2563. {
  2564. lock (this)
  2565. {
  2566. this.B = b;
  2567. this.N = n;
  2568. this.Dest = dest;
  2569. this.Off = off;
  2570. Ready = false;
  2571. Runtime.Notify(this);
  2572. }
  2573. }
  2574. public override void Run()
  2575. {
  2576. lock (this)
  2577. {
  2578. try
  2579. {
  2580. for (; ; )
  2581. {
  2582. Runtime.Notify(this);
  2583. Ready = true;
  2584. while (Ready)
  2585. {
  2586. Runtime.Wait(this);
  2587. }
  2588. if (N == -1)
  2589. {
  2590. return;
  2591. }
  2592. if (UseNtSmbs)
  2593. {
  2594. Reqx.SetParam(Dest.Fid, Off, N, B, 0, N);
  2595. Dest.Send(Reqx, Resp);
  2596. }
  2597. else
  2598. {
  2599. Req.SetParam(Dest.Fid, Off, N, B, 0, N);
  2600. Dest.Send(Req, Resp);
  2601. }
  2602. }
  2603. }
  2604. catch (SmbException e)
  2605. {
  2606. this.E = e;
  2607. }
  2608. catch (Exception x)
  2609. {
  2610. E = new SmbException("WriterThread", x);
  2611. }
  2612. Runtime.Notify(this);
  2613. }
  2614. }
  2615. private readonly SmbFile _enclosing;
  2616. }
  2617. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2618. internal virtual void CopyTo0(SmbFile dest, byte[][] b, int bsize, WriterThread
  2619. w, SmbComReadAndX req, SmbComReadAndXResponse resp)
  2620. {
  2621. int i;
  2622. if (_attrExpiration < Runtime.CurrentTimeMillis())
  2623. {
  2624. _attributes = AttrReadonly | AttrDirectory;
  2625. _createTime = 0L;
  2626. _lastModified = 0L;
  2627. _isExists = false;
  2628. IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
  2629. );
  2630. _attributes = info.GetAttributes();
  2631. _createTime = info.GetCreateTime();
  2632. _lastModified = info.GetLastWriteTime();
  2633. _isExists = true;
  2634. _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  2635. }
  2636. if (IsDirectory())
  2637. {
  2638. SmbFile[] files;
  2639. SmbFile ndest;
  2640. string path = dest.GetUncPath0();
  2641. if (path.Length > 1)
  2642. {
  2643. try
  2644. {
  2645. dest.Mkdir();
  2646. dest.SetPathInformation(_attributes, _createTime, _lastModified);
  2647. }
  2648. catch (SmbException se)
  2649. {
  2650. if (se.GetNtStatus() != NtStatus.NtStatusAccessDenied && se.GetNtStatus() != NtStatus
  2651. .NtStatusObjectNameCollision)
  2652. {
  2653. throw;
  2654. }
  2655. }
  2656. }
  2657. files = ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
  2658. try
  2659. {
  2660. for (i = 0; i < files.Length; i++)
  2661. {
  2662. ndest = new SmbFile(dest, files[i].GetName(), files[i].Type, files[i]._attributes,
  2663. files[i]._createTime, files[i]._lastModified, files[i]._size);
  2664. files[i].CopyTo0(ndest, b, bsize, w, req, resp);
  2665. }
  2666. }
  2667. catch (UnknownHostException uhe)
  2668. {
  2669. throw new SmbException(Url.ToString(), uhe);
  2670. }
  2671. catch (UriFormatException mue)
  2672. {
  2673. throw new SmbException(Url.ToString(), mue);
  2674. }
  2675. }
  2676. else
  2677. {
  2678. long off;
  2679. try
  2680. {
  2681. Open(ORdonly, 0, AttrNormal, 0);
  2682. try
  2683. {
  2684. dest.Open(OCreat | OWronly | OTrunc, SmbConstants.FileWriteData |
  2685. SmbConstants.FileWriteAttributes, _attributes, 0);
  2686. }
  2687. catch (SmbAuthException sae)
  2688. {
  2689. if ((dest._attributes & AttrReadonly) != 0)
  2690. {
  2691. dest.SetPathInformation(dest._attributes & ~AttrReadonly, 0L, 0L);
  2692. dest.Open(OCreat | OWronly | OTrunc, SmbConstants.FileWriteData |
  2693. SmbConstants.FileWriteAttributes, _attributes, 0);
  2694. }
  2695. else
  2696. {
  2697. throw;
  2698. }
  2699. }
  2700. i = 0;
  2701. off = 0L;
  2702. for (; ; )
  2703. {
  2704. req.SetParam(Fid, off, bsize);
  2705. resp.SetParam(b[i], 0);
  2706. Send(req, resp);
  2707. lock (w)
  2708. {
  2709. if (w.E != null)
  2710. {
  2711. throw w.E;
  2712. }
  2713. while (!w.Ready)
  2714. {
  2715. try
  2716. {
  2717. Runtime.Wait(w);
  2718. }
  2719. catch (Exception ie)
  2720. {
  2721. throw new SmbException(dest.Url.ToString(), ie);
  2722. }
  2723. }
  2724. if (w.E != null)
  2725. {
  2726. throw w.E;
  2727. }
  2728. if (resp.DataLength <= 0)
  2729. {
  2730. break;
  2731. }
  2732. w.Write(b[i], resp.DataLength, dest, off);
  2733. }
  2734. i = i == 1 ? 0 : 1;
  2735. off += resp.DataLength;
  2736. }
  2737. dest.Send(new Trans2SetFileInformation(dest.Fid, _attributes, _createTime, _lastModified
  2738. ), new Trans2SetFileInformationResponse());
  2739. dest.Close(0L);
  2740. }
  2741. catch (SmbException se)
  2742. {
  2743. if (IgnoreCopyToException == false)
  2744. {
  2745. throw new SmbException("Failed to copy file from [" + ToString() + "] to ["
  2746. + dest + "]", se);
  2747. }
  2748. if (Log.Level > 1)
  2749. {
  2750. Runtime.PrintStackTrace(se, Log);
  2751. }
  2752. }
  2753. finally
  2754. {
  2755. Close();
  2756. }
  2757. }
  2758. }
  2759. /// <summary>
  2760. /// This method will copy the file or directory represented by this
  2761. /// <tt>SmbFile</tt> and it's sub-contents to the location specified by the
  2762. /// <tt>dest</tt> parameter.
  2763. /// </summary>
  2764. /// <remarks>
  2765. /// This method will copy the file or directory represented by this
  2766. /// <tt>SmbFile</tt> and it's sub-contents to the location specified by the
  2767. /// <tt>dest</tt> parameter. This file and the destination file do not
  2768. /// need to be on the same host. This operation does not copy extended
  2769. /// file attibutes such as ACLs but it does copy regular attributes as
  2770. /// well as create and last write times. This method is almost twice as
  2771. /// efficient as manually copying as it employs an additional write
  2772. /// thread to read and write data concurrently.
  2773. /// <p/>
  2774. /// It is not possible (nor meaningful) to copy entire workgroups or
  2775. /// servers.
  2776. /// </remarks>
  2777. /// <param name="dest">the destination file or directory</param>
  2778. /// <exception cref="SmbException">SmbException</exception>
  2779. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2780. public virtual void CopyTo(SmbFile dest)
  2781. {
  2782. SmbComReadAndX req;
  2783. SmbComReadAndXResponse resp;
  2784. WriterThread w;
  2785. int bsize;
  2786. byte[][] b;
  2787. if (_share == null || dest._share == null)
  2788. {
  2789. throw new SmbException("Invalid operation for workgroups or servers");
  2790. }
  2791. req = new SmbComReadAndX();
  2792. resp = new SmbComReadAndXResponse();
  2793. Connect0();
  2794. dest.Connect0();
  2795. ResolveDfs(null);
  2796. try
  2797. {
  2798. if (GetAddress().Equals(dest.GetAddress()) && _canon.RegionMatches(true, 0, dest._canon
  2799. , 0, Math.Min(_canon.Length, dest._canon.Length)))
  2800. {
  2801. throw new SmbException("Source and destination paths overlap.");
  2802. }
  2803. }
  2804. catch (UnknownHostException)
  2805. {
  2806. }
  2807. w = new WriterThread(this);
  2808. w.SetDaemon(true);
  2809. w.Start();
  2810. SmbTransport t1 = Tree.Session.transport;
  2811. SmbTransport t2 = dest.Tree.Session.transport;
  2812. if (t1.SndBufSize < t2.SndBufSize)
  2813. {
  2814. t2.SndBufSize = t1.SndBufSize;
  2815. }
  2816. else
  2817. {
  2818. t1.SndBufSize = t2.SndBufSize;
  2819. }
  2820. bsize = Math.Min(t1.RcvBufSize - 70, t1.SndBufSize - 70);
  2821. b = new[] { new byte[bsize], new byte[bsize] };
  2822. try
  2823. {
  2824. CopyTo0(dest, b, bsize, w, req, resp);
  2825. }
  2826. finally
  2827. {
  2828. w.Write(null, -1, null, 0);
  2829. }
  2830. }
  2831. /// <summary>
  2832. /// This method will delete the file or directory specified by this
  2833. /// <code>SmbFile</code>.
  2834. /// </summary>
  2835. /// <remarks>
  2836. /// This method will delete the file or directory specified by this
  2837. /// <code>SmbFile</code>. If the target is a directory, the contents of
  2838. /// the directory will be deleted as well. If a file within the directory or
  2839. /// it's sub-directories is marked read-only, the read-only status will
  2840. /// be removed and the file will be deleted.
  2841. /// </remarks>
  2842. /// <exception cref="SmbException">SmbException</exception>
  2843. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2844. public virtual void Delete()
  2845. {
  2846. Exists();
  2847. GetUncPath0();
  2848. Delete(Unc);
  2849. }
  2850. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2851. internal virtual void Delete(string fileName)
  2852. {
  2853. if (GetUncPath0().Length == 1)
  2854. {
  2855. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  2856. }
  2857. if (Runtime.CurrentTimeMillis() > _attrExpiration)
  2858. {
  2859. _attributes = AttrReadonly | AttrDirectory;
  2860. _createTime = 0L;
  2861. _lastModified = 0L;
  2862. _isExists = false;
  2863. IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
  2864. );
  2865. _attributes = info.GetAttributes();
  2866. _createTime = info.GetCreateTime();
  2867. _lastModified = info.GetLastWriteTime();
  2868. _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  2869. _isExists = true;
  2870. }
  2871. if ((_attributes & AttrReadonly) != 0)
  2872. {
  2873. SetReadWrite();
  2874. }
  2875. if (Log.Level >= 3)
  2876. {
  2877. Log.WriteLine("delete: " + fileName);
  2878. }
  2879. if ((_attributes & AttrDirectory) != 0)
  2880. {
  2881. try
  2882. {
  2883. SmbFile[] l = ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null
  2884. );
  2885. for (int i = 0; i < l.Length; i++)
  2886. {
  2887. l[i].Delete();
  2888. }
  2889. }
  2890. catch (SmbException se)
  2891. {
  2892. if (se.GetNtStatus() != NtStatus.NtStatusNoSuchFile)
  2893. {
  2894. throw;
  2895. }
  2896. }
  2897. Send(new SmbComDeleteDirectory(fileName), Blank_resp());
  2898. }
  2899. else
  2900. {
  2901. Send(new SmbComDelete(fileName), Blank_resp());
  2902. }
  2903. _attrExpiration = _sizeExpiration = 0;
  2904. }
  2905. /// <summary>Returns the length of this <tt>SmbFile</tt> in bytes.</summary>
  2906. /// <remarks>
  2907. /// Returns the length of this <tt>SmbFile</tt> in bytes. If this object
  2908. /// is a <tt>TYPE_SHARE</tt> the total capacity of the disk shared in
  2909. /// bytes is returned. If this object is a directory or a type other than
  2910. /// <tt>TYPE_SHARE</tt>, 0L is returned.
  2911. /// </remarks>
  2912. /// <returns>
  2913. /// The length of the file in bytes or 0 if this
  2914. /// <code>SmbFile</code> is not a file.
  2915. /// </returns>
  2916. /// <exception cref="SmbException">SmbException</exception>
  2917. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2918. public virtual long Length()
  2919. {
  2920. if (_sizeExpiration > Runtime.CurrentTimeMillis())
  2921. {
  2922. return _size;
  2923. }
  2924. if (GetType() == TypeShare)
  2925. {
  2926. Trans2QueryFsInformationResponse response;
  2927. int level = Trans2QueryFsInformationResponse.SMB_INFO_ALLOCATION;
  2928. response = new Trans2QueryFsInformationResponse(level);
  2929. Send(new Trans2QueryFsInformation(level), response);
  2930. _size = response.Info.GetCapacity();
  2931. }
  2932. else
  2933. {
  2934. if (GetUncPath0().Length > 1 && Type != TypeNamedPipe)
  2935. {
  2936. IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_STANDARD_INFO
  2937. );
  2938. _size = info.GetSize();
  2939. }
  2940. else
  2941. {
  2942. _size = 0L;
  2943. }
  2944. }
  2945. _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  2946. return _size;
  2947. }
  2948. /// <summary>
  2949. /// This method returns the free disk space in bytes of the drive this share
  2950. /// represents or the drive on which the directory or file resides.
  2951. /// </summary>
  2952. /// <remarks>
  2953. /// This method returns the free disk space in bytes of the drive this share
  2954. /// represents or the drive on which the directory or file resides. Objects
  2955. /// other than <tt>TYPE_SHARE</tt> or <tt>TYPE_FILESYSTEM</tt> will result
  2956. /// in 0L being returned.
  2957. /// </remarks>
  2958. /// <returns>
  2959. /// the free disk space in bytes of the drive on which this file or
  2960. /// directory resides
  2961. /// </returns>
  2962. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2963. public virtual long GetDiskFreeSpace()
  2964. {
  2965. if (GetType() == TypeShare || Type == TypeFilesystem)
  2966. {
  2967. int level = Trans2QueryFsInformationResponse.SmbFsFullSizeInformation;
  2968. try
  2969. {
  2970. return QueryFsInformation(level);
  2971. }
  2972. catch (SmbException ex)
  2973. {
  2974. switch (ex.GetNtStatus())
  2975. {
  2976. case NtStatus.NtStatusInvalidInfoClass:
  2977. case NtStatus.NtStatusUnsuccessful:
  2978. {
  2979. // NetApp Filer
  2980. // SMB_FS_FULL_SIZE_INFORMATION not supported by the server.
  2981. level = Trans2QueryFsInformationResponse.SMB_INFO_ALLOCATION;
  2982. return QueryFsInformation(level);
  2983. }
  2984. }
  2985. throw;
  2986. }
  2987. }
  2988. return 0L;
  2989. }
  2990. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  2991. private long QueryFsInformation(int level)
  2992. {
  2993. Trans2QueryFsInformationResponse response;
  2994. response = new Trans2QueryFsInformationResponse(level);
  2995. Send(new Trans2QueryFsInformation(level), response);
  2996. if (Type == TypeShare)
  2997. {
  2998. _size = response.Info.GetCapacity();
  2999. _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
  3000. }
  3001. return response.Info.GetFree();
  3002. }
  3003. /// <summary>
  3004. /// Creates a directory with the path specified by this
  3005. /// <code>SmbFile</code>.
  3006. /// </summary>
  3007. /// <remarks>
  3008. /// Creates a directory with the path specified by this
  3009. /// <code>SmbFile</code>. For this method to be successful, the target
  3010. /// must not already exist. This method will fail when
  3011. /// used with <code>smb://</code>, <code>smb://workgroup/</code>,
  3012. /// <code>smb://server/</code>, or <code>smb://server/share/</code> URLs
  3013. /// because workgroups, servers, and shares cannot be dynamically created
  3014. /// (although in the future it may be possible to create shares).
  3015. /// </remarks>
  3016. /// <exception cref="SmbException">SmbException</exception>
  3017. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3018. public virtual void Mkdir()
  3019. {
  3020. string path = GetUncPath0();
  3021. if (path.Length == 1)
  3022. {
  3023. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  3024. }
  3025. if (Log.Level >= 3)
  3026. {
  3027. Log.WriteLine("mkdir: " + path);
  3028. }
  3029. Send(new SmbComCreateDirectory(path), Blank_resp());
  3030. _attrExpiration = _sizeExpiration = 0;
  3031. }
  3032. /// <summary>
  3033. /// Creates a directory with the path specified by this <tt>SmbFile</tt>
  3034. /// and any parent directories that do not exist.
  3035. /// </summary>
  3036. /// <remarks>
  3037. /// Creates a directory with the path specified by this <tt>SmbFile</tt>
  3038. /// and any parent directories that do not exist. This method will fail
  3039. /// when used with <code>smb://</code>, <code>smb://workgroup/</code>,
  3040. /// <code>smb://server/</code>, or <code>smb://server/share/</code> URLs
  3041. /// because workgroups, servers, and shares cannot be dynamically created
  3042. /// (although in the future it may be possible to create shares).
  3043. /// </remarks>
  3044. /// <exception cref="SmbException">SmbException</exception>
  3045. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3046. public virtual void Mkdirs()
  3047. {
  3048. SmbFile parent;
  3049. try
  3050. {
  3051. parent = new SmbFile(GetParent(), Auth);
  3052. }
  3053. catch (IOException)
  3054. {
  3055. return;
  3056. }
  3057. if (parent.Exists() == false)
  3058. {
  3059. parent.Mkdirs();
  3060. }
  3061. Mkdir();
  3062. }
  3063. /// <summary>Create a new file but fail if it already exists.</summary>
  3064. /// <remarks>
  3065. /// Create a new file but fail if it already exists. The check for
  3066. /// existance of the file and it's creation are an atomic operation with
  3067. /// respect to other filesystem activities.
  3068. /// </remarks>
  3069. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3070. public virtual void CreateNewFile()
  3071. {
  3072. if (GetUncPath0().Length == 1)
  3073. {
  3074. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  3075. }
  3076. Close(Open0(ORdwr | OCreat | OExcl, 0, AttrNormal, 0), 0L);
  3077. }
  3078. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3079. internal virtual void SetPathInformation(int attrs, long ctime, long mtime)
  3080. {
  3081. int f;
  3082. int dir;
  3083. Exists();
  3084. dir = _attributes & AttrDirectory;
  3085. f = Open0(ORdonly, SmbConstants.FileWriteAttributes, dir, dir != 0 ? 0x0001
  3086. : 0x0040);
  3087. Send(new Trans2SetFileInformation(f, attrs | dir, ctime, mtime), new Trans2SetFileInformationResponse
  3088. ());
  3089. Close(f, 0L);
  3090. _attrExpiration = 0;
  3091. }
  3092. /// <summary>Set the create time of the file.</summary>
  3093. /// <remarks>
  3094. /// Set the create time of the file. The time is specified as milliseconds
  3095. /// from Jan 1, 1970 which is the same as that which is returned by the
  3096. /// <tt>createTime()</tt> method.
  3097. /// <p/>
  3098. /// This method does not apply to workgroups, servers, or shares.
  3099. /// </remarks>
  3100. /// <param name="time">the create time as milliseconds since Jan 1, 1970</param>
  3101. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3102. public virtual void SetCreateTime(long time)
  3103. {
  3104. if (GetUncPath0().Length == 1)
  3105. {
  3106. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  3107. }
  3108. SetPathInformation(0, time, 0L);
  3109. }
  3110. /// <summary>Set the last modified time of the file.</summary>
  3111. /// <remarks>
  3112. /// Set the last modified time of the file. The time is specified as milliseconds
  3113. /// from Jan 1, 1970 which is the same as that which is returned by the
  3114. /// <tt>lastModified()</tt>, <tt>getLastModified()</tt>, and <tt>getDate()</tt> methods.
  3115. /// <p/>
  3116. /// This method does not apply to workgroups, servers, or shares.
  3117. /// </remarks>
  3118. /// <param name="time">the last modified time as milliseconds since Jan 1, 1970</param>
  3119. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3120. public virtual void SetLastModified(long time)
  3121. {
  3122. if (GetUncPath0().Length == 1)
  3123. {
  3124. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  3125. }
  3126. SetPathInformation(0, 0L, time);
  3127. }
  3128. /// <summary>Return the attributes of this file.</summary>
  3129. /// <remarks>
  3130. /// Return the attributes of this file. Attributes are represented as a
  3131. /// bitset that must be masked with <tt>ATTR_*</tt> constants to determine
  3132. /// if they are set or unset. The value returned is suitable for use with
  3133. /// the <tt>setAttributes()</tt> method.
  3134. /// </remarks>
  3135. /// <returns>the <tt>ATTR_*</tt> attributes associated with this file</returns>
  3136. /// <exception cref="SmbException">SmbException</exception>
  3137. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3138. public virtual int GetAttributes()
  3139. {
  3140. if (GetUncPath0().Length == 1)
  3141. {
  3142. return 0;
  3143. }
  3144. Exists();
  3145. return _attributes & AttrGetMask;
  3146. }
  3147. /// <summary>Set the attributes of this file.</summary>
  3148. /// <remarks>
  3149. /// Set the attributes of this file. Attributes are composed into a
  3150. /// bitset by bitwise ORing the <tt>ATTR_*</tt> constants. Setting the
  3151. /// value returned by <tt>getAttributes</tt> will result in both files
  3152. /// having the same attributes.
  3153. /// </remarks>
  3154. /// <exception cref="SmbException">SmbException</exception>
  3155. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3156. public virtual void SetAttributes(int attrs)
  3157. {
  3158. if (GetUncPath0().Length == 1)
  3159. {
  3160. throw new SmbException("Invalid operation for workgroups, servers, or shares");
  3161. }
  3162. SetPathInformation(attrs & AttrSetMask, 0L, 0L);
  3163. }
  3164. /// <summary>Make this file read-only.</summary>
  3165. /// <remarks>
  3166. /// Make this file read-only. This is shorthand for <tt>setAttributes(
  3167. /// getAttributes() | ATTR_READ_ONLY )</tt>.
  3168. /// </remarks>
  3169. /// <exception cref="SmbException">SmbException</exception>
  3170. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  3171. public virtual void SetReadOnly()
  3172. {
  3173. SetAttributes(GetAttributes() | AttrReadonly);
  3174. }
  3175. /// <summary>Turn off the read-only attribute of this file.</summary>
  3176. /// <remarks>
  3177. /// Turn off the read-only attribute of this file. This is shorthand for
  3178. /// <tt>setAttributes( getAttributes() & ~ATTR_READONLY )</tt>.
  3179. /// </remarks>
  3180. /// <exception cref="SmbException">SmbException</exception>
  3181. /// <exception cref="SmbException"></exception>
  3182. public virtual void SetReadWrite()
  3183. {
  3184. SetAttributes(GetAttributes() & ~AttrReadonly);
  3185. }
  3186. /// <summary>
  3187. /// Computes a hashCode for this file based on the URL string and IP
  3188. /// address if the server.
  3189. /// </summary>
  3190. /// <remarks>
  3191. /// Computes a hashCode for this file based on the URL string and IP
  3192. /// address if the server. The hashing function uses the hashcode of the
  3193. /// server address, the canonical representation of the URL, and does not
  3194. /// compare authentication information. In essance, two
  3195. /// <code>SmbFile</code> objects that refer to
  3196. /// the same file should generate the same hashcode provided it is possible
  3197. /// to make such a determination.
  3198. /// </remarks>
  3199. /// <returns>A hashcode for this abstract file</returns>
  3200. /// <exception cref="SmbException">SmbException</exception>
  3201. public override int GetHashCode()
  3202. {
  3203. int hash;
  3204. try
  3205. {
  3206. hash = GetAddress().GetHashCode();
  3207. }
  3208. catch (UnknownHostException)
  3209. {
  3210. hash = GetServer().ToUpper().GetHashCode();
  3211. }
  3212. GetUncPath0();
  3213. return hash + _canon.ToUpper().GetHashCode();
  3214. }
  3215. protected internal virtual bool PathNamesPossiblyEqual(string path1, string path2
  3216. )
  3217. {
  3218. int p1;
  3219. int p2;
  3220. int l1;
  3221. int l2;
  3222. // if unsure return this method returns true
  3223. p1 = path1.LastIndexOf('/');
  3224. p2 = path2.LastIndexOf('/');
  3225. l1 = path1.Length - p1;
  3226. l2 = path2.Length - p2;
  3227. // anything with dots voids comparison
  3228. if (l1 > 1 && path1[p1 + 1] == '.')
  3229. {
  3230. return true;
  3231. }
  3232. if (l2 > 1 && path2[p2 + 1] == '.')
  3233. {
  3234. return true;
  3235. }
  3236. return l1 == l2 && path1.RegionMatches(true, p1, path2, p2, l1);
  3237. }
  3238. /// <summary>Tests to see if two <code>SmbFile</code> objects are equal.</summary>
  3239. /// <remarks>
  3240. /// Tests to see if two <code>SmbFile</code> objects are equal. Two
  3241. /// SmbFile objects are equal when they reference the same SMB
  3242. /// resource. More specifically, two <code>SmbFile</code> objects are
  3243. /// equals if their server IP addresses are equal and the canonicalized
  3244. /// representation of their URLs, minus authentication parameters, are
  3245. /// case insensitivly and lexographically equal.
  3246. /// <p/>
  3247. /// For example, assuming the server <code>angus</code> resolves to the
  3248. /// <code>192.168.1.15</code> IP address, the below URLs would result in
  3249. /// <code>SmbFile</code>s that are equal.
  3250. /// <p><blockquote><pre>
  3251. /// smb://192.168.1.15/share/DIR/foo.txt
  3252. /// smb://angus/share/data/../dir/foo.txt
  3253. /// </pre></blockquote>
  3254. /// </remarks>
  3255. /// <param name="obj">Another <code>SmbFile</code> object to compare for equality</param>
  3256. /// <returns>
  3257. /// <code>true</code> if the two objects refer to the same SMB resource
  3258. /// and <code>false</code> otherwise
  3259. /// </returns>
  3260. /// <exception cref="SmbException">SmbException</exception>
  3261. public override bool Equals(object obj)
  3262. {
  3263. if (obj is SmbFile)
  3264. {
  3265. SmbFile f = (SmbFile)obj;
  3266. bool ret;
  3267. if (this == f)
  3268. {
  3269. return true;
  3270. }
  3271. if (PathNamesPossiblyEqual(Url.AbsolutePath, f.Url.AbsolutePath))
  3272. {
  3273. GetUncPath0();
  3274. f.GetUncPath0();
  3275. if (Runtime.EqualsIgnoreCase(_canon, f._canon))
  3276. {
  3277. try
  3278. {
  3279. ret = GetAddress().Equals(f.GetAddress());
  3280. }
  3281. catch (UnknownHostException)
  3282. {
  3283. ret = Runtime.EqualsIgnoreCase(GetServer(), f.GetServer());
  3284. }
  3285. return ret;
  3286. }
  3287. }
  3288. }
  3289. return false;
  3290. }
  3291. /// <summary>Returns the string representation of this SmbFile object.</summary>
  3292. /// <remarks>
  3293. /// Returns the string representation of this SmbFile object. This will
  3294. /// be the same as the URL used to construct this <code>SmbFile</code>.
  3295. /// This method will return the same value
  3296. /// as <code>getPath</code>.
  3297. /// </remarks>
  3298. /// <returns>The original URL representation of this SMB resource</returns>
  3299. /// <exception cref="SmbException">SmbException</exception>
  3300. public override string ToString()
  3301. {
  3302. return Url.ToString();
  3303. }
  3304. /// <summary>This URLConnection method just returns the result of <tt>length()</tt>.</summary>
  3305. /// <remarks>This URLConnection method just returns the result of <tt>length()</tt>.</remarks>
  3306. /// <returns>the length of this file or 0 if it refers to a directory</returns>
  3307. public int GetContentLength()
  3308. {
  3309. try
  3310. {
  3311. return (int)(Length() & unchecked(0xFFFFFFFFL));
  3312. }
  3313. catch (SmbException)
  3314. {
  3315. }
  3316. return 0;
  3317. }
  3318. /// <summary>This URLConnection method just returns the result of <tt>lastModified</tt>.
  3319. /// </summary>
  3320. /// <remarks>This URLConnection method just returns the result of <tt>lastModified</tt>.
  3321. /// </remarks>
  3322. /// <returns>the last modified data as milliseconds since Jan 1, 1970</returns>
  3323. public long GetDate()
  3324. {
  3325. try
  3326. {
  3327. return LastModified();
  3328. }
  3329. catch (SmbException)
  3330. {
  3331. }
  3332. return 0L;
  3333. }
  3334. /// <summary>This URLConnection method just returns the result of <tt>lastModified</tt>.
  3335. /// </summary>
  3336. /// <remarks>This URLConnection method just returns the result of <tt>lastModified</tt>.
  3337. /// </remarks>
  3338. /// <returns>the last modified data as milliseconds since Jan 1, 1970</returns>
  3339. public long GetLastModified()
  3340. {
  3341. try
  3342. {
  3343. return LastModified();
  3344. }
  3345. catch (SmbException)
  3346. {
  3347. }
  3348. return 0L;
  3349. }
  3350. /// <summary>This URLConnection method just returns a new <tt>SmbFileInputStream</tt> created with this file.
  3351. /// </summary>
  3352. /// <remarks>This URLConnection method just returns a new <tt>SmbFileInputStream</tt> created with this file.
  3353. /// </remarks>
  3354. /// <exception cref="System.IO.IOException">thrown by <tt>SmbFileInputStream</tt> constructor
  3355. /// </exception>
  3356. public InputStream GetInputStream()
  3357. {
  3358. return new SmbFileInputStream(this);
  3359. }
  3360. /// <summary>This URLConnection method just returns a new <tt>SmbFileOutputStream</tt> created with this file.
  3361. /// </summary>
  3362. /// <remarks>This URLConnection method just returns a new <tt>SmbFileOutputStream</tt> created with this file.
  3363. /// </remarks>
  3364. /// <exception cref="System.IO.IOException">thrown by <tt>SmbFileOutputStream</tt> constructor
  3365. /// </exception>
  3366. public OutputStream GetOutputStream()
  3367. {
  3368. return new SmbFileOutputStream(this);
  3369. }
  3370. /// <exception cref="System.IO.IOException"></exception>
  3371. private void ProcessAces(Ace[] aces, bool resolveSids)
  3372. {
  3373. string server = GetServerWithDfs();
  3374. int ai;
  3375. if (resolveSids)
  3376. {
  3377. Sid[] sids = new Sid[aces.Length];
  3378. string[] names = null;
  3379. for (ai = 0; ai < aces.Length; ai++)
  3380. {
  3381. sids[ai] = aces[ai].Sid;
  3382. }
  3383. for (int off = 0; off < sids.Length; off += 64)
  3384. {
  3385. int len = sids.Length - off;
  3386. if (len > 64)
  3387. {
  3388. len = 64;
  3389. }
  3390. Sid.ResolveSids(server, Auth, sids, off, len);
  3391. }
  3392. }
  3393. else
  3394. {
  3395. for (ai = 0; ai < aces.Length; ai++)
  3396. {
  3397. aces[ai].Sid.OriginServer = server;
  3398. aces[ai].Sid.OriginAuth = Auth;
  3399. }
  3400. }
  3401. }
  3402. /// <summary>
  3403. /// Return an array of Access Control Entry (ACE) objects representing
  3404. /// the security descriptor associated with this file or directory.
  3405. /// </summary>
  3406. /// <remarks>
  3407. /// Return an array of Access Control Entry (ACE) objects representing
  3408. /// the security descriptor associated with this file or directory.
  3409. /// If no DACL is present, null is returned. If the DACL is empty, an array with 0 elements is returned.
  3410. /// </remarks>
  3411. /// <param name="resolveSids">
  3412. /// Attempt to resolve the SIDs within each ACE form
  3413. /// their numeric representation to their corresponding account names.
  3414. /// </param>
  3415. /// <exception cref="System.IO.IOException"></exception>
  3416. public virtual Ace[] GetSecurity(bool resolveSids)
  3417. {
  3418. int f;
  3419. Ace[] aces;
  3420. f = Open0(ORdonly, SmbConstants.ReadControl, 0, IsDirectory() ? 1 : 0);
  3421. NtTransQuerySecurityDesc request = new NtTransQuerySecurityDesc(f, 0x04);
  3422. NtTransQuerySecurityDescResponse response = new NtTransQuerySecurityDescResponse(
  3423. );
  3424. try
  3425. {
  3426. Send(request, response);
  3427. }
  3428. finally
  3429. {
  3430. Close(f, 0L);
  3431. }
  3432. aces = response.SecurityDescriptor.Aces;
  3433. if (aces != null)
  3434. {
  3435. ProcessAces(aces, resolveSids);
  3436. }
  3437. return aces;
  3438. }
  3439. /// <summary>
  3440. /// Return an array of Access Control Entry (ACE) objects representing
  3441. /// the share permissions on the share exporting this file or directory.
  3442. /// </summary>
  3443. /// <remarks>
  3444. /// Return an array of Access Control Entry (ACE) objects representing
  3445. /// the share permissions on the share exporting this file or directory.
  3446. /// If no DACL is present, null is returned. If the DACL is empty, an array with 0 elements is returned.
  3447. /// <p>
  3448. /// Note that this is different from calling <tt>getSecurity</tt> on a
  3449. /// share. There are actually two different ACLs for shares - the ACL on
  3450. /// the share and the ACL on the folder being shared.
  3451. /// Go to <i>Computer Management</i>
  3452. /// &gt; <i>System Tools</i> &gt; <i>Shared Folders</i> &gt <i>Shares</i> and
  3453. /// look at the <i>Properties</i> for a share. You will see two tabs - one
  3454. /// for "Share Permissions" and another for "Security". These correspond to
  3455. /// the ACLs returned by <tt>getShareSecurity</tt> and <tt>getSecurity</tt>
  3456. /// respectively.
  3457. /// </remarks>
  3458. /// <param name="resolveSids">
  3459. /// Attempt to resolve the SIDs within each ACE form
  3460. /// their numeric representation to their corresponding account names.
  3461. /// </param>
  3462. /// <exception cref="System.IO.IOException"></exception>
  3463. public virtual Ace[] GetShareSecurity(bool resolveSids)
  3464. {
  3465. string p = Url.AbsolutePath;
  3466. MsrpcShareGetInfo rpc;
  3467. DcerpcHandle handle;
  3468. Ace[] aces;
  3469. ResolveDfs(null);
  3470. string server = GetServerWithDfs();
  3471. rpc = new MsrpcShareGetInfo(server, Tree.Share);
  3472. handle = DcerpcHandle.GetHandle("ncacn_np:" + server + "[\\PIPE\\srvsvc]", Auth);
  3473. try
  3474. {
  3475. handle.Sendrecv(rpc);
  3476. if (rpc.Retval != 0)
  3477. {
  3478. throw new SmbException(rpc.Retval, true);
  3479. }
  3480. aces = rpc.GetSecurity();
  3481. if (aces != null)
  3482. {
  3483. ProcessAces(aces, resolveSids);
  3484. }
  3485. }
  3486. finally
  3487. {
  3488. try
  3489. {
  3490. handle.Close();
  3491. }
  3492. catch (IOException ioe)
  3493. {
  3494. if (Log.Level >= 1)
  3495. {
  3496. Runtime.PrintStackTrace(ioe, Log);
  3497. }
  3498. }
  3499. }
  3500. return aces;
  3501. }
  3502. /// <summary>
  3503. /// Return an array of Access Control Entry (ACE) objects representing
  3504. /// the security descriptor associated with this file or directory.
  3505. /// </summary>
  3506. /// <remarks>
  3507. /// Return an array of Access Control Entry (ACE) objects representing
  3508. /// the security descriptor associated with this file or directory.
  3509. /// <p>
  3510. /// Initially, the SIDs within each ACE will not be resolved however when
  3511. /// <tt>getType()</tt>, <tt>getDomainName()</tt>, <tt>getAccountName()</tt>,
  3512. /// or <tt>toString()</tt> is called, the names will attempt to be
  3513. /// resolved. If the names cannot be resolved (e.g. due to temporary
  3514. /// network failure), the said methods will return default values (usually
  3515. /// <tt>S-X-Y-Z</tt> strings of fragments of).
  3516. /// <p>
  3517. /// Alternatively <tt>getSecurity(true)</tt> may be used to resolve all
  3518. /// SIDs together and detect network failures.
  3519. /// </remarks>
  3520. /// <exception cref="System.IO.IOException"></exception>
  3521. public virtual Ace[] GetSecurity()
  3522. {
  3523. return GetSecurity(false);
  3524. }
  3525. }
  3526. }