source: node_modules/detect-libc/lib/filesystem.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1// Copyright 2017 Lovell Fuller and others.
2// SPDX-License-Identifier: Apache-2.0
3
4'use strict';
5
6const fs = require('fs');
7
8const LDD_PATH = '/usr/bin/ldd';
9const SELF_PATH = '/proc/self/exe';
10const MAX_LENGTH = 2048;
11
12/**
13 * Read the content of a file synchronous
14 *
15 * @param {string} path
16 * @returns {Buffer}
17 */
18const 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 */
32const 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
46module.exports = {
47 LDD_PATH,
48 SELF_PATH,
49 readFileSync,
50 readFile
51};
Note: See TracBrowser for help on using the repository browser.