5
5
6
6
const childProcess = require ( 'child_process' ) ;
7
7
const { isLinux, getReport } = require ( './process' ) ;
8
+ const { LDD_PATH , readFile, readFileSync } = require ( './filesystem' ) ;
9
+
10
+ let cachedFamilyFilesystem ;
11
+ let cachedVersionFilesystem ;
8
12
9
13
const command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true' ;
10
14
let commandOut = '' ;
@@ -39,6 +43,13 @@ const safeCommandSync = () => {
39
43
*/
40
44
const GLIBC = 'glibc' ;
41
45
46
+ /**
47
+ * A Regexp constant to get the GLIBC Version.
48
+ * @type {string }
49
+ * @public
50
+ */
51
+ const RE_GLIBC_VERSION = / G L I B C \s ( \d + \. \d + ) / ;
52
+
42
53
/**
43
54
* A String constant containing the value `musl`.
44
55
* @type {string }
@@ -72,14 +83,49 @@ const familyFromCommand = (out) => {
72
83
return null ;
73
84
} ;
74
85
86
+ const familyFromFilesystem = async ( ) => {
87
+ if ( cachedFamilyFilesystem !== undefined ) {
88
+ return cachedFamilyFilesystem ;
89
+ }
90
+ cachedFamilyFilesystem = null ;
91
+ try {
92
+ const lddContent = await readFile ( LDD_PATH ) ;
93
+ if ( lddContent . includes ( 'musl' ) ) {
94
+ cachedFamilyFilesystem = MUSL ;
95
+ } else if ( lddContent . includes ( 'GLIBC' ) ) {
96
+ cachedFamilyFilesystem = GLIBC ;
97
+ }
98
+ } catch ( e ) { }
99
+ return cachedFamilyFilesystem ;
100
+ } ;
101
+
102
+ const familyFromFilesystemSync = ( ) => {
103
+ if ( cachedFamilyFilesystem !== undefined ) {
104
+ return cachedFamilyFilesystem ;
105
+ }
106
+ cachedFamilyFilesystem = null ;
107
+ try {
108
+ const lddContent = readFileSync ( LDD_PATH ) ;
109
+ if ( lddContent . includes ( 'musl' ) ) {
110
+ cachedFamilyFilesystem = MUSL ;
111
+ } else if ( lddContent . includes ( 'GLIBC' ) ) {
112
+ cachedFamilyFilesystem = GLIBC ;
113
+ }
114
+ } catch ( e ) { }
115
+ return cachedFamilyFilesystem ;
116
+ } ;
117
+
75
118
/**
76
119
* Resolves with the libc family when it can be determined, `null` otherwise.
77
120
* @returns {Promise<?string> }
78
121
*/
79
122
const family = async ( ) => {
80
123
let family = null ;
81
124
if ( isLinux ( ) ) {
82
- family = familyFromReport ( ) ;
125
+ family = await familyFromFilesystem ( ) ;
126
+ if ( ! family ) {
127
+ family = familyFromReport ( ) ;
128
+ }
83
129
if ( ! family ) {
84
130
const out = await safeCommand ( ) ;
85
131
family = familyFromCommand ( out ) ;
@@ -95,7 +141,10 @@ const family = async () => {
95
141
const familySync = ( ) => {
96
142
let family = null ;
97
143
if ( isLinux ( ) ) {
98
- family = familyFromReport ( ) ;
144
+ family = familyFromFilesystemSync ( ) ;
145
+ if ( ! family ) {
146
+ family = familyFromReport ( ) ;
147
+ }
99
148
if ( ! family ) {
100
149
const out = safeCommandSync ( ) ;
101
150
family = familyFromCommand ( out ) ;
@@ -116,6 +165,38 @@ const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC;
116
165
*/
117
166
const isNonGlibcLinuxSync = ( ) => isLinux ( ) && familySync ( ) !== GLIBC ;
118
167
168
+ const versionFromFilesystem = async ( ) => {
169
+ if ( cachedVersionFilesystem !== undefined ) {
170
+ return cachedVersionFilesystem ;
171
+ }
172
+ cachedVersionFilesystem = null ;
173
+ try {
174
+ const lddContent = await readFile ( LDD_PATH ) ;
175
+ const versionMatch = lddContent . match ( RE_GLIBC_VERSION ) ;
176
+
177
+ if ( versionMatch ) {
178
+ cachedVersionFilesystem = versionMatch [ 1 ] ;
179
+ }
180
+ } catch ( e ) { }
181
+ return cachedVersionFilesystem ;
182
+ } ;
183
+
184
+ const versionFromFilesystemSync = ( ) => {
185
+ if ( cachedVersionFilesystem !== undefined ) {
186
+ return cachedVersionFilesystem ;
187
+ }
188
+ cachedVersionFilesystem = null ;
189
+ try {
190
+ const lddContent = readFileSync ( LDD_PATH ) ;
191
+ const versionMatch = lddContent . match ( RE_GLIBC_VERSION ) ;
192
+
193
+ if ( versionMatch ) {
194
+ cachedVersionFilesystem = versionMatch [ 1 ] ;
195
+ }
196
+ } catch ( e ) { }
197
+ return cachedVersionFilesystem ;
198
+ } ;
199
+
119
200
const versionFromReport = ( ) => {
120
201
const report = getReport ( ) ;
121
202
if ( report . header && report . header . glibcVersionRuntime ) {
@@ -144,7 +225,10 @@ const versionFromCommand = (out) => {
144
225
const version = async ( ) => {
145
226
let version = null ;
146
227
if ( isLinux ( ) ) {
147
- version = versionFromReport ( ) ;
228
+ version = await versionFromFilesystem ( ) ;
229
+ if ( ! version ) {
230
+ version = versionFromReport ( ) ;
231
+ }
148
232
if ( ! version ) {
149
233
const out = await safeCommand ( ) ;
150
234
version = versionFromCommand ( out ) ;
@@ -160,7 +244,10 @@ const version = async () => {
160
244
const versionSync = ( ) => {
161
245
let version = null ;
162
246
if ( isLinux ( ) ) {
163
- version = versionFromReport ( ) ;
247
+ version = versionFromFilesystemSync ( ) ;
248
+ if ( ! version ) {
249
+ version = versionFromReport ( ) ;
250
+ }
164
251
if ( ! version ) {
165
252
const out = safeCommandSync ( ) ;
166
253
version = versionFromCommand ( out ) ;
0 commit comments