<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace DevAudit.AuditLibrary
{
    public class DockerizedLocalEnvironment : LocalEnvironment
    {
        #region Constructors
        public DockerizedLocalEnvironment(EventHandler<EnvironmentEventArgs> message_handler) : base(message_handler)
        {
            if (Directory.Exists("/hostroot"))
            {
                this.HostRootIsMounted = true;
                //throw new Exception(string.Format("The host root directory is not mounted on the DevAudit Docker image at {0}.", "/hostroot"));
            }  
            else
            {
                this.Warning("The Docker host root directory is not mounted on the DevAudit Docker image at /hostroot so no chroot for executables is possible.");
            }         
        }
        #endregion

        #region Overriden methods
        public override AuditDirectoryInfo ConstructDirectory(string dir_path)
        {
            return this.HostRootIsMounted ? base.ConstructDirectory("/hostroot" + (dir_path.StartsWith(this.PathSeparator) ? dir_path : this.PathSeparator + dir_path)) 
                : base.ConstructDirectory(dir_path);
        }

        public override AuditFileInfo ConstructFile(string file_path)
        {
            return this.HostRootIsMounted ? base.ConstructFile("/hostroot" + (file_path.StartsWith(this.PathSeparator) ? file_path : this.PathSeparator + file_path)) 
                : base.ConstructFile(file_path);
        }

        public override bool FileExists(string file_path)
        {
            return this.HostRootIsMounted ? File.Exists("/hostroot" + (file_path.StartsWith(this.PathSeparator) ? file_path : this.PathSeparator + file_path)) : 
                File.Exists(file_path);
        }

        public override bool DirectoryExists(string dir_path)
        {
            return this.HostRootIsMounted ? Directory.Exists("/hostroot" + (dir_path.StartsWith(this.PathSeparator) ? dir_path : this.PathSeparator + dir_path)) : 
                Directory.Exists(dir_path);
        }

        public override bool Execute(string command, string arguments,
            out ProcessExecuteStatus process_status, out string process_output, out string process_error, Action<string> OutputDataReceived = null, Action<string> OutputErrorReceived = null, [CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0)
        {
            if (!HostRootIsMounted)
            {
                throw new InvalidOperationException("The Docker host root directory is not mounted at /hostroot so chroot for executables is not possible.\n Mount the host root using the the -v /:/hostroot:ro docker run  option.");
            }

            if (command.Contains("/hostroot"))
            {
                command = command.Replace("/hostroot", string.Empty);
            }
            if (arguments.Contains("/hostroot"))
            {
                arguments = arguments.Replace("/hostroot", string.Empty);
            }

            return base.Execute("chroot", " /hostroot "+ command + " " + arguments, out process_status, out process_output, out process_error);
        }
        #endregion

        #region Properties
        public bool HostRootIsMounted { get; private set; }
        #endregion<commit_msg>Bug in *Exists in DockerizedLocalEnv.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace DevAudit.AuditLibrary
{
    public class DockerizedLocalEnvironment : LocalEnvironment
    {
        #region Constructors
        public DockerizedLocalEnvironment(EventHandler<EnvironmentEventArgs> message_handler) : base(message_handler)
        {
            if (Directory.Exists("/hostroot"))
            {
                this.HostRootIsMounted = true;
                //throw new Exception(string.Format("The host root directory is not mounted on the DevAudit Docker image at {0}.", "/hostroot"));
            }  
            else
            {
                this.Warning("The Docker host root directory is not mounted on the DevAudit Docker image at /hostroot so no chroot for executables is possible.");
            }         
        }
        #endregion

        #region Overriden methods
        public override AuditDirectoryInfo ConstructDirectory(string dir_path)
        {
            return this.HostRootIsMounted ? base.ConstructDirectory("/hostroot" + (dir_path.StartsWith(this.PathSeparator) ? dir_path : this.PathSeparator + dir_path)) 
                : base.ConstructDirectory(dir_path);
        }

        public override AuditFileInfo ConstructFile(string file_path)
        {
            return this.HostRootIsMounted ? base.ConstructFile("/hostroot" + (file_path.StartsWith(this.PathSeparator) ? file_path : this.PathSeparator + file_path)) 
                : base.ConstructFile(file_path);
        }

        public override bool FileExists(string file_path)
        {
            file_path = file_path.Replace("/hostroot", string.Empty);
            return this.HostRootIsMounted ? File.Exists("/hostroot" + (file_path.StartsWith(this.PathSeparator) ? file_path : this.PathSeparator + file_path)) : 
                File.Exists(file_path);
        }

        public override bool DirectoryExists(string dir_path)
        {
            dir_path = dir_path.Replace("/hostroot", string.Empty);
            return this.HostRootIsMounted ? Directory.Exists("/hostroot" + (dir_path.StartsWith(this.PathSeparator) ? dir_path : this.PathSeparator + dir_path)) : 
                Directory.Exists(dir_path);
        }

        public override bool Execute(string command, string arguments,
            out ProcessExecuteStatus process_status, out string process_output, out string process_error, Action<string> OutputDataReceived = null, Action<string> OutputErrorReceived = null, [CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0)
        {
            if (!HostRootIsMounted)
            {
                throw new InvalidOperationException("The Docker host root directory is not mounted at /hostroot so chroot for executables is not possible.\n Mount the host root using the the -v /:/hostroot:ro docker run  option.");
            }

            if (command.Contains("/hostroot"))
            {
                command = command.Replace("/hostroot", string.Empty);
            }
            if (arguments.Contains("/hostroot"))
            {
                arguments = arguments.Replace("/hostroot", string.Empty);
            }

            return base.Execute("chroot", " /hostroot "+ command + " " + arguments, out process_status, out process_output, out process_error);
        }
        #endregion

        #region Properties
        public bool HostRootIsMounted { get; private set; }
        #endregion
{'fraction_non_alphanumeric': 0.08802153432032302, 'fraction_numerical': 0.0005383580080753701, 'mean_word_length': 2.56573896353167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6882989', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1850, 'n_words': 473}