-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.js
133 lines (126 loc) · 2.68 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const fs = require('fs');
/*
* @prototype
*/
const StorageBase = function(){
/*
* @param {integer}
* @public
* @return {string}
*/
this.getString = async function(block){
if (!Number.isInteger( block ))
return false;
if (block >= _block_count)
return false;
return await _getString(block);
}
/*
* @param {string}
* @param {integer}
* @param {integer}
* @public
* @return {boolean}
*/
this.create = async function(
file_name,
block_size,
block_count
){
if (typeof file_name !== 'string')
return false;
if (!Number.isInteger( block_size ))
return false;
if (!Number.isInteger( block_count ))
return false;
return await _createFile(
file_name,
block_size,
block_count
);
}
/*
* @param {string}
* @public
* @return {boolean}
*/
this.open = async function(file_name){
if (typeof file_name !== 'string')
return false;
return await _openFile(file_name);
}
let _busy = false;
let _started = false;
let _file_name = false;
let _file = false;
let _buffer = false;
/*
* @param {string}
* @param {integer}
* @param {integer}
* @private
* @return {boolean}
*/
const _createFile = async function(
file_name,
block_size,
block_count
){
_busy = true;
_file_name = file_name;
_started = true;
}
/*
* @param {string}
* @private
* @return {boolean}
*/
const _checkFile = async function(){
let error;
let stat;
await fs.stat(
_file_name,
function(e, s){
error = e;
stat = s;
}
);
if(error === null)
return true;
return false;
}
/*
* @param {string}
* @private
* @return {boolean}
*/
const _openFile = async function(file_name){
if((await)
let buffer = Buffer.alloc(32)
_file_name = file_name;
await fs.open(
_file_name,
'r',
function(err, fd){
if (err) throw err;
_file = fd;
}
);
}
/*
* @param {integer}
* @private
* @return {string}
*/
const _getString = async function(){
await fs.read(
_file,
_buffer,
0,
2,
16
);
}
}
exports.Base = StorageBase;