• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkgxdev / pkgx / 6916159303

18 Nov 2023 08:36PM UTC coverage: 97.388% (-0.003%) from 97.391%
6916159303

push

github

mxcl
warn if ~/.local/bin is not in PATH after install

401 of 420 branches covered (0.0%)

1426 of 1456 relevant lines covered (97.94%)

138.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.76
/src/utils/execve.ts
1
import { ProgrammerError } from "./error.ts"
6✔
2
import { utils, Path, PkgxError } from "pkgx"
6✔
3
const { host } = utils
6✔
4

5
export default function({cmd: args, env}: {cmd: string[], env: Record<string, string>}): never {
6✔
6
  /// we need to forward any other env, some vars like HOME are super important
24✔
7
  /// but otherwise the user has set stuff so we should use it
24✔
8
  for (const [key, value] of Object.entries(Deno.env.toObject())) {
24✔
9
    env[key] ??= value
2,121✔
10
  }
2,121✔
11

12
  find_in_PATH(args, env.PATH, Path.abs(env.HOME))
24✔
13

14
  const path = cstr(args[0])
24✔
15
  const argv = new CStringArray(args)
24✔
16
  const envp = new CStringArray(Object.entries(env).map(([key, value]) => `${key}=${value}`))
24✔
17

18
  const errno = _internals.execve(
24✔
19
    Deno.UnsafePointer.of(path),
24✔
20
    Deno.UnsafePointer.of(argv),
24✔
21
    Deno.UnsafePointer.of(envp))
24✔
22

23
  switch (errno) {
24✔
24
    case 2:  //ENOENT:
24✔
25
      // yes: strange behavior from execve here indeed
32✔
26
      if (parse_Path(args[0])?.exists()) {
32✔
27
        throw new Deno.errors.PermissionDenied()
36✔
28
      } else {
36✔
29
        throw new Deno.errors.NotFound()
36✔
30
      }
36✔
31
    case 13:
12✔
32
    case 316:  //FIXME ALERT! ALERT! BUG! SOMETHING IS WRONG WITH OUR USE OR ERRNO AND THIS SOMETIMES RESULTS :/
24✔
33
      throw new Deno.errors.PermissionDenied()
26✔
34
    case 63: //ENAMETOOLONG:
12✔
35
    case 7:  //E2BIG:
12✔
36
    case 14: //EFAULT:
12✔
37
    case 5:  //EIO:
12✔
38
    case 62: //ELOOP:
12✔
39
    case 8:  //ENOEXEC:
12✔
40
    case 12: //ENOMEM:
12✔
41
    case 20: //ENOTDIR:
12✔
42
    case 26: //ETXTBSY:
12✔
43
      throw new PkgxError(`execve (${errno})`)
13✔
44
  }
24✔
45

46
  throw new ProgrammerError(`execve (${errno})`)
31✔
47
}
24✔
48

49
export function parse_Path(input: string) {
6✔
50
  const p = Path.abs(input)
14✔
51
  if (p) return p
14✔
52
  try {
18✔
53
    return _internals.getcwd().join(input)
18✔
54
  } catch {
14✔
55
    return
16✔
56
  }
16✔
57
}
14✔
58

59
function find_in_PATH(cmd: string[], PATH: string | undefined, HOME: Path | undefined) {
24✔
60
  if (cmd[0].startsWith("/")) {
24✔
61
    return
30✔
62
  }
30✔
63
  if (cmd[0].includes("/")) {
24✔
64
    cmd[0] = _internals.getcwd().join(cmd[0]).string
26✔
65
    return
26✔
66
  }
26✔
67

68
  PATH ??= "/usr/bin:/bin"  // see manpage for execvp(3)
34✔
69

70
  for (const part of PATH.split(':')) {
24✔
71
    const path = (() => {
192✔
72
      if (part == '.') return _internals.getcwd()
360✔
73
      if (part == '~') return HOME
360✔
74
      if (part.startsWith('~/')) return HOME?.join(part.slice(2))
360✔
75
      //FIXME: not handled: ~user/...
522✔
76
      return new Path(part)
522✔
77
    })()?.join(cmd[0])
192✔
78
    if (path?.isExecutableFile()) {
192✔
79
      cmd[0] = path.string
194✔
80
      return
194✔
81
    }
194✔
82
  }
192✔
83
}
24✔
84

85

86
////// COPY PASTA
2✔
87
// actually minor mods to add trailing null
2✔
88
// https://github.com/aapoalas/libclang_deno/blob/main/lib/utils.ts
2✔
89

90
const ENCODER = new TextEncoder();
6✔
91

92
export class CStringArray extends Uint8Array {
6✔
93
  constructor(strings: string[]) {
6✔
94
    let stringsLength = 0;
42✔
95
    for (const string of strings) {
42✔
96
      // maximum bytes for a utf8 string is 4×length
2,243✔
97
      stringsLength += string.length * 4 + 1;
2,243✔
98
    }
2,243✔
99
    super(8 * (strings.length + 1) + stringsLength);
42✔
100
    const pointerBuffer = new BigUint64Array(this.buffer, 0, strings.length + 1);
42✔
101
    const stringsBuffer = new Uint8Array(this.buffer).subarray(
42✔
102
      (strings.length + 1) * 8,
42✔
103
    );
104
    const basePointer = BigInt(
42✔
105
      Deno.UnsafePointer.value(Deno.UnsafePointer.of(stringsBuffer)),
42✔
106
    );
107
    let index = 0;
42✔
108
    let offset = 0;
42✔
109
    for (const string of strings) {
42✔
110
      const start = offset;
2,243✔
111
      const result = ENCODER.encodeInto(
2,243✔
112
        string,
2,243✔
113
        stringsBuffer.subarray(start),
2,243✔
114
      );
115
      offset = start + result.written + 1; // Leave null byte
2,243✔
116
      pointerBuffer[index++] = basePointer + BigInt(start);
2,243✔
117
    }
2,243✔
118
  }
42✔
119
}
6✔
120

121
export const cstr = (string: string): Uint8Array =>
6✔
122
  ENCODER.encode(`${string}\0`);
6✔
123

124
function execve(arg0: Deno.PointerValue, args: Deno.PointerValue, env: Deno.PointerValue) {
18✔
125
  const filename = host().platform == 'darwin' ? '/usr/lib/libSystem.dylib' : 'libc.so.6'
×
126

127
  const libc = Deno.dlopen(
18✔
128
    filename, {
18✔
129
      execve: {
18✔
130
        parameters: ["pointer", "pointer", "pointer"],
90✔
131
        result: "i32"
18✔
132
      },
18✔
133
      errno: {
18✔
134
        type: "i32"
18✔
135
      }
18✔
136
    }
18✔
137
  )
138

139
  try {
18✔
140
    libc.symbols.execve(arg0, args, env)
18✔
141
    return libc.symbols.errno
18✔
142
  } finally {
18✔
143
    libc.close()
18✔
144
  }
18✔
145
}
18✔
146

147
export const _internals = {
6✔
148
  execve,
6✔
149
  getcwd: Path.cwd
6✔
150
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc