|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var storj = require('storj-lib'); |
| 5 | +var bitcore = storj.deps.bitcore; |
| 6 | +var du = require('du'); |
| 7 | +var diskspace = require('fd-diskspace').diskSpace; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Validate the given payout address |
| 12 | + * @param {String} address |
| 13 | + */ |
| 14 | +module.exports._isValidPayoutAddress = function(address) { |
| 15 | + return bitcore.Address.isValid(address) || |
| 16 | + bitcore.Address.isValid(address, bitcore.Networks.testnet); |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Validate the given dataserv directory |
| 21 | + * @param {String} directory |
| 22 | + */ |
| 23 | +module.exports._isValidDirectory = function(directory) { |
| 24 | + return this.existsSync(directory); |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Validate the given size |
| 29 | + * @param {String} size |
| 30 | + */ |
| 31 | +module.exports._isValidSize = function(size) { |
| 32 | + return Number(size) > 0 && typeof size !== 'undefined'; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Validates the space being allocated exists |
| 37 | + * #validateAllocation |
| 38 | + * @param {Object} tab |
| 39 | + */ |
| 40 | +module.exports.validateAllocation = function(conf, callback) { |
| 41 | + this.getFreeSpace(conf.storage.path, function(err, free) { |
| 42 | + var allocatedSpace = this.manualConvert( |
| 43 | + { size: conf.storage.size, unit: conf.storage.unit }, 'B', 0 |
| 44 | + ); |
| 45 | + |
| 46 | + this.getDirectorySize(conf.storage.dataDir, function(err, usedspacebytes) { |
| 47 | + if(err) { |
| 48 | + return callback(err); |
| 49 | + } |
| 50 | + |
| 51 | + var usedspace = this.autoConvert( |
| 52 | + { size: usedspacebytes, unit: 'B' }, 0 |
| 53 | + ); |
| 54 | + |
| 55 | + conf.usedspace = usedspace; |
| 56 | + |
| 57 | + if(allocatedSpace.size > free + usedspacebytes) { |
| 58 | + return callback(new Error('Invalid storage size')); |
| 59 | + } |
| 60 | + return callback(null); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Check if file exists |
| 67 | + * @param {String} file - Path to file |
| 68 | + */ |
| 69 | +module.exports.existsSync = function(file) { |
| 70 | + try { |
| 71 | + fs.statSync(file); |
| 72 | + } catch(err) { |
| 73 | + return !(err); |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Converts to a reasonable unit of bytes |
| 81 | + * @param {Object} bytes |
| 82 | + * @param {Number} precision |
| 83 | + */ |
| 84 | +module.exports.autoConvert = function(object, precision) { |
| 85 | + var kilobyte = 1000; |
| 86 | + var megabyte = kilobyte * 1000; |
| 87 | + var gigabyte = megabyte * 1000; |
| 88 | + var terabyte = gigabyte * 1000; |
| 89 | + |
| 90 | + var byteobject = (this.manualConvert(object, 'B')); |
| 91 | + var bytes = byteobject.size; |
| 92 | + |
| 93 | + if ((bytes >= kilobyte) && (bytes < megabyte)) { |
| 94 | + return this.manualConvert(byteobject, 'KB', (precision || 1)); |
| 95 | + } else if ((bytes >= megabyte) && (bytes < gigabyte)) { |
| 96 | + return this.manualConvert(byteobject, 'MB', (precision || 2)); |
| 97 | + } else if ((bytes >= gigabyte) && (bytes < terabyte)) { |
| 98 | + return this.manualConvert(byteobject, 'GB', (precision || 3)); |
| 99 | + } else if (bytes >= terabyte) { |
| 100 | + return this.manualConvert(byteobject, 'TB', (precision || 4)); |
| 101 | + } |
| 102 | + |
| 103 | + return byteobject; |
| 104 | + |
| 105 | +}; |
| 106 | + |
| 107 | +/** |
| 108 | + * Converts units of bytes to other units |
| 109 | + * @param {Object} object to be converted |
| 110 | + * @param {String} Unit Object will be converted to |
| 111 | + */ |
| 112 | +module.exports.manualConvert = function(object, unit, precision) { |
| 113 | + var table = { |
| 114 | + 'B': 0, |
| 115 | + 'KB': 1, |
| 116 | + 'MB': 2, |
| 117 | + 'GB': 3, |
| 118 | + 'TB': 4 |
| 119 | + }; |
| 120 | + |
| 121 | + precision = (!precision) ? (table[unit] ? table[unit] : 6) : precision; |
| 122 | + |
| 123 | + var diff = table[object.unit] - table[unit]; |
| 124 | + |
| 125 | + if (diff < 0) { |
| 126 | + return { |
| 127 | + size: (object.size / Math.pow(1000, Math.abs(diff))).toFixed(precision), |
| 128 | + unit: unit |
| 129 | + }; |
| 130 | + } else if (diff > 0) { |
| 131 | + return { |
| 132 | + size: (object.size * Math.pow(1000, Math.abs(diff))).toFixed(precision), |
| 133 | + unit: unit |
| 134 | + }; |
| 135 | + } else { |
| 136 | + return object; |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +/** |
| 141 | + * find the difference between two file sizes |
| 142 | + * @param {Object} object |
| 143 | + * @param {Object} object |
| 144 | + */ |
| 145 | +module.exports.subtract = function(object1, object2) { |
| 146 | + var bytes1 = this.manualConvert(object1, 'B'); |
| 147 | + var bytes2 = this.manualConvert(object2, 'B'); |
| 148 | + |
| 149 | + var difference = bytes1.size - bytes2.size; |
| 150 | + |
| 151 | + return this.autoConvert({size: difference, unit: 'B'}); |
| 152 | +}; |
| 153 | + |
| 154 | +/** |
| 155 | + * Recursively determines the size of a directory |
| 156 | + * @param {String} dir - Directory to traverse |
| 157 | + * @param {Function} callback |
| 158 | + */ |
| 159 | +module.exports.getDirectorySize = function(dir, callback) { |
| 160 | + du( |
| 161 | + dir, |
| 162 | + { |
| 163 | + filter: function(f) { |
| 164 | + return ( |
| 165 | + f.indexOf('contracts.db') !== -1 || |
| 166 | + f.indexOf('sharddata.kfs') !== -1 |
| 167 | + ); |
| 168 | + } |
| 169 | + }, |
| 170 | + function (err, size) { |
| 171 | + callback(err,size); |
| 172 | + } |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +/** |
| 177 | + * get free space on disk of path |
| 178 | + * @param {String} path |
| 179 | + */ |
| 180 | +module.exports.getFreeSpace = function(path, callback) { |
| 181 | + var self = this; |
| 182 | + |
| 183 | + if (!this.existsSync(path)) { |
| 184 | + return callback(null, 0); |
| 185 | + } |
| 186 | + |
| 187 | + diskspace(function(err, result) { |
| 188 | + if (err) { |
| 189 | + return callback(err); |
| 190 | + } |
| 191 | + |
| 192 | + var free = 0; |
| 193 | + |
| 194 | + for (var disk in result.disks) { |
| 195 | + var diskDrive = disk; |
| 196 | + |
| 197 | + if (process.platform === 'win32') { |
| 198 | + diskDrive += ':\\'; |
| 199 | + } |
| 200 | + |
| 201 | + if (self.existsSync(diskDrive)) { |
| 202 | + if (fs.statSync(path).dev === fs.statSync(diskDrive).dev) { |
| 203 | + // The `df` command on linux returns KB by default, so we need to |
| 204 | + // convert to bytes. |
| 205 | + free = process.platform === 'win32' ? |
| 206 | + result.disks[disk].free : |
| 207 | + result.disks[disk].free * 1000; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return callback(null, free); |
| 213 | + }); |
| 214 | +}; |
0 commit comments