<commit_before>(* Copyright (C) 2013, <NAME>
 * See the README file for details, or visit http://0install.net.
 *)

(** Executing a selections document *)

open General
open Support.Common
module Basedir = Support.Basedir

let re_exec_name = Str.regexp "^[^./'][^/']*$";;

let validate_exec_name name =
  if Str.string_match re_exec_name name 0 then
    ()
  else
    raise_safe "Invalid name in executable binding: %s" name

class virtual launcher_builder config script =
  let hash = String.sub (Digest.to_hex @@ Digest.string script) 0 6 in
  object
    method make_dir name =
      Basedir.save_path config.system ("0install.net" +/ "injector" +/ ("exec-" ^ hash) +/ name) config.basedirs.Basedir.cache

    method add_launcher path =
      if not @@ Sys.file_exists path then (
        let write ch = output_string ch script in
        config.system#atomic_write [Open_wronly; Open_binary] write path 0o755
      )

    method setenv name command_argv env =
      let open Yojson.Basic in
      let json :json = `List (List.map (fun a -> `String a) command_argv) in
      Env.putenv ("zeroinstall-runenv-" ^ name) (to_string json) env
  end

(** If abspath_0install is a native binary, we can avoid starting a shell here. *)
class native_launcher_builder config =
  let script = Printf.sprintf "#!%s runenv\n" config.abspath_0install in
  object (_ : #launcher_builder)
    inherit launcher_builder config script
  end

(** We can't use interpreted bytecode as a #! interpreter, so use a shell script instead. *)
class bytecode_launcher_builder config =
  let script = Printf.sprintf "#!/bin/sh\nexec '%s' runenv \"$0\" \"$@\"\n" config.abspath_0install in
  object (_ : #launcher_builder)
    inherit launcher_builder config script
  end

class windows_launcher_builder config =
  let runenv_path =
    match config.system#getenv "ZEROINSTALL_RUNENV" with
    | None -> Filename.dirname config.abspath_0install +/ "0install-runenv.exe"
    | Some path -> path in
  let digest = config.system#with_open_in [Open_rdonly; Open_binary] 0 runenv_path Digest.input in
  object (_ : #launcher_builder)
    inherit launcher_builder config digest

    method! add_launcher path =
      if not @@ Sys.file_exists path then
        config.system#atomic_hardlink ~link_to:runenv_path ~replace:path
  end

let get_launcher_builder config =
  if on_windows then new windows_launcher_builder config
  else
    let buf = String.create 2 in
    let () = config.system#with_open_in [Open_rdonly; Open_binary] 0 config.abspath_0install (fun ch ->
      really_input ch buf 0 2
    )
    in
    if buf = "!#" then
      new bytecode_launcher_builder config
    else
      new native_launcher_builder config

let do_exec_binding dry_run builder env impls (iface_uri, {Binding.exec_type; Binding.name; Binding.command}) =
  validate_exec_name name;

  let exec_dir = builder#make_dir name in
  let exec_path = exec_dir +/ name in
  if not dry_run then (<commit_msg>Fixed #! detection (typo)<commit_after>(* Copyright (C) 2013, <NAME>
 * See the README file for details, or visit http://0install.net.
 *)

(** Executing a selections document *)

open General
open Support.Common
module Basedir = Support.Basedir

let re_exec_name = Str.regexp "^[^./'][^/']*$";;

let validate_exec_name name =
  if Str.string_match re_exec_name name 0 then
    ()
  else
    raise_safe "Invalid name in executable binding: %s" name

class virtual launcher_builder config script =
  let hash = String.sub (Digest.to_hex @@ Digest.string script) 0 6 in
  object
    method make_dir name =
      Basedir.save_path config.system ("0install.net" +/ "injector" +/ ("exec-" ^ hash) +/ name) config.basedirs.Basedir.cache

    method add_launcher path =
      if not @@ Sys.file_exists path then (
        let write ch = output_string ch script in
        config.system#atomic_write [Open_wronly; Open_binary] write path 0o755
      )

    method setenv name command_argv env =
      let open Yojson.Basic in
      let json :json = `List (List.map (fun a -> `String a) command_argv) in
      Env.putenv ("zeroinstall-runenv-" ^ name) (to_string json) env
  end

(** If abspath_0install is a native binary, we can avoid starting a shell here. *)
class native_launcher_builder config =
  let script = Printf.sprintf "#!%s runenv\n" config.abspath_0install in
  object (_ : #launcher_builder)
    inherit launcher_builder config script
  end

(** We can't use interpreted bytecode as a #! interpreter, so use a shell script instead. *)
class bytecode_launcher_builder config =
  let script = Printf.sprintf "#!/bin/sh\nexec '%s' runenv \"$0\" \"$@\"\n" config.abspath_0install in
  object (_ : #launcher_builder)
    inherit launcher_builder config script
  end

class windows_launcher_builder config =
  let runenv_path =
    match config.system#getenv "ZEROINSTALL_RUNENV" with
    | None -> Filename.dirname config.abspath_0install +/ "0install-runenv.exe"
    | Some path -> path in
  let digest = config.system#with_open_in [Open_rdonly; Open_binary] 0 runenv_path Digest.input in
  object (_ : #launcher_builder)
    inherit launcher_builder config digest

    method! add_launcher path =
      if not @@ Sys.file_exists path then
        config.system#atomic_hardlink ~link_to:runenv_path ~replace:path
  end

let get_launcher_builder config =
  if on_windows then new windows_launcher_builder config
  else
    let buf = String.create 2 in
    let () = config.system#with_open_in [Open_rdonly; Open_binary] 0 config.abspath_0install (fun ch ->
      really_input ch buf 0 2
    )
    in
    if buf = "#!" then
      new bytecode_launcher_builder config
    else
      new native_launcher_builder config

let do_exec_binding dry_run builder env impls (iface_uri, {Binding.exec_type; Binding.name; Binding.command}) =
  validate_exec_name name;

  let exec_dir = builder#make_dir name in
  let exec_path = exec_dir +/ name in
  if not dry_run then (
{'fraction_non_alphanumeric': 0.09069373942470389, 'fraction_numerical': 0.008460236886632826, 'mean_word_length': 3.6251956181533647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187988', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1867, 'n_words': 588}