diff --git a/app/ffi/api/appendable_data.js b/app/ffi/api/appendable_data.js index a906e3a6..adc79906 100644 --- a/app/ffi/api/appendable_data.js +++ b/app/ffi/api/appendable_data.js @@ -8,6 +8,7 @@ import { FILTER_TYPE } from '../model/enum'; import log from '../../logger/log'; const int32 = ref.types.int32; +const Int = ref.types.int; const u8 = ref.types.uint8; const u64 = ref.types.uint64; const Void = ref.types.void; @@ -23,8 +24,6 @@ const boolPointer = ref.refType(bool); const size_tPointer = ref.refType(size_t); const Ffi_FilterType = new Enum({ BlackList: 0, WhiteList: 1 }); - -const filterTypePointer = ref.refType(Ffi_FilterType); /* eslint-enable camelcase */ class AppendableData extends FfiApi { @@ -57,7 +56,7 @@ class AppendableData extends FfiApi { appendable_data_clear_deleted_data: [int32, [u64]], appendable_data_remove_nth_deleted_data: [int32, [u64, size_t]], appendable_data_remove_from_filter: [int32, [u64, u64]], - appendable_data_filter_type: [int32, [u64, filterTypePointer]], + appendable_data_filter_type: [int32, [u64, ref.refType(Int)]], appendable_data_num_of_filter_keys: [int32, [u64, size_tPointer]], appendable_data_nth_filter_key: [int32, [u64, size_t, u64Pointer]] // 'appendable_data_delete': [int32, [AppHandle, u64]] diff --git a/app/ffi/api/dns.js b/app/ffi/api/dns.js index 01420e9a..6fe3c5f9 100644 --- a/app/ffi/api/dns.js +++ b/app/ffi/api/dns.js @@ -3,7 +3,8 @@ import ref from 'ref'; import appManager from '../util/app_manager'; import { error, consumeStringListHandle, derefFileMetadataStruct, - FileDetails, FileMetadata, parseExceptionForLog + FileDetails, FileMetadata, parseExceptionForLog, + safeBufferGetter } from '../util/utils'; import FfiApi from '../ffi_api'; import nfs from './nfs'; @@ -75,17 +76,16 @@ class DNS extends FfiApi { return error('Application parameter is mandatory'); } return new Promise((resolve, reject) => { - const listHandlePointer = ref.alloc(PointerToVoidPointer); + const listHandle = ref.alloc(PointerHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: DNS :: List longname :: ${err || res}`); return reject(err || res); } - const listHandle = listHandlePointer.deref(); - resolve(consumeStringListHandle(this.safeCore, listHandle)); + resolve(consumeStringListHandle(this.safeCore, listHandle.deref())); }; this.safeCore.dns_get_long_names.async(appManager.getHandle(app), - listHandlePointer, onResult); + listHandle, onResult); }); } @@ -155,7 +155,7 @@ class DNS extends FfiApi { listServices(app, longName) { return new Promise((resolve, reject) => { - const listHandle = ref.alloc(PointerToVoidPointer); + const listHandle = ref.alloc(PointerHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: DNS :: List service :: ${err || res}`); @@ -172,20 +172,20 @@ class DNS extends FfiApi { getServiceDirectory(app, longName, serviceName) { return new Promise((resolve, reject) => { - const directoryDetailsHandlePointer = ref.alloc(PointerToVoidPointer); + const directoryDetailsHandle = ref.alloc(PointerHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: DNS :: Get service directory :: ${err || res}`); return reject(err || res); } - resolve(nfs.derefDirectoryDetailsHandle(directoryDetailsHandlePointer.deref())); + resolve(nfs.derefDirectoryDetailsHandle(directoryDetailsHandle.deref())); }; const longNameBuffer = new Buffer(longName); const serviceNameBuffer = new Buffer(serviceName); this.safeCore.dns_get_service_dir.async(appManager.getHandle(app), longNameBuffer, longNameBuffer.length, serviceNameBuffer, serviceNameBuffer.length, - directoryDetailsHandlePointer, onResult); + directoryDetailsHandle, onResult); }); } @@ -209,17 +209,15 @@ class DNS extends FfiApi { getFileMetadata(app, longName, serviceName, path) { return new Promise((resolve, reject) => { - const fileMetadataRefRef = ref.alloc(PointerToFileMetadataPointer); + const fileMetadataHandle = ref.alloc(FileMetadataHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: DNS :: Get file metadata :: ${err || res}`); return reject(err || res); } try { - const fileMetadataHandle = fileMetadataRefRef.deref(); - const fileMetadataRef = ref.alloc(FileMetadataHandle, fileMetadataHandle).deref(); - const metadata = derefFileMetadataStruct(fileMetadataRef.deref()); - this.safeCore.file_metadata_drop.async(fileMetadataHandle, () => {}); + const metadata = derefFileMetadataStruct(new FileMetadata(fileMetadataHandle.deref())); + this.safeCore.file_metadata_drop.async(fileMetadataHandle.deref(), () => {}); resolve(metadata); } catch (e) { log.warn(`FFI :: DNS :: Get file metadata :: Caught exception - @@ -233,23 +231,21 @@ class DNS extends FfiApi { longNameBuffer, longNameBuffer.length, serviceNameBuffer, serviceNameBuffer.length, pathBuffer, pathBuffer.length, - fileMetadataRefRef, onResult); + fileMetadataHandle, onResult); }); } readFile(app, longName, serviceName, path, offset, length) { return new Promise((resolve, reject) => { - const fileDetailsPointerHandle = ref.alloc(PointerToFileDetailsPointer); + const fileDetailsHandle = ref.alloc(FileDetailsHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: DNS :: Read file :: ${err || res}`); return reject(err || res); } - const fileDetailsHandle = fileDetailsPointerHandle.deref(); - const handle = ref.alloc(FileDetailsHandle, fileDetailsHandle).deref(); - const fileDetails = handle.deref(); - const data = Buffer.concat([ref.reinterpret(fileDetails.content, fileDetails.content_len)]); - this.safeCore.file_details_drop.async(handle, (e) => { + const fileDetails = new FileDetails(fileDetailsHandle.deref()); + const data = safeBufferGetter(fileDetails, 'content'); + this.safeCore.file_details_drop.async(fileDetailsHandle.deref(), (e) => { if (e) { log.error(`FFI :: DNS :: File details drop :: ${e}`); } @@ -263,7 +259,7 @@ class DNS extends FfiApi { longNameBuffer, longNameBuffer.length, serviceNameBuffer, serviceNameBuffer.length, pathBuffer, pathBuffer.length, - offset, length, false, fileDetailsPointerHandle, onResult); + offset, length, false, fileDetailsHandle, onResult); }); } diff --git a/app/ffi/api/nfs.js b/app/ffi/api/nfs.js index b2b13c34..f057c19b 100644 --- a/app/ffi/api/nfs.js +++ b/app/ffi/api/nfs.js @@ -4,7 +4,7 @@ import uuid from 'uuid'; import { FileMetadata, DirectoryMetadata, FileDetails, error, derefFileMetadataStruct, derefDirectoryMetadataStruct, - parseExceptionForLog + parseExceptionForLog, safeBufferGetter } from '../util/utils'; import FfiApi from '../ffi_api'; import appManager from '../util/app_manager'; @@ -199,7 +199,7 @@ class NFS extends FfiApi { return error('Invalid parameters'); } const self = this; - const dirDetailsHandle = ref.alloc(PointerToVoidPointer); + const dirDetailsHandle = ref.alloc(VoidPointerHandle); const executor = (resolve, reject) => { const onResult = (err, res) => { @@ -290,14 +290,14 @@ class NFS extends FfiApi { } const self = this; const executor = (resolve, reject) => { - const writerVoidPointer = ref.alloc(PointerToVoidPointer); + const writerPointer = ref.alloc(VoidPointerHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: NFS :: Create file :: ${err || res}`); return reject(err || res); } const key = { writerId: uuid.v4() }; - self.writerHolder.set(key, writerVoidPointer.deref()); + self.writerHolder.set(key, writerPointer.deref()); resolve(key); }; const filePathBuff = new Buffer(filePath); @@ -309,7 +309,7 @@ class NFS extends FfiApi { filePathBuff, filePathBuff.length, metadataBuff, (metadataBuff ? metadataBuff.length : 0), isShared, - writerVoidPointer, onResult); + writerPointer, onResult); }; return new Promise(executor); } @@ -430,7 +430,7 @@ class NFS extends FfiApi { return error('Invalid parameters'); } const self = this; - const fileMetadataPointerHandle = ref.alloc(PointerToFileMetadataPointer); + const fileMetadataHandle = ref.alloc(FileMetadataHandle); const executor = (resolve, reject) => { const onResult = (err, res) => { if (err || res !== 0) { @@ -438,10 +438,8 @@ class NFS extends FfiApi { return reject(err || res); } try { - const fileMetadataHandle = fileMetadataPointerHandle.deref(); - const fileMetadataRef = ref.alloc(FileMetadataHandle, fileMetadataHandle).deref(); - const metadata = derefFileMetadataStruct(fileMetadataRef.deref()); - self.safeCore.file_metadata_drop.async(fileMetadataHandle, () => {}); + const metadata = derefFileMetadataStruct(new FileMetadata(fileMetadataHandle.deref())); + self.safeCore.file_metadata_drop.async(fileMetadataHandle.deref(), () => {}); resolve(metadata); } catch (e) { log.error(`FFI :: NFS :: Get file metadata :: Caught exception - @@ -451,7 +449,7 @@ class NFS extends FfiApi { const pathBuff = new Buffer(path); self.safeCore.nfs_get_file_metadata.async(appManager.getHandle(app), - pathBuff, pathBuff.length, isShared, fileMetadataPointerHandle, onResult); + pathBuff, pathBuff.length, isShared, fileMetadataHandle, onResult); }; return new Promise(executor); } @@ -465,17 +463,15 @@ class NFS extends FfiApi { if (length === 0) { return resolve(new Buffer(0)); } - const fileDetailsPointerHandle = ref.alloc(PointerToFileDetailsPointer); + const fileDetailsHandle = ref.alloc(FileDetailsHandle); const onResult = (err, res) => { if (err || res !== 0) { log.error(`FFI :: NFS :: Read file :: ${err || res}`); return reject(err || res); } - const fileDetailsHandle = fileDetailsPointerHandle.deref(); - const handle = ref.alloc(FileDetailsHandle, fileDetailsHandle).deref(); - const fileDetails = handle.deref(); - const data = Buffer.concat([ref.reinterpret(fileDetails.content, fileDetails.content_len)]); - self.safeCore.file_details_drop.async(handle, (e) => { + const fileDetails = new FileDetails(fileDetailsHandle.deref()); + const data = safeBufferGetter(fileDetails, 'content'); + self.safeCore.file_details_drop.async(fileDetailsHandle.deref(), (e) => { if (e) { log.error(`FFI :: NFS :: Drop file detail handle :: ${e}`); } @@ -484,7 +480,7 @@ class NFS extends FfiApi { }; const pathBuffer = new Buffer(path); self.safeCore.nfs_get_file.async(appManager.getHandle(app), offset, length, - pathBuffer, pathBuffer.length, isShared, false, fileDetailsPointerHandle, onResult); + pathBuffer, pathBuffer.length, isShared, false, fileDetailsHandle, onResult); }; return new Promise(executor); } diff --git a/app/ffi/util/utils.js b/app/ffi/util/utils.js index c1454d1c..0b40e41c 100644 --- a/app/ffi/util/utils.js +++ b/app/ffi/util/utils.js @@ -77,6 +77,17 @@ export const derefFileMetadataStruct = (metadataStruct) => { }; }; +export const safeBufferGetter = (obj, ptrFieldName, lenFieldName) => { + let computedLenFieldName = lenFieldName; + if (!lenFieldName) { + computedLenFieldName = `${ptrFieldName}_len`; + } + if (obj[computedLenFieldName] === 0) { + return Buffer.alloc(0); + } + return Buffer.concat([ref.reinterpret(obj[ptrFieldName], obj[computedLenFieldName])]); +}; + export const derefDirectoryMetadataStruct = (metadataStruct) => { let dirName = ''; let dirMetadata = '';