sftp.js 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. // TODO: support EXTENDED request packets
  2. var TransformStream = require('stream').Transform;
  3. var ReadableStream = require('stream').Readable;
  4. var WritableStream = require('stream').Writable;
  5. var constants = require('fs').constants || process.binding('constants');
  6. var util = require('util');
  7. var inherits = util.inherits;
  8. var isDate = util.isDate;
  9. var listenerCount = require('events').EventEmitter.listenerCount;
  10. var fs = require('fs');
  11. var readString = require('./utils').readString;
  12. var readInt = require('./utils').readInt;
  13. var readUInt32BE = require('./buffer-helpers').readUInt32BE;
  14. var writeUInt32BE = require('./buffer-helpers').writeUInt32BE;
  15. var ATTR = {
  16. SIZE: 0x00000001,
  17. UIDGID: 0x00000002,
  18. PERMISSIONS: 0x00000004,
  19. ACMODTIME: 0x00000008,
  20. EXTENDED: 0x80000000
  21. };
  22. var STATUS_CODE = {
  23. OK: 0,
  24. EOF: 1,
  25. NO_SUCH_FILE: 2,
  26. PERMISSION_DENIED: 3,
  27. FAILURE: 4,
  28. BAD_MESSAGE: 5,
  29. NO_CONNECTION: 6,
  30. CONNECTION_LOST: 7,
  31. OP_UNSUPPORTED: 8
  32. };
  33. Object.keys(STATUS_CODE).forEach(function(key) {
  34. STATUS_CODE[STATUS_CODE[key]] = key;
  35. });
  36. var STATUS_CODE_STR = {
  37. 0: 'No error',
  38. 1: 'End of file',
  39. 2: 'No such file or directory',
  40. 3: 'Permission denied',
  41. 4: 'Failure',
  42. 5: 'Bad message',
  43. 6: 'No connection',
  44. 7: 'Connection lost',
  45. 8: 'Operation unsupported'
  46. };
  47. SFTPStream.STATUS_CODE = STATUS_CODE;
  48. var REQUEST = {
  49. INIT: 1,
  50. OPEN: 3,
  51. CLOSE: 4,
  52. READ: 5,
  53. WRITE: 6,
  54. LSTAT: 7,
  55. FSTAT: 8,
  56. SETSTAT: 9,
  57. FSETSTAT: 10,
  58. OPENDIR: 11,
  59. READDIR: 12,
  60. REMOVE: 13,
  61. MKDIR: 14,
  62. RMDIR: 15,
  63. REALPATH: 16,
  64. STAT: 17,
  65. RENAME: 18,
  66. READLINK: 19,
  67. SYMLINK: 20,
  68. EXTENDED: 200
  69. };
  70. Object.keys(REQUEST).forEach(function(key) {
  71. REQUEST[REQUEST[key]] = key;
  72. });
  73. var RESPONSE = {
  74. VERSION: 2,
  75. STATUS: 101,
  76. HANDLE: 102,
  77. DATA: 103,
  78. NAME: 104,
  79. ATTRS: 105,
  80. EXTENDED: 201
  81. };
  82. Object.keys(RESPONSE).forEach(function(key) {
  83. RESPONSE[RESPONSE[key]] = key;
  84. });
  85. var OPEN_MODE = {
  86. READ: 0x00000001,
  87. WRITE: 0x00000002,
  88. APPEND: 0x00000004,
  89. CREAT: 0x00000008,
  90. TRUNC: 0x00000010,
  91. EXCL: 0x00000020
  92. };
  93. SFTPStream.OPEN_MODE = OPEN_MODE;
  94. var MAX_PKT_LEN = 34000;
  95. var MAX_REQID = Math.pow(2, 32) - 1;
  96. var CLIENT_VERSION_BUFFER = Buffer.from([0, 0, 0, 5 /* length */,
  97. REQUEST.INIT,
  98. 0, 0, 0, 3 /* version */]);
  99. var SERVER_VERSION_BUFFER = Buffer.from([0, 0, 0, 5 /* length */,
  100. RESPONSE.VERSION,
  101. 0, 0, 0, 3 /* version */]);
  102. /*
  103. http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02:
  104. The maximum size of a packet is in practice determined by the client
  105. (the maximum size of read or write requests that it sends, plus a few
  106. bytes of packet overhead). All servers SHOULD support packets of at
  107. least 34000 bytes (where the packet size refers to the full length,
  108. including the header above). This should allow for reads and writes
  109. of at most 32768 bytes.
  110. OpenSSH caps this to 256kb instead of the ~34kb as mentioned in the sftpv3
  111. spec.
  112. */
  113. var RE_OPENSSH = /^SSH-2.0-(?:OpenSSH|dropbear)/;
  114. var OPENSSH_MAX_DATA_LEN = (256 * 1024) - (2 * 1024)/*account for header data*/;
  115. function DEBUG_NOOP(msg) {}
  116. function SFTPStream(cfg, remoteIdentRaw) {
  117. if (typeof cfg === 'string' && !remoteIdentRaw) {
  118. remoteIdentRaw = cfg;
  119. cfg = undefined;
  120. }
  121. if (typeof cfg !== 'object' || !cfg)
  122. cfg = {};
  123. TransformStream.call(this, {
  124. highWaterMark: (typeof cfg.highWaterMark === 'number'
  125. ? cfg.highWaterMark
  126. : 32 * 1024)
  127. });
  128. this.debug = (typeof cfg.debug === 'function' ? cfg.debug : DEBUG_NOOP);
  129. this.server = (cfg.server ? true : false);
  130. this._isOpenSSH = (remoteIdentRaw && RE_OPENSSH.test(remoteIdentRaw));
  131. this._needContinue = false;
  132. this._state = {
  133. // common
  134. status: 'packet_header',
  135. writeReqid: -1,
  136. pktLeft: undefined,
  137. pktHdrBuf: Buffer.allocUnsafe(9), // room for pktLen + pktType + req id
  138. pktBuf: undefined,
  139. pktType: undefined,
  140. version: undefined,
  141. extensions: {},
  142. // client
  143. maxDataLen: (this._isOpenSSH ? OPENSSH_MAX_DATA_LEN : 32768),
  144. requests: {}
  145. };
  146. var self = this;
  147. this.on('end', function() {
  148. self.readable = false;
  149. }).on('finish', onFinish)
  150. .on('prefinish', onFinish);
  151. function onFinish() {
  152. self.writable = false;
  153. self._cleanup(false);
  154. }
  155. if (!this.server)
  156. this.push(CLIENT_VERSION_BUFFER);
  157. }
  158. inherits(SFTPStream, TransformStream);
  159. SFTPStream.prototype.__read = TransformStream.prototype._read;
  160. SFTPStream.prototype._read = function(n) {
  161. if (this._needContinue) {
  162. this._needContinue = false;
  163. this.emit('continue');
  164. }
  165. return this.__read(n);
  166. };
  167. SFTPStream.prototype.__push = TransformStream.prototype.push;
  168. SFTPStream.prototype.push = function(chunk, encoding) {
  169. if (!this.readable)
  170. return false;
  171. if (chunk === null)
  172. this.readable = false;
  173. var ret = this.__push(chunk, encoding);
  174. this._needContinue = (ret === false);
  175. return ret;
  176. };
  177. SFTPStream.prototype._cleanup = function(callback) {
  178. var state = this._state;
  179. state.pktBuf = undefined; // give GC something to do
  180. var requests = state.requests;
  181. var keys = Object.keys(requests);
  182. var len = keys.length;
  183. if (len) {
  184. if (this.readable) {
  185. var err = new Error('SFTP session ended early');
  186. for (var i = 0, cb; i < len; ++i)
  187. (cb = requests[keys[i]].cb) && cb(err);
  188. }
  189. state.requests = {};
  190. }
  191. if (this.readable)
  192. this.push(null);
  193. if (!this._readableState.endEmitted && !this._readableState.flowing) {
  194. // Ugh!
  195. this.resume();
  196. }
  197. if (callback !== false) {
  198. this.debug('DEBUG[SFTP]: Parser: Malformed packet');
  199. callback && callback(new Error('Malformed packet'));
  200. }
  201. };
  202. SFTPStream.prototype._transform = function(chunk, encoding, callback) {
  203. var state = this._state;
  204. var server = this.server;
  205. var status = state.status;
  206. var pktType = state.pktType;
  207. var pktBuf = state.pktBuf;
  208. var pktLeft = state.pktLeft;
  209. var version = state.version;
  210. var pktHdrBuf = state.pktHdrBuf;
  211. var requests = state.requests;
  212. var debug = this.debug;
  213. var chunkLen = chunk.length;
  214. var chunkPos = 0;
  215. var buffer;
  216. var chunkLeft;
  217. var id;
  218. while (true) {
  219. if (status === 'discard') {
  220. chunkLeft = (chunkLen - chunkPos);
  221. if (pktLeft <= chunkLeft) {
  222. chunkPos += pktLeft;
  223. pktLeft = 0;
  224. status = 'packet_header';
  225. buffer = pktBuf = undefined;
  226. } else {
  227. pktLeft -= chunkLeft;
  228. break;
  229. }
  230. } else if (pktBuf !== undefined) {
  231. chunkLeft = (chunkLen - chunkPos);
  232. if (pktLeft <= chunkLeft) {
  233. chunk.copy(pktBuf,
  234. pktBuf.length - pktLeft,
  235. chunkPos,
  236. chunkPos + pktLeft);
  237. chunkPos += pktLeft;
  238. pktLeft = 0;
  239. buffer = pktBuf;
  240. pktBuf = undefined;
  241. continue;
  242. } else {
  243. chunk.copy(pktBuf, pktBuf.length - pktLeft, chunkPos);
  244. pktLeft -= chunkLeft;
  245. break;
  246. }
  247. } else if (status === 'packet_header') {
  248. if (!buffer) {
  249. pktLeft = 5;
  250. pktBuf = pktHdrBuf;
  251. } else {
  252. // here we read the right-most 5 bytes from buffer (pktHdrBuf)
  253. pktLeft = readUInt32BE(buffer, 4) - 1; // account for type byte
  254. pktType = buffer[8];
  255. if (server) {
  256. if (version === undefined && pktType !== REQUEST.INIT) {
  257. debug('DEBUG[SFTP]: Parser: Unexpected packet before init');
  258. this._cleanup(false);
  259. return callback(new Error('Unexpected packet before init'));
  260. } else if (version !== undefined && pktType === REQUEST.INIT) {
  261. debug('DEBUG[SFTP]: Parser: Unexpected duplicate init');
  262. status = 'bad_pkt';
  263. } else if (pktLeft > MAX_PKT_LEN) {
  264. var msg = 'Packet length ('
  265. + pktLeft
  266. + ') exceeds max length ('
  267. + MAX_PKT_LEN
  268. + ')';
  269. debug('DEBUG[SFTP]: Parser: ' + msg);
  270. this._cleanup(false);
  271. return callback(new Error(msg));
  272. } else if (pktType === REQUEST.EXTENDED) {
  273. status = 'bad_pkt';
  274. } else if (REQUEST[pktType] === undefined) {
  275. debug('DEBUG[SFTP]: Parser: Unsupported packet type: ' + pktType);
  276. status = 'discard';
  277. }
  278. } else if (version === undefined && pktType !== RESPONSE.VERSION) {
  279. debug('DEBUG[SFTP]: Parser: Unexpected packet before version');
  280. this._cleanup(false);
  281. return callback(new Error('Unexpected packet before version'));
  282. } else if (version !== undefined && pktType === RESPONSE.VERSION) {
  283. debug('DEBUG[SFTP]: Parser: Unexpected duplicate version');
  284. status = 'bad_pkt';
  285. } else if (RESPONSE[pktType] === undefined) {
  286. status = 'discard';
  287. }
  288. if (status === 'bad_pkt') {
  289. // copy original packet info
  290. writeUInt32BE(pktHdrBuf, pktLeft, 0);
  291. pktHdrBuf[4] = pktType;
  292. pktLeft = 4;
  293. pktBuf = pktHdrBuf;
  294. } else {
  295. pktBuf = Buffer.allocUnsafe(pktLeft);
  296. status = 'payload';
  297. }
  298. }
  299. } else if (status === 'payload') {
  300. if (pktType === RESPONSE.VERSION || pktType === REQUEST.INIT) {
  301. /*
  302. uint32 version
  303. <extension data>
  304. */
  305. version = state.version = readInt(buffer, 0, this, callback);
  306. if (version === false)
  307. return;
  308. if (version < 3) {
  309. this._cleanup(false);
  310. return callback(new Error('Incompatible SFTP version: ' + version));
  311. } else if (server)
  312. this.push(SERVER_VERSION_BUFFER);
  313. var buflen = buffer.length;
  314. var extname;
  315. var extdata;
  316. buffer._pos = 4;
  317. while (buffer._pos < buflen) {
  318. extname = readString(buffer, buffer._pos, 'ascii', this, callback);
  319. if (extname === false)
  320. return;
  321. extdata = readString(buffer, buffer._pos, 'ascii', this, callback);
  322. if (extdata === false)
  323. return;
  324. if (state.extensions[extname])
  325. state.extensions[extname].push(extdata);
  326. else
  327. state.extensions[extname] = [ extdata ];
  328. }
  329. this.emit('ready');
  330. } else {
  331. /*
  332. All other packets (client and server) begin with a (client) request
  333. id:
  334. uint32 id
  335. */
  336. id = readInt(buffer, 0, this, callback);
  337. if (id === false)
  338. return;
  339. var filename;
  340. var attrs;
  341. var handle;
  342. var data;
  343. if (!server) {
  344. var req = requests[id];
  345. var cb = req && req.cb;
  346. debug('DEBUG[SFTP]: Parser: Response: ' + RESPONSE[pktType]);
  347. if (req && cb) {
  348. if (pktType === RESPONSE.STATUS) {
  349. /*
  350. uint32 error/status code
  351. string error message (ISO-10646 UTF-8)
  352. string language tag
  353. */
  354. var code = readInt(buffer, 4, this, callback);
  355. if (code === false)
  356. return;
  357. if (code === STATUS_CODE.OK) {
  358. cb();
  359. } else {
  360. // We borrow OpenSSH behavior here, specifically we make the
  361. // message and language fields optional, despite the
  362. // specification requiring them (even if they are empty). This
  363. // helps to avoid problems with buggy implementations that do
  364. // not fully conform to the SFTP(v3) specification.
  365. var msg;
  366. var lang = '';
  367. if (buffer.length >= 12) {
  368. msg = readString(buffer, 8, 'utf8', this, callback);
  369. if (msg === false)
  370. return;
  371. if ((buffer._pos + 4) < buffer.length) {
  372. lang = readString(buffer,
  373. buffer._pos,
  374. 'ascii',
  375. this,
  376. callback);
  377. if (lang === false)
  378. return;
  379. }
  380. }
  381. var err = new Error(msg
  382. || STATUS_CODE_STR[code]
  383. || 'Unknown status');
  384. err.code = code;
  385. err.lang = lang;
  386. cb(err);
  387. }
  388. } else if (pktType === RESPONSE.HANDLE) {
  389. /*
  390. string handle
  391. */
  392. handle = readString(buffer, 4, this, callback);
  393. if (handle === false)
  394. return;
  395. cb(undefined, handle);
  396. } else if (pktType === RESPONSE.DATA) {
  397. /*
  398. string data
  399. */
  400. if (req.buffer) {
  401. // we have already pre-allocated space to store the data
  402. var dataLen = readInt(buffer, 4, this, callback);
  403. if (dataLen === false)
  404. return;
  405. var reqBufLen = req.buffer.length;
  406. if (dataLen > reqBufLen) {
  407. // truncate response data to fit expected size
  408. writeUInt32BE(buffer, reqBufLen, 4);
  409. }
  410. data = readString(buffer, 4, req.buffer, this, callback);
  411. if (data === false)
  412. return;
  413. cb(undefined, data, dataLen);
  414. } else {
  415. data = readString(buffer, 4, this, callback);
  416. if (data === false)
  417. return;
  418. cb(undefined, data);
  419. }
  420. } else if (pktType === RESPONSE.NAME) {
  421. /*
  422. uint32 count
  423. repeats count times:
  424. string filename
  425. string longname
  426. ATTRS attrs
  427. */
  428. var namesLen = readInt(buffer, 4, this, callback);
  429. if (namesLen === false)
  430. return;
  431. var names = [],
  432. longname;
  433. buffer._pos = 8;
  434. for (var i = 0; i < namesLen; ++i) {
  435. // we are going to assume UTF-8 for filenames despite the SFTPv3
  436. // spec not specifying an encoding because the specs for newer
  437. // versions of the protocol all explicitly specify UTF-8 for
  438. // filenames
  439. filename = readString(buffer,
  440. buffer._pos,
  441. 'utf8',
  442. this,
  443. callback);
  444. if (filename === false)
  445. return;
  446. // `longname` only exists in SFTPv3 and since it typically will
  447. // contain the filename, we assume it is also UTF-8
  448. longname = readString(buffer,
  449. buffer._pos,
  450. 'utf8',
  451. this,
  452. callback);
  453. if (longname === false)
  454. return;
  455. attrs = readAttrs(buffer, buffer._pos, this, callback);
  456. if (attrs === false)
  457. return;
  458. names.push({
  459. filename: filename,
  460. longname: longname,
  461. attrs: attrs
  462. });
  463. }
  464. cb(undefined, names);
  465. } else if (pktType === RESPONSE.ATTRS) {
  466. /*
  467. ATTRS attrs
  468. */
  469. attrs = readAttrs(buffer, 4, this, callback);
  470. if (attrs === false)
  471. return;
  472. cb(undefined, attrs);
  473. } else if (pktType === RESPONSE.EXTENDED) {
  474. if (req.extended) {
  475. switch (req.extended) {
  476. case 'statvfs@openssh.com':
  477. case 'fstatvfs@openssh.com':
  478. /*
  479. uint64 f_bsize // file system block size
  480. uint64 f_frsize // fundamental fs block size
  481. uint64 f_blocks // number of blocks (unit f_frsize)
  482. uint64 f_bfree // free blocks in file system
  483. uint64 f_bavail // free blocks for non-root
  484. uint64 f_files // total file inodes
  485. uint64 f_ffree // free file inodes
  486. uint64 f_favail // free file inodes for to non-root
  487. uint64 f_fsid // file system id
  488. uint64 f_flag // bit mask of f_flag values
  489. uint64 f_namemax // maximum filename length
  490. */
  491. var stats = {
  492. f_bsize: undefined,
  493. f_frsize: undefined,
  494. f_blocks: undefined,
  495. f_bfree: undefined,
  496. f_bavail: undefined,
  497. f_files: undefined,
  498. f_ffree: undefined,
  499. f_favail: undefined,
  500. f_sid: undefined,
  501. f_flag: undefined,
  502. f_namemax: undefined
  503. };
  504. stats.f_bsize = readUInt64BE(buffer, 4, this, callback);
  505. if (stats.f_bsize === false)
  506. return;
  507. stats.f_frsize = readUInt64BE(buffer, 12, this, callback);
  508. if (stats.f_frsize === false)
  509. return;
  510. stats.f_blocks = readUInt64BE(buffer, 20, this, callback);
  511. if (stats.f_blocks === false)
  512. return;
  513. stats.f_bfree = readUInt64BE(buffer, 28, this, callback);
  514. if (stats.f_bfree === false)
  515. return;
  516. stats.f_bavail = readUInt64BE(buffer, 36, this, callback);
  517. if (stats.f_bavail === false)
  518. return;
  519. stats.f_files = readUInt64BE(buffer, 44, this, callback);
  520. if (stats.f_files === false)
  521. return;
  522. stats.f_ffree = readUInt64BE(buffer, 52, this, callback);
  523. if (stats.f_ffree === false)
  524. return;
  525. stats.f_favail = readUInt64BE(buffer, 60, this, callback);
  526. if (stats.f_favail === false)
  527. return;
  528. stats.f_sid = readUInt64BE(buffer, 68, this, callback);
  529. if (stats.f_sid === false)
  530. return;
  531. stats.f_flag = readUInt64BE(buffer, 76, this, callback);
  532. if (stats.f_flag === false)
  533. return;
  534. stats.f_namemax = readUInt64BE(buffer, 84, this, callback);
  535. if (stats.f_namemax === false)
  536. return;
  537. cb(undefined, stats);
  538. break;
  539. }
  540. }
  541. // XXX: at least provide the raw buffer data to the callback in
  542. // case of unexpected extended response?
  543. cb();
  544. }
  545. }
  546. if (req)
  547. delete requests[id];
  548. } else {
  549. // server
  550. var evName = REQUEST[pktType];
  551. var offset;
  552. var path;
  553. debug('DEBUG[SFTP]: Parser: Request: ' + evName);
  554. if (listenerCount(this, evName)) {
  555. if (pktType === REQUEST.OPEN) {
  556. /*
  557. string filename
  558. uint32 pflags
  559. ATTRS attrs
  560. */
  561. filename = readString(buffer, 4, 'utf8', this, callback);
  562. if (filename === false)
  563. return;
  564. var pflags = readInt(buffer, buffer._pos, this, callback);
  565. if (pflags === false)
  566. return;
  567. attrs = readAttrs(buffer, buffer._pos + 4, this, callback);
  568. if (attrs === false)
  569. return;
  570. this.emit(evName, id, filename, pflags, attrs);
  571. } else if (pktType === REQUEST.CLOSE
  572. || pktType === REQUEST.FSTAT
  573. || pktType === REQUEST.READDIR) {
  574. /*
  575. string handle
  576. */
  577. handle = readString(buffer, 4, this, callback);
  578. if (handle === false)
  579. return;
  580. this.emit(evName, id, handle);
  581. } else if (pktType === REQUEST.READ) {
  582. /*
  583. string handle
  584. uint64 offset
  585. uint32 len
  586. */
  587. handle = readString(buffer, 4, this, callback);
  588. if (handle === false)
  589. return;
  590. offset = readUInt64BE(buffer, buffer._pos, this, callback);
  591. if (offset === false)
  592. return;
  593. var len = readInt(buffer, buffer._pos, this, callback);
  594. if (len === false)
  595. return;
  596. this.emit(evName, id, handle, offset, len);
  597. } else if (pktType === REQUEST.WRITE) {
  598. /*
  599. string handle
  600. uint64 offset
  601. string data
  602. */
  603. handle = readString(buffer, 4, this, callback);
  604. if (handle === false)
  605. return;
  606. offset = readUInt64BE(buffer, buffer._pos, this, callback);
  607. if (offset === false)
  608. return;
  609. data = readString(buffer, buffer._pos, this, callback);
  610. if (data === false)
  611. return;
  612. this.emit(evName, id, handle, offset, data);
  613. } else if (pktType === REQUEST.LSTAT
  614. || pktType === REQUEST.STAT
  615. || pktType === REQUEST.OPENDIR
  616. || pktType === REQUEST.REMOVE
  617. || pktType === REQUEST.RMDIR
  618. || pktType === REQUEST.REALPATH
  619. || pktType === REQUEST.READLINK) {
  620. /*
  621. string path
  622. */
  623. path = readString(buffer, 4, 'utf8', this, callback);
  624. if (path === false)
  625. return;
  626. this.emit(evName, id, path);
  627. } else if (pktType === REQUEST.SETSTAT
  628. || pktType === REQUEST.MKDIR) {
  629. /*
  630. string path
  631. ATTRS attrs
  632. */
  633. path = readString(buffer, 4, 'utf8', this, callback);
  634. if (path === false)
  635. return;
  636. attrs = readAttrs(buffer, buffer._pos, this, callback);
  637. if (attrs === false)
  638. return;
  639. this.emit(evName, id, path, attrs);
  640. } else if (pktType === REQUEST.FSETSTAT) {
  641. /*
  642. string handle
  643. ATTRS attrs
  644. */
  645. handle = readString(buffer, 4, this, callback);
  646. if (handle === false)
  647. return;
  648. attrs = readAttrs(buffer, buffer._pos, this, callback);
  649. if (attrs === false)
  650. return;
  651. this.emit(evName, id, handle, attrs);
  652. } else if (pktType === REQUEST.RENAME
  653. || pktType === REQUEST.SYMLINK) {
  654. /*
  655. RENAME:
  656. string oldpath
  657. string newpath
  658. SYMLINK:
  659. string linkpath
  660. string targetpath
  661. */
  662. var str1;
  663. var str2;
  664. str1 = readString(buffer, 4, 'utf8', this, callback);
  665. if (str1 === false)
  666. return;
  667. str2 = readString(buffer, buffer._pos, 'utf8', this, callback);
  668. if (str2 === false)
  669. return;
  670. if (pktType === REQUEST.SYMLINK && this._isOpenSSH) {
  671. // OpenSSH has linkpath and targetpath positions switched
  672. this.emit(evName, id, str2, str1);
  673. } else
  674. this.emit(evName, id, str1, str2);
  675. }
  676. } else {
  677. // automatically reject request if no handler for request type
  678. this.status(id, STATUS_CODE.OP_UNSUPPORTED);
  679. }
  680. }
  681. }
  682. // prepare for next packet
  683. status = 'packet_header';
  684. buffer = pktBuf = undefined;
  685. } else if (status === 'bad_pkt') {
  686. if (server && buffer[4] !== REQUEST.INIT) {
  687. var errCode = (buffer[4] === REQUEST.EXTENDED
  688. ? STATUS_CODE.OP_UNSUPPORTED
  689. : STATUS_CODE.FAILURE);
  690. // no request id for init/version packets, so we have no way to send a
  691. // status response, so we just close up shop ...
  692. if (buffer[4] === REQUEST.INIT || buffer[4] === RESPONSE.VERSION)
  693. return this._cleanup(callback);
  694. id = readInt(buffer, 5, this, callback);
  695. if (id === false)
  696. return;
  697. this.status(id, errCode);
  698. }
  699. // by this point we have already read the type byte and the id bytes, so
  700. // we subtract those from the number of bytes to skip
  701. pktLeft = readUInt32BE(buffer, 0) - 5;
  702. status = 'discard';
  703. }
  704. if (chunkPos >= chunkLen)
  705. break;
  706. }
  707. state.status = status;
  708. state.pktType = pktType;
  709. state.pktBuf = pktBuf;
  710. state.pktLeft = pktLeft;
  711. state.version = version;
  712. callback();
  713. };
  714. // client
  715. SFTPStream.prototype.createReadStream = function(path, options) {
  716. if (this.server)
  717. throw new Error('Client-only method called in server mode');
  718. return new ReadStream(this, path, options);
  719. };
  720. SFTPStream.prototype.createWriteStream = function(path, options) {
  721. if (this.server)
  722. throw new Error('Client-only method called in server mode');
  723. return new WriteStream(this, path, options);
  724. };
  725. SFTPStream.prototype.open = function(path, flags_, attrs, cb) {
  726. if (this.server)
  727. throw new Error('Client-only method called in server mode');
  728. var state = this._state;
  729. if (typeof attrs === 'function') {
  730. cb = attrs;
  731. attrs = undefined;
  732. }
  733. var flags = stringToFlags(flags_);
  734. if (flags === null)
  735. throw new Error('Unknown flags string: ' + flags_);
  736. var attrFlags = 0;
  737. var attrBytes = 0;
  738. if (typeof attrs === 'string' || typeof attrs === 'number') {
  739. attrs = { mode: attrs };
  740. }
  741. if (typeof attrs === 'object') {
  742. attrs = attrsToBytes(attrs);
  743. attrFlags = attrs.flags;
  744. attrBytes = attrs.nbytes;
  745. attrs = attrs.bytes;
  746. }
  747. /*
  748. uint32 id
  749. string filename
  750. uint32 pflags
  751. ATTRS attrs
  752. */
  753. var pathlen = Buffer.byteLength(path);
  754. var p = 9;
  755. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen + 4 + 4 + attrBytes);
  756. writeUInt32BE(buf, buf.length - 4, 0);
  757. buf[4] = REQUEST.OPEN;
  758. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  759. writeUInt32BE(buf, reqid, 5);
  760. writeUInt32BE(buf, pathlen, p);
  761. buf.write(path, p += 4, pathlen, 'utf8');
  762. writeUInt32BE(buf, flags, p += pathlen);
  763. writeUInt32BE(buf, attrFlags, p += 4);
  764. if (attrs && attrFlags) {
  765. p += 4;
  766. for (var i = 0, len = attrs.length; i < len; ++i)
  767. for (var j = 0, len2 = attrs[i].length; j < len2; ++j)
  768. buf[p++] = attrs[i][j];
  769. }
  770. state.requests[reqid] = { cb: cb };
  771. this.debug('DEBUG[SFTP]: Outgoing: Writing OPEN');
  772. return this.push(buf);
  773. };
  774. SFTPStream.prototype.close = function(handle, cb) {
  775. if (this.server)
  776. throw new Error('Client-only method called in server mode');
  777. else if (!Buffer.isBuffer(handle))
  778. throw new Error('handle is not a Buffer');
  779. var state = this._state;
  780. /*
  781. uint32 id
  782. string handle
  783. */
  784. var handlelen = handle.length;
  785. var p = 9;
  786. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen);
  787. writeUInt32BE(buf, buf.length - 4, 0);
  788. buf[4] = REQUEST.CLOSE;
  789. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  790. writeUInt32BE(buf, reqid, 5);
  791. writeUInt32BE(buf, handlelen, p);
  792. handle.copy(buf, p += 4);
  793. state.requests[reqid] = { cb: cb };
  794. this.debug('DEBUG[SFTP]: Outgoing: Writing CLOSE');
  795. return this.push(buf);
  796. };
  797. SFTPStream.prototype.readData = function(handle, buf, off, len, position, cb) {
  798. if (this.server)
  799. throw new Error('Client-only method called in server mode');
  800. else if (!Buffer.isBuffer(handle))
  801. throw new Error('handle is not a Buffer');
  802. else if (!Buffer.isBuffer(buf))
  803. throw new Error('buffer is not a Buffer');
  804. else if (off >= buf.length)
  805. throw new Error('offset is out of bounds');
  806. else if (off + len > buf.length)
  807. throw new Error('length extends beyond buffer');
  808. else if (position === null)
  809. throw new Error('null position currently unsupported');
  810. var state = this._state;
  811. /*
  812. uint32 id
  813. string handle
  814. uint64 offset
  815. uint32 len
  816. */
  817. var handlelen = handle.length;
  818. var p = 9;
  819. var pos = position;
  820. var out = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen + 8 + 4);
  821. writeUInt32BE(out, out.length - 4, 0);
  822. out[4] = REQUEST.READ;
  823. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  824. writeUInt32BE(out, reqid, 5);
  825. writeUInt32BE(out, handlelen, p);
  826. handle.copy(out, p += 4);
  827. p += handlelen;
  828. for (var i = 7; i >= 0; --i) {
  829. out[p + i] = pos & 0xFF;
  830. pos /= 256;
  831. }
  832. writeUInt32BE(out, len, p += 8);
  833. state.requests[reqid] = {
  834. cb: function(err, data, nb) {
  835. if (err) {
  836. if (cb._wantEOFError || err.code !== STATUS_CODE.EOF)
  837. return cb(err);
  838. } else if (nb > len) {
  839. return cb(new Error('Received more data than requested'));
  840. }
  841. cb(undefined, nb || 0, data, position);
  842. },
  843. buffer: buf.slice(off, off + len)
  844. };
  845. this.debug('DEBUG[SFTP]: Outgoing: Writing READ');
  846. return this.push(out);
  847. };
  848. SFTPStream.prototype.writeData = function(handle, buf, off, len, position, cb) {
  849. if (this.server)
  850. throw new Error('Client-only method called in server mode');
  851. else if (!Buffer.isBuffer(handle))
  852. throw new Error('handle is not a Buffer');
  853. else if (!Buffer.isBuffer(buf))
  854. throw new Error('buffer is not a Buffer');
  855. else if (off > buf.length)
  856. throw new Error('offset is out of bounds');
  857. else if (off + len > buf.length)
  858. throw new Error('length extends beyond buffer');
  859. else if (position === null)
  860. throw new Error('null position currently unsupported');
  861. var self = this;
  862. var state = this._state;
  863. if (!len) {
  864. cb && process.nextTick(function() { cb(undefined, 0); });
  865. return;
  866. }
  867. var overflow = (len > state.maxDataLen
  868. ? len - state.maxDataLen
  869. : 0);
  870. var origPosition = position;
  871. if (overflow)
  872. len = state.maxDataLen;
  873. /*
  874. uint32 id
  875. string handle
  876. uint64 offset
  877. string data
  878. */
  879. var handlelen = handle.length;
  880. var p = 9;
  881. var out = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen + 8 + 4 + len);
  882. writeUInt32BE(out, out.length - 4, 0);
  883. out[4] = REQUEST.WRITE;
  884. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  885. writeUInt32BE(out, reqid, 5);
  886. writeUInt32BE(out, handlelen, p);
  887. handle.copy(out, p += 4);
  888. p += handlelen;
  889. for (var i = 7; i >= 0; --i) {
  890. out[p + i] = position & 0xFF;
  891. position /= 256;
  892. }
  893. writeUInt32BE(out, len, p += 8);
  894. buf.copy(out, p += 4, off, off + len);
  895. state.requests[reqid] = {
  896. cb: function(err) {
  897. if (err)
  898. cb && cb(err);
  899. else if (overflow) {
  900. self.writeData(handle,
  901. buf,
  902. off + len,
  903. overflow,
  904. origPosition + len,
  905. cb);
  906. } else
  907. cb && cb(undefined, off + len);
  908. }
  909. };
  910. this.debug('DEBUG[SFTP]: Outgoing: Writing WRITE');
  911. return this.push(out);
  912. };
  913. function tryCreateBuffer(size) {
  914. try {
  915. return Buffer.allocUnsafe(size);
  916. } catch (ex) {
  917. return ex;
  918. }
  919. }
  920. function fastXfer(src, dst, srcPath, dstPath, opts, cb) {
  921. var concurrency = 64;
  922. var chunkSize = 32768;
  923. //var preserve = false;
  924. var onstep;
  925. var mode;
  926. if (typeof opts === 'function') {
  927. cb = opts;
  928. } else if (typeof opts === 'object') {
  929. if (typeof opts.concurrency === 'number'
  930. && opts.concurrency > 0
  931. && !isNaN(opts.concurrency))
  932. concurrency = opts.concurrency;
  933. if (typeof opts.chunkSize === 'number'
  934. && opts.chunkSize > 0
  935. && !isNaN(opts.chunkSize))
  936. chunkSize = opts.chunkSize;
  937. if (typeof opts.step === 'function')
  938. onstep = opts.step;
  939. //preserve = (opts.preserve ? true : false);
  940. if (typeof opts.mode === 'string' || typeof opts.mode === 'number')
  941. mode = modeNum(opts.mode);
  942. }
  943. // internal state variables
  944. var fsize;
  945. var pdst = 0;
  946. var total = 0;
  947. var hadError = false;
  948. var srcHandle;
  949. var dstHandle;
  950. var readbuf;
  951. var bufsize = chunkSize * concurrency;
  952. function onerror(err) {
  953. if (hadError)
  954. return;
  955. hadError = true;
  956. var left = 0;
  957. var cbfinal;
  958. if (srcHandle || dstHandle) {
  959. cbfinal = function() {
  960. if (--left === 0)
  961. cb(err);
  962. };
  963. if (srcHandle && (src === fs || src.writable))
  964. ++left;
  965. if (dstHandle && (dst === fs || dst.writable))
  966. ++left;
  967. if (srcHandle && (src === fs || src.writable))
  968. src.close(srcHandle, cbfinal);
  969. if (dstHandle && (dst === fs || dst.writable))
  970. dst.close(dstHandle, cbfinal);
  971. } else
  972. cb(err);
  973. }
  974. src.open(srcPath, 'r', function(err, sourceHandle) {
  975. if (err)
  976. return onerror(err);
  977. srcHandle = sourceHandle;
  978. src.fstat(srcHandle, function tryStat(err, attrs) {
  979. if (err) {
  980. if (src !== fs) {
  981. // Try stat() for sftp servers that may not support fstat() for
  982. // whatever reason
  983. src.stat(srcPath, function(err_, attrs_) {
  984. if (err_)
  985. return onerror(err);
  986. tryStat(null, attrs_);
  987. });
  988. return;
  989. }
  990. return onerror(err);
  991. }
  992. fsize = attrs.size;
  993. dst.open(dstPath, 'w', function(err, destHandle) {
  994. if (err)
  995. return onerror(err);
  996. dstHandle = destHandle;
  997. if (fsize <= 0)
  998. return onerror();
  999. // Use less memory where possible
  1000. while (bufsize > fsize) {
  1001. if (concurrency === 1) {
  1002. bufsize = fsize;
  1003. break;
  1004. }
  1005. bufsize -= chunkSize;
  1006. --concurrency;
  1007. }
  1008. readbuf = tryCreateBuffer(bufsize);
  1009. if (readbuf instanceof Error)
  1010. return onerror(readbuf);
  1011. if (mode !== undefined) {
  1012. dst.fchmod(dstHandle, mode, function tryAgain(err) {
  1013. if (err) {
  1014. // Try chmod() for sftp servers that may not support fchmod() for
  1015. // whatever reason
  1016. dst.chmod(dstPath, mode, function(err_) {
  1017. tryAgain();
  1018. });
  1019. return;
  1020. }
  1021. startReads();
  1022. });
  1023. } else {
  1024. startReads();
  1025. }
  1026. function onread(err, nb, data, dstpos, datapos, origChunkLen) {
  1027. if (err)
  1028. return onerror(err);
  1029. datapos = datapos || 0;
  1030. if (src === fs)
  1031. dst.writeData(dstHandle, readbuf, datapos, nb, dstpos, writeCb);
  1032. else
  1033. dst.write(dstHandle, readbuf, datapos, nb, dstpos, writeCb);
  1034. function writeCb(err) {
  1035. if (err)
  1036. return onerror(err);
  1037. total += nb;
  1038. onstep && onstep(total, nb, fsize);
  1039. if (nb < origChunkLen)
  1040. return singleRead(datapos, dstpos + nb, origChunkLen - nb);
  1041. if (total === fsize) {
  1042. dst.close(dstHandle, function(err) {
  1043. dstHandle = undefined;
  1044. if (err)
  1045. return onerror(err);
  1046. src.close(srcHandle, function(err) {
  1047. srcHandle = undefined;
  1048. if (err)
  1049. return onerror(err);
  1050. cb();
  1051. });
  1052. });
  1053. return;
  1054. }
  1055. if (pdst >= fsize)
  1056. return;
  1057. var chunk = (pdst + chunkSize > fsize ? fsize - pdst : chunkSize);
  1058. singleRead(datapos, pdst, chunk);
  1059. pdst += chunk;
  1060. }
  1061. }
  1062. function makeCb(psrc, pdst, chunk) {
  1063. return function(err, nb, data) {
  1064. onread(err, nb, data, pdst, psrc, chunk);
  1065. };
  1066. }
  1067. function singleRead(psrc, pdst, chunk) {
  1068. if (src === fs) {
  1069. src.read(srcHandle,
  1070. readbuf,
  1071. psrc,
  1072. chunk,
  1073. pdst,
  1074. makeCb(psrc, pdst, chunk));
  1075. } else {
  1076. src.readData(srcHandle,
  1077. readbuf,
  1078. psrc,
  1079. chunk,
  1080. pdst,
  1081. makeCb(psrc, pdst, chunk));
  1082. }
  1083. }
  1084. function startReads() {
  1085. var reads = 0;
  1086. var psrc = 0;
  1087. while (pdst < fsize && reads < concurrency) {
  1088. var chunk = (pdst + chunkSize > fsize ? fsize - pdst : chunkSize);
  1089. singleRead(psrc, pdst, chunk);
  1090. psrc += chunk;
  1091. pdst += chunk;
  1092. ++reads;
  1093. }
  1094. }
  1095. });
  1096. });
  1097. });
  1098. }
  1099. SFTPStream.prototype.fastGet = function(remotePath, localPath, opts, cb) {
  1100. if (this.server)
  1101. throw new Error('Client-only method called in server mode');
  1102. fastXfer(this, fs, remotePath, localPath, opts, cb);
  1103. };
  1104. SFTPStream.prototype.fastPut = function(localPath, remotePath, opts, cb) {
  1105. if (this.server)
  1106. throw new Error('Client-only method called in server mode');
  1107. fastXfer(fs, this, localPath, remotePath, opts, cb);
  1108. };
  1109. SFTPStream.prototype.readFile = function(path, options, callback_) {
  1110. if (this.server)
  1111. throw new Error('Client-only method called in server mode');
  1112. var callback;
  1113. if (typeof callback_ === 'function') {
  1114. callback = callback_;
  1115. } else if (typeof options === 'function') {
  1116. callback = options;
  1117. options = undefined;
  1118. }
  1119. var self = this;
  1120. if (typeof options === 'string')
  1121. options = { encoding: options, flag: 'r' };
  1122. else if (!options)
  1123. options = { encoding: null, flag: 'r' };
  1124. else if (typeof options !== 'object')
  1125. throw new TypeError('Bad arguments');
  1126. var encoding = options.encoding;
  1127. if (encoding && !Buffer.isEncoding(encoding))
  1128. throw new Error('Unknown encoding: ' + encoding);
  1129. // first, stat the file, so we know the size.
  1130. var size;
  1131. var buffer; // single buffer with file data
  1132. var buffers; // list for when size is unknown
  1133. var pos = 0;
  1134. var handle;
  1135. // SFTPv3 does not support using -1 for read position, so we have to track
  1136. // read position manually
  1137. var bytesRead = 0;
  1138. var flag = options.flag || 'r';
  1139. this.open(path, flag, 438 /*=0666*/, function(er, handle_) {
  1140. if (er)
  1141. return callback && callback(er);
  1142. handle = handle_;
  1143. self.fstat(handle, function tryStat(er, st) {
  1144. if (er) {
  1145. // Try stat() for sftp servers that may not support fstat() for
  1146. // whatever reason
  1147. self.stat(path, function(er_, st_) {
  1148. if (er_) {
  1149. return self.close(handle, function() {
  1150. callback && callback(er);
  1151. });
  1152. }
  1153. tryStat(null, st_);
  1154. });
  1155. return;
  1156. }
  1157. size = st.size || 0;
  1158. if (size === 0) {
  1159. // the kernel lies about many files.
  1160. // Go ahead and try to read some bytes.
  1161. buffers = [];
  1162. return read();
  1163. }
  1164. buffer = Buffer.allocUnsafe(size);
  1165. read();
  1166. });
  1167. });
  1168. function read() {
  1169. if (size === 0) {
  1170. buffer = Buffer.allocUnsafe(8192);
  1171. self.readData(handle, buffer, 0, 8192, bytesRead, afterRead);
  1172. } else {
  1173. self.readData(handle, buffer, pos, size - pos, bytesRead, afterRead);
  1174. }
  1175. }
  1176. function afterRead(er, nbytes) {
  1177. var eof;
  1178. if (er) {
  1179. eof = (er.code === STATUS_CODE.EOF);
  1180. if (!eof) {
  1181. return self.close(handle, function() {
  1182. return callback && callback(er);
  1183. });
  1184. }
  1185. } else {
  1186. eof = false;
  1187. }
  1188. if (eof || (size === 0 && nbytes === 0))
  1189. return close();
  1190. bytesRead += nbytes;
  1191. pos += nbytes;
  1192. if (size !== 0) {
  1193. if (pos === size)
  1194. close();
  1195. else
  1196. read();
  1197. } else {
  1198. // unknown size, just read until we don't get bytes.
  1199. buffers.push(buffer.slice(0, nbytes));
  1200. read();
  1201. }
  1202. }
  1203. afterRead._wantEOFError = true;
  1204. function close() {
  1205. self.close(handle, function(er) {
  1206. if (size === 0) {
  1207. // collected the data into the buffers list.
  1208. buffer = Buffer.concat(buffers, pos);
  1209. } else if (pos < size) {
  1210. buffer = buffer.slice(0, pos);
  1211. }
  1212. if (encoding)
  1213. buffer = buffer.toString(encoding);
  1214. return callback && callback(er, buffer);
  1215. });
  1216. }
  1217. };
  1218. function writeAll(self, handle, buffer, offset, length, position, callback_) {
  1219. var callback = (typeof callback_ === 'function' ? callback_ : undefined);
  1220. self.writeData(handle,
  1221. buffer,
  1222. offset,
  1223. length,
  1224. position,
  1225. function(writeErr, written) {
  1226. if (writeErr) {
  1227. return self.close(handle, function() {
  1228. callback && callback(writeErr);
  1229. });
  1230. }
  1231. if (written === length)
  1232. self.close(handle, callback);
  1233. else {
  1234. offset += written;
  1235. length -= written;
  1236. position += written;
  1237. writeAll(self, handle, buffer, offset, length, position, callback);
  1238. }
  1239. });
  1240. }
  1241. SFTPStream.prototype.writeFile = function(path, data, options, callback_) {
  1242. if (this.server)
  1243. throw new Error('Client-only method called in server mode');
  1244. var callback;
  1245. if (typeof callback_ === 'function') {
  1246. callback = callback_;
  1247. } else if (typeof options === 'function') {
  1248. callback = options;
  1249. options = undefined;
  1250. }
  1251. var self = this;
  1252. if (typeof options === 'string')
  1253. options = { encoding: options, mode: 438, flag: 'w' };
  1254. else if (!options)
  1255. options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
  1256. else if (typeof options !== 'object')
  1257. throw new TypeError('Bad arguments');
  1258. if (options.encoding && !Buffer.isEncoding(options.encoding))
  1259. throw new Error('Unknown encoding: ' + options.encoding);
  1260. var flag = options.flag || 'w';
  1261. this.open(path, flag, options.mode, function(openErr, handle) {
  1262. if (openErr)
  1263. callback && callback(openErr);
  1264. else {
  1265. var buffer = (Buffer.isBuffer(data)
  1266. ? data
  1267. : Buffer.from('' + data, options.encoding || 'utf8'));
  1268. var position = (/a/.test(flag) ? null : 0);
  1269. // SFTPv3 does not support the notion of 'current position'
  1270. // (null position), so we just attempt to append to the end of the file
  1271. // instead
  1272. if (position === null) {
  1273. self.fstat(handle, function tryStat(er, st) {
  1274. if (er) {
  1275. // Try stat() for sftp servers that may not support fstat() for
  1276. // whatever reason
  1277. self.stat(path, function(er_, st_) {
  1278. if (er_) {
  1279. return self.close(handle, function() {
  1280. callback && callback(er);
  1281. });
  1282. }
  1283. tryStat(null, st_);
  1284. });
  1285. return;
  1286. }
  1287. writeAll(self, handle, buffer, 0, buffer.length, st.size, callback);
  1288. });
  1289. return;
  1290. }
  1291. writeAll(self, handle, buffer, 0, buffer.length, position, callback);
  1292. }
  1293. });
  1294. };
  1295. SFTPStream.prototype.appendFile = function(path, data, options, callback_) {
  1296. if (this.server)
  1297. throw new Error('Client-only method called in server mode');
  1298. var callback;
  1299. if (typeof callback_ === 'function') {
  1300. callback = callback_;
  1301. } else if (typeof options === 'function') {
  1302. callback = options;
  1303. options = undefined;
  1304. }
  1305. if (typeof options === 'string')
  1306. options = { encoding: options, mode: 438, flag: 'a' };
  1307. else if (!options)
  1308. options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
  1309. else if (typeof options !== 'object')
  1310. throw new TypeError('Bad arguments');
  1311. if (!options.flag)
  1312. options = util._extend({ flag: 'a' }, options);
  1313. this.writeFile(path, data, options, callback);
  1314. };
  1315. SFTPStream.prototype.exists = function(path, cb) {
  1316. if (this.server)
  1317. throw new Error('Client-only method called in server mode');
  1318. this.stat(path, function(err) {
  1319. cb && cb(err ? false : true);
  1320. });
  1321. };
  1322. SFTPStream.prototype.unlink = function(filename, cb) {
  1323. if (this.server)
  1324. throw new Error('Client-only method called in server mode');
  1325. var state = this._state;
  1326. /*
  1327. uint32 id
  1328. string filename
  1329. */
  1330. var fnamelen = Buffer.byteLength(filename);
  1331. var p = 9;
  1332. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + fnamelen);
  1333. writeUInt32BE(buf, buf.length - 4, 0);
  1334. buf[4] = REQUEST.REMOVE;
  1335. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1336. writeUInt32BE(buf, reqid, 5);
  1337. writeUInt32BE(buf, fnamelen, p);
  1338. buf.write(filename, p += 4, fnamelen, 'utf8');
  1339. state.requests[reqid] = { cb: cb };
  1340. this.debug('DEBUG[SFTP]: Outgoing: Writing REMOVE');
  1341. return this.push(buf);
  1342. };
  1343. SFTPStream.prototype.rename = function(oldPath, newPath, cb) {
  1344. if (this.server)
  1345. throw new Error('Client-only method called in server mode');
  1346. var state = this._state;
  1347. /*
  1348. uint32 id
  1349. string oldpath
  1350. string newpath
  1351. */
  1352. var oldlen = Buffer.byteLength(oldPath);
  1353. var newlen = Buffer.byteLength(newPath);
  1354. var p = 9;
  1355. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + oldlen + 4 + newlen);
  1356. writeUInt32BE(buf, buf.length - 4, 0);
  1357. buf[4] = REQUEST.RENAME;
  1358. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1359. writeUInt32BE(buf, reqid, 5);
  1360. writeUInt32BE(buf, oldlen, p);
  1361. buf.write(oldPath, p += 4, oldlen, 'utf8');
  1362. writeUInt32BE(buf, newlen, p += oldlen);
  1363. buf.write(newPath, p += 4, newlen, 'utf8');
  1364. state.requests[reqid] = { cb: cb };
  1365. this.debug('DEBUG[SFTP]: Outgoing: Writing RENAME');
  1366. return this.push(buf);
  1367. };
  1368. SFTPStream.prototype.mkdir = function(path, attrs, cb) {
  1369. if (this.server)
  1370. throw new Error('Client-only method called in server mode');
  1371. var flags = 0;
  1372. var attrBytes = 0;
  1373. var state = this._state;
  1374. if (typeof attrs === 'function') {
  1375. cb = attrs;
  1376. attrs = undefined;
  1377. }
  1378. if (typeof attrs === 'object') {
  1379. attrs = attrsToBytes(attrs);
  1380. flags = attrs.flags;
  1381. attrBytes = attrs.nbytes;
  1382. attrs = attrs.bytes;
  1383. }
  1384. /*
  1385. uint32 id
  1386. string path
  1387. ATTRS attrs
  1388. */
  1389. var pathlen = Buffer.byteLength(path);
  1390. var p = 9;
  1391. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen + 4 + attrBytes);
  1392. writeUInt32BE(buf, buf.length - 4, 0);
  1393. buf[4] = REQUEST.MKDIR;
  1394. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1395. writeUInt32BE(buf, reqid, 5);
  1396. writeUInt32BE(buf, pathlen, p);
  1397. buf.write(path, p += 4, pathlen, 'utf8');
  1398. writeUInt32BE(buf, flags, p += pathlen);
  1399. if (flags) {
  1400. p += 4;
  1401. for (var i = 0, len = attrs.length; i < len; ++i)
  1402. for (var j = 0, len2 = attrs[i].length; j < len2; ++j)
  1403. buf[p++] = attrs[i][j];
  1404. }
  1405. state.requests[reqid] = { cb: cb };
  1406. this.debug('DEBUG[SFTP]: Outgoing: Writing MKDIR');
  1407. return this.push(buf);
  1408. };
  1409. SFTPStream.prototype.rmdir = function(path, cb) {
  1410. if (this.server)
  1411. throw new Error('Client-only method called in server mode');
  1412. var state = this._state;
  1413. /*
  1414. uint32 id
  1415. string path
  1416. */
  1417. var pathlen = Buffer.byteLength(path);
  1418. var p = 9;
  1419. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1420. writeUInt32BE(buf, buf.length - 4, 0);
  1421. buf[4] = REQUEST.RMDIR;
  1422. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1423. writeUInt32BE(buf, reqid, 5);
  1424. writeUInt32BE(buf, pathlen, p);
  1425. buf.write(path, p += 4, pathlen, 'utf8');
  1426. state.requests[reqid] = { cb: cb };
  1427. this.debug('DEBUG[SFTP]: Outgoing: Writing RMDIR');
  1428. return this.push(buf);
  1429. };
  1430. SFTPStream.prototype.readdir = function(where, opts, cb) {
  1431. if (this.server)
  1432. throw new Error('Client-only method called in server mode');
  1433. var state = this._state;
  1434. var doFilter;
  1435. if (typeof opts === 'function') {
  1436. cb = opts;
  1437. opts = {};
  1438. }
  1439. if (typeof opts !== 'object')
  1440. opts = {};
  1441. doFilter = (opts && opts.full ? false : true);
  1442. if (!Buffer.isBuffer(where) && typeof where !== 'string')
  1443. throw new Error('missing directory handle or path');
  1444. if (typeof where === 'string') {
  1445. var self = this;
  1446. var entries = [];
  1447. var e = 0;
  1448. return this.opendir(where, function reread(err, handle) {
  1449. if (err)
  1450. return cb(err);
  1451. self.readdir(handle, opts, function(err, list) {
  1452. var eof = (err && err.code === STATUS_CODE.EOF);
  1453. if (err && !eof) {
  1454. return self.close(handle, function() {
  1455. cb(err);
  1456. });
  1457. } else if (eof) {
  1458. return self.close(handle, function(err) {
  1459. if (err)
  1460. return cb(err);
  1461. cb(undefined, entries);
  1462. });
  1463. }
  1464. for (var i = 0, len = list.length; i < len; ++i, ++e)
  1465. entries[e] = list[i];
  1466. reread(undefined, handle);
  1467. });
  1468. });
  1469. }
  1470. /*
  1471. uint32 id
  1472. string handle
  1473. */
  1474. var handlelen = where.length;
  1475. var p = 9;
  1476. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen);
  1477. writeUInt32BE(buf, buf.length - 4, 0);
  1478. buf[4] = REQUEST.READDIR;
  1479. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1480. writeUInt32BE(buf, reqid, 5);
  1481. writeUInt32BE(buf, handlelen, p);
  1482. where.copy(buf, p += 4);
  1483. state.requests[reqid] = {
  1484. cb: (doFilter
  1485. ? function(err, list) {
  1486. if (err)
  1487. return cb(err);
  1488. for (var i = list.length - 1; i >= 0; --i) {
  1489. if (list[i].filename === '.' || list[i].filename === '..')
  1490. list.splice(i, 1);
  1491. }
  1492. cb(undefined, list);
  1493. }
  1494. : cb)
  1495. };
  1496. this.debug('DEBUG[SFTP]: Outgoing: Writing READDIR');
  1497. return this.push(buf);
  1498. };
  1499. SFTPStream.prototype.fstat = function(handle, cb) {
  1500. if (this.server)
  1501. throw new Error('Client-only method called in server mode');
  1502. else if (!Buffer.isBuffer(handle))
  1503. throw new Error('handle is not a Buffer');
  1504. var state = this._state;
  1505. /*
  1506. uint32 id
  1507. string handle
  1508. */
  1509. var handlelen = handle.length;
  1510. var p = 9;
  1511. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen);
  1512. writeUInt32BE(buf, buf.length - 4, 0);
  1513. buf[4] = REQUEST.FSTAT;
  1514. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1515. writeUInt32BE(buf, reqid, 5);
  1516. writeUInt32BE(buf, handlelen, p);
  1517. handle.copy(buf, p += 4);
  1518. state.requests[reqid] = { cb: cb };
  1519. this.debug('DEBUG[SFTP]: Outgoing: Writing FSTAT');
  1520. return this.push(buf);
  1521. };
  1522. SFTPStream.prototype.stat = function(path, cb) {
  1523. if (this.server)
  1524. throw new Error('Client-only method called in server mode');
  1525. var state = this._state;
  1526. /*
  1527. uint32 id
  1528. string path
  1529. */
  1530. var pathlen = Buffer.byteLength(path);
  1531. var p = 9;
  1532. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1533. writeUInt32BE(buf, buf.length - 4, 0);
  1534. buf[4] = REQUEST.STAT;
  1535. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1536. writeUInt32BE(buf, reqid, 5);
  1537. writeUInt32BE(buf, pathlen, p);
  1538. buf.write(path, p += 4, pathlen, 'utf8');
  1539. state.requests[reqid] = { cb: cb };
  1540. this.debug('DEBUG[SFTP]: Outgoing: Writing STAT');
  1541. return this.push(buf);
  1542. };
  1543. SFTPStream.prototype.lstat = function(path, cb) {
  1544. if (this.server)
  1545. throw new Error('Client-only method called in server mode');
  1546. var state = this._state;
  1547. /*
  1548. uint32 id
  1549. string path
  1550. */
  1551. var pathlen = Buffer.byteLength(path);
  1552. var p = 9;
  1553. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1554. writeUInt32BE(buf, buf.length - 4, 0);
  1555. buf[4] = REQUEST.LSTAT;
  1556. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1557. writeUInt32BE(buf, reqid, 5);
  1558. writeUInt32BE(buf, pathlen, p);
  1559. buf.write(path, p += 4, pathlen, 'utf8');
  1560. state.requests[reqid] = { cb: cb };
  1561. this.debug('DEBUG[SFTP]: Outgoing: Writing LSTAT');
  1562. return this.push(buf);
  1563. };
  1564. SFTPStream.prototype.opendir = function(path, cb) {
  1565. if (this.server)
  1566. throw new Error('Client-only method called in server mode');
  1567. var state = this._state;
  1568. /*
  1569. uint32 id
  1570. string path
  1571. */
  1572. var pathlen = Buffer.byteLength(path);
  1573. var p = 9;
  1574. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1575. writeUInt32BE(buf, buf.length - 4, 0);
  1576. buf[4] = REQUEST.OPENDIR;
  1577. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1578. writeUInt32BE(buf, reqid, 5);
  1579. writeUInt32BE(buf, pathlen, p);
  1580. buf.write(path, p += 4, pathlen, 'utf8');
  1581. state.requests[reqid] = { cb: cb };
  1582. this.debug('DEBUG[SFTP]: Outgoing: Writing OPENDIR');
  1583. return this.push(buf);
  1584. };
  1585. SFTPStream.prototype.setstat = function(path, attrs, cb) {
  1586. if (this.server)
  1587. throw new Error('Client-only method called in server mode');
  1588. var flags = 0;
  1589. var attrBytes = 0;
  1590. var state = this._state;
  1591. if (typeof attrs === 'object') {
  1592. attrs = attrsToBytes(attrs);
  1593. flags = attrs.flags;
  1594. attrBytes = attrs.nbytes;
  1595. attrs = attrs.bytes;
  1596. } else if (typeof attrs === 'function')
  1597. cb = attrs;
  1598. /*
  1599. uint32 id
  1600. string path
  1601. ATTRS attrs
  1602. */
  1603. var pathlen = Buffer.byteLength(path);
  1604. var p = 9;
  1605. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen + 4 + attrBytes);
  1606. writeUInt32BE(buf, buf.length - 4, 0);
  1607. buf[4] = REQUEST.SETSTAT;
  1608. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1609. writeUInt32BE(buf, reqid, 5);
  1610. writeUInt32BE(buf, pathlen, p);
  1611. buf.write(path, p += 4, pathlen, 'utf8');
  1612. writeUInt32BE(buf, flags, p += pathlen);
  1613. if (flags) {
  1614. p += 4;
  1615. for (var i = 0, len = attrs.length; i < len; ++i)
  1616. for (var j = 0, len2 = attrs[i].length; j < len2; ++j)
  1617. buf[p++] = attrs[i][j];
  1618. }
  1619. state.requests[reqid] = { cb: cb };
  1620. this.debug('DEBUG[SFTP]: Outgoing: Writing SETSTAT');
  1621. return this.push(buf);
  1622. };
  1623. SFTPStream.prototype.fsetstat = function(handle, attrs, cb) {
  1624. if (this.server)
  1625. throw new Error('Client-only method called in server mode');
  1626. else if (!Buffer.isBuffer(handle))
  1627. throw new Error('handle is not a Buffer');
  1628. var flags = 0;
  1629. var attrBytes = 0;
  1630. var state = this._state;
  1631. if (typeof attrs === 'object') {
  1632. attrs = attrsToBytes(attrs);
  1633. flags = attrs.flags;
  1634. attrBytes = attrs.nbytes;
  1635. attrs = attrs.bytes;
  1636. } else if (typeof attrs === 'function')
  1637. cb = attrs;
  1638. /*
  1639. uint32 id
  1640. string handle
  1641. ATTRS attrs
  1642. */
  1643. var handlelen = handle.length;
  1644. var p = 9;
  1645. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handlelen + 4 + attrBytes);
  1646. writeUInt32BE(buf, buf.length - 4, 0);
  1647. buf[4] = REQUEST.FSETSTAT;
  1648. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1649. writeUInt32BE(buf, reqid, 5);
  1650. writeUInt32BE(buf, handlelen, p);
  1651. handle.copy(buf, p += 4);
  1652. writeUInt32BE(buf, flags, p += handlelen);
  1653. if (flags) {
  1654. p += 4;
  1655. for (var i = 0, len = attrs.length; i < len; ++i)
  1656. for (var j = 0, len2 = attrs[i].length; j < len2; ++j)
  1657. buf[p++] = attrs[i][j];
  1658. }
  1659. state.requests[reqid] = { cb: cb };
  1660. this.debug('DEBUG[SFTP]: Outgoing: Writing FSETSTAT');
  1661. return this.push(buf);
  1662. };
  1663. SFTPStream.prototype.futimes = function(handle, atime, mtime, cb) {
  1664. return this.fsetstat(handle, {
  1665. atime: toUnixTimestamp(atime),
  1666. mtime: toUnixTimestamp(mtime)
  1667. }, cb);
  1668. };
  1669. SFTPStream.prototype.utimes = function(path, atime, mtime, cb) {
  1670. return this.setstat(path, {
  1671. atime: toUnixTimestamp(atime),
  1672. mtime: toUnixTimestamp(mtime)
  1673. }, cb);
  1674. };
  1675. SFTPStream.prototype.fchown = function(handle, uid, gid, cb) {
  1676. return this.fsetstat(handle, {
  1677. uid: uid,
  1678. gid: gid
  1679. }, cb);
  1680. };
  1681. SFTPStream.prototype.chown = function(path, uid, gid, cb) {
  1682. return this.setstat(path, {
  1683. uid: uid,
  1684. gid: gid
  1685. }, cb);
  1686. };
  1687. SFTPStream.prototype.fchmod = function(handle, mode, cb) {
  1688. return this.fsetstat(handle, {
  1689. mode: mode
  1690. }, cb);
  1691. };
  1692. SFTPStream.prototype.chmod = function(path, mode, cb) {
  1693. return this.setstat(path, {
  1694. mode: mode
  1695. }, cb);
  1696. };
  1697. SFTPStream.prototype.readlink = function(path, cb) {
  1698. if (this.server)
  1699. throw new Error('Client-only method called in server mode');
  1700. var state = this._state;
  1701. /*
  1702. uint32 id
  1703. string path
  1704. */
  1705. var pathlen = Buffer.byteLength(path);
  1706. var p = 9;
  1707. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1708. writeUInt32BE(buf, buf.length - 4, 0);
  1709. buf[4] = REQUEST.READLINK;
  1710. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1711. writeUInt32BE(buf, reqid, 5);
  1712. writeUInt32BE(buf, pathlen, p);
  1713. buf.write(path, p += 4, pathlen, 'utf8');
  1714. state.requests[reqid] = {
  1715. cb: function(err, names) {
  1716. if (err)
  1717. return cb(err);
  1718. else if (!names || !names.length)
  1719. return cb(new Error('Response missing link info'));
  1720. cb(undefined, names[0].filename);
  1721. }
  1722. };
  1723. this.debug('DEBUG[SFTP]: Outgoing: Writing READLINK');
  1724. return this.push(buf);
  1725. };
  1726. SFTPStream.prototype.symlink = function(targetPath, linkPath, cb) {
  1727. if (this.server)
  1728. throw new Error('Client-only method called in server mode');
  1729. var state = this._state;
  1730. /*
  1731. uint32 id
  1732. string linkpath
  1733. string targetpath
  1734. */
  1735. var linklen = Buffer.byteLength(linkPath);
  1736. var targetlen = Buffer.byteLength(targetPath);
  1737. var p = 9;
  1738. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + linklen + 4 + targetlen);
  1739. writeUInt32BE(buf, buf.length - 4, 0);
  1740. buf[4] = REQUEST.SYMLINK;
  1741. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1742. writeUInt32BE(buf, reqid, 5);
  1743. if (this._isOpenSSH) {
  1744. // OpenSSH has linkpath and targetpath positions switched
  1745. writeUInt32BE(buf, targetlen, p);
  1746. buf.write(targetPath, p += 4, targetlen, 'utf8');
  1747. writeUInt32BE(buf, linklen, p += targetlen);
  1748. buf.write(linkPath, p += 4, linklen, 'utf8');
  1749. } else {
  1750. writeUInt32BE(buf, linklen, p);
  1751. buf.write(linkPath, p += 4, linklen, 'utf8');
  1752. writeUInt32BE(buf, targetlen, p += linklen);
  1753. buf.write(targetPath, p += 4, targetlen, 'utf8');
  1754. }
  1755. state.requests[reqid] = { cb: cb };
  1756. this.debug('DEBUG[SFTP]: Outgoing: Writing SYMLINK');
  1757. return this.push(buf);
  1758. };
  1759. SFTPStream.prototype.realpath = function(path, cb) {
  1760. if (this.server)
  1761. throw new Error('Client-only method called in server mode');
  1762. var state = this._state;
  1763. /*
  1764. uint32 id
  1765. string path
  1766. */
  1767. var pathlen = Buffer.byteLength(path);
  1768. var p = 9;
  1769. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathlen);
  1770. writeUInt32BE(buf, buf.length - 4, 0);
  1771. buf[4] = REQUEST.REALPATH;
  1772. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1773. writeUInt32BE(buf, reqid, 5);
  1774. writeUInt32BE(buf, pathlen, p);
  1775. buf.write(path, p += 4, pathlen, 'utf8');
  1776. state.requests[reqid] = {
  1777. cb: function(err, names) {
  1778. if (err)
  1779. return cb(err);
  1780. else if (!names || !names.length)
  1781. return cb(new Error('Response missing path info'));
  1782. cb(undefined, names[0].filename);
  1783. }
  1784. };
  1785. this.debug('DEBUG[SFTP]: Outgoing: Writing REALPATH');
  1786. return this.push(buf);
  1787. };
  1788. // extended requests
  1789. SFTPStream.prototype.ext_openssh_rename = function(oldPath, newPath, cb) {
  1790. var state = this._state;
  1791. if (this.server)
  1792. throw new Error('Client-only method called in server mode');
  1793. else if (!state.extensions['posix-rename@openssh.com']
  1794. || state.extensions['posix-rename@openssh.com'].indexOf('1') === -1)
  1795. throw new Error('Server does not support this extended request');
  1796. /*
  1797. uint32 id
  1798. string "posix-rename@openssh.com"
  1799. string oldpath
  1800. string newpath
  1801. */
  1802. var oldlen = Buffer.byteLength(oldPath);
  1803. var newlen = Buffer.byteLength(newPath);
  1804. var p = 9;
  1805. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 24 + 4 + oldlen + 4 + newlen);
  1806. writeUInt32BE(buf, buf.length - 4, 0);
  1807. buf[4] = REQUEST.EXTENDED;
  1808. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1809. writeUInt32BE(buf, reqid, 5);
  1810. writeUInt32BE(buf, 24, p);
  1811. buf.write('posix-rename@openssh.com', p += 4, 24, 'ascii');
  1812. writeUInt32BE(buf, oldlen, p += 24);
  1813. buf.write(oldPath, p += 4, oldlen, 'utf8');
  1814. writeUInt32BE(buf, newlen, p += oldlen);
  1815. buf.write(newPath, p += 4, newlen, 'utf8');
  1816. state.requests[reqid] = { cb: cb };
  1817. this.debug('DEBUG[SFTP]: Outgoing: Writing posix-rename@openssh.com');
  1818. return this.push(buf);
  1819. };
  1820. SFTPStream.prototype.ext_openssh_statvfs = function(path, cb) {
  1821. var state = this._state;
  1822. if (this.server)
  1823. throw new Error('Client-only method called in server mode');
  1824. else if (!state.extensions['statvfs@openssh.com']
  1825. || state.extensions['statvfs@openssh.com'].indexOf('2') === -1)
  1826. throw new Error('Server does not support this extended request');
  1827. /*
  1828. uint32 id
  1829. string "statvfs@openssh.com"
  1830. string path
  1831. */
  1832. var pathlen = Buffer.byteLength(path);
  1833. var p = 9;
  1834. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 19 + 4 + pathlen);
  1835. writeUInt32BE(buf, buf.length - 4, 0);
  1836. buf[4] = REQUEST.EXTENDED;
  1837. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1838. writeUInt32BE(buf, reqid, 5);
  1839. writeUInt32BE(buf, 19, p);
  1840. buf.write('statvfs@openssh.com', p += 4, 19, 'ascii');
  1841. writeUInt32BE(buf, pathlen, p += 19);
  1842. buf.write(path, p += 4, pathlen, 'utf8');
  1843. state.requests[reqid] = {
  1844. extended: 'statvfs@openssh.com',
  1845. cb: cb
  1846. };
  1847. this.debug('DEBUG[SFTP]: Outgoing: Writing statvfs@openssh.com');
  1848. return this.push(buf);
  1849. };
  1850. SFTPStream.prototype.ext_openssh_fstatvfs = function(handle, cb) {
  1851. var state = this._state;
  1852. if (this.server)
  1853. throw new Error('Client-only method called in server mode');
  1854. else if (!state.extensions['fstatvfs@openssh.com']
  1855. || state.extensions['fstatvfs@openssh.com'].indexOf('2') === -1)
  1856. throw new Error('Server does not support this extended request');
  1857. else if (!Buffer.isBuffer(handle))
  1858. throw new Error('handle is not a Buffer');
  1859. /*
  1860. uint32 id
  1861. string "fstatvfs@openssh.com"
  1862. string handle
  1863. */
  1864. var handlelen = handle.length;
  1865. var p = 9;
  1866. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 20 + 4 + handlelen);
  1867. writeUInt32BE(buf, buf.length - 4, 0);
  1868. buf[4] = REQUEST.EXTENDED;
  1869. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1870. writeUInt32BE(buf, reqid, 5);
  1871. writeUInt32BE(buf, 20, p);
  1872. buf.write('fstatvfs@openssh.com', p += 4, 20, 'ascii');
  1873. writeUInt32BE(buf, handlelen, p += 20);
  1874. buf.write(handle, p += 4, handlelen, 'utf8');
  1875. state.requests[reqid] = {
  1876. extended: 'fstatvfs@openssh.com',
  1877. cb: cb
  1878. };
  1879. this.debug('DEBUG[SFTP]: Outgoing: Writing fstatvfs@openssh.com');
  1880. return this.push(buf);
  1881. };
  1882. SFTPStream.prototype.ext_openssh_hardlink = function(oldPath, newPath, cb) {
  1883. var state = this._state;
  1884. if (this.server)
  1885. throw new Error('Client-only method called in server mode');
  1886. else if (!state.extensions['hardlink@openssh.com']
  1887. || state.extensions['hardlink@openssh.com'].indexOf('1') === -1)
  1888. throw new Error('Server does not support this extended request');
  1889. /*
  1890. uint32 id
  1891. string "hardlink@openssh.com"
  1892. string oldpath
  1893. string newpath
  1894. */
  1895. var oldlen = Buffer.byteLength(oldPath);
  1896. var newlen = Buffer.byteLength(newPath);
  1897. var p = 9;
  1898. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 20 + 4 + oldlen + 4 + newlen);
  1899. writeUInt32BE(buf, buf.length - 4, 0);
  1900. buf[4] = REQUEST.EXTENDED;
  1901. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1902. writeUInt32BE(buf, reqid, 5);
  1903. writeUInt32BE(buf, 20, p);
  1904. buf.write('hardlink@openssh.com', p += 4, 20, 'ascii');
  1905. writeUInt32BE(buf, oldlen, p += 20);
  1906. buf.write(oldPath, p += 4, oldlen, 'utf8');
  1907. writeUInt32BE(buf, newlen, p += oldlen);
  1908. buf.write(newPath, p += 4, newlen, 'utf8');
  1909. state.requests[reqid] = { cb: cb };
  1910. this.debug('DEBUG[SFTP]: Outgoing: Writing hardlink@openssh.com');
  1911. return this.push(buf);
  1912. };
  1913. SFTPStream.prototype.ext_openssh_fsync = function(handle, cb) {
  1914. var state = this._state;
  1915. if (this.server)
  1916. throw new Error('Client-only method called in server mode');
  1917. else if (!state.extensions['fsync@openssh.com']
  1918. || state.extensions['fsync@openssh.com'].indexOf('1') === -1)
  1919. throw new Error('Server does not support this extended request');
  1920. else if (!Buffer.isBuffer(handle))
  1921. throw new Error('handle is not a Buffer');
  1922. /*
  1923. uint32 id
  1924. string "fsync@openssh.com"
  1925. string handle
  1926. */
  1927. var handlelen = handle.length;
  1928. var p = 9;
  1929. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 17 + 4 + handlelen);
  1930. writeUInt32BE(buf, buf.length - 4, 0);
  1931. buf[4] = REQUEST.EXTENDED;
  1932. var reqid = state.writeReqid = (state.writeReqid + 1) % MAX_REQID;
  1933. writeUInt32BE(buf, reqid, 5);
  1934. writeUInt32BE(buf, 17, p);
  1935. buf.write('fsync@openssh.com', p += 4, 17, 'ascii');
  1936. writeUInt32BE(buf, handlelen, p += 17);
  1937. buf.write(handle, p += 4, handlelen, 'utf8');
  1938. state.requests[reqid] = { cb: cb };
  1939. this.debug('DEBUG[SFTP]: Outgoing: Writing fsync@openssh.com');
  1940. return this.push(buf);
  1941. };
  1942. // server
  1943. SFTPStream.prototype.status = function(id, code, message, lang) {
  1944. if (!this.server)
  1945. throw new Error('Server-only method called in client mode');
  1946. if (!STATUS_CODE[code] || typeof code !== 'number')
  1947. throw new Error('Bad status code: ' + code);
  1948. message || (message = '');
  1949. lang || (lang = '');
  1950. var msgLen = Buffer.byteLength(message);
  1951. var langLen = Buffer.byteLength(lang);
  1952. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 4 + msgLen + 4 + langLen);
  1953. writeUInt32BE(buf, buf.length - 4, 0);
  1954. buf[4] = RESPONSE.STATUS;
  1955. writeUInt32BE(buf, id, 5);
  1956. writeUInt32BE(buf, code, 9);
  1957. writeUInt32BE(buf, msgLen, 13);
  1958. if (msgLen)
  1959. buf.write(message, 17, msgLen, 'utf8');
  1960. writeUInt32BE(buf, langLen, 17 + msgLen);
  1961. if (langLen)
  1962. buf.write(lang, 17 + msgLen + 4, langLen, 'ascii');
  1963. this.debug('DEBUG[SFTP]: Outgoing: Writing STATUS');
  1964. return this.push(buf);
  1965. };
  1966. SFTPStream.prototype.handle = function(id, handle) {
  1967. if (!this.server)
  1968. throw new Error('Server-only method called in client mode');
  1969. if (!Buffer.isBuffer(handle))
  1970. throw new Error('handle is not a Buffer');
  1971. var handleLen = handle.length;
  1972. if (handleLen > 256)
  1973. throw new Error('handle too large (> 256 bytes)');
  1974. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handleLen);
  1975. writeUInt32BE(buf, buf.length - 4, 0);
  1976. buf[4] = RESPONSE.HANDLE;
  1977. writeUInt32BE(buf, id, 5);
  1978. writeUInt32BE(buf, handleLen, 9);
  1979. if (handleLen)
  1980. handle.copy(buf, 13);
  1981. this.debug('DEBUG[SFTP]: Outgoing: Writing HANDLE');
  1982. return this.push(buf);
  1983. };
  1984. SFTPStream.prototype.data = function(id, data, encoding) {
  1985. if (!this.server)
  1986. throw new Error('Server-only method called in client mode');
  1987. var isBuffer = Buffer.isBuffer(data);
  1988. if (!isBuffer && typeof data !== 'string')
  1989. throw new Error('data is not a Buffer or string');
  1990. if (!isBuffer)
  1991. encoding || (encoding = 'utf8');
  1992. var dataLen = (isBuffer ? data.length : Buffer.byteLength(data, encoding));
  1993. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + dataLen);
  1994. writeUInt32BE(buf, buf.length - 4, 0);
  1995. buf[4] = RESPONSE.DATA;
  1996. writeUInt32BE(buf, id, 5);
  1997. writeUInt32BE(buf, dataLen, 9);
  1998. if (dataLen) {
  1999. if (isBuffer)
  2000. data.copy(buf, 13);
  2001. else
  2002. buf.write(data, 13, dataLen, encoding);
  2003. }
  2004. this.debug('DEBUG[SFTP]: Outgoing: Writing DATA');
  2005. return this.push(buf);
  2006. };
  2007. SFTPStream.prototype.name = function(id, names) {
  2008. if (!this.server)
  2009. throw new Error('Server-only method called in client mode');
  2010. if (!Array.isArray(names) && typeof names === 'object')
  2011. names = [ names ];
  2012. else if (!Array.isArray(names))
  2013. throw new Error('names is not an object or array');
  2014. var count = names.length;
  2015. var namesLen = 0;
  2016. var nameAttrs;
  2017. var attrs = [];
  2018. var name;
  2019. var filename;
  2020. var longname;
  2021. var attr;
  2022. var len;
  2023. var len2;
  2024. var buf;
  2025. var p;
  2026. var i;
  2027. var j;
  2028. var k;
  2029. for (i = 0; i < count; ++i) {
  2030. name = names[i];
  2031. filename = (!name || !name.filename || typeof name.filename !== 'string'
  2032. ? ''
  2033. : name.filename);
  2034. namesLen += 4 + Buffer.byteLength(filename);
  2035. longname = (!name || !name.longname || typeof name.longname !== 'string'
  2036. ? ''
  2037. : name.longname);
  2038. namesLen += 4 + Buffer.byteLength(longname);
  2039. if (typeof name.attrs === 'object') {
  2040. nameAttrs = attrsToBytes(name.attrs);
  2041. namesLen += 4 + nameAttrs.nbytes;
  2042. attrs.push(nameAttrs);
  2043. } else {
  2044. namesLen += 4;
  2045. attrs.push(null);
  2046. }
  2047. }
  2048. buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + namesLen);
  2049. writeUInt32BE(buf, buf.length - 4, 0);
  2050. buf[4] = RESPONSE.NAME;
  2051. writeUInt32BE(buf, id, 5);
  2052. writeUInt32BE(buf, count, 9);
  2053. p = 13;
  2054. for (i = 0; i < count; ++i) {
  2055. name = names[i];
  2056. filename = (!name || !name.filename || typeof name.filename !== 'string'
  2057. ? ''
  2058. : name.filename);
  2059. len = Buffer.byteLength(filename);
  2060. writeUInt32BE(buf, len, p);
  2061. p += 4;
  2062. if (len) {
  2063. buf.write(filename, p, len, 'utf8');
  2064. p += len;
  2065. }
  2066. longname = (!name || !name.longname || typeof name.longname !== 'string'
  2067. ? ''
  2068. : name.longname);
  2069. len = Buffer.byteLength(longname);
  2070. writeUInt32BE(buf, len, p);
  2071. p += 4;
  2072. if (len) {
  2073. buf.write(longname, p, len, 'utf8');
  2074. p += len;
  2075. }
  2076. attr = attrs[i];
  2077. if (attr) {
  2078. writeUInt32BE(buf, attr.flags, p);
  2079. p += 4;
  2080. if (attr.flags && attr.bytes) {
  2081. var bytes = attr.bytes;
  2082. for (j = 0, len = bytes.length; j < len; ++j)
  2083. for (k = 0, len2 = bytes[j].length; k < len2; ++k)
  2084. buf[p++] = bytes[j][k];
  2085. }
  2086. } else {
  2087. writeUInt32BE(buf, 0, p);
  2088. p += 4;
  2089. }
  2090. }
  2091. this.debug('DEBUG[SFTP]: Outgoing: Writing NAME');
  2092. return this.push(buf);
  2093. };
  2094. SFTPStream.prototype.attrs = function(id, attrs) {
  2095. if (!this.server)
  2096. throw new Error('Server-only method called in client mode');
  2097. if (typeof attrs !== 'object')
  2098. throw new Error('attrs is not an object');
  2099. var info = attrsToBytes(attrs);
  2100. var buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + info.nbytes);
  2101. var p = 13;
  2102. writeUInt32BE(buf, buf.length - 4, 0);
  2103. buf[4] = RESPONSE.ATTRS;
  2104. writeUInt32BE(buf, id, 5);
  2105. writeUInt32BE(buf, info.flags, 9);
  2106. if (info.flags && info.bytes) {
  2107. var bytes = info.bytes;
  2108. for (var j = 0, len = bytes.length; j < len; ++j)
  2109. for (var k = 0, len2 = bytes[j].length; k < len2; ++k)
  2110. buf[p++] = bytes[j][k];
  2111. }
  2112. this.debug('DEBUG[SFTP]: Outgoing: Writing ATTRS');
  2113. return this.push(buf);
  2114. };
  2115. function readAttrs(buf, p, stream, callback) {
  2116. /*
  2117. uint32 flags
  2118. uint64 size present only if flag SSH_FILEXFER_ATTR_SIZE
  2119. uint32 uid present only if flag SSH_FILEXFER_ATTR_UIDGID
  2120. uint32 gid present only if flag SSH_FILEXFER_ATTR_UIDGID
  2121. uint32 permissions present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
  2122. uint32 atime present only if flag SSH_FILEXFER_ACMODTIME
  2123. uint32 mtime present only if flag SSH_FILEXFER_ACMODTIME
  2124. uint32 extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
  2125. string extended_type
  2126. string extended_data
  2127. ... more extended data (extended_type - extended_data pairs),
  2128. so that number of pairs equals extended_count
  2129. */
  2130. var flags = readUInt32BE(buf, p);
  2131. var attrs = new Stats();
  2132. p += 4;
  2133. if (flags & ATTR.SIZE) {
  2134. var size = readUInt64BE(buf, p, stream, callback);
  2135. if (size === false)
  2136. return false;
  2137. attrs.size = size;
  2138. p += 8;
  2139. }
  2140. if (flags & ATTR.UIDGID) {
  2141. var uid;
  2142. var gid;
  2143. uid = readInt(buf, p, this, callback);
  2144. if (uid === false)
  2145. return false;
  2146. attrs.uid = uid;
  2147. p += 4;
  2148. gid = readInt(buf, p, this, callback);
  2149. if (gid === false)
  2150. return false;
  2151. attrs.gid = gid;
  2152. p += 4;
  2153. }
  2154. if (flags & ATTR.PERMISSIONS) {
  2155. var mode = readInt(buf, p, this, callback);
  2156. if (mode === false)
  2157. return false;
  2158. attrs.mode = mode;
  2159. // backwards compatibility
  2160. attrs.permissions = mode;
  2161. p += 4;
  2162. }
  2163. if (flags & ATTR.ACMODTIME) {
  2164. var atime;
  2165. var mtime;
  2166. atime = readInt(buf, p, this, callback);
  2167. if (atime === false)
  2168. return false;
  2169. attrs.atime = atime;
  2170. p += 4;
  2171. mtime = readInt(buf, p, this, callback);
  2172. if (mtime === false)
  2173. return false;
  2174. attrs.mtime = mtime;
  2175. p += 4;
  2176. }
  2177. if (flags & ATTR.EXTENDED) {
  2178. // TODO: read/parse extended data
  2179. var extcount = readInt(buf, p, this, callback);
  2180. if (extcount === false)
  2181. return false;
  2182. p += 4;
  2183. for (var i = 0, len; i < extcount; ++i) {
  2184. len = readInt(buf, p, this, callback);
  2185. if (len === false)
  2186. return false;
  2187. p += 4 + len;
  2188. }
  2189. }
  2190. buf._pos = p;
  2191. return attrs;
  2192. }
  2193. function readUInt64BE(buffer, p, stream, callback) {
  2194. if ((buffer.length - p) < 8) {
  2195. stream && stream._cleanup(callback);
  2196. return false;
  2197. }
  2198. var val = 0;
  2199. for (var len = p + 8; p < len; ++p) {
  2200. val *= 256;
  2201. val += buffer[p];
  2202. }
  2203. buffer._pos = p;
  2204. return val;
  2205. }
  2206. function attrsToBytes(attrs) {
  2207. var flags = 0;
  2208. var attrBytes = 0;
  2209. var ret = [];
  2210. var i = 0;
  2211. if (typeof attrs.size === 'number') {
  2212. flags |= ATTR.SIZE;
  2213. attrBytes += 8;
  2214. var sizeBytes = new Array(8);
  2215. var val = attrs.size;
  2216. for (i = 7; i >= 0; --i) {
  2217. sizeBytes[i] = val & 0xFF;
  2218. val /= 256;
  2219. }
  2220. ret.push(sizeBytes);
  2221. }
  2222. if (typeof attrs.uid === 'number' && typeof attrs.gid === 'number') {
  2223. flags |= ATTR.UIDGID;
  2224. attrBytes += 8;
  2225. ret.push([(attrs.uid >> 24) & 0xFF, (attrs.uid >> 16) & 0xFF,
  2226. (attrs.uid >> 8) & 0xFF, attrs.uid & 0xFF]);
  2227. ret.push([(attrs.gid >> 24) & 0xFF, (attrs.gid >> 16) & 0xFF,
  2228. (attrs.gid >> 8) & 0xFF, attrs.gid & 0xFF]);
  2229. }
  2230. if (typeof attrs.permissions === 'number'
  2231. || typeof attrs.permissions === 'string'
  2232. || typeof attrs.mode === 'number'
  2233. || typeof attrs.mode === 'string') {
  2234. var mode = modeNum(attrs.mode || attrs.permissions);
  2235. flags |= ATTR.PERMISSIONS;
  2236. attrBytes += 4;
  2237. ret.push([(mode >> 24) & 0xFF,
  2238. (mode >> 16) & 0xFF,
  2239. (mode >> 8) & 0xFF,
  2240. mode & 0xFF]);
  2241. }
  2242. if ((typeof attrs.atime === 'number' || isDate(attrs.atime))
  2243. && (typeof attrs.mtime === 'number' || isDate(attrs.mtime))) {
  2244. var atime = toUnixTimestamp(attrs.atime);
  2245. var mtime = toUnixTimestamp(attrs.mtime);
  2246. flags |= ATTR.ACMODTIME;
  2247. attrBytes += 8;
  2248. ret.push([(atime >> 24) & 0xFF, (atime >> 16) & 0xFF,
  2249. (atime >> 8) & 0xFF, atime & 0xFF]);
  2250. ret.push([(mtime >> 24) & 0xFF, (mtime >> 16) & 0xFF,
  2251. (mtime >> 8) & 0xFF, mtime & 0xFF]);
  2252. }
  2253. // TODO: extended attributes
  2254. return { flags: flags, nbytes: attrBytes, bytes: ret };
  2255. }
  2256. function toUnixTimestamp(time) {
  2257. if (typeof time === 'number' && !isNaN(time))
  2258. return time;
  2259. else if (isDate(time))
  2260. return parseInt(time.getTime() / 1000, 10);
  2261. throw new Error('Cannot parse time: ' + time);
  2262. }
  2263. function modeNum(mode) {
  2264. if (typeof mode === 'number' && !isNaN(mode))
  2265. return mode;
  2266. else if (typeof mode === 'string')
  2267. return modeNum(parseInt(mode, 8));
  2268. throw new Error('Cannot parse mode: ' + mode);
  2269. }
  2270. var stringFlagMap = {
  2271. 'r': OPEN_MODE.READ,
  2272. 'r+': OPEN_MODE.READ | OPEN_MODE.WRITE,
  2273. 'w': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE,
  2274. 'wx': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE | OPEN_MODE.EXCL,
  2275. 'xw': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE | OPEN_MODE.EXCL,
  2276. 'w+': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE,
  2277. 'wx+': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE
  2278. | OPEN_MODE.EXCL,
  2279. 'xw+': OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE
  2280. | OPEN_MODE.EXCL,
  2281. 'a': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.WRITE,
  2282. 'ax': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.WRITE | OPEN_MODE.EXCL,
  2283. 'xa': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.WRITE | OPEN_MODE.EXCL,
  2284. 'a+': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE,
  2285. 'ax+': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE
  2286. | OPEN_MODE.EXCL,
  2287. 'xa+': OPEN_MODE.APPEND | OPEN_MODE.CREAT | OPEN_MODE.READ | OPEN_MODE.WRITE
  2288. | OPEN_MODE.EXCL
  2289. };
  2290. var stringFlagMapKeys = Object.keys(stringFlagMap);
  2291. function stringToFlags(str) {
  2292. var flags = stringFlagMap[str];
  2293. if (flags !== undefined)
  2294. return flags;
  2295. return null;
  2296. }
  2297. SFTPStream.stringToFlags = stringToFlags;
  2298. function flagsToString(flags) {
  2299. for (var i = 0; i < stringFlagMapKeys.length; ++i) {
  2300. var key = stringFlagMapKeys[i];
  2301. if (stringFlagMap[key] === flags)
  2302. return key;
  2303. }
  2304. return null;
  2305. }
  2306. SFTPStream.flagsToString = flagsToString;
  2307. function Stats(initial) {
  2308. this.mode = (initial && initial.mode);
  2309. this.permissions = this.mode; // backwards compatiblity
  2310. this.uid = (initial && initial.uid);
  2311. this.gid = (initial && initial.gid);
  2312. this.size = (initial && initial.size);
  2313. this.atime = (initial && initial.atime);
  2314. this.mtime = (initial && initial.mtime);
  2315. }
  2316. Stats.prototype._checkModeProperty = function(property) {
  2317. return ((this.mode & constants.S_IFMT) === property);
  2318. };
  2319. Stats.prototype.isDirectory = function() {
  2320. return this._checkModeProperty(constants.S_IFDIR);
  2321. };
  2322. Stats.prototype.isFile = function() {
  2323. return this._checkModeProperty(constants.S_IFREG);
  2324. };
  2325. Stats.prototype.isBlockDevice = function() {
  2326. return this._checkModeProperty(constants.S_IFBLK);
  2327. };
  2328. Stats.prototype.isCharacterDevice = function() {
  2329. return this._checkModeProperty(constants.S_IFCHR);
  2330. };
  2331. Stats.prototype.isSymbolicLink = function() {
  2332. return this._checkModeProperty(constants.S_IFLNK);
  2333. };
  2334. Stats.prototype.isFIFO = function() {
  2335. return this._checkModeProperty(constants.S_IFIFO);
  2336. };
  2337. Stats.prototype.isSocket = function() {
  2338. return this._checkModeProperty(constants.S_IFSOCK);
  2339. };
  2340. SFTPStream.Stats = Stats;
  2341. // ReadStream-related
  2342. var kMinPoolSpace = 128;
  2343. var pool;
  2344. function allocNewPool(poolSize) {
  2345. pool = Buffer.allocUnsafe(poolSize);
  2346. pool.used = 0;
  2347. }
  2348. function ReadStream(sftp, path, options) {
  2349. if (!(this instanceof ReadStream))
  2350. return new ReadStream(sftp, path, options);
  2351. var self = this;
  2352. if (options === undefined)
  2353. options = {};
  2354. else if (typeof options === 'string')
  2355. options = { encoding: options };
  2356. else if (options === null || typeof options !== 'object')
  2357. throw new TypeError('"options" argument must be a string or an object');
  2358. else
  2359. options = Object.create(options);
  2360. // a little bit bigger buffer and water marks by default
  2361. if (options.highWaterMark === undefined)
  2362. options.highWaterMark = 64 * 1024;
  2363. ReadableStream.call(this, options);
  2364. this.path = path;
  2365. this.handle = options.handle === undefined ? null : options.handle;
  2366. this.flags = options.flags === undefined ? 'r' : options.flags;
  2367. this.mode = options.mode === undefined ? 438/*0666*/ : options.mode;
  2368. this.start = options.start === undefined ? undefined : options.start;
  2369. this.end = options.end === undefined ? undefined : options.end;
  2370. this.autoClose = options.autoClose === undefined ? true : options.autoClose;
  2371. this.pos = 0;
  2372. this.sftp = sftp;
  2373. if (this.start !== undefined) {
  2374. if (typeof this.start !== 'number')
  2375. throw new TypeError('start must be a Number');
  2376. if (this.end === undefined)
  2377. this.end = Infinity;
  2378. else if (typeof this.end !== 'number')
  2379. throw new TypeError('end must be a Number');
  2380. if (this.start > this.end)
  2381. throw new Error('start must be <= end');
  2382. else if (this.start < 0)
  2383. throw new Error('start must be >= zero');
  2384. this.pos = this.start;
  2385. }
  2386. this.on('end', function() {
  2387. if (self.autoClose) {
  2388. self.destroy();
  2389. }
  2390. });
  2391. if (!Buffer.isBuffer(this.handle))
  2392. this.open();
  2393. }
  2394. inherits(ReadStream, ReadableStream);
  2395. ReadStream.prototype.open = function() {
  2396. var self = this;
  2397. this.sftp.open(this.path, this.flags, this.mode, function(er, handle) {
  2398. if (er) {
  2399. self.emit('error', er);
  2400. this.destroyed = this.closed = true;
  2401. self.emit('close');
  2402. return;
  2403. }
  2404. self.handle = handle;
  2405. self.emit('open', handle);
  2406. // start the flow of data.
  2407. self.read();
  2408. });
  2409. };
  2410. ReadStream.prototype._read = function(n) {
  2411. if (!Buffer.isBuffer(this.handle)) {
  2412. return this.once('open', function() {
  2413. this._read(n);
  2414. });
  2415. }
  2416. if (this.destroyed)
  2417. return;
  2418. if (!pool || pool.length - pool.used < kMinPoolSpace) {
  2419. // discard the old pool.
  2420. pool = null;
  2421. allocNewPool(this._readableState.highWaterMark);
  2422. }
  2423. // Grab another reference to the pool in the case that while we're
  2424. // in the thread pool another read() finishes up the pool, and
  2425. // allocates a new one.
  2426. var thisPool = pool;
  2427. var toRead = Math.min(pool.length - pool.used, n);
  2428. var start = pool.used;
  2429. if (this.end !== undefined)
  2430. toRead = Math.min(this.end - this.pos + 1, toRead);
  2431. // already read everything we were supposed to read!
  2432. // treat as EOF.
  2433. if (toRead <= 0)
  2434. return this.push(null);
  2435. // the actual read.
  2436. var self = this;
  2437. this.sftp.readData(this.handle, pool, pool.used, toRead, this.pos, onread);
  2438. // move the pool positions, and internal position for reading.
  2439. this.pos += toRead;
  2440. pool.used += toRead;
  2441. function onread(er, bytesRead) {
  2442. if (er) {
  2443. if (self.autoClose)
  2444. self.destroy();
  2445. self.emit('error', er);
  2446. } else {
  2447. var b = null;
  2448. if (bytesRead > 0)
  2449. b = thisPool.slice(start, start + bytesRead);
  2450. self.push(b);
  2451. }
  2452. }
  2453. };
  2454. ReadStream.prototype.destroy = function() {
  2455. if (this.destroyed)
  2456. return;
  2457. this.destroyed = true;
  2458. if (Buffer.isBuffer(this.handle))
  2459. this.close();
  2460. };
  2461. ReadStream.prototype.close = function(cb) {
  2462. var self = this;
  2463. if (cb)
  2464. this.once('close', cb);
  2465. if (this.closed || !Buffer.isBuffer(this.handle)) {
  2466. if (!Buffer.isBuffer(this.handle)) {
  2467. this.once('open', close);
  2468. return;
  2469. }
  2470. return process.nextTick(this.emit.bind(this, 'close'));
  2471. }
  2472. this.closed = true;
  2473. close();
  2474. function close(handle) {
  2475. self.sftp.close(handle || self.handle, function(er) {
  2476. if (er)
  2477. self.emit('error', er);
  2478. else
  2479. self.emit('close');
  2480. });
  2481. self.handle = null;
  2482. }
  2483. };
  2484. function WriteStream(sftp, path, options) {
  2485. if (!(this instanceof WriteStream))
  2486. return new WriteStream(sftp, path, options);
  2487. if (options === undefined)
  2488. options = {};
  2489. else if (typeof options === 'string')
  2490. options = { encoding: options };
  2491. else if (options === null || typeof options !== 'object')
  2492. throw new TypeError('"options" argument must be a string or an object');
  2493. else
  2494. options = Object.create(options);
  2495. WritableStream.call(this, options);
  2496. this.path = path;
  2497. this.handle = options.handle === undefined ? null : options.handle;
  2498. this.flags = options.flags === undefined ? 'w' : options.flags;
  2499. this.mode = options.mode === undefined ? 438/*0666*/ : options.mode;
  2500. this.start = options.start === undefined ? undefined : options.start;
  2501. this.autoClose = options.autoClose === undefined ? true : options.autoClose;
  2502. this.pos = 0;
  2503. this.bytesWritten = 0;
  2504. this.sftp = sftp;
  2505. if (this.start !== undefined) {
  2506. if (typeof this.start !== 'number')
  2507. throw new TypeError('start must be a Number');
  2508. if (this.start < 0)
  2509. throw new Error('start must be >= zero');
  2510. this.pos = this.start;
  2511. }
  2512. if (options.encoding)
  2513. this.setDefaultEncoding(options.encoding);
  2514. if (!Buffer.isBuffer(this.handle))
  2515. this.open();
  2516. // dispose on finish.
  2517. this.once('finish', function onclose() {
  2518. if (this.autoClose)
  2519. this.close();
  2520. });
  2521. }
  2522. inherits(WriteStream, WritableStream);
  2523. WriteStream.prototype.open = function() {
  2524. var self = this;
  2525. this.sftp.open(this.path, this.flags, this.mode, function(er, handle) {
  2526. if (er) {
  2527. self.emit('error', er);
  2528. if (self.autoClose) {
  2529. self.destroyed = self.closed = true;
  2530. self.emit('close');
  2531. }
  2532. return;
  2533. }
  2534. self.handle = handle;
  2535. self.sftp.fchmod(handle, self.mode, function tryAgain(err) {
  2536. if (err) {
  2537. // Try chmod() for sftp servers that may not support fchmod() for
  2538. // whatever reason
  2539. self.sftp.chmod(self.path, self.mode, function(err_) {
  2540. tryAgain();
  2541. });
  2542. return;
  2543. }
  2544. // SFTPv3 requires absolute offsets, no matter the open flag used
  2545. if (self.flags[0] === 'a') {
  2546. self.sftp.fstat(handle, function tryStat(err, st) {
  2547. if (err) {
  2548. // Try stat() for sftp servers that may not support fstat() for
  2549. // whatever reason
  2550. self.sftp.stat(self.path, function(err_, st_) {
  2551. if (err_) {
  2552. self.destroy();
  2553. self.emit('error', err);
  2554. return;
  2555. }
  2556. tryStat(null, st_);
  2557. });
  2558. return;
  2559. }
  2560. self.pos = st.size;
  2561. self.emit('open', handle);
  2562. });
  2563. return;
  2564. }
  2565. self.emit('open', handle);
  2566. });
  2567. });
  2568. };
  2569. WriteStream.prototype._write = function(data, encoding, cb) {
  2570. if (!Buffer.isBuffer(data))
  2571. return this.emit('error', new Error('Invalid data'));
  2572. if (!Buffer.isBuffer(this.handle)) {
  2573. return this.once('open', function() {
  2574. this._write(data, encoding, cb);
  2575. });
  2576. }
  2577. var self = this;
  2578. this.sftp.writeData(this.handle,
  2579. data,
  2580. 0,
  2581. data.length,
  2582. this.pos,
  2583. function(er, bytes) {
  2584. if (er) {
  2585. if (self.autoClose)
  2586. self.destroy();
  2587. return cb(er);
  2588. }
  2589. self.bytesWritten += bytes;
  2590. cb();
  2591. });
  2592. this.pos += data.length;
  2593. };
  2594. WriteStream.prototype._writev = function(data, cb) {
  2595. if (!Buffer.isBuffer(this.handle)) {
  2596. return this.once('open', function() {
  2597. this._writev(data, cb);
  2598. });
  2599. }
  2600. var sftp = this.sftp;
  2601. var handle = this.handle;
  2602. var writesLeft = data.length;
  2603. var self = this;
  2604. for (var i = 0; i < data.length; ++i) {
  2605. var chunk = data[i].chunk;
  2606. sftp.writeData(handle, chunk, 0, chunk.length, this.pos, onwrite);
  2607. this.pos += chunk.length;
  2608. }
  2609. function onwrite(er, bytes) {
  2610. if (er) {
  2611. self.destroy();
  2612. return cb(er);
  2613. }
  2614. self.bytesWritten += bytes;
  2615. if (--writesLeft === 0)
  2616. cb();
  2617. }
  2618. };
  2619. WriteStream.prototype.destroy = ReadStream.prototype.destroy;
  2620. WriteStream.prototype.close = ReadStream.prototype.close;
  2621. // There is no shutdown() for files.
  2622. WriteStream.prototype.destroySoon = WriteStream.prototype.end;
  2623. module.exports = SFTPStream;