Skip to content

Commit

Permalink
perf(detect-libc): faster musl check by looking for ldd file
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 16, 2023
1 parent 43e5a43 commit 908f5a1
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 12 deletions.
36 changes: 34 additions & 2 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

const childProcess = require('child_process');
const { isLinux, getReport } = require('./process');
const { LDD_PATH, readFile, readFileSync } = require('./filesystem');

let cachedFilesystem;
const command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true';
let commandOut = '';

Expand Down Expand Up @@ -72,14 +74,41 @@ const familyFromCommand = (out) => {
return null;
};

const familyFromFilesystem = async () => {
if (cachedFilesystem !== undefined) {
return cachedFilesystem;
}
cachedFilesystem = null;
try {
cachedFilesystem = await readFile(LDD_PATH)
.then(content => content.includes(MUSL))
.then(isMusl => isMusl ? MUSL : GLIBC);
} catch (e) {}
return cachedFilesystem;
};

const familyFromFilesystemSync = () => {
if (cachedFilesystem !== undefined) {
return cachedFilesystem;
}
cachedFilesystem = null;
try {
cachedFilesystem = readFileSync(LDD_PATH).includes(MUSL) ? MUSL : GLIBC;
} catch (e) {}
return cachedFilesystem;
};

/**
* Resolves with the libc family when it can be determined, `null` otherwise.
* @returns {Promise<?string>}
*/
const family = async () => {
let family = null;
if (isLinux()) {
family = familyFromReport();
family = await familyFromFilesystem();
if (!family) {
family = familyFromReport();
}
if (!family) {
const out = await safeCommand();
family = familyFromCommand(out);
Expand All @@ -95,7 +124,10 @@ const family = async () => {
const familySync = () => {
let family = null;
if (isLinux()) {
family = familyFromReport();
family = familyFromFilesystemSync();
if (!family) {
family = familyFromReport();
}
if (!family) {
const out = safeCommandSync();
family = familyFromCommand(out);
Expand Down
36 changes: 36 additions & 0 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');

/**
* The path where we can find the ldd
*/
const LDD_PATH = '/usr/bin/ldd';

/**
* Read the content of a file synchronous
*
* @param {string} path
* @returns {string}
*/
const readFileSync = path => fs.readFileSync(path, 'utf-8');

/**
* Read the content of a file
*
* @param {string} path
* @returns {Promise<string>}
*/
const readFile = path => new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});

module.exports = {
LDD_PATH,
readFileSync,
readFile
};
1 change: 1 addition & 0 deletions test/fixtexture-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Loading

0 comments on commit 908f5a1

Please sign in to comment.