From b9f5cda917a391c1c6612903980381f6e746318a7b83a9a281bb72d37429efb3 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Fri, 1 Feb 2019 15:58:55 +0000 Subject: [PATCH] - Change the shell script to regular executable OBS-URL: https://build.opensuse.org/package/show/devel:languages:nodejs/nodejs-common?expand=0&rev=11 --- node | 11 ------ node.c | 85 +++++++++++++++++++++++++++++++++++++++++++ nodejs-common.changes | 5 +++ nodejs-common.spec | 22 +++++------ 4 files changed, 100 insertions(+), 23 deletions(-) delete mode 100644 node create mode 100644 node.c diff --git a/node b/node deleted file mode 100644 index aa4ce44..0000000 --- a/node +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -PROG=$(basename $0) -PROG_VERSION=${NODE_VERSION:--default} - -if [ ! -x /usr/bin/${PROG}${PROG_VERSION} ]; then - echo "${PROG}${PROG_VERSION} is unavailable." - exit 127 -fi -exec /usr/bin/${PROG}${PROG_VERSION} "$@" - diff --git a/node.c b/node.c new file mode 100644 index 0000000..0bec3ef --- /dev/null +++ b/node.c @@ -0,0 +1,85 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include + +const unsigned min_version = 4; +const unsigned max_version = 20; +const char *default_version = "-default"; + +const char * const supported_execs[] = { + "node", + "npm", + "npx", + NULL +}; + + +static void __attribute__((noreturn)) printInvalidVersion(const char *version) { + fprintf(stderr, "Invalid node version: %s\n", version); + exit(-2); +} + + +int main(int argc, char *argv[]) +{ + if (argc < 1) { + fprintf(stderr, "Invalid parameters ... no basename?\n"); + return 128; + } + + /* Verify we are called with supported name */ + const char *program_name = basename(argv[0]); + const char * const * bn = supported_execs; + + for (; *bn!=NULL; bn++) { + if (strcmp(*bn, program_name) == 0) + break; + } + + if (*bn == NULL) { + fprintf(stderr, "Invalid program called: '%s'\n", program_name); + return 129; + } + + /* Verify we have one of probably supported versions */ + const char *version = getenv("NODE_VERSION"); + char *endptr = 0; + if (version == NULL || strcmp(version, default_version) == 0) + version = default_version; + + unsigned long node_ver = strtoul(version, &endptr, 10); + + if (*endptr == '\0' && + ( node_ver < min_version || node_ver > max_version)) + { + printInvalidVersion(version); + } + else if (*endptr != '\0' && version != default_version) + { + printInvalidVersion(version); + } + + /* Generate our program path and check that we can execute it */ + char *program_path, *program; + if (asprintf(&program, "%s%s", *bn, version) == -1 || + asprintf(&program_path, "/usr/bin/%s", program) == -1) { + + fputs("Memory allocation error... terminating\n", stderr); + return 130; + } + + if (access(program_path, X_OK) != 0) { + perror(program_path); + return -1; + } + + argv[0] = program; + + execv(program_path, argv); + perror("execv failed"); + return -1; +} + diff --git a/nodejs-common.changes b/nodejs-common.changes index 769a088..e1e6140 100644 --- a/nodejs-common.changes +++ b/nodejs-common.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Fri Feb 1 15:03:12 UTC 2019 - adam.majer@suse.de + +- Change the shell script to regular executable + ------------------------------------------------------------------- Fri Jun 22 12:31:05 UTC 2018 - dcassany@suse.de diff --git a/nodejs-common.spec b/nodejs-common.spec index d051e58..f85d09a 100644 --- a/nodejs-common.spec +++ b/nodejs-common.spec @@ -1,7 +1,7 @@ # # spec file for package nodejs-common # -# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany. +# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -12,7 +12,7 @@ # license that conforms to the Open Source Definition (Version 1.9) # published by the Open Source Initiative. -# Please submit bugfixes or comments via http://bugs.opensuse.org/ +# Please submit bugfixes or comments via https://bugs.opensuse.org/ # @@ -26,22 +26,20 @@ ########################################################### Name: nodejs-common -Version: 2.0 +Version: 3.0 Release: 0 Summary: Common files for the NodeJS ecosystem License: MIT Group: Development/Languages/NodeJS Url: https://github.com/AdamMajer/nodejs-packaging -Source1: node +Source1: node.c Source2: LICENSE Requires: nodejs Conflicts: nodejs4 < 4.8.4 Conflicts: nodejs6 < 6.11.1 Conflicts: nodejs7 < 7.10.1 Conflicts: nodejs8 < 8.1.4 - -BuildArch: noarch -BuildRoot: %{_tmppath}/%{name}-%{version}-build +BuildRequires: gcc %description Common NodeJS files that allow recursive invocation of Node executable @@ -50,17 +48,17 @@ while retaining the same codestream version. %prep %build cp %{S:2} . +gcc ${RPM_OPT_FLAGS} -g -o node %{S:1} %install -install -D -m 0755 %{S:1} %{buildroot}%{_bindir}/node -ln -s node %{buildroot}%{_bindir}/npm -ln -s node %{buildroot}%{_bindir}/npx +install -D -m 0755 node %{buildroot}%{_bindir}/node +ln node %{buildroot}%{_bindir}/npm +ln node %{buildroot}%{_bindir}/npx %files -%defattr(-,root,root) +%license LICENSE %{_bindir}/node %{_bindir}/npm %{_bindir}/npx -%license LICENSE %changelog