Skip to content

Commit

Permalink
Wrap readdirSync to support image without /usr/sbin (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreprocess authored and lovell committed Nov 22, 2017
1 parent b575859 commit 5173db0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
35 changes: 21 additions & 14 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function versionFromMuslLdd (out) {
return out.split(/[\r\n]+/)[1].trim().split(/\s/)[1];
}

function safeReaddirSync (path) {
try {
return readdirSync(path);
} catch (e) {}
return [];
}

var family = '';
var version = '';
var method = '';
Expand All @@ -52,23 +59,23 @@ if (platform === 'linux') {
method = 'ldd';
} else {
// Try filesystem (family only)
try {
var lib = readdirSync('/lib');
var usrSbin = readdirSync('/usr/sbin');
if (lib.some(contains('-linux-gnu'))) {
family = GLIBC;
method = 'filesystem';
} else if (lib.some(contains('libc.musl-'))) {
family = MUSL;
method = 'filesystem';
} else if (lib.some(contains('ld-musl-'))) {
family = MUSL;
method = 'filesystem';
} else if (usrSbin.some(contains('glibc'))) {
var lib = safeReaddirSync('/lib');
if (lib.some(contains('-linux-gnu'))) {
family = GLIBC;
method = 'filesystem';
} else if (lib.some(contains('libc.musl-'))) {
family = MUSL;
method = 'filesystem';
} else if (lib.some(contains('ld-musl-'))) {
family = MUSL;
method = 'filesystem';
} else {
var usrSbin = safeReaddirSync('/usr/sbin');
if (usrSbin.some(contains('glibc'))) {
family = GLIBC;
method = 'filesystem';
}
} catch (e) {}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "detect-libc",
"version": "1.0.2",
"version": "1.0.3",
"description": "Node.js module to detect the C standard library (libc) implementation family and version",
"main": "lib/detect-libc.js",
"bin": {
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,14 @@
"4.x": { "$detect.method": "getconf" }
},
"$libc.family": "glibc"
},

"minimal": {
"dist": {
"rmg": { "$detect.method": "filesystem", "$libc.family": "glibc", "$libc.version": "unknown", "$dist.name": "unknown", "$node.version": "8.8.1" }
},
"node": {
"8.x": { }
}
}
}
19 changes: 19 additions & 0 deletions tests/integration/spec/minimal/minimal-rmg-nodejs-8.x.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Provided by https://github.com/rmg

FROM node:8.8.1
RUN mkdir -p /rootfs
RUN ldd /bin/sh \
/usr/local/bin/node \
/lib/x86_64-linux-gnu/libnss_files.so.* \
/lib/x86_64-linux-gnu/libnss_dns.so.* \
| grep -o -e '\/\(usr\|lib\)[^ :]\+' \
| sort -u | tee /rootfs.list \
&& echo /bin/sh >> /rootfs.list

# The use of 'tar | tar' here is a quick'n'dirty replacement for rsync without
# adding any deps: rsync --files-from=/rootfs.list -L -pog / /rootfs/
RUN cat /rootfs.list | tar -T- -cphf- | tar -C /rootfs -xpf-

# noop
FROM scratch
COPY --from=0 /rootfs/ /

0 comments on commit 5173db0

Please sign in to comment.