<commit_before>-- Scratchbox2 universal redirector dynamic path translation scripts
-- Copyright (C) 2006, 2007 <NAME>
-- Licensed under MIT license.

local forced_modename = sb.get_forced_mapmode()

-- These must match the flag definitions in mapping.h:
local RULE_FLAGS_READONLY = 1
local RULE_FLAGS_CALL_TRANSLATE_FOR_ALL = 2
local RULE_FLAGS_FORCE_ORIG_PATH = 4

-- rule_file_path and rev_rule_file_path are global varibales
if forced_modename == nil then
	rule_file_path = session_dir .. "/rules/Default.lua"
	rev_rule_file_path = session_dir .. "/rev_rules/Default.lua"
	exec_rule_file_path = session_dir .. "/exec_rules/Default.lua"
	active_mapmode = sbox_mapmode
else
	rule_file_path = session_dir .. "/rules/" .. forced_modename .. ".lua"
	rev_rule_file_path = session_dir .. "/rev_rules/" .. forced_modename .. ".lua"
	exec_rule_file_path = session_dir .. "/exec_rules/" .. forced_modename .. ".lua"
	active_mapmode = forced_modename
end

function basename(path)
	if (path == "/") then
		return "/"
	else
		return string.match(path, "[^/]*$")
	end
end

function dirname(path)
	local dir

	if (path == "/") then
		return "/"
	end
	dir = string.match(path, ".*/")
	if (dir == nil) then
		return "."
	end

	if (dir == "/") then return dir end

	-- chop off the trailing /
	if (string.sub(dir, string.len(dir)) == "/") then
		dir = string.sub(dir, 1, string.len(dir) - 1)
	end
	return dir 
end

-- isprefix(a, b) is now implemented in C; used to be:
--    function isprefix(a, b)
--	if (not a or not b) then return false end
--	return string.sub(b, 1, string.len(a)) == a
--    end
isprefix = sb.isprefix

function sb2_procfs_mapper(binary_name, func_name, rp, path, rule)
	local ret_path = path;

	if (debug_messages_enabled) then
		sb.log("debug", "sb2_procfs_mapper "..path.." : "..rp)
	end

	local mapped = sb.procfs_mapping_request(path)

	-- Returns exec_policy_name, path, flags
	if (mapped) then
		ret_path = mapped
	end
	return nil, ret_path, 0
end

-- Override mapping rules from override_nomap table
--  Each entry from table override_nomap should specify path that shouldn't be remapped
--  Entries from this table will be searched before all other rules
function override_export_chains()
	if (not override_nomap) then
		return export_chains
	end

	override_chain = {
		next_chain = export_chains[1],
		binary = nil,
		rules = { },
	}

	for i, key in pairs(override_nomap) do
		if (key) then
			table.insert(override_chain.rules, {path = key, use_orig_path = true})
		end
	end

	local new_chain = { override_chain, }
	for i, key in pairs(export_chains) do
		table.insert(new_chain, key)
	end
	return new_chain
end

-- Load mode-specific rules.
-- A mode file must define these variables:
--  1. rule_file_interface_version (string) is checked and must match,
--     mismatch is a fatal error.
--  2. fs_mapping_rules (array) contains mapping rules; this array
--     is searched sequentially with the original (unmapped) path as the key
-- Additionally, following variables may be modified:
-- "enable_cross_gcc_toolchain" (default=true): All special processing
--     for the gcc-related tools (gcc,as,ld,..) will be disabled if set
--     to false.
--
function load_and_check_rules()

	-- initialize global variables:
	rule_file_interface_version = nil

	tools = tools_root
	if (not tools) then
		tools = "/"
	end

	-- INTERFACE VERSION between this file, the
	-- exec mapping code (argvenp.lua) and the
	-- rule files:
	--
        -- Version 100:
        -- - Fs rules (path mapping rules) and exec rules 
        --   (exec policies) have been separated.
        --   Fs rules are loaded "always", while exec rules
        --   are loaded only on demand. To be able to do this,
        --   the "exec_policy" field of mapping rules (which
        --   contained a reference to the exec policy object)
        --   was replaced with "exec_policy_name" (string).
        --
	-- Version 25:
	-- - CPU Transparency is now defined by three variables:
	--    - sbox_cputransparency_method is the complete string,
	--      containing both the command and options. This is
	--	defined by the '-c' option of sb2-init.
	--    - sbox_cputransparency_cmd is path to the binary,
	--    - sbox_cputransparency_args contains optional
	--	arguments.
	--   Previously only sbox_cputransparency_method was
	--   available.
	-- Version 24:
	-- - Added support for gconv_path for native applications
	-- Version 23:
	-- - LD_LIBRARY_PATH and LD_PRELOAD are now always
	--   set by argvenvp.lua => the exec_policies must
	--   have proper policies for these (otherwise "fakeroot"
	--   may not perform as expected).
	-- - Note the by default the real LD_LIBRARY_PATH and
	--   LD_PRELOAD that will be used the real exec is now
	--   different than what the user will see in these
	--   variables!
	-- Version 22:
	-- - interface to custom_map_func was modified again:
	--   Last return value is now a bitmask (was a boolean)
	-- Version 21: native_app_message_catalog_prefix has
	-- been removed (it was an exec policy attribute.)
	-- Version 20 changed "script_interpreter_rule" field in
	-- exec policies to "script_interpreter_rules"; find_rule()
	-- is now used to select the rule (there may be more than one!)
	-- (version 19 was an intermediate version)
	-- - added "all_exec_policies" list to all
	--   mapping modes
	-- Differences between version 17 and 18:
	-- - added sb2_procfs_mapper()
	-- Differences between version 16 and 17:
	-- - Added support for hierarcic rules (i.e. rule
	--   trees. 16 supports only linear rule lists)
	-- Differences between version 15 and 16:
	-- - "match" rules are not supported anymore
	-- - interface to custom_map_func was modified:
	--   "work_dir" was removed, name changed to "custom_map_funct",
	--   and such functions are now expected to return 3 values
	--   (previously only one was expected)
	-- - variables "esc_tools_root" and "esc_target_root"
	--   were removed
	local current_rule_interface_version = "100"

	do_file(rule_file_path)
	export_chains = override_export_chains()

	-- fail and die if interface version is incorrect
	if (rule_file_interface_version == nil) or 
           (type(rule_file_interface_version) ~= "string") then
		sb.log("error", string.format(
			"Fatal: Rule file interface version check failed: "..
			"No version information in %s",
			rule_file_path))
		os.exit(89)
	end
	if rule_file_interface_version ~= current_rule_interface_version then
		sb.log("error", string.format(
			"Fatal: Rule file interface version check failed: "..
			"got %s, expected %s", rule_file_interface_version,
			current_rule_interface_version))
		os.exit(88)
	end

        if (type(fs_mapping_rules) ~= "table") then
		sb.log("error", "'fs_mapping_rule' is not an array.");
		os.exit(87)
	end
end

target_root = sbox_target_root
if (not target_root or target_root == "") then<commit_msg>mapping logic: removed support for override_nomap tables.<commit_after>-- Scratchbox2 universal redirector dynamic path translation scripts
-- Copyright (C) 2006, 2007 <NAME>
-- Licensed under MIT license.

local forced_modename = sb.get_forced_mapmode()

-- These must match the flag definitions in mapping.h:
local RULE_FLAGS_READONLY = 1
local RULE_FLAGS_CALL_TRANSLATE_FOR_ALL = 2
local RULE_FLAGS_FORCE_ORIG_PATH = 4

-- rule_file_path and rev_rule_file_path are global varibales
if forced_modename == nil then
	rule_file_path = session_dir .. "/rules/Default.lua"
	rev_rule_file_path = session_dir .. "/rev_rules/Default.lua"
	exec_rule_file_path = session_dir .. "/exec_rules/Default.lua"
	active_mapmode = sbox_mapmode
else
	rule_file_path = session_dir .. "/rules/" .. forced_modename .. ".lua"
	rev_rule_file_path = session_dir .. "/rev_rules/" .. forced_modename .. ".lua"
	exec_rule_file_path = session_dir .. "/exec_rules/" .. forced_modename .. ".lua"
	active_mapmode = forced_modename
end

function basename(path)
	if (path == "/") then
		return "/"
	else
		return string.match(path, "[^/]*$")
	end
end

function dirname(path)
	local dir

	if (path == "/") then
		return "/"
	end
	dir = string.match(path, ".*/")
	if (dir == nil) then
		return "."
	end

	if (dir == "/") then return dir end

	-- chop off the trailing /
	if (string.sub(dir, string.len(dir)) == "/") then
		dir = string.sub(dir, 1, string.len(dir) - 1)
	end
	return dir 
end

-- isprefix(a, b) is now implemented in C; used to be:
--    function isprefix(a, b)
--	if (not a or not b) then return false end
--	return string.sub(b, 1, string.len(a)) == a
--    end
isprefix = sb.isprefix

function sb2_procfs_mapper(binary_name, func_name, rp, path, rule)
	local ret_path = path;

	if (debug_messages_enabled) then
		sb.log("debug", "sb2_procfs_mapper "..path.." : "..rp)
	end

	local mapped = sb.procfs_mapping_request(path)

	-- Returns exec_policy_name, path, flags
	if (mapped) then
		ret_path = mapped
	end
	return nil, ret_path, 0
end

-- Load mode-specific rules.
-- A mode file must define these variables:
--  1. rule_file_interface_version (string) is checked and must match,
--     mismatch is a fatal error.
--  2. fs_mapping_rules (array) contains mapping rules; this array
--     is searched sequentially with the original (unmapped) path as the key
-- Additionally, following variables may be modified:
-- "enable_cross_gcc_toolchain" (default=true): All special processing
--     for the gcc-related tools (gcc,as,ld,..) will be disabled if set
--     to false.
--
function load_and_check_rules()

	-- initialize global variables:
	rule_file_interface_version = nil

	tools = tools_root
	if (not tools) then
		tools = "/"
	end

	-- INTERFACE VERSION between this file, the
	-- exec mapping code (argvenp.lua) and the
	-- rule files:
	--
        -- Version 100:
        -- - Fs rules (path mapping rules) and exec rules 
        --   (exec policies) have been separated.
        --   Fs rules are loaded "always", while exec rules
        --   are loaded only on demand. To be able to do this,
        --   the "exec_policy" field of mapping rules (which
        --   contained a reference to the exec policy object)
        --   was replaced with "exec_policy_name" (string).
        --
	-- Version 25:
	-- - CPU Transparency is now defined by three variables:
	--    - sbox_cputransparency_method is the complete string,
	--      containing both the command and options. This is
	--	defined by the '-c' option of sb2-init.
	--    - sbox_cputransparency_cmd is path to the binary,
	--    - sbox_cputransparency_args contains optional
	--	arguments.
	--   Previously only sbox_cputransparency_method was
	--   available.
	-- Version 24:
	-- - Added support for gconv_path for native applications
	-- Version 23:
	-- - LD_LIBRARY_PATH and LD_PRELOAD are now always
	--   set by argvenvp.lua => the exec_policies must
	--   have proper policies for these (otherwise "fakeroot"
	--   may not perform as expected).
	-- - Note the by default the real LD_LIBRARY_PATH and
	--   LD_PRELOAD that will be used the real exec is now
	--   different than what the user will see in these
	--   variables!
	-- Version 22:
	-- - interface to custom_map_func was modified again:
	--   Last return value is now a bitmask (was a boolean)
	-- Version 21: native_app_message_catalog_prefix has
	-- been removed (it was an exec policy attribute.)
	-- Version 20 changed "script_interpreter_rule" field in
	-- exec policies to "script_interpreter_rules"; find_rule()
	-- is now used to select the rule (there may be more than one!)
	-- (version 19 was an intermediate version)
	-- - added "all_exec_policies" list to all
	--   mapping modes
	-- Differences between version 17 and 18:
	-- - added sb2_procfs_mapper()
	-- Differences between version 16 and 17:
	-- - Added support for hierarcic rules (i.e. rule
	--   trees. 16 supports only linear rule lists)
	-- Differences between version 15 and 16:
	-- - "match" rules are not supported anymore
	-- - interface to custom_map_func was modified:
	--   "work_dir" was removed, name changed to "custom_map_funct",
	--   and such functions are now expected to return 3 values
	--   (previously only one was expected)
	-- - variables "esc_tools_root" and "esc_target_root"
	--   were removed
	local current_rule_interface_version = "100"

	do_file(rule_file_path)

	-- fail and die if interface version is incorrect
	if (rule_file_interface_version == nil) or 
           (type(rule_file_interface_version) ~= "string") then
		sb.log("error", string.format(
			"Fatal: Rule file interface version check failed: "..
			"No version information in %s",
			rule_file_path))
		os.exit(89)
	end
	if rule_file_interface_version ~= current_rule_interface_version then
		sb.log("error", string.format(
			"Fatal: Rule file interface version check failed: "..
			"got %s, expected %s", rule_file_interface_version,
			current_rule_interface_version))
		os.exit(88)
	end

        if (type(fs_mapping_rules) ~= "table") then
		sb.log("error", "'fs_mapping_rule' is not an array.");
		os.exit(87)
	end
end

target_root = sbox_target_root
if (not target_root or target_root == "") then
{'fraction_non_alphanumeric': 0.1034033420205427, 'fraction_numerical': 0.009734784608309061, 'mean_word_length': 3.2707037643207855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6503843', 'n_tokens_mistral': 4381, 'n_tokens_neox': 4141, 'n_words': 1418}