| 1 | // Copyright 2017 Lovell Fuller and others.
|
|---|
| 2 | // SPDX-License-Identifier: Apache-2.0
|
|---|
| 3 |
|
|---|
| 4 | 'use strict';
|
|---|
| 5 |
|
|---|
| 6 | const fs = require('fs');
|
|---|
| 7 |
|
|---|
| 8 | const LDD_PATH = '/usr/bin/ldd';
|
|---|
| 9 | const SELF_PATH = '/proc/self/exe';
|
|---|
| 10 | const MAX_LENGTH = 2048;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Read the content of a file synchronous
|
|---|
| 14 | *
|
|---|
| 15 | * @param {string} path
|
|---|
| 16 | * @returns {Buffer}
|
|---|
| 17 | */
|
|---|
| 18 | const readFileSync = (path) => {
|
|---|
| 19 | const fd = fs.openSync(path, 'r');
|
|---|
| 20 | const buffer = Buffer.alloc(MAX_LENGTH);
|
|---|
| 21 | const bytesRead = fs.readSync(fd, buffer, 0, MAX_LENGTH, 0);
|
|---|
| 22 | fs.close(fd, () => {});
|
|---|
| 23 | return buffer.subarray(0, bytesRead);
|
|---|
| 24 | };
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Read the content of a file
|
|---|
| 28 | *
|
|---|
| 29 | * @param {string} path
|
|---|
| 30 | * @returns {Promise<Buffer>}
|
|---|
| 31 | */
|
|---|
| 32 | const readFile = (path) => new Promise((resolve, reject) => {
|
|---|
| 33 | fs.open(path, 'r', (err, fd) => {
|
|---|
| 34 | if (err) {
|
|---|
| 35 | reject(err);
|
|---|
| 36 | } else {
|
|---|
| 37 | const buffer = Buffer.alloc(MAX_LENGTH);
|
|---|
| 38 | fs.read(fd, buffer, 0, MAX_LENGTH, 0, (_, bytesRead) => {
|
|---|
| 39 | resolve(buffer.subarray(0, bytesRead));
|
|---|
| 40 | fs.close(fd, () => {});
|
|---|
| 41 | });
|
|---|
| 42 | }
|
|---|
| 43 | });
|
|---|
| 44 | });
|
|---|
| 45 |
|
|---|
| 46 | module.exports = {
|
|---|
| 47 | LDD_PATH,
|
|---|
| 48 | SELF_PATH,
|
|---|
| 49 | readFileSync,
|
|---|
| 50 | readFile
|
|---|
| 51 | };
|
|---|