<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Kudu.Contracts.Infrastructure;
using Kudu.Contracts.Tracing;
using Kudu.Core.Tracing;

namespace Kudu.Core.Infrastructure
{
    /// <summary>
    /// A thread safe/multi application safe lock file.
    /// </summary>
    public class LockFile : IOperationLock
    {
        private readonly string _path;
        private Stream _lockStream;
        private readonly ITraceFactory _traceFactory;
        private event Action _lockReleased;

        public LockFile(ITraceFactory traceFactory, string path)
        {
            _traceFactory = traceFactory;
            _path = Path.GetFullPath(path);
        }


        public bool IsHeld
        {
            get
            {
                try
                {
                    // If there's no file then there's no process holding onto it
                    if (!File.Exists(_path))
                    {
                        return false;
                    }

                    // If there is a file, lets see if someone has an open handle to it, or if it's
                    // just hanging there for no reason
                    using (new FileStream(_path, FileMode.Open, FileAccess.Write, FileShare.None))
                    {
                        // Nobody is here
                        return false;
                    }
                }
                catch
                {
                    return true;
                }
            }
        }

        public bool Lock()
        {
            try
            {
                if (Interlocked.Exchange(ref _lockStream, new FileStream(_path, FileMode.Create, FileAccess.Write, FileShare.None)) == null)
                {
                    TraceLock("Aquired Lock");
                    _lockStream.WriteByte(0);
                    return true;
                }<commit_msg>Small tweaks to lock file.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Kudu.Contracts.Infrastructure;
using Kudu.Contracts.Tracing;
using Kudu.Core.Tracing;

namespace Kudu.Core.Infrastructure
{
    /// <summary>
    /// A thread safe/multi application safe lock file.
    /// </summary>
    public class LockFile : IOperationLock
    {
        private readonly string _path;
        private Stream _lockStream;
        private readonly ITraceFactory _traceFactory;
        private event Action _lockReleased;

        public LockFile(ITraceFactory traceFactory, string path)
        {
            _traceFactory = traceFactory;
            _path = Path.GetFullPath(path);
        }


        public bool IsHeld
        {
            get
            {
                // If there's no file then there's no process holding onto it
                if (!File.Exists(_path))
                {
                    return false;
                }

                try
                {
                    // If there is a file, lets see if someone has an open handle to it, or if it's
                    // just hanging there for no reason
                    using (new FileStream(_path, FileMode.Open, FileAccess.Write, FileShare.None))
                    {
                        try
                        {
                            // Nobody is here, so delete the turd file
                            File.Delete(_path);
                        }
                        catch { }
                        return false;
                    }
                }
                catch(IOException)
                {
                    return true;
                }
            }
        }

        public bool Lock()
        {
            try
            {
                if (Interlocked.Exchange(ref _lockStream, new FileStream(_path, FileMode.Create, FileAccess.Write, FileShare.None)) == null)
                {
                    TraceLock("Aquired Lock");
                    _lockStream.WriteByte(0);
                    return true;
                }
{'fraction_non_alphanumeric': 0.06891459512675363, 'fraction_numerical': 0.0004922471080482402, 'mean_word_length': 1.1468568409931326, 'pattern_counts': {'":': 0, '<': 7, '<?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': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6522498', 'n_tokens_mistral': 993, 'n_tokens_neox': 918, 'n_words': 270}