1 | 'use strict';
|
2 |
|
3 | import {
|
4 | MUTABLE_DATUM_DIR_TYPE,
|
5 | MUTABLE_DATUM_FILE_TYPE,
|
6 | DATASTORE_SCHEMA,
|
7 | DATASTORE_RESPONSE_SCHEMA,
|
8 | MUTABLE_DATUM_INODE_SCHEMA,
|
9 | MUTABLE_DATUM_DIR_IDATA_SCHEMA,
|
10 | MUTABLE_DATUM_EXTENDED_RESPONS_SCHEMA,
|
11 | SUCCESS_FAIL_SCHEMA,
|
12 | DATASTORE_LOOKUP_RESPONSE_SCHEMA,
|
13 | DATASTORE_LOOKUP_EXTENDED_RESPONSE_SCHEMA,
|
14 | CORE_ERROR_SCHEMA,
|
15 | } from './schemas';
|
16 |
|
17 | import {
|
18 | makeFileInodeBlob,
|
19 | makeDirInodeBlob,
|
20 | makeMutableDataInfo,
|
21 | signDataPayload,
|
22 | signRawData,
|
23 | hashDataPayload,
|
24 | inodeDirLink,
|
25 | inodeDirUnlink,
|
26 | decodePrivateKey,
|
27 | makeInodeTombstones,
|
28 | makeMutableDataTombstones,
|
29 | signMutableDataTombstones,
|
30 | getChildVersion,
|
31 | } from './inode';
|
32 |
|
33 | import {
|
34 | jsonStableSerialize
|
35 | } from './util';
|
36 |
|
37 | const http = require('http');
|
38 | const uuid4 = require('uuid/v4');
|
39 | const bitcoinjs = require('bitcoinjs-lib');
|
40 | const BigInteger = require('bigi');
|
41 | const Promise = require('promise');
|
42 | const assert = require('assert');
|
43 | const Ajv = require('ajv');
|
44 | const jsontokens = require('jsontokens');
|
45 |
|
46 | const EPERM = 1;
|
47 | const ENOENT = 2;
|
48 | const EACCES = 13;
|
49 | const EEXIST = 17;
|
50 | const ENOTDIR = 20;
|
51 | const EINVAL = 22;
|
52 | const EREMOTEIO = 121;
|
53 |
|
54 | const LOCAL_STORAGE_ID = "blockstack";
|
55 | const SUPPORTED_STORAGE_CLASSES = ["read_public", "write_public", "read_private", "write_private", "read_local", "write_local"];
|
56 | const REPLICATION_STRATEGY_CLASSES = {
|
57 | 'local': new Set(['read_local', 'write_local']),
|
58 | 'publish': new Set(['read_public', 'write_private']),
|
59 | 'public': new Set(['read_public', 'write_public']),
|
60 | 'private': new Set(['read_private', 'write_private']),
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function validateJSONResponse(resp, result_schema) {
|
69 |
|
70 | const ajv = new Ajv();
|
71 | if (result_schema) {
|
72 | try {
|
73 | const valid = ajv.validate(result_schema, resp);
|
74 | assert(valid);
|
75 | return resp;
|
76 | }
|
77 | catch(e) {
|
78 | try {
|
79 |
|
80 | const valid = ajv.validate(CORE_ERROR_SCHEMA, resp);
|
81 | assert(valid);
|
82 | return resp;
|
83 | }
|
84 | catch(e2) {
|
85 | console.log("Failed to validate with desired schema");
|
86 | console.log(e.stack);
|
87 | console.log("Failed to validate with error schema");
|
88 | console.log(e2.stack);
|
89 | console.log("Desired schema:");
|
90 | console.log(result_schema);
|
91 | console.log("Parsed message:");
|
92 | console.log(resp);
|
93 | throw new Error("Invalid core message");
|
94 | }
|
95 | }
|
96 | }
|
97 | else {
|
98 | return resp;
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function httpRequest(options, result_schema, body) {
|
114 |
|
115 | if (body) {
|
116 | options['body'] = body;
|
117 | }
|
118 |
|
119 | const url = `http://${options.host}:${options.port}${options.path}`;
|
120 | return fetch(url, options)
|
121 | .then((response) => {
|
122 |
|
123 | if(response.status >= 500) {
|
124 | throw new Error(response.statusText);
|
125 | }
|
126 |
|
127 | if(response.status === 404) {
|
128 | return {'error': 'No such file or directory', 'errno': ENOENT};
|
129 | }
|
130 |
|
131 | if(response.status === 403) {
|
132 | return {'error': 'Access denied', 'errno': EACCES};
|
133 | }
|
134 |
|
135 | if(response.status === 401) {
|
136 | return {'error': 'Invalid request', 'errno': EINVAL};
|
137 | }
|
138 |
|
139 | if(response.status === 400) {
|
140 | return {'error': 'Operation not permitted', 'errno': EPERM};
|
141 | }
|
142 |
|
143 | let resp = null;
|
144 | if (response.headers.get('content-type') === 'application/json') {
|
145 | return response.json().then( (resp) => {
|
146 | return validateJSONResponse(resp, result_schema);
|
147 | });
|
148 | }
|
149 | else {
|
150 | return response.text();
|
151 | }
|
152 | });
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | export function datastoreGetId( ds_public_key_hex) {
|
161 | let ec = bitcoinjs.ECPair.fromPublicKeyBuffer( Buffer.from(ds_public_key_hex, 'hex') );
|
162 | return ec.getAddress();
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function getPubkeyHex(privkey_hex) {
|
170 | let privkey = BigInteger.fromBuffer( decodePrivateKey(privkey_hex) );
|
171 | let public_key = new bitcoinjs.ECPair(privkey);
|
172 |
|
173 | public_key.compressed = false;
|
174 | let public_key_str = public_key.getPublicKeyBuffer().toString('hex');
|
175 | return public_key_str;
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function getDeviceList(datastore_ctx) {
|
183 | const escaped_device_ids = [];
|
184 | for (let dk of datastore_ctx.app_public_keys) {
|
185 | escaped_device_ids.push(escape(dk.device_id));
|
186 | }
|
187 | const res = escaped_device_ids.join(',');
|
188 | return res;
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | function getPublicKeyList(datastore_ctx) {
|
196 | const escaped_public_keys = [];
|
197 | for (let dk of datastore_ctx.app_public_keys) {
|
198 | escaped_public_keys.push(escape(dk.public_key));
|
199 | }
|
200 | const res = escaped_public_keys.join(',');
|
201 | return res;
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | export function sanitizePath( path) {
|
211 |
|
212 | const parts = path.split('/').filter(function(x) {return x.length > 0;});
|
213 | const retparts = [];
|
214 |
|
215 | for(let i = 0; i < parts.length; i++) {
|
216 | if (parts[i] === '..') {
|
217 | retparts.pop();
|
218 | }
|
219 | else {
|
220 | retparts.push(parts[i]);
|
221 | }
|
222 | }
|
223 |
|
224 | return '/' + retparts.join('/');
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | export function dirname(path) {
|
234 | return '/' + path.split('/').slice(0, -1).join('/');
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | export function basename(path) {
|
244 | return path.split('/').slice(-1)[0];
|
245 | }
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | function splitHostPort(hostport) {
|
259 |
|
260 | let host = hostport;
|
261 | let port = 80;
|
262 | const parts = hostport.split(':');
|
263 | if (parts.length > 1) {
|
264 | host = parts[0];
|
265 | port = parts[1];
|
266 | }
|
267 |
|
268 | return {'host': host, 'port': port};
|
269 | }
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | export function datastoreCreateRequest( ds_type, ds_private_key_hex, drivers, device_id, all_device_ids) {
|
280 |
|
281 | assert(ds_type === 'datastore' || ds_type === 'collection');
|
282 | const root_uuid = uuid4();
|
283 |
|
284 | const ds_public_key = getPubkeyHex(ds_private_key_hex);
|
285 | const datastore_id = datastoreGetId( ds_public_key );
|
286 | const root_blob_info = makeDirInodeBlob( datastore_id, datastore_id, root_uuid, {}, device_id, 1 );
|
287 |
|
288 |
|
289 | const datastore_info = {
|
290 | 'type': ds_type,
|
291 | 'pubkey': ds_public_key,
|
292 | 'drivers': drivers,
|
293 | 'device_ids': all_device_ids,
|
294 | 'root_uuid': root_uuid,
|
295 | };
|
296 |
|
297 | const data_id = `${datastore_id}.datastore`;
|
298 | const datastore_blob = makeMutableDataInfo( data_id, jsonStableSerialize(datastore_info), device_id, 1 );
|
299 |
|
300 | const datastore_str = jsonStableSerialize(datastore_blob);
|
301 |
|
302 |
|
303 | const root_sig = signDataPayload( root_blob_info.header, ds_private_key_hex );
|
304 | const datastore_sig = signDataPayload( datastore_str, ds_private_key_hex );
|
305 |
|
306 |
|
307 | const root_tombstones = makeInodeTombstones(datastore_id, root_uuid, all_device_ids);
|
308 | const signed_tombstones = signMutableDataTombstones(root_tombstones, ds_private_key_hex);
|
309 |
|
310 | const info = {
|
311 | 'datastore_info': {
|
312 | 'datastore_id': datastore_id,
|
313 | 'datastore_blob': datastore_str,
|
314 | 'root_blob_header': root_blob_info.header,
|
315 | 'root_blob_idata': root_blob_info.idata,
|
316 | },
|
317 | 'datastore_sigs': {
|
318 | 'datastore_sig': datastore_sig,
|
319 | 'root_sig': root_sig,
|
320 | },
|
321 | 'root_tombstones': signed_tombstones,
|
322 | };
|
323 |
|
324 | return info;
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | export function datastoreCreate( blockstack_hostport, blockstack_session_token, datastore_request) {
|
334 |
|
335 | const payload = {
|
336 | 'datastore_info': {
|
337 | 'datastore_blob': datastore_request.datastore_info.datastore_blob,
|
338 | 'root_blob_header': datastore_request.datastore_info.root_blob_header,
|
339 | 'root_blob_idata': datastore_request.datastore_info.root_blob_idata,
|
340 | },
|
341 | 'datastore_sigs': {
|
342 | 'datastore_sig': datastore_request.datastore_sigs.datastore_sig,
|
343 | 'root_sig': datastore_request.datastore_sigs.root_sig,
|
344 | },
|
345 | 'root_tombstones': datastore_request.root_tombstones,
|
346 | };
|
347 |
|
348 | const hostinfo = splitHostPort(blockstack_hostport);
|
349 |
|
350 | const options = {
|
351 | 'method': 'POST',
|
352 | 'host': hostinfo.host,
|
353 | 'port': hostinfo.port,
|
354 | 'path': '/v1/stores'
|
355 | };
|
356 |
|
357 | if (blockstack_session_token) {
|
358 | options['headers'] = {'Authorization': `bearer ${blockstack_session_token}`};
|
359 | }
|
360 |
|
361 | const body = JSON.stringify(payload);
|
362 | options['headers']['Content-Type'] = 'application/json';
|
363 | options['headers']['Content-Length'] = body.length;
|
364 |
|
365 | return httpRequest(options, SUCCESS_FAIL_SCHEMA, body);
|
366 | }
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | export function datastoreDeleteRequest(ds=null) {
|
377 |
|
378 | if (!ds) {
|
379 | const blockchain_id = getSessionBlockchainID();
|
380 | assert(blockchain_id);
|
381 |
|
382 | ds = getCachedMountContext(blockchain_id);
|
383 | assert(ds);
|
384 | }
|
385 |
|
386 | const datastore_id = ds.datastore_id;
|
387 | const device_ids = ds.datastore.device_ids;
|
388 | const root_uuid = ds.datastore.root_uuid;
|
389 | const data_id = `${datastore_id}.datastore`;
|
390 |
|
391 | const tombstones = makeMutableDataTombstones( device_ids, data_id );
|
392 | const signed_tombstones = signMutableDataTombstones( tombstones, ds.privkey_hex );
|
393 |
|
394 | const root_tombstones = makeInodeTombstones(datastore_id, root_uuid, device_ids);
|
395 | const signed_root_tombstones = signMutableDataTombstones( root_tombstones, ds.privkey_hex );
|
396 |
|
397 | const ret = {
|
398 | 'datastore_tombstones': signed_tombstones,
|
399 | 'root_tombstones': signed_root_tombstones,
|
400 | };
|
401 |
|
402 | return ret;
|
403 | }
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 | export function datastoreDelete(ds=null, ds_tombstones=null, root_tombstones=null) {
|
416 |
|
417 | if (!ds) {
|
418 | const blockchain_id = getSessionBlockchainID();
|
419 | assert(blockchain_id);
|
420 |
|
421 | ds = getCachedMountContext(blockchain_id);
|
422 | assert(ds);
|
423 | }
|
424 |
|
425 | if (!ds_tombstones || !root_tombstones) {
|
426 | const delete_info = datastoreDeleteRequest(ds);
|
427 | ds_tombstones = delete_info['datastore_tombstones'];
|
428 | root_tombstones = delete_info['root_tombstones'];
|
429 | }
|
430 |
|
431 | const device_list = getDeviceList(ds);
|
432 | const payload = {
|
433 | 'datastore_tombstones': ds_tombstones,
|
434 | 'root_tombstones': root_tombstones,
|
435 | };
|
436 |
|
437 | const options = {
|
438 | 'method': 'DELETE',
|
439 | 'host': ds.host,
|
440 | 'port': ds.port,
|
441 | 'path': `/v1/stores?device_ids=${device_list}`
|
442 | };
|
443 |
|
444 | if (ds.session_token) {
|
445 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
446 | }
|
447 |
|
448 | const body = JSON.stringify(payload);
|
449 | options['headers']['Content-Type'] = 'application/json';
|
450 | options['headers']['Content-Length'] = body.length;
|
451 |
|
452 | return httpRequest(options, SUCCESS_FAIL_SCHEMA, body);
|
453 | }
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | export function datastoreMount(opts) {
|
482 |
|
483 | const data_privkey_hex = opts.appPrivateKey;
|
484 | let sessionToken = opts.sessionToken;
|
485 |
|
486 |
|
487 | assert(data_privkey_hex);
|
488 |
|
489 | let datastore_id = null;
|
490 | let device_id = null;
|
491 | let blockchain_id = null;
|
492 | let api_endpoint = null;
|
493 | let app_public_keys = null;
|
494 |
|
495 | if (!sessionToken) {
|
496 |
|
497 | const userData = getUserData();
|
498 |
|
499 | sessionToken = userData.coreSessionToken;
|
500 | assert(sessionToken);
|
501 | }
|
502 |
|
503 | const session = jsontokens.decodeToken(sessionToken).payload;
|
504 |
|
505 | if (data_privkey_hex) {
|
506 | datastore_id = datastoreGetId(getPubkeyHex(data_privkey_hex));
|
507 | }
|
508 | else {
|
509 | blockchain_id = opts.blockchainID;
|
510 | const app_name = opts.appName;
|
511 |
|
512 | assert(blockchain_id);
|
513 | assert(app_name);
|
514 |
|
515 |
|
516 |
|
517 |
|
518 | }
|
519 |
|
520 | if (!device_id) {
|
521 | device_id = session.device_id;
|
522 | assert(device_id);
|
523 | }
|
524 |
|
525 | if (!api_endpoint) {
|
526 | api_endpoint = session.api_endpoint;
|
527 | assert(api_endpoint);
|
528 | }
|
529 |
|
530 | if (!blockchain_id) {
|
531 | blockchain_id = session.blockchain_id;
|
532 | assert(blockchain_id);
|
533 | }
|
534 |
|
535 | if (!app_public_keys) {
|
536 | app_public_keys = session.app_public_keys;
|
537 | assert(app_public_keys);
|
538 | }
|
539 |
|
540 | const blockstack_hostport = api_endpoint.split('://').reverse()[0];
|
541 | const hostinfo = splitHostPort(blockstack_hostport);
|
542 |
|
543 | const ctx = {
|
544 | 'host': hostinfo.host,
|
545 | 'port': hostinfo.port,
|
546 | 'blockchain_id': blockchain_id,
|
547 | 'device_id': device_id,
|
548 | 'datastore_id': datastore_id,
|
549 | 'session_token': sessionToken,
|
550 | 'app_public_keys': app_public_keys,
|
551 | 'session': session,
|
552 | 'datastore': null,
|
553 | };
|
554 |
|
555 | if (data_privkey_hex) {
|
556 | ctx.privkey_hex = data_privkey_hex;
|
557 | }
|
558 |
|
559 | const options = {
|
560 | 'method': 'GET',
|
561 | 'host': hostinfo.host,
|
562 | 'port': hostinfo.port,
|
563 | 'path': `/v1/stores/${datastore_id}?device_ids=${device_id}&blockchain_id=${blockchain_id}`,
|
564 | }
|
565 |
|
566 | options['headers'] = {'Authorization': `bearer ${sessionToken}`};
|
567 |
|
568 | return httpRequest(options, DATASTORE_RESPONSE_SCHEMA).then((ds) => {
|
569 | if (!ds || ds.error) {
|
570 |
|
571 | if (!ds || ds.errno === ENOENT) {
|
572 | return null;
|
573 | }
|
574 | else {
|
575 | let errorMsg = ds.error || 'No response given';
|
576 | throw new Error(`Failed to get datastore: ${errorMsg}`);
|
577 | }
|
578 | }
|
579 | else {
|
580 | ctx['datastore'] = ds.datastore;
|
581 |
|
582 |
|
583 | setCachedMountContext(blockchain_id, ctx);
|
584 |
|
585 |
|
586 | let userData = getUserData();
|
587 | if (!userData.coreSessionToken) {
|
588 | console.log("In test framework; saving session token");
|
589 | userData.coreSessionToken = sessionToken;
|
590 | setUserData(userData);
|
591 | }
|
592 |
|
593 | return ctx;
|
594 | }
|
595 | });
|
596 | }
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 | function getUserData() {
|
604 | let localStorage = null;
|
605 |
|
606 | if (typeof window === 'undefined' || window === null) {
|
607 | const LocalStorage = require('node-localstorage').LocalStorage;
|
608 | localStorage = new LocalStorage('./scratch');
|
609 | }
|
610 | else {
|
611 | localStorage = window.localStorage;
|
612 | }
|
613 |
|
614 | let userData = localStorage.getItem(LOCAL_STORAGE_ID);
|
615 | if (userData === null) {
|
616 | userData = '{}';
|
617 | }
|
618 |
|
619 | userData = JSON.parse(userData);
|
620 | return userData;
|
621 | }
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | function setUserData(userData) {
|
628 | let localStorage = null;
|
629 |
|
630 | if (typeof window === 'undefined' || window === null) {
|
631 | const LocalStorage = require('node-localstorage').LocalStorage;
|
632 | localStorage = new LocalStorage('./scratch');
|
633 | }
|
634 | else {
|
635 | localStorage = window.localStorage;
|
636 | }
|
637 |
|
638 | localStorage.setItem(LOCAL_STORAGE_ID, JSON.stringify(userData));
|
639 | }
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 | function getCachedMountContext(blockchain_id) {
|
648 |
|
649 | let userData = getUserData();
|
650 | if (!userData.datastore_contexts) {
|
651 | console.log("No datastore contexts defined");
|
652 | return null;
|
653 | }
|
654 |
|
655 | if (!userData.datastore_contexts[blockchain_id]) {
|
656 | console.log(`No datastore contexts for ${blockchain_id}`);
|
657 | return null;
|
658 | }
|
659 |
|
660 | let ctx = userData.datastore_contexts[blockchain_id];
|
661 | if (!ctx) {
|
662 | console.log(`Null datastore context for ${blockchain_id}`);
|
663 | return null;
|
664 | }
|
665 |
|
666 | return ctx;
|
667 | }
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 | function setCachedMountContext(blockchain_id, datastore_context) {
|
674 |
|
675 | let userData = getUserData();
|
676 | if (!userData.datastore_contexts) {
|
677 | userData.datastore_contexts = {};
|
678 | }
|
679 |
|
680 | userData.datastore_contexts[blockchain_id] = datastore_context;
|
681 | setUserData(userData);
|
682 | }
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 | function getSessionBlockchainID() {
|
690 |
|
691 | let userData = getUserData();
|
692 | assert(userData);
|
693 | assert(userData.coreSessionToken);
|
694 |
|
695 | const session = jsontokens.decodeToken(userData.coreSessionToken).payload;
|
696 |
|
697 | assert(session.blockchain_id);
|
698 | return session.blockchain_id;
|
699 | }
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 | function selectDrivers(replication_strategy, classes) {
|
712 |
|
713 |
|
714 | let driver_sets = [];
|
715 | let driver_classes = {};
|
716 | let all_drivers = new Set([]);
|
717 | let available_drivers = [];
|
718 | let selected_drivers = [];
|
719 | let have_drivers = false;
|
720 |
|
721 | for (let i = 0; i < SUPPORTED_STORAGE_CLASSES.length; i++) {
|
722 | let driver_set = new Set(classes[SUPPORTED_STORAGE_CLASSES[i]]);
|
723 | driver_sets.push(driver_set);
|
724 |
|
725 | for(let d of driver_set) {
|
726 | all_drivers.add(d);
|
727 | }
|
728 |
|
729 | for( let d of driver_set ) {
|
730 | console.log(`Driver ${d} implementes ${SUPPORTED_STORAGE_CLASSES[i]}`);
|
731 | if (driver_classes[d]) {
|
732 | driver_classes[d].push(SUPPORTED_STORAGE_CLASSES[i]);
|
733 | }
|
734 | else {
|
735 | driver_classes[d] = [SUPPORTED_STORAGE_CLASSES[i]];
|
736 | }
|
737 | }
|
738 | }
|
739 |
|
740 | let concern_fulfillment = {};
|
741 |
|
742 | for (let d of all_drivers) {
|
743 | let classes = driver_classes[d];
|
744 |
|
745 |
|
746 |
|
747 | for (let concern of Object.keys(replication_strategy)) {
|
748 |
|
749 | let matches = false;
|
750 | for (let dclass of classes) {
|
751 | if (REPLICATION_STRATEGY_CLASSES[concern].has(dclass)) {
|
752 | matches = true;
|
753 | break;
|
754 | }
|
755 | }
|
756 |
|
757 | if (matches) {
|
758 | console.log(`Driver ${d} fulfills replication concern ${concern}`);
|
759 |
|
760 | if (concern_fulfillment[concern]) {
|
761 | concern_fulfillment[concern] += 1;
|
762 | }
|
763 | else {
|
764 | concern_fulfillment[concern] = 1;
|
765 | }
|
766 |
|
767 | if (concern_fulfillment[concern] <= replication_strategy[concern]) {
|
768 | console.log(`Select driver ${d}`);
|
769 | selected_drivers.push(d);
|
770 | }
|
771 | }
|
772 |
|
773 |
|
774 | let fulfilled = true;
|
775 | for (let concern of Object.keys(replication_strategy)) {
|
776 | let count = 0;
|
777 | if (concern_fulfillment[concern]) {
|
778 | count = concern_fulfillment[concern];
|
779 | }
|
780 |
|
781 | if (count < replication_strategy[concern]) {
|
782 | fulfilled = false;
|
783 | break;
|
784 | }
|
785 | }
|
786 |
|
787 | if (fulfilled) {
|
788 | have_drivers = true;
|
789 | break;
|
790 | }
|
791 | }
|
792 |
|
793 | if (have_drivers) {
|
794 | break;
|
795 | }
|
796 | }
|
797 |
|
798 | if (!have_drivers) {
|
799 | throw new Error("Unsatisfiable replication strategy");
|
800 | }
|
801 |
|
802 | return selected_drivers;
|
803 | }
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 | export function datastoreMountOrCreate(replication_strategy={'public': 1, 'local': 1}, sessionToken=null, appPrivateKey=null) {
|
816 |
|
817 | if(!sessionToken) {
|
818 | const userData = getUserData();
|
819 |
|
820 | sessionToken = userData.coreSessionToken;
|
821 | assert(sessionToken);
|
822 | }
|
823 |
|
824 |
|
825 | const session = jsontokens.decodeToken(sessionToken).payload;
|
826 | assert(session.blockchain_id);
|
827 |
|
828 | let ds = getCachedMountContext(session.blockchain_id);
|
829 | if (ds) {
|
830 | return new Promise((resolve, reject) => { resolve(ds); });
|
831 | }
|
832 |
|
833 |
|
834 |
|
835 | if(!appPrivateKey) {
|
836 | const userData = getUserData();
|
837 |
|
838 | appPrivateKey = userData.appPrivateKey;
|
839 | assert(appPrivateKey);
|
840 | }
|
841 |
|
842 |
|
843 | for (let strategy of Object.keys(replication_strategy)) {
|
844 | let supported = false;
|
845 | for (let supported_strategy of Object.keys(REPLICATION_STRATEGY_CLASSES)) {
|
846 | if (supported_strategy === strategy) {
|
847 | supported = true;
|
848 | break;
|
849 | }
|
850 | }
|
851 |
|
852 | if (!supported) {
|
853 | throw new Error(`Unsupported replication strategy ${strategy}`);
|
854 | }
|
855 | }
|
856 |
|
857 | let drivers = null;
|
858 |
|
859 |
|
860 | if (Object.keys(session.storage.preferences).includes(session.app_domain)) {
|
861 |
|
862 |
|
863 | drivers = session.storage.preferences[app_domain];
|
864 | }
|
865 | else {
|
866 |
|
867 |
|
868 | drivers = selectDrivers(replication_strategy, session.storage.classes);
|
869 | }
|
870 |
|
871 | const hostport = session.api_endpoint.split('://').reverse()[0];
|
872 | const appPublicKeys = session.app_public_keys;
|
873 | const deviceID = session.device_id;
|
874 | const allDeviceIDs = [];
|
875 |
|
876 | for (let i = 0; i < appPublicKeys.length; i++) {
|
877 | allDeviceIDs.push(appPublicKeys[i].device_id);
|
878 | }
|
879 |
|
880 | console.log(`Will use drivers ${drivers.join(',')}`);
|
881 | console.log(`Datastore will span devices ${allDeviceIDs.join(',')}`);
|
882 |
|
883 | const datastoreOpts = {
|
884 | 'appPrivateKey': appPrivateKey,
|
885 | 'sessionToken': sessionToken,
|
886 | };
|
887 |
|
888 | return datastoreMount(datastoreOpts)
|
889 | .then((datastore_ctx) => {
|
890 | if (!datastore_ctx) {
|
891 |
|
892 | console.log("Datastore does not exist; creating...");
|
893 |
|
894 | const info = datastoreCreateRequest('datastore', appPrivateKey, drivers, deviceID, allDeviceIDs );
|
895 |
|
896 |
|
897 | return datastoreCreate( hostport, sessionToken, info )
|
898 | .then((res) => {
|
899 | if (res.error) {
|
900 | console.log(error);
|
901 | let errorNo = res.errno || 'UNKNOWN';
|
902 | let errorMsg = res.error || 'UNKNOWN';
|
903 | throw new Error(`Failed to create datastore (errno ${errorNo}): ${errorMsg}`);
|
904 | }
|
905 |
|
906 |
|
907 | return datastoreMount(datastoreOpts);
|
908 | });
|
909 | }
|
910 | else if (datastore_ctx.error) {
|
911 |
|
912 | let errorMsg = datastore_ctx.error || 'UNKNOWN';
|
913 | let errorNo = datastore_ctx.errno || 'UNKNOWN';
|
914 | throw new Error(`Failed to access datastore (errno ${errorNo}): ${errorMsg}`);
|
915 | }
|
916 | else {
|
917 |
|
918 | return datastore_ctx;
|
919 | }
|
920 | });
|
921 | }
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 | export function lookup(path, opts={}) {
|
939 |
|
940 | let blockchain_id = opts.blockchain_id;
|
941 |
|
942 | if (!opts.blockchain_id) {
|
943 | blockchain_id = getSessionBlockchainID();
|
944 | }
|
945 |
|
946 | return datastoreMountOrCreate()
|
947 | .then((ds) => {
|
948 | assert(ds);
|
949 |
|
950 | const datastore_id = ds.datastore_id;
|
951 | const device_list = getDeviceList(ds);
|
952 | const device_pubkeys = getPublicKeyList(ds);
|
953 | const options = {
|
954 | 'method': 'GET',
|
955 | 'host': ds.host,
|
956 | 'port': ds.port,
|
957 | 'path': `/v1/stores/${datastore_id}/inodes?path=${escape(sanitizePath(path))}&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`,
|
958 | };
|
959 |
|
960 | if (!opts) {
|
961 | opts = {};
|
962 | }
|
963 |
|
964 | let schema = DATASTORE_LOOKUP_RESPONSE_SCHEMA;
|
965 |
|
966 | if (opts.extended) {
|
967 | options['path'] += '&extended=1';
|
968 | schema = DATASTORE_LOOKUP_EXTENDED_RESPONSE_SCHEMA;
|
969 | }
|
970 |
|
971 | if (opts.force) {
|
972 | options['path'] += '&force=1';
|
973 | }
|
974 |
|
975 | if (opts.idata) {
|
976 | options['idata'] += '&idata=1';
|
977 | }
|
978 |
|
979 |
|
980 | return httpRequest(options, schema)
|
981 | .then((lookup_response) => {
|
982 | if (lookup_response.error || lookup_response.errno) {
|
983 | let errorMsg = lookup_response.error || 'UNKNOWN';
|
984 | let errorNo = lookup_response.errno || 'UNKNOWN';
|
985 | throw new Error(`Failed to look up ${path} (errno: ${errorNo}): ${errorMsg}`);
|
986 | }
|
987 | else {
|
988 | return lookup_response;
|
989 | }
|
990 | });
|
991 | });
|
992 | }
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 | export function listdir(path, opts={}) {
|
1009 |
|
1010 | let blockchain_id = opts.blockchain_id;
|
1011 |
|
1012 | if (!opts.blockchain_id) {
|
1013 | blockchain_id = getSessionBlockchainID();
|
1014 | }
|
1015 |
|
1016 | return datastoreMountOrCreate()
|
1017 | .then((ds) => {
|
1018 |
|
1019 | assert(ds);
|
1020 |
|
1021 | const datastore_id = ds.datastore_id;
|
1022 | const device_list = getDeviceList(ds);
|
1023 | const device_pubkeys = getPublicKeyList(ds);
|
1024 | const options = {
|
1025 | 'method': 'GET',
|
1026 | 'host': ds.host,
|
1027 | 'port': ds.port,
|
1028 | 'path': `/v1/stores/${datastore_id}/directories?path=${escape(sanitizePath(path))}&idata=1&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`,
|
1029 | };
|
1030 |
|
1031 | let schema = MUTABLE_DATUM_DIR_IDATA_SCHEMA;
|
1032 |
|
1033 | if (!opts) {
|
1034 | opts = {};
|
1035 | }
|
1036 |
|
1037 | if (opts.extended) {
|
1038 | options['path'] += '&extended=1';
|
1039 | schema = MUTABLE_DATUM_EXTENDED_RESPONSE_SCHEMA;
|
1040 | }
|
1041 |
|
1042 | if (opts.force) {
|
1043 | optsion['path'] += '&force=1';
|
1044 | }
|
1045 |
|
1046 | if (ds.session_token) {
|
1047 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
1048 | }
|
1049 |
|
1050 | return httpRequest(options, schema)
|
1051 | .then((response) => {
|
1052 | if (response.error || response.errno) {
|
1053 | let errorMsg = response.error || 'UNKNOWN';
|
1054 | let errorNo = response.errno || 'UNKNOWN';
|
1055 | throw new Error(`Failed to listdir ${path} (errno: ${errorNo}): ${errorMsg}`);
|
1056 | }
|
1057 | else {
|
1058 | return response;
|
1059 | }
|
1060 | });
|
1061 | });
|
1062 | }
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 | export function stat(path, opts={}) {
|
1079 |
|
1080 | let ds = opts.ds;
|
1081 | let blockchain_id = opts.blockchain_id;
|
1082 |
|
1083 | if (!opts.blockchain_id) {
|
1084 | blockchain_id = getSessionBlockchainID();
|
1085 | }
|
1086 |
|
1087 | return datastoreMountOrCreate()
|
1088 | .then((ds) => {
|
1089 |
|
1090 | assert(ds);
|
1091 |
|
1092 | const datastore_id = ds.datastore_id;
|
1093 | const device_list = getDeviceList(ds);
|
1094 | const device_pubkeys = getPublicKeyList(ds);
|
1095 | const options = {
|
1096 | 'method': 'GET',
|
1097 | 'host': ds.host,
|
1098 | 'port': ds.port,
|
1099 | 'path': `/v1/stores/${datastore_id}/inodes?path=${escape(sanitizePath(path))}&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`,
|
1100 | };
|
1101 |
|
1102 | let schema = MUTABLE_DATUM_INODE_SCHEMA;
|
1103 |
|
1104 | if (!opts) {
|
1105 | opts = {};
|
1106 | }
|
1107 |
|
1108 | if (opts.extended) {
|
1109 | options['path'] += '&extended=1';
|
1110 | schema = MUTABLE_DATUM_EXTENDED_RESPONSE_SCHEMA;
|
1111 | }
|
1112 |
|
1113 | if (opts.force) {
|
1114 | optsion['path'] += '&force=1';
|
1115 | }
|
1116 |
|
1117 | if (ds.session_token) {
|
1118 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
1119 | }
|
1120 |
|
1121 | return httpRequest(options, schema)
|
1122 | .then((response) => {
|
1123 | if (response.error || response.errno) {
|
1124 | let errorMsg = response.error || 'UNKNOWN';
|
1125 | let errorNo = response.errno || 'UNKNOWN';
|
1126 | throw new Error(`Failed to stat ${path} (errno: ${errorNo}): ${errorMsg}`);
|
1127 | }
|
1128 | else {
|
1129 | return response;
|
1130 | }
|
1131 | });
|
1132 | });
|
1133 | }
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 | function getInode(path, opts=null) {
|
1151 |
|
1152 | let blockchain_id = opts.blockchain_id;
|
1153 |
|
1154 | if (!opts.blockchain_id) {
|
1155 | blockchain_id = getSessionBlockchainID();
|
1156 | }
|
1157 |
|
1158 | return datastoreMountOrCreate()
|
1159 | .then((ds) => {
|
1160 |
|
1161 | assert(ds);
|
1162 |
|
1163 | const datastore_id = ds.datastore_id;
|
1164 | const device_list = getDeviceList(ds);
|
1165 | const device_pubkeys = getPublicKeyList(ds);
|
1166 | const options = {
|
1167 | 'method': 'GET',
|
1168 | 'host': ds.host,
|
1169 | 'port': ds.port,
|
1170 | 'path': `/v1/stores/${datastore_id}/inodes?path=${escape(sanitizePath(path))}&idata=1&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`,
|
1171 | };
|
1172 |
|
1173 | let schema = MUTABLE_DATUM_INODE_SCHEMA;
|
1174 |
|
1175 | if (!opts) {
|
1176 | opts = {};
|
1177 | }
|
1178 |
|
1179 | if (opts.extended) {
|
1180 | options['path'] += '&extended=1';
|
1181 | schema = MUTABLE_DATUM_EXTENDED_RESPONSE_SCHEMA;
|
1182 | }
|
1183 |
|
1184 | if (opts.force) {
|
1185 | options['path'] += '&force=1';
|
1186 | }
|
1187 |
|
1188 | if (ds.session_token) {
|
1189 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
1190 | }
|
1191 |
|
1192 | return httpRequest(options, schema)
|
1193 | .then((response) => {
|
1194 | if (response.error || response.errno) {
|
1195 | let errorMsg = response.error || 'UNKNOWN';
|
1196 | let errorNo = response.errno || 'UNKNOWN';
|
1197 | throw new Error(`Failed to getInode ${path} (errno: ${errorNo}): ${errorMsg}`);
|
1198 | }
|
1199 | else {
|
1200 | return response;
|
1201 | }
|
1202 | });
|
1203 | });
|
1204 | }
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | export function getFile(path, opts={}) {
|
1222 |
|
1223 | let blockchain_id = opts.blockchain_id;
|
1224 |
|
1225 | if (!opts.blockchain_id) {
|
1226 | blockchain_id = getSessionBlockchainID();
|
1227 | }
|
1228 |
|
1229 | return datastoreMountOrCreate()
|
1230 | .then((ds) => {
|
1231 | assert(ds);
|
1232 |
|
1233 | const datastore_id = ds.datastore_id;
|
1234 | const device_list = getDeviceList(ds);
|
1235 | const device_pubkeys = getPublicKeyList(ds);
|
1236 | const options = {
|
1237 | 'method': 'GET',
|
1238 | 'host': ds.host,
|
1239 | 'port': ds.port,
|
1240 | 'path': `/v1/stores/${datastore_id}/files?path=${escape(sanitizePath(path))}&idata=1&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`,
|
1241 | };
|
1242 |
|
1243 | let schema = 'bytes';
|
1244 |
|
1245 | if (!opts) {
|
1246 | opts = {};
|
1247 | }
|
1248 |
|
1249 | if (opts.extended) {
|
1250 | options['path'] += '&extended=1';
|
1251 | schema = MUTABLE_DATUM_EXTENDED_RESPONSE_SCHEMA;
|
1252 | }
|
1253 |
|
1254 | if (opts.force) {
|
1255 | options['path'] += '&force=1';
|
1256 | }
|
1257 |
|
1258 | if (ds.session_token) {
|
1259 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
1260 | }
|
1261 |
|
1262 | return httpRequest(options, schema)
|
1263 | .then((response) => {
|
1264 | if (response.error || response.errno) {
|
1265 |
|
1266 | if (response.errno === ENOENT) {
|
1267 | return null;
|
1268 | }
|
1269 |
|
1270 |
|
1271 | let errorMsg = response.error || 'UNKNOWN';
|
1272 | let errorNo = response.errno || 'UNKNOWN';
|
1273 | throw new Error(`Failed to getFile ${path} (errno: ${errorNo}): ${errorMsg}`);
|
1274 | }
|
1275 | else {
|
1276 | return response;
|
1277 | }
|
1278 | });
|
1279 | });
|
1280 | }
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 | function datastoreOperation(ds, operation, path, inodes, payloads, signatures, tombstones) {
|
1297 |
|
1298 | let request_path = null;
|
1299 | let http_operation = null;
|
1300 | const datastore_id = ds.datastore_id;
|
1301 | const datastore_privkey = ds.privkey_hex;
|
1302 | const device_list = getDeviceList(ds);
|
1303 | const device_pubkeys = getPublicKeyList(ds);
|
1304 |
|
1305 | assert(inodes.length === payloads.length);
|
1306 | assert(payloads.length === signatures.length);
|
1307 |
|
1308 | if (operation === 'mkdir') {
|
1309 | request_path = `/v1/stores/${datastore_id}/directories?path=${escape(sanitizePath(path))}&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`;
|
1310 | http_operation = 'POST';
|
1311 |
|
1312 | assert(inodes.length === 2);
|
1313 | }
|
1314 | else if (operation === 'putFile') {
|
1315 | request_path = `/v1/stores/${datastore_id}/files?path=${escape(sanitizePath(path))}&device_ids=${device_list}&device_pubkeys=${device_pubkeys}&blockchain_id=${ds.blockchain_id}`;
|
1316 | http_operation = 'PUT';
|
1317 |
|
1318 | assert(inodes.length === 1 || inodes.length === 2);
|
1319 | }
|
1320 | else if (operation === 'rmdir') {
|
1321 | request_path = `/v1/stores/${datastore_id}/directories?path=${escape(sanitizePath(path))}&device_pubkeys=${device_pubkeys}&device_ids=${device_list}&blockchain_id=${ds.blockchain_id}`;
|
1322 | http_operation = 'DELETE';
|
1323 |
|
1324 | assert(inodes.length === 1);
|
1325 | assert(tombstones.length >= 1);
|
1326 | }
|
1327 | else if (operation === 'deleteFile') {
|
1328 | request_path = `/v1/stores/${datastore_id}/files?path=${escape(sanitizePath(path))}&device_pubkeys=${device_pubkeys}&device_ids=${device_list}&blockchain_id=${ds.blockchain_id}`;
|
1329 | http_operation = 'DELETE';
|
1330 |
|
1331 | assert(inodes.length === 1);
|
1332 | assert(tombstones.length >= 1);
|
1333 | }
|
1334 | else {
|
1335 | console.log(`invalid operation ${operation}`);
|
1336 | throw new Error(`Invalid operation ${operation}`);
|
1337 | }
|
1338 |
|
1339 | const options = {
|
1340 | 'method': http_operation,
|
1341 | 'host': ds.host,
|
1342 | 'port': ds.port,
|
1343 | 'path': request_path,
|
1344 | };
|
1345 |
|
1346 | if (ds.session_token) {
|
1347 | options['headers'] = {'Authorization': `bearer ${ds.session_token}`};
|
1348 | }
|
1349 |
|
1350 | const datastore_str = JSON.stringify(ds.datastore);
|
1351 | const datastore_sig = signRawData( datastore_str, datastore_privkey );
|
1352 |
|
1353 | const body_struct = {
|
1354 | 'inodes': inodes,
|
1355 | 'payloads': payloads,
|
1356 | 'signatures': signatures,
|
1357 | 'tombstones': tombstones,
|
1358 | 'datastore_str': datastore_str,
|
1359 | 'datastore_sig': datastore_sig,
|
1360 | }
|
1361 |
|
1362 | const body = JSON.stringify(body_struct);
|
1363 | options['headers']['Content-Type'] = 'application/json';
|
1364 | options['headers']['Content-Length'] = body.length;
|
1365 |
|
1366 | return httpRequest(options, SUCCESS_FAIL_SCHEMA, body)
|
1367 | .then((response) => {
|
1368 | if (response.error || response.errno) {
|
1369 | let errorMsg = response.error || 'UNKNOWN';
|
1370 | let errorNo = response.errno || 'UNKNOWN';
|
1371 | throw new Error(`Failed to ${operation} ${path} (errno: ${errorNo}): ${errorMsg}`);
|
1372 | }
|
1373 | else {
|
1374 | return true;
|
1375 | }
|
1376 | });
|
1377 | }
|
1378 |
|
1379 |
|
1380 |
|
1381 |
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 |
|
1392 |
|
1393 |
|
1394 | function getParent(path, opts={}) {
|
1395 | const dirpath = dirname(path);
|
1396 | return getInode(dirpath, opts)
|
1397 | .then((inode) => {
|
1398 | if (!inode) {
|
1399 | return {'error': 'Failed to get parent', 'errno': EREMOTEIO};
|
1400 | }
|
1401 | if (inode.type !== MUTABLE_DATUM_DIR_TYPE) {
|
1402 | return {'error': 'Not a directory', 'errno': ENOTDIR}
|
1403 | }
|
1404 | else {
|
1405 | return inode;
|
1406 | }
|
1407 | },
|
1408 | (error_resp) => {
|
1409 | return {'error': 'Failed to get inode', 'errno': EREMOTEIO};
|
1410 | });
|
1411 | }
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 |
|
1422 |
|
1423 |
|
1424 |
|
1425 |
|
1426 |
|
1427 |
|
1428 | export function putFile(path, file_buffer, opts={}) {
|
1429 |
|
1430 | let blockchain_id = opts.blockchain_id;
|
1431 |
|
1432 | if (!opts.blockchain_id) {
|
1433 | blockchain_id = getSessionBlockchainID();
|
1434 | }
|
1435 |
|
1436 | return datastoreMountOrCreate()
|
1437 | .then((ds) => {
|
1438 |
|
1439 | assert(ds);
|
1440 |
|
1441 | const datastore_id = ds.datastore_id;
|
1442 | const device_id = ds.device_id;
|
1443 | const privkey_hex = ds.privkey_hex;
|
1444 |
|
1445 | path = sanitizePath(path);
|
1446 | const child_name = basename(path);
|
1447 |
|
1448 | assert(typeof(file_buffer) === 'string' || (file_buffer instanceof Buffer));
|
1449 |
|
1450 |
|
1451 | return getParent(path, opts)
|
1452 | .then((parent_dir) => {
|
1453 | if (parent_dir.error) {
|
1454 | return parent_dir;
|
1455 | }
|
1456 |
|
1457 |
|
1458 | let file_payload = file_buffer;
|
1459 | let file_hash = null;
|
1460 | if (typeof(file_payload) !== 'string') {
|
1461 |
|
1462 | file_payload = file_buffer.toString('base64');
|
1463 | file_hash = hashDataPayload( file_buffer.toString() );
|
1464 | }
|
1465 | else {
|
1466 |
|
1467 | file_payload = Buffer.from(file_buffer).toString('base64');
|
1468 | file_hash = hashDataPayload( file_buffer );
|
1469 | }
|
1470 |
|
1471 | assert(file_hash);
|
1472 |
|
1473 | let inode_uuid = null;
|
1474 | let new_parent_dir_inode = null;
|
1475 | let child_version = null;
|
1476 |
|
1477 |
|
1478 | if (Object.keys(parent_dir['idata']['children']).includes(child_name)) {
|
1479 |
|
1480 |
|
1481 | inode_uuid = parent_dir['idata']['children'][child_name]['uuid'];
|
1482 | new_parent_dir_inode = inodeDirLink(parent_dir, MUTABLE_DATUM_FILE_TYPE, child_name, inode_uuid, true );
|
1483 | }
|
1484 | else {
|
1485 |
|
1486 |
|
1487 | inode_uuid = uuid4();
|
1488 | new_parent_dir_inode = inodeDirLink(parent_dir, MUTABLE_DATUM_FILE_TYPE, child_name, inode_uuid, false );
|
1489 | }
|
1490 |
|
1491 | const version = getChildVersion(parent_dir, child_name);
|
1492 | const inode_info = makeFileInodeBlob( datastore_id, datastore_id, inode_uuid, file_hash, device_id, version );
|
1493 | const inode_sig = signDataPayload( inode_info['header'], privkey_hex );
|
1494 |
|
1495 |
|
1496 | const new_parent_info = makeDirInodeBlob( datastore_id, new_parent_dir_inode['owner'], new_parent_dir_inode['uuid'], new_parent_dir_inode['idata']['children'], device_id, new_parent_dir_inode['version'] + 1);
|
1497 | const new_parent_sig = signDataPayload( new_parent_info['header'], privkey_hex );
|
1498 |
|
1499 |
|
1500 | const new_parent_info_b64 = new Buffer(new_parent_info['idata']).toString('base64');
|
1501 | return datastoreOperation(ds, 'putFile', path, [inode_info['header'], new_parent_info['header']], [file_payload, new_parent_info_b64], [inode_sig, new_parent_sig], []);
|
1502 | });
|
1503 | });
|
1504 | }
|
1505 |
|
1506 |
|
1507 |
|
1508 |
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 | export function mkdir(path, opts={}) {
|
1519 |
|
1520 | let blockchain_id = opts.blockchain_id;
|
1521 |
|
1522 | if (!opts.blockchain_id) {
|
1523 | blockchain_id = getSessionBlockchainID();
|
1524 | }
|
1525 |
|
1526 | return datastoreMountOrCreate()
|
1527 | .then((ds) => {
|
1528 |
|
1529 | assert(ds);
|
1530 |
|
1531 | const datastore_id = ds.datastore_id;
|
1532 | const device_id = ds.device_id;
|
1533 | const privkey_hex = ds.privkey_hex;
|
1534 |
|
1535 | path = sanitizePath(path);
|
1536 | const child_name = basename(path);
|
1537 |
|
1538 | return getParent(path, opts)
|
1539 | .then((parent_dir) => {
|
1540 | if (parent_dir.error) {
|
1541 | return parent_dir;
|
1542 | }
|
1543 |
|
1544 |
|
1545 | if (Object.keys(parent_dir['idata']['children']).includes(child_name)) {
|
1546 | return {'error': 'File or directory exists', 'errno': EEXIST};
|
1547 | }
|
1548 |
|
1549 |
|
1550 | const inode_uuid = uuid4();
|
1551 | const inode_info = makeDirInodeBlob( datastore_id, datastore_id, inode_uuid, {}, device_id);
|
1552 | const inode_sig = signDataPayload( inode_info['header'], privkey_hex );
|
1553 |
|
1554 |
|
1555 | const new_parent_dir_inode = inodeDirLink(parent_dir, MUTABLE_DATUM_DIR_TYPE, child_name, inode_uuid);
|
1556 | const new_parent_info = makeDirInodeBlob( datastore_id, new_parent_dir_inode['owner'], new_parent_dir_inode['uuid'], new_parent_dir_inode['idata']['children'], device_id, new_parent_dir_inode['version'] + 1);
|
1557 | const new_parent_sig = signDataPayload( new_parent_info['header'], privkey_hex );
|
1558 |
|
1559 |
|
1560 | return datastoreOperation(ds, 'mkdir', path, [inode_info['header'], new_parent_info['header']], [inode_info['idata'], new_parent_info['idata']], [inode_sig, new_parent_sig], []);
|
1561 | });
|
1562 | });
|
1563 | }
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 |
|
1577 | export function deleteFile(path, opts={}) {
|
1578 |
|
1579 | let blockchain_id = opts.blockchain_id;
|
1580 |
|
1581 | if (!opts.blockchain_id) {
|
1582 | blockchain_id = getSessionBlockchainID();
|
1583 | }
|
1584 |
|
1585 | return datastoreMountOrCreate()
|
1586 | .then((ds) => {
|
1587 |
|
1588 | assert(ds);
|
1589 |
|
1590 | const datastore_id = ds.datastore_id;
|
1591 | const device_id = ds.device_id;
|
1592 | const privkey_hex = ds.privkey_hex;
|
1593 | const all_device_ids = ds.datastore.device_ids;
|
1594 |
|
1595 | path = sanitizePath(path);
|
1596 | const child_name = basename(path);
|
1597 |
|
1598 | return getParent(path, opts)
|
1599 | .then((parent_dir) => {
|
1600 | if (parent_dir.error) {
|
1601 | return parent_dir;
|
1602 | }
|
1603 |
|
1604 |
|
1605 | if (!Object.keys(parent_dir['idata']['children']).includes(child_name)) {
|
1606 | return {'error': 'No such file or directory', 'errno': ENOENT};
|
1607 | }
|
1608 |
|
1609 | const inode_uuid = parent_dir['idata']['children'][child_name]['uuid'];
|
1610 |
|
1611 |
|
1612 | const new_parent_dir_inode = inodeDirUnlink(parent_dir, child_name);
|
1613 | const new_parent_info = makeDirInodeBlob( datastore_id, new_parent_dir_inode['owner'], new_parent_dir_inode['uuid'], new_parent_dir_inode['idata']['children'], device_id, new_parent_dir_inode['version'] + 1 );
|
1614 | const new_parent_sig = signDataPayload( new_parent_info['header'], privkey_hex );
|
1615 |
|
1616 |
|
1617 | const tombstones = makeInodeTombstones(datastore_id, inode_uuid, all_device_ids);
|
1618 | const signed_tombstones = signMutableDataTombstones(tombstones, privkey_hex);
|
1619 |
|
1620 |
|
1621 | return datastoreOperation(ds, 'deleteFile', path, [new_parent_info['header']], [new_parent_info['idata']], [new_parent_sig], signed_tombstones);
|
1622 | });
|
1623 | });
|
1624 | }
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 | export function rmdir(path, opts={}) {
|
1639 |
|
1640 | let blockchain_id = opts.blockchain_id;
|
1641 |
|
1642 | if (!opts.blockchain_id) {
|
1643 | blockchain_id = getSessionBlockchainID();
|
1644 | }
|
1645 |
|
1646 | return datastoreMountOrCreate()
|
1647 | .then((ds) => {
|
1648 |
|
1649 | assert(ds);
|
1650 |
|
1651 | const datastore_id = ds.datastore_id;
|
1652 | const device_id = ds.device_id;
|
1653 | const privkey_hex = ds.privkey_hex;
|
1654 | const all_device_ids = ds.datastore.device_ids;
|
1655 |
|
1656 | path = sanitizePath(path);
|
1657 | const child_name = basename(path);
|
1658 |
|
1659 | return getParent(path, opts)
|
1660 | .then((parent_dir) => {
|
1661 | if (parent_dir.error) {
|
1662 | return parent_dir;
|
1663 | }
|
1664 |
|
1665 |
|
1666 | if (!Object.keys(parent_dir['idata']['children']).includes(child_name)) {
|
1667 | return {'error': 'No such file or directory', 'errno': ENOENT};
|
1668 | }
|
1669 |
|
1670 | const inode_uuid = parent_dir['idata']['children'][child_name]['uuid'];
|
1671 |
|
1672 |
|
1673 | const new_parent_dir_inode = inodeDirUnlink(parent_dir, child_name);
|
1674 | const new_parent_info = makeDirInodeBlob( datastore_id, new_parent_dir_inode['owner'], new_parent_dir_inode['uuid'], new_parent_dir_inode['idata']['children'], device_id, new_parent_dir_inode['version'] + 1 );
|
1675 | const new_parent_sig = signDataPayload( new_parent_info['header'], privkey_hex );
|
1676 |
|
1677 |
|
1678 | const tombstones = makeInodeTombstones(datastore_id, inode_uuid, all_device_ids);
|
1679 | const signed_tombstones = signMutableDataTombstones(tombstones, privkey_hex);
|
1680 |
|
1681 |
|
1682 | return datastoreOperation(ds, 'rmdir', path, [new_parent_info['header']], [new_parent_info['idata']], [new_parent_sig], signed_tombstones);
|
1683 | });
|
1684 | });
|
1685 | }
|
1686 |
|
1687 |
|