<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace BackyLogic
{
    public interface IFileSystem
    {
        void CreateFile(string filename);

        void Copy(string sourceFileName, string destFileName);

        IEnumerable<string> EnumerateFiles(string dirName);

        IEnumerable<string> GetDirectories(string dirName);

        DateTime GetLastWriteTime(string fullname);

        IEnumerable<string> ReadLines(string fullname);

        void AppendLine(string filename, string line);

        byte[] GetContent(string physicalPath);

        void MakeDirectoryReadOnly(string dirName);
    }


    public class FileSystem : IFileSystem
    {
        public void AppendLine(string filename, string line)
        {
            File.AppendAllLines(filename, new[] { line });
        }

        public void Copy(string sourceFileName, string destFileName)
        {
            if (!Directory.Exists(Path.GetDirectoryName(destFileName)))
                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));

            File.Copy(sourceFileName, destFileName);
        }

        public void CreateFile(string filename)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filename)))
                Directory.CreateDirectory(Path.GetDirectoryName(filename));

            File.WriteAllText(filename, "");
        }

        public IEnumerable<string> EnumerateFiles(string source)
        {
            return Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories);
        }

        public byte[] GetContent(string physicalPath)
        {
            var ret = File.ReadAllBytes(physicalPath);
            return ret;
        }

        public IEnumerable<string> GetDirectories(string target)
        {
            return Directory.GetDirectories(target);
        }

        public DateTime GetLastWriteTime(string fullname)
        {
            return File.GetLastWriteTime(fullname);
        }

        public void MakeDirectoryReadOnly(string dirName)
        {
            var directories = Directory.GetDirectories(dirName, "*", SearchOption.AllDirectories).Concat(new[] { dirName });
            foreach (var dir in directories)
            {
                DirectoryInfo dInfo = new DirectoryInfo(dir);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                dSecurity.SetAccessRuleProtection(true, false); // Disable inheritance
                dSecurity.AddAccessRule(
                    new FileSystemAccessRule(
                        "Everyone",
                        FileSystemRights.ReadAndExecute | FileSystemRights.Traverse,
                        InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
                        PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                dInfo.SetAccessControl(dSecurity);
            }
        }

        public IEnumerable<string> ReadLines(string filename)
        {<commit_msg>applied read-only permissions to sub-folder by inheritence instead of explicitly<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace BackyLogic
{
    public interface IFileSystem
    {
        void CreateFile(string filename);

        void Copy(string sourceFileName, string destFileName);

        IEnumerable<string> EnumerateFiles(string dirName);

        IEnumerable<string> GetDirectories(string dirName);

        DateTime GetLastWriteTime(string fullname);

        IEnumerable<string> ReadLines(string fullname);

        void AppendLine(string filename, string line);

        byte[] GetContent(string physicalPath);

        void MakeDirectoryReadOnly(string dirName);
    }


    public class FileSystem : IFileSystem
    {
        public void AppendLine(string filename, string line)
        {
            File.AppendAllLines(filename, new[] { line });
        }

        public void Copy(string sourceFileName, string destFileName)
        {
            if (!Directory.Exists(Path.GetDirectoryName(destFileName)))
                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));

            File.Copy(sourceFileName, destFileName);
        }

        public void CreateFile(string filename)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filename)))
                Directory.CreateDirectory(Path.GetDirectoryName(filename));

            File.WriteAllText(filename, "");
        }

        public IEnumerable<string> EnumerateFiles(string source)
        {
            return Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories);
        }

        public byte[] GetContent(string physicalPath)
        {
            var ret = File.ReadAllBytes(physicalPath);
            return ret;
        }

        public IEnumerable<string> GetDirectories(string target)
        {
            return Directory.GetDirectories(target);
        }

        public DateTime GetLastWriteTime(string fullname)
        {
            return File.GetLastWriteTime(fullname);
        }

        public void MakeDirectoryReadOnly(string dirName)
        {
            DirectoryInfo dInfo = new DirectoryInfo(dirName);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.SetAccessRuleProtection(true, false); // Disable inheritance
            dSecurity.AddAccessRule(
                new FileSystemAccessRule(
                    "Everyone",
                    FileSystemRights.ReadAndExecute | FileSystemRights.Traverse,
                    InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
                    PropagationFlags.None, AccessControlType.Allow));
            dInfo.SetAccessControl(dSecurity);
        }

        public IEnumerable<string> ReadLines(string filename)
        {
{'fraction_non_alphanumeric': 0.07805523400033074, 'fraction_numerical': 0.0, 'mean_word_length': 2.373117679866146, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7626450', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1426, 'n_words': 299}