file.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var Basefile, C, E, Infile, Outfile, Queue, Stdout, base58, concat, constants, crypto, fix_stat, fs, iced, log, make_esc, msgpack_packed_numlen, purepack, tmp_filename, __iced_k, __iced_k_noop,
  4. __hasProp = {}.hasOwnProperty,
  5. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  6. __slice = [].slice;
  7. iced = require('iced-runtime').iced;
  8. __iced_k = __iced_k_noop = function() {};
  9. fs = require('fs');
  10. log = require('./log');
  11. constants = require('./constants').constants;
  12. base58 = require('./basex').base58;
  13. crypto = require('crypto');
  14. C = require('constants');
  15. make_esc = require('iced-error').make_esc;
  16. purepack = require('purepack');
  17. E = require('./err').E;
  18. fix_stat = function(stat) {
  19. var f, _i, _len, _ref;
  20. _ref = ["ctime", "mtime", "atime"];
  21. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  22. f = _ref[_i];
  23. stat[f] = Math.floor(stat[f].getTime() / 1000);
  24. }
  25. return stat;
  26. };
  27. msgpack_packed_numlen = function(byt) {
  28. if (byt < 0x80) {
  29. return 1;
  30. } else if (byt === 0xcc) {
  31. return 2;
  32. } else if (byt === 0xcd) {
  33. return 3;
  34. } else if (byt === 0xce) {
  35. return 5;
  36. } else if (byt === 0xcf) {
  37. return 9;
  38. } else {
  39. return 0;
  40. }
  41. };
  42. exports.tmp_filename = tmp_filename = function(stem) {
  43. var ext;
  44. ext = base58.encode(crypto.rng(8));
  45. return [stem, ext].join('.');
  46. };
  47. exports.Basefile = Basefile = (function() {
  48. function Basefile(_arg) {
  49. this.fd = _arg.fd;
  50. if (this.fd == null) {
  51. this.fd = -1;
  52. }
  53. this.i = 0;
  54. }
  55. Basefile.prototype.offset = function() {
  56. return this.i;
  57. };
  58. Basefile.prototype.close = function() {
  59. if ((this.fd != null) >= 0) {
  60. fs.close(this.fd);
  61. this.fd = -1;
  62. return this.i = 0;
  63. }
  64. };
  65. return Basefile;
  66. })();
  67. exports.Stdout = Stdout = (function(_super) {
  68. __extends(Stdout, _super);
  69. function Stdout() {
  70. this.filename = "<stdout>";
  71. this.pos = 0;
  72. this.stream = process.stdout;
  73. }
  74. Stdout.prototype._open = function(cb) {
  75. return cb(null);
  76. };
  77. Stdout.prototype.close = function() {};
  78. Stdout.open = function(_arg, cb) {
  79. var file;
  80. _arg;
  81. file = new Stdout();
  82. return cb(err, file);
  83. };
  84. Stdout.prototype.finish = function(ok, cb) {
  85. return cb(null);
  86. };
  87. Stdout.prototype.write = function(block, cb) {
  88. var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  89. __iced_k = __iced_k_noop;
  90. ___iced_passed_deferral = iced.findDeferral(arguments);
  91. (function(_this) {
  92. return (function(__iced_k) {
  93. if (block.offset !== _this.pos) {
  94. return __iced_k(err = new E.InvalError("Can't seek stdout"));
  95. } else {
  96. (function(__iced_k) {
  97. __iced_deferrals = new iced.Deferrals(__iced_k, {
  98. parent: ___iced_passed_deferral,
  99. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  100. funcname: "Stdout.write"
  101. });
  102. _this.stream.write(block.buf, null, __iced_deferrals.defer({
  103. assign_fn: (function() {
  104. return function() {
  105. return err = arguments[0];
  106. };
  107. })(),
  108. lineno: 80
  109. }));
  110. __iced_deferrals._fulfill();
  111. })(function() {
  112. return __iced_k(_this.pos += block.buf.length);
  113. });
  114. }
  115. });
  116. })(this)((function(_this) {
  117. return function() {
  118. return cb(err);
  119. };
  120. })(this));
  121. };
  122. return Stdout;
  123. })(Basefile);
  124. exports.Outfile = Outfile = (function(_super) {
  125. __extends(Outfile, _super);
  126. function Outfile(_arg) {
  127. this.target = _arg.target, this.mode = _arg.mode;
  128. Outfile.__super__.constructor.call(this, {});
  129. if (this.mode == null) {
  130. this.mode = 0x1a4;
  131. }
  132. this.tmpname = tmp_filename(this.target);
  133. this.renamed = false;
  134. this.buf = null;
  135. this.i = 0;
  136. }
  137. Outfile.open = function(_arg, cb) {
  138. var err, file, mode, target, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  139. __iced_k = __iced_k_noop;
  140. ___iced_passed_deferral = iced.findDeferral(arguments);
  141. target = _arg.target, mode = _arg.mode;
  142. file = target != null ? new Outfile({
  143. target: target,
  144. mode: mode
  145. }) : new Stdout({});
  146. (function(_this) {
  147. return (function(__iced_k) {
  148. __iced_deferrals = new iced.Deferrals(__iced_k, {
  149. parent: ___iced_passed_deferral,
  150. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  151. funcname: "Outfile.open"
  152. });
  153. file._open(__iced_deferrals.defer({
  154. assign_fn: (function() {
  155. return function() {
  156. return err = arguments[0];
  157. };
  158. })(),
  159. lineno: 103
  160. }));
  161. __iced_deferrals._fulfill();
  162. });
  163. })(this)((function(_this) {
  164. return function() {
  165. if (typeof err !== "undefined" && err !== null) {
  166. file = null;
  167. }
  168. return cb(err, file);
  169. };
  170. })(this));
  171. };
  172. Outfile.prototype._open = function(cb) {
  173. var esc, flags, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  174. __iced_k = __iced_k_noop;
  175. ___iced_passed_deferral = iced.findDeferral(arguments);
  176. esc = make_esc(cb, "Open " + this.target + " for writing");
  177. flags = C.O_WRONLY | C.O_TRUNC | C.O_EXCL | C.O_CREAT;
  178. (function(_this) {
  179. return (function(__iced_k) {
  180. __iced_deferrals = new iced.Deferrals(__iced_k, {
  181. parent: ___iced_passed_deferral,
  182. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  183. funcname: "Outfile._open"
  184. });
  185. fs.open(_this.tmpname, flags, _this.mode, esc(__iced_deferrals.defer({
  186. assign_fn: (function(__slot_1) {
  187. return function() {
  188. return __slot_1.fd = arguments[0];
  189. };
  190. })(_this),
  191. lineno: 112
  192. })));
  193. __iced_deferrals._fulfill();
  194. });
  195. })(this)((function(_this) {
  196. return function() {
  197. (function(__iced_k) {
  198. __iced_deferrals = new iced.Deferrals(__iced_k, {
  199. parent: ___iced_passed_deferral,
  200. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  201. funcname: "Outfile._open"
  202. });
  203. fs.realpath(_this.tmpname, esc(__iced_deferrals.defer({
  204. assign_fn: (function(__slot_1) {
  205. return function() {
  206. return __slot_1.realpath = arguments[0];
  207. };
  208. })(_this),
  209. lineno: 113
  210. })));
  211. __iced_deferrals._fulfill();
  212. })(function() {
  213. return cb(null);
  214. });
  215. };
  216. })(this));
  217. };
  218. Outfile.prototype._rename = function(cb) {
  219. var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  220. __iced_k = __iced_k_noop;
  221. ___iced_passed_deferral = iced.findDeferral(arguments);
  222. (function(_this) {
  223. return (function(__iced_k) {
  224. __iced_deferrals = new iced.Deferrals(__iced_k, {
  225. parent: ___iced_passed_deferral,
  226. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  227. funcname: "Outfile._rename"
  228. });
  229. fs.rename(_this.tmpname, _this.target, __iced_deferrals.defer({
  230. assign_fn: (function() {
  231. return function() {
  232. return err = arguments[0];
  233. };
  234. })(),
  235. lineno: 119
  236. }));
  237. __iced_deferrals._fulfill();
  238. });
  239. })(this)((function(_this) {
  240. return function() {
  241. if (typeof err !== "undefined" && err !== null) {
  242. log.error("Failed to rename temporary file: " + err);
  243. } else {
  244. _this.renamed = true;
  245. }
  246. return cb(typeof err === "undefined" || err === null);
  247. };
  248. })(this));
  249. };
  250. Outfile.prototype.finish = function(success, cb) {
  251. var ___iced_passed_deferral, __iced_deferrals, __iced_k;
  252. __iced_k = __iced_k_noop;
  253. ___iced_passed_deferral = iced.findDeferral(arguments);
  254. this.close();
  255. (function(_this) {
  256. return (function(__iced_k) {
  257. if (success) {
  258. (function(__iced_k) {
  259. __iced_deferrals = new iced.Deferrals(__iced_k, {
  260. parent: ___iced_passed_deferral,
  261. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  262. funcname: "Outfile.finish"
  263. });
  264. _this._rename(__iced_deferrals.defer({
  265. lineno: 130
  266. }));
  267. __iced_deferrals._fulfill();
  268. })(__iced_k);
  269. } else {
  270. return __iced_k();
  271. }
  272. });
  273. })(this)((function(_this) {
  274. return function() {
  275. (function(__iced_k) {
  276. __iced_deferrals = new iced.Deferrals(__iced_k, {
  277. parent: ___iced_passed_deferral,
  278. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  279. funcname: "Outfile.finish"
  280. });
  281. _this._cleanup(__iced_deferrals.defer({
  282. lineno: 131
  283. }));
  284. __iced_deferrals._fulfill();
  285. })(function() {
  286. return cb();
  287. });
  288. };
  289. })(this));
  290. };
  291. Outfile.prototype._cleanup = function(cb) {
  292. var err, ok, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  293. __iced_k = __iced_k_noop;
  294. ___iced_passed_deferral = iced.findDeferral(arguments);
  295. ok = false;
  296. (function(_this) {
  297. return (function(__iced_k) {
  298. if (!_this.renamed) {
  299. (function(__iced_k) {
  300. __iced_deferrals = new iced.Deferrals(__iced_k, {
  301. parent: ___iced_passed_deferral,
  302. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  303. funcname: "Outfile._cleanup"
  304. });
  305. fs.unlink(_this.tmpname, __iced_deferrals.defer({
  306. assign_fn: (function() {
  307. return function() {
  308. return err = arguments[0];
  309. };
  310. })(),
  311. lineno: 139
  312. }));
  313. __iced_deferrals._fulfill();
  314. })(function() {
  315. return __iced_k(typeof err !== "undefined" && err !== null ? (log.error("failed to remove temporary file: " + err), ok = false) : void 0);
  316. });
  317. } else {
  318. return __iced_k();
  319. }
  320. });
  321. })(this)((function(_this) {
  322. return function() {
  323. return cb(ok);
  324. };
  325. })(this));
  326. };
  327. Outfile.prototype.write = function(block, cb) {
  328. var b, err, l, nw, o, ok, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  329. __iced_k = __iced_k_noop;
  330. ___iced_passed_deferral = iced.findDeferral(arguments);
  331. ok = false;
  332. l = block.buf.length;
  333. b = block.buf;
  334. o = block.offset;
  335. (function(_this) {
  336. return (function(__iced_k) {
  337. __iced_deferrals = new iced.Deferrals(__iced_k, {
  338. parent: ___iced_passed_deferral,
  339. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  340. funcname: "Outfile.write"
  341. });
  342. fs.write(_this.fd, b, 0, l, o, __iced_deferrals.defer({
  343. assign_fn: (function() {
  344. return function() {
  345. err = arguments[0];
  346. return nw = arguments[1];
  347. };
  348. })(),
  349. lineno: 152
  350. }));
  351. __iced_deferrals._fulfill();
  352. });
  353. })(this)((function(_this) {
  354. return function() {
  355. if (typeof err !== "undefined" && err !== null) {
  356. err = new E.BadIoError("In writing " + _this.tmpname + "@" + o + ": " + err);
  357. } else if (nw !== l) {
  358. err = new E.BadIoError("Short write in " + _this.tmpname + ": " + nw + " != " + l);
  359. }
  360. return cb(err);
  361. };
  362. })(this));
  363. };
  364. return Outfile;
  365. })(Basefile);
  366. exports.Infile = Infile = (function(_super) {
  367. __extends(Infile, _super);
  368. function Infile(_arg) {
  369. this.stat = _arg.stat, this.realpath = _arg.realpath, this.filename = _arg.filename, this.fd = _arg.fd;
  370. Infile.__super__.constructor.call(this, {
  371. fd: this.fd
  372. });
  373. this.buf = null;
  374. this.eof = false;
  375. }
  376. Infile.prototype.more_to_go = function() {
  377. return !this.eof;
  378. };
  379. Infile.prototype.size = function() {
  380. if (!this.stat) {
  381. throw new E.InternalError("file is not opened");
  382. }
  383. return this.stat.size;
  384. };
  385. Infile.prototype.read = function(offset, n, cb) {
  386. var br, err, ret, ___iced_passed_deferral, __iced_deferrals, __iced_k, _ref;
  387. __iced_k = __iced_k_noop;
  388. ___iced_passed_deferral = iced.findDeferral(arguments);
  389. ret = null;
  390. if (((_ref = this.buf) != null ? _ref.length : void 0) !== n) {
  391. this.buf = new Buffer(n);
  392. }
  393. (function(_this) {
  394. return (function(__iced_k) {
  395. __iced_deferrals = new iced.Deferrals(__iced_k, {
  396. parent: ___iced_passed_deferral,
  397. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  398. funcname: "Infile.read"
  399. });
  400. fs.read(_this.fd, _this.buf, 0, n, offset, __iced_deferrals.defer({
  401. assign_fn: (function() {
  402. return function() {
  403. err = arguments[0];
  404. return br = arguments[1];
  405. };
  406. })(),
  407. lineno: 183
  408. }));
  409. __iced_deferrals._fulfill();
  410. });
  411. })(this)((function(_this) {
  412. return function() {
  413. if (typeof err !== "undefined" && err !== null) {
  414. err = new E.BadIoError("" + _this.filename + "/" + offset + "-" + (offset + n) + ": " + err);
  415. } else if (br !== n) {
  416. err = new E.BadIoError("Short read: " + br + " != " + n);
  417. } else {
  418. ret = new Block({
  419. buf: _this.buf,
  420. offset: offset
  421. });
  422. }
  423. return cb(err, ret);
  424. };
  425. })(this));
  426. };
  427. Infile.prototype.next = function(n, cb) {
  428. var block, eof, err, rem, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  429. __iced_k = __iced_k_noop;
  430. ___iced_passed_deferral = iced.findDeferral(arguments);
  431. eof = false;
  432. if ((rem = this.stat.size - this.i) < n) {
  433. n = rem;
  434. eof = true;
  435. }
  436. (function(_this) {
  437. return (function(__iced_k) {
  438. __iced_deferrals = new iced.Deferrals(__iced_k, {
  439. parent: ___iced_passed_deferral,
  440. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  441. funcname: "Infile.next"
  442. });
  443. _this.read(_this.i, n, __iced_deferrals.defer({
  444. assign_fn: (function() {
  445. return function() {
  446. err = arguments[0];
  447. return block = arguments[1];
  448. };
  449. })(),
  450. lineno: 199
  451. }));
  452. __iced_deferrals._fulfill();
  453. });
  454. })(this)((function(_this) {
  455. return function() {
  456. if (typeof block !== "undefined" && block !== null) {
  457. _this.i += block.len();
  458. } else {
  459. eof = true;
  460. }
  461. _this.eof = eof;
  462. return cb(err, block, eof);
  463. };
  464. })(this));
  465. };
  466. Infile.open = function(filename, cb) {
  467. var err, file, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  468. __iced_k = __iced_k_noop;
  469. ___iced_passed_deferral = iced.findDeferral(arguments);
  470. file = new Infile({
  471. filename: filename
  472. });
  473. (function(_this) {
  474. return (function(__iced_k) {
  475. __iced_deferrals = new iced.Deferrals(__iced_k, {
  476. parent: ___iced_passed_deferral,
  477. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  478. funcname: "Infile.open"
  479. });
  480. file._open(__iced_deferrals.defer({
  481. assign_fn: (function() {
  482. return function() {
  483. return err = arguments[0];
  484. };
  485. })(),
  486. lineno: 211
  487. }));
  488. __iced_deferrals._fulfill();
  489. });
  490. })(this)((function(_this) {
  491. return function() {
  492. if (typeof err !== "undefined" && err !== null) {
  493. file = null;
  494. }
  495. return cb(err, file);
  496. };
  497. })(this));
  498. };
  499. Infile.prototype.finish = function(ok, cb) {
  500. this.close();
  501. return cb(null);
  502. };
  503. Infile.prototype._open = function(cb) {
  504. var esc, flags, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  505. __iced_k = __iced_k_noop;
  506. ___iced_passed_deferral = iced.findDeferral(arguments);
  507. esc = make_esc(cb, "Open " + this.filename);
  508. flags = C.O_RDONLY;
  509. (function(_this) {
  510. return (function(__iced_k) {
  511. __iced_deferrals = new iced.Deferrals(__iced_k, {
  512. parent: ___iced_passed_deferral,
  513. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  514. funcname: "Infile._open"
  515. });
  516. fs.open(_this.filename, flags, esc(__iced_deferrals.defer({
  517. assign_fn: (function(__slot_1) {
  518. return function() {
  519. return __slot_1.fd = arguments[0];
  520. };
  521. })(_this),
  522. lineno: 226
  523. })));
  524. __iced_deferrals._fulfill();
  525. });
  526. })(this)((function(_this) {
  527. return function() {
  528. (function(__iced_k) {
  529. __iced_deferrals = new iced.Deferrals(__iced_k, {
  530. parent: ___iced_passed_deferral,
  531. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  532. funcname: "Infile._open"
  533. });
  534. fs.fstat(_this.fd, esc(__iced_deferrals.defer({
  535. assign_fn: (function(__slot_1) {
  536. return function() {
  537. return __slot_1.stat = arguments[0];
  538. };
  539. })(_this),
  540. lineno: 227
  541. })));
  542. __iced_deferrals._fulfill();
  543. })(function() {
  544. (function(__iced_k) {
  545. __iced_deferrals = new iced.Deferrals(__iced_k, {
  546. parent: ___iced_passed_deferral,
  547. filename: "/Users/max/src/keybase/node-client/src/file.iced",
  548. funcname: "Infile._open"
  549. });
  550. fs.realpath(_this.filename, esc(__iced_deferrals.defer({
  551. assign_fn: (function(__slot_1) {
  552. return function() {
  553. return __slot_1.realpath = arguments[0];
  554. };
  555. })(_this),
  556. lineno: 228
  557. })));
  558. __iced_deferrals._fulfill();
  559. })(function() {
  560. return cb(null);
  561. });
  562. });
  563. };
  564. })(this));
  565. };
  566. return Infile;
  567. })(Basefile);
  568. concat = function(lst) {
  569. return Buffer.concat(lst);
  570. };
  571. Queue = (function() {
  572. function Queue() {
  573. this._q = [];
  574. this._cb = null;
  575. }
  576. Queue.prototype.push = function() {
  577. var args, tmp;
  578. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  579. if (this._cb != null) {
  580. tmp = this._cb;
  581. this._cb = null;
  582. return tmp.apply(null, args);
  583. } else {
  584. return this._q.push(args);
  585. }
  586. };
  587. Queue.prototype.pop = function(i, cb) {
  588. var trip;
  589. if (this._q.length != null) {
  590. trip = this._q[0];
  591. this._q = this.q.slice(1);
  592. return cb.apply(null, trip);
  593. } else {
  594. return this._cb = cb;
  595. }
  596. };
  597. return Queue;
  598. })();
  599. }).call(this);