Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #255 from vinipsmaker/fix-nfs-get-file
Browse files Browse the repository at this point in the history
Fix usage of Node FFI libs
  • Loading branch information
hitman401 authored Dec 1, 2016
2 parents df9f04c + 8f5ebc5 commit fc60f5e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
5 changes: 2 additions & 3 deletions app/ffi/api/appendable_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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]]
Expand Down
40 changes: 18 additions & 22 deletions app/ffi/api/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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}`);
Expand All @@ -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);
});
}

Expand All @@ -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 -
Expand All @@ -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}`);
}
Expand All @@ -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);
});
}

Expand Down
32 changes: 14 additions & 18 deletions app/ffi/api/nfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -430,18 +430,16 @@ 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) {
log.error(`FFI :: NFS :: Get file metadata :: ${err || res}`);
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 -
Expand All @@ -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);
}
Expand All @@ -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}`);
}
Expand All @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions app/ffi/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down

0 comments on commit fc60f5e

Please sign in to comment.