예제 #1
0
        private static async Task <TOut> ExecuteFileOperation <TIn, TOut>(BotData data, string path, TIn parameter, Func <string, TIn, Task <TOut> > func)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(path);
            }

            FileUtils.CreatePath(path);

            TOut result;
            var  fileLock = FileLocker.GetHandle(path);

            Monitor.Enter(fileLock);

            try
            {
                result = await func.Invoke(path, parameter);
            }
            finally
            {
                Monitor.Exit(fileLock);
            }

            return(result);
        }
예제 #2
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static IEnumerable <ProfileInfo> ReadProfiles(string filePath)
        {
            byte[] sign = ProfileSignature;

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var reader = new RawDataReader(fs, Encoding.UTF8);

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int count   = reader.ReadInt();
                    var prInfos = new List <ProfileInfo>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        prInfos.Add(ProfileInfo.LoadProfileInfo(reader));
                    }

                    return(prInfos);
                }
        }
예제 #3
0
        private static Task <TOut> ExecuteFileOperation <TIn, TOut>(BotData data, string path, TIn parameter, Func <string, TIn, Task <TOut> > func)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(path);
            }

            FileUtils.CreatePath(path);

            // TODO: Implement an asynchronous lock, otherwise it will throw a
            // SynchronizationLockException since we cannot call Monitor.Exit() in an async context
            // https://stackoverflow.com/questions/21404144/synchronizationlockexception-on-monitor-exit-when-using-await

            TOut result;
            var  fileLock = FileLocker.GetHandle(path);

            Monitor.Enter(fileLock);

            try
            {
                // HACK: Execute synchronously as a temporary fix
                result = func.Invoke(path, parameter).Result;
            }
            finally
            {
                Monitor.Exit(fileLock);
            }

            return(Task.FromResult(result));
        }
예제 #4
0
        protected void PageLoad()
        {
            var file = new File();

            try
            {
                ShareLink = Request[UrlConstant.DocUrlKey] ?? "";
                WithLink  = !string.IsNullOrEmpty(ShareLink);

                var fileId = WithLink ? (object)-1 : Request[UrlConstant.FileId];
                FileNew = !string.IsNullOrEmpty(Request[UrlConstant.New]) && Request[UrlConstant.New] == "true";

                file = DocumentUtils.EditIframeSrc(fileId, FileNew, ShareLink, out SrcIframe);

                if (SecurityContext.IsAuthenticated)
                {
                    var parent = Global.DaoFactory.GetFolderDao().GetFolder(file.FolderID);
                    if (file.RootFolderType == FolderType.USER &&
                        file.RootFolderCreator != SecurityContext.CurrentAccount.ID &&
                        !Global.GetFilesSecurity().CanRead(parent))
                    {
                        FolderUrl = PathProvider.GetFolderUrl(Global.FolderShare, false, null);
                    }
                    else
                    {
                        FolderUrl = PathProvider.GetFolderUrl(parent);

                        if (string.IsNullOrEmpty(FolderUrl))
                        {
                            FolderUrl = Request.UrlReferrer == null
                                            ? VirtualPathUtility.ToAbsolute(PathProvider.StartURL)
                                            : Request.UrlReferrer.ToString();
                        }

                        if (Global.EnableShare)
                        {
                            CommonContainerHolder.Controls.Add(LoadControl(AccessRights.Location));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect(PathProvider.BaseVirtualPath + "docviewer.aspx" + "?" + Request.QueryString + "#" + UrlConstant.Error + "/" + HttpUtility.UrlEncode(ex.Message));
            }

            Title       = file.Title;
            FileId      = file.ID;
            FileTitle   = file.Title;
            FileVersion = file.Version;

            DocKeyForTrack = DocumentUtils.GetDocKey(file.ID, -1, DateTime.MinValue);

            using (var tagDao = Global.DaoFactory.GetTagDao())
            {
                tagDao.RemoveTags(Tag.New(SecurityContext.CurrentAccount.ID, file));
            }

            FileLocker.Add(file.ID);
        }
예제 #5
0
        public void Download(string destPath, string srcFilePath, bool retryOnErr = false)
        {
            Assert(Uri.IsWellFormedUriString(srcFilePath, UriKind.Relative));

            string tmpFile = Path.GetTempFileName();

            FluentFTP.FtpVerify flags = FluentFTP.FtpVerify.Throw | FluentFTP.FtpVerify.OnlyChecksum;
            flags |= FluentFTP.FtpVerify.Retry;

            using (var ftpClient = CreateFtpClient())
                try
                {
                    ftpClient.Connect();
                    ftpClient.DownloadFile(tmpFile, srcFilePath, verifyOptions: flags);
                }
                catch (FluentFTP.FtpException ex)
                {
                    throw ex.InnerException ?? ex;
                }

            using (new AutoReleaser(() => File.Delete(tmpFile)))
            {
                string decFilePath = DecodeFile(tmpFile);

                using (FileLocker.Lock(destPath))
                {
                    if (File.Exists(destPath))
                    {
                        File.Delete(destPath);
                    }

                    File.Move(decFilePath, destPath);
                }
            }
        }
예제 #6
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void AppendConnectionsResp(string filePath, IEnumerable <Message> messages)
        {
            Assert(messages != null);

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    var reader   = new RawDataReader(fs, Encoding.UTF8);
                    int countPos = SrvCxnSignature.Length;;
                    fs.Position = countPos;

                    int msgCount = reader.ReadInt();

                    var writer = new RawDataWriter(fs, Encoding.UTF8);
                    fs.Seek(0, SeekOrigin.End);

                    foreach (Message msg in messages)
                    {
                        msg.Write(writer);
                        ++msgCount;
                    }

                    fs.Position = countPos;
                    writer.Write(msgCount);
                }
        }
예제 #7
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static IEnumerable <Message> ReadConnectionsReq(string filePath)
        {
            byte[] sign = HubCxnSignature;

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var reader = new RawDataReader(fs, Encoding.UTF8);

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int count = reader.ReadInt();
                    var cxns  = new List <Message>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        cxns.Add(Message.LoadMessage(reader));
                    }

                    return(cxns);
                }
        }
예제 #8
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static IEnumerable <Message> ReadHubDialog(string filePath, uint clID)
        {
            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var    reader = new RawDataReader(fs, Encoding.UTF8);
                    byte[] sign   = HubDialogSignature;

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    uint id = reader.ReadUInt();

                    if (id != clID)
                    {
                        throw new CorruptedFileException(filePath);
                    }

                    int count = reader.ReadInt();
                    var msgs  = new List <Message>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        msgs.Add(Message.LoadMessage(reader));
                    }

                    return(msgs);
                }
        }
예제 #9
0
            public void WhenLockFileExist_ThenThrowException()
            {
                var fileInfo = FileBuilder.InFileSystem.WithPath(Path.Combine(WorkingDir, "Test1.txt")).Build();

                FileBuilder.InFileSystem.WithPath(fileInfo.FullName + ".lock").Build();

                Assert.Throws <InvalidOperationException>(() => FileLocker.Lock(fileInfo));
            }
예제 #10
0
            public void WhenFileIsNotLocked_ThenReturnFalse()
            {
                var fileInfo = FileBuilder.InFileSystem.WithPath(Path.Combine(WorkingDir, "Test1.txt")).Build();

                var result = FileLocker.IsLocked(fileInfo);

                Assert.That(result, Is.False);
            }
예제 #11
0
            public void WhenFileIsLocked_ThenUnlock()
            {
                var fileInfo = FileBuilder.InFileSystem.WithPath(Path.Combine(WorkingDir, "Test1.txt")).Build();

                FileLocker.Lock(fileInfo);

                FileLocker.Unlock(fileInfo);

                Assert.That(FileLocker.IsLocked(fileInfo), Is.False);
            }
예제 #12
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void AppendHubDialog(string filePath, uint clID, Message msg)
        {
            Assert(msg != null);

            using (FileLocker.Lock(filePath))
            {
                IEnumerable <Message> prevMsgs = ReadHubDialog(filePath, clID);
                WriteHubDialog(filePath, clID, prevMsgs.Add(msg));
            }
        }
예제 #13
0
            public void WhenExistingFileIsLockFile_ThenCreateLockFile()
            {
                var fileInfo = FileBuilder.InFileSystem.WithPath(Path.Combine(WorkingDir, "Test1.lock")).Build();

                var result = FileLocker.Lock(fileInfo);

                Assert.That(result.File.FullName, Is.EqualTo(fileInfo.FullName));
                Assert.That(result.LockFile.FullName, Is.EqualTo(fileInfo.FullName + ".lock"));
                AssertFile.Exists(result.LockFile);
            }
예제 #14
0
        public void TestSingleFileLocker()
        {
            var fileLocker = new FileLocker(LockFilePath);

            using var manualResetEvent = new ManualResetEventSlim(false);
            var threadsCount = 3;

            using var countdownEvent = new CountdownEvent(threadsCount);
            var spinWait = new SpinWait();

            var threads = Enumerable.Range(0, threadsCount).Select(x => {
                var thread = new Thread(new ThreadStart(() => {
                    void lockFile()
                    {
                        manualResetEvent.Wait();

                        for (var index = 0; index < 25; index++)
                        {
                            using var lockUse = fileLocker.WaitUntilAcquired();

                            if (index % 7 == 0)
                            {
                                fileLocker.Unlock(lockUse.LockId);
                            }

                            spinWait.SpinOnce();
                        }
                    }

                    lockFile();
                    countdownEvent.Signal();
                }))
                {
                    Name = x.ToString()
                };

                return(thread);
            }).ToList();

            foreach (var thread in threads)
            {
                thread.Start();
            }

            manualResetEvent.Set();
            countdownEvent.Wait();

            foreach (var thread in threads)
            {
                thread.Join();
            }

            Assert.True(fileLocker.LocksInUse == 0, "The number of locks in use is not zero.");
        }
예제 #15
0
 public void Log(int jobId, string message, LogKind kind = LogKind.Info)
 {
     if (settings.RuriLibSettings.GeneralSettings.LogJobActivityToFile)
     {
         var fileName = Path.Combine(baseFolder, $"job{jobId}.log");
         lock (FileLocker.GetHandle(fileName))
         {
             var str = $"[{DateTime.Now.ToLongTimeString()}][{kind}] {message.Replace("\r\n", " ").Replace("\n", " ")}{Environment.NewLine}";
             File.AppendAllText(fileName, str);
         }
     }
 }
예제 #16
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void AppendSrvDialog(string filePath, Message msg)
        {
            Assert(msg != null);

            using (FileLocker.Lock(filePath))
            {
                ClientDialog clDlg  = ReadSrvDialog(filePath);
                var          newDlg = new ClientDialog(clDlg.ClientID, clDlg.ClientStatus, clDlg.Messages.Add(msg));

                WriteSrvDialog(filePath, newDlg);
            }
        }
예제 #17
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void AppendSrvDialog(string filePath, IEnumerable <Message> msgs)
        {
            Assert(msgs != null);

            using (FileLocker.Lock(filePath))
            {
                ClientDialog clDlg  = ReadSrvDialog(filePath);
                var          newDlg = new ClientDialog(clDlg.ClientID, clDlg.ClientStatus, clDlg.Messages.Concat(msgs));

                WriteSrvDialog(filePath, newDlg);
            }
        }
예제 #18
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void WriteSrvDialog(string filePath, ClientDialog clDlg)
        {
            Assert(clDlg != null);

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.Create(filePath))
                {
                    var writer = new RawDataWriter(fs, Encoding.UTF8);

                    writer.Write(SrvDialogSignature);
                    clDlg.Write(writer);
                }
        }
예제 #19
0
        public static void FileDelete(BotData data, string path)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(path);
            }

            lock (FileLocker.GetHandle(path))
                File.Delete(path);

            data.Logger.LogHeader();
            data.Logger.Log($"Deleted {path}", LogColors.Flavescent);
        }
예제 #20
0
        private static void FoundHit(IRunnerMessaging obj, Hit hit)
        {
            // Print the hit information to the screen
            Console.WriteLine($"[{DateTime.Now}][{hit.Type}][{hit.Proxy}] {hit.Data}", Color.GreenYellow);

            // If an output file was specified, print them to the output file as well
            if (outFile != string.Empty)
            {
                lock (FileLocker.GetLock(outFile))
                {
                    File.AppendAllText(outFile, $"[{ DateTime.Now}][{hit.Type}][{hit.Proxy}] {hit.Data}{Environment.NewLine}");
                }
            }
        }
예제 #21
0
        string EncodeFile(string filePath)
        {
            string tmpFile = Path.GetTempFileName();

            using (FileStream ofs = File.OpenWrite(tmpFile))
                using (var xs = new XorStream(ofs))
                    using (var gzs = new GZipStream(xs, CompressionMode.Compress))
                    {
                        using (FileLocker.Lock(filePath))
                            using (FileStream ifs = File.OpenRead(filePath))
                                ifs.CopyTo(gzs);
                    }

            return(tmpFile);
        }
예제 #22
0
        private static async Task <TOut> ExecuteFileOperation <TIn, TOut>(BotData data, string path, TIn parameter,
                                                                          Func <string, TIn, Task <TOut> > func, bool isWriteOperation = false)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(path);
            }

            FileUtils.CreatePath(path);

            TOut result;
            var  fileLock = FileLocker.GetHandle(path);

            try
            {
                if (isWriteOperation)
                {
                    // If we need write access, try to acquire a write lock periodically every 5 seconds
                    while (!fileLock.TryEnterWriteLock(5000))
                    {
                        await Task.Delay(10, data.CancellationToken);
                    }
                }
                else
                {
                    // If we need read access, try to acquire a read lock periodically every 5 seconds
                    while (!fileLock.TryEnterReadLock(5000))
                    {
                        await Task.Delay(10, data.CancellationToken);
                    }
                }

                result = await func.Invoke(path, parameter);
            }
            finally
            {
                if (isWriteOperation)
                {
                    fileLock.ExitWriteLock();
                }
                else
                {
                    fileLock.ExitReadLock();
                }
            }

            return(result);
        }
예제 #23
0
        public void TestMultipleFileLockers()
        {
            using var manualResetEvent = new ManualResetEventSlim(false);
            var threadsCount = 2;

            using var countdownEvent = new CountdownEvent(threadsCount);
            var spinWait = new SpinWait();
            var random   = new Random();

            var threads = Enumerable.Range(0, threadsCount).Select(x => {
                var thread = new Thread(new ThreadStart(() => {
                    void lockFile()
                    {
                        manualResetEvent.Wait();

                        for (var index = 0; index < 25; index++)
                        {
                            var fileLocker    = new FileLocker(LockFilePath);
                            using var lockUse = fileLocker.WaitUntilAcquired();
                            spinWait.SpinOnce();
                        }
                    }

                    lockFile();
                    countdownEvent.Signal();
                }))
                {
                    Name = x.ToString()
                };

                return(thread);
            }).ToList();

            foreach (var thread in threads)
            {
                thread.Start();
            }

            manualResetEvent.Set();
            countdownEvent.Wait();

            foreach (var thread in threads)
            {
                // When joining we want no excpetion to appear.
                thread.Join();
            }
        }
예제 #24
0
        public Task Store(Hit hit)
        {
            Directory.CreateDirectory(BaseDir);

            var folderName = Path.Combine(BaseDir, hit.Config.Metadata.Name.ToValidFileName());

            Directory.CreateDirectory(folderName);

            var fileName = Path.Combine(folderName, $"{hit.Type.ToValidFileName()}.txt");

            lock (FileLocker.GetHandle(fileName))
            {
                File.AppendAllTextAsync(fileName, $"{hit}\r\n");
            }

            return(Task.CompletedTask);
        }
예제 #25
0
        public static void FileMove(BotData data, string originPath, string destinationPath)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(originPath);
                FileUtils.ThrowIfNotInCWD(destinationPath);
            }

            FileUtils.CreatePath(destinationPath);

            lock (FileLocker.GetHandle(originPath))
                lock (FileLocker.GetHandle(destinationPath))
                    File.Move(originPath, destinationPath);

            data.Logger.LogHeader();
            data.Logger.Log($"Moved {originPath} to {destinationPath}", LogColors.Flavescent);
        }
예제 #26
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void WriteConnectionsReq(string filePath, IEnumerable <Message> messages)
        {
            Assert(messages != null);

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.Create(filePath))
                {
                    var writer = new RawDataWriter(fs, Encoding.UTF8);

                    writer.Write(HubCxnSignature);

                    writer.Write(messages.Count());

                    foreach (Message msg in messages)
                    {
                        msg.Write(writer);
                    }
                }
        }
예제 #27
0
        public void Download(string destFolder, IEnumerable <string> srcURLs, bool retryOnErr = false)
        {
            Assert(!srcURLs.Any(s => Uri.IsWellFormedUriString(s, UriKind.Relative) == false));

            string tmpDir = Path.GetTempPath();

            FluentFTP.FtpVerify flags = FluentFTP.FtpVerify.Throw | FluentFTP.FtpVerify.OnlyChecksum;
            flags |= FluentFTP.FtpVerify.Retry;

            using (var ftpClient = CreateFtpClient())
                try
                {
                    ftpClient.Connect();
                    ftpClient.DownloadFiles(tmpDir, srcURLs, verifyOptions: flags, errorHandling: FluentFTP.FtpError.Throw);
                }
                catch (FluentFTP.FtpException ex)
                {
                    throw ex.InnerException ?? ex;
                }


            foreach (string src in srcURLs)
            {
                string srcFile = Path.GetFileName(src);
                string dlFile  = Path.Combine(tmpDir, srcFile);

                string decFilePath = DecodeFile(dlFile);

                string destPath = Path.Combine(destFolder, srcFile);

                using (FileLocker.Lock(destPath))
                {
                    if (File.Exists(destPath))
                    {
                        File.Delete(destPath);
                    }

                    File.Move(decFilePath, destPath);
                }

                File.Delete(dlFile);
            }
        }
예제 #28
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static ClientDialog ReadSrvDialog(string filePath)
        {
            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var    reader = new RawDataReader(fs, Encoding.UTF8);
                    byte[] sign   = SrvDialogSignature;

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    return(ClientDialog.LoadClientDialog(reader));
                }
        }
예제 #29
0
파일: DialogEngin.cs 프로젝트: Adel-dz/Hub
        public static void WriteProfiles(string filePath, IEnumerable <ProfileInfo> profiles)
        {
            Assert(profiles != null);

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.Create(filePath))
                {
                    var writer = new RawDataWriter(fs, Encoding.UTF8);

                    writer.Write(ProfileSignature);

                    writer.Write(profiles.Count());

                    foreach (ProfileInfo prInfo in profiles)
                    {
                        prInfo.Write(writer);
                    }
                }
        }
예제 #30
0
        private static async Task <TOut> ExecuteFileOperation <TIn, TOut>(BotData data, string path, TIn parameter,
                                                                          Func <string, TIn, Task <TOut> > func, bool isWriteOperation = false)
        {
            if (data.Providers.Security.RestrictBlocksToCWD)
            {
                FileUtils.ThrowIfNotInCWD(path);
            }

            FileUtils.CreatePath(path);

            TOut result;
            var  fileLock = FileLocker.GetHandle(path);

            try
            {
                if (isWriteOperation)
                {
                    await fileLock.EnterWriteLock(data.CancellationToken);
                }
                else
                {
                    await fileLock.EnterReadLock(data.CancellationToken).ConfigureAwait(false);
                }

                result = await func.Invoke(path, parameter).ConfigureAwait(false);
            }
            finally
            {
                if (isWriteOperation)
                {
                    fileLock.ExitWriteLock();
                }
                else
                {
                    fileLock.ExitReadLock();
                }
            }

            return(result);
        }