public static void TestCheckActiveFilesIsLocked() { DateTime utcNow = OS.Current.UtcNow; DateTime utcYesterday = utcNow.AddDays(-1); FakeRuntimeFileInfo.AddFile(_encryptedFile1, utcNow, utcNow, utcNow, Stream.Null); FakeRuntimeFileInfo.AddFile(_decryptedFile1, utcYesterday, utcYesterday, utcYesterday, Stream.Null); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedFile1), OS.Current.FileInfo(_decryptedFile1), new AesKey(), ActiveFileStatus.NotDecrypted, null); SetupAssembly.FakeRuntimeEnvironment.TimeFunction = (() => { return(utcNow.AddMinutes(10)); }); bool changedWasRaised = false; _fileSystemState.Add(activeFile); _fileSystemState.Changed += ((object sender, ActiveFileChangedEventArgs e) => { changedWasRaised = true; }); using (FileLock fileLock = FileLock.Lock(activeFile.EncryptedFileInfo)) { _fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); } Assert.That(changedWasRaised, Is.False, "The file should be not be detected as decrypted being created because the encrypted file is locked."); using (FileLock fileLock = FileLock.Lock(activeFile.DecryptedFileInfo)) { _fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); } Assert.That(changedWasRaised, Is.False, "The file should be not be detected as decrypted being created because the decrypted file is locked."); }
private static ActiveFile TryDecrypt(IRuntimeFileInfo sourceFileInfo, IRuntimeFileInfo destinationFolderInfo, IEnumerable <AesKey> keys, ProgressContext progress) { ActiveFile destinationActiveFile = null; foreach (AesKey key in keys) { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Decrypting '{0}'".InvariantFormat(sourceFileInfo.FullName)); } using (FileLock sourceLock = FileLock.Lock(sourceFileInfo)) { using (AxCryptDocument document = AxCryptFile.Document(sourceFileInfo, key, new ProgressContext())) { if (!document.PassphraseIsValid) { continue; } destinationActiveFile = DecryptActiveFileDocument(sourceFileInfo, destinationFolderInfo, document, progress); break; } } } return(destinationActiveFile); }
private void HandleFailedSend(FileLock markLock, string bufferName, ref LineReader streamReader, FileLock readerLock, List <string> memoryBuffer, int failLimit, Action <List <string> > alternativeWrite) { try { markLock.Lock(); var buffers = ReadMark(markLock).Buffers; var buffer = GetBufferIndex(bufferName, buffers, out var index); if (buffer.FailedSendCount >= failLimit) { alternativeWrite(memoryBuffer); HandleSuccessSend(markLock, bufferName, ref streamReader, readerLock, memoryBuffer); } else { var modifiedBuffer = new BufferInfo(buffer.Name, buffer.Written, buffer.Read, buffer.WriteActive, buffer.LastFlushTime, buffer.FailedSendCount + 1, DateTime.Now); buffers[index] = modifiedBuffer; WriteMark(markLock, buffers); streamReader.Close(); streamReader = null; } } finally { markLock.Unlock(); } }
public static void TestPurgeActiveFilesWhenFileIsLocked() { DateTime utcNow = OS.Current.UtcNow; FakeRuntimeFileInfo.AddFile(_encryptedFile1, utcNow, utcNow, utcNow, Stream.Null); FakeRuntimeFileInfo.AddFile(_decryptedFile1, utcNow, utcNow, utcNow, Stream.Null); IRuntimeFileInfo encryptedFileInfo = OS.Current.FileInfo(_encryptedFile1); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(_decryptedFile1); ActiveFile activeFile = new ActiveFile(encryptedFileInfo, decryptedFileInfo, new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted, null); _fileSystemState.Add(activeFile); bool changedWasRaised = false; _fileSystemState.Changed += ((object sender, ActiveFileChangedEventArgs e) => { changedWasRaised = true; }); using (FileLock fileLock = FileLock.Lock(decryptedFileInfo)) { _fileSystemState.PurgeActiveFiles(new ProgressContext()); } Assert.That(changedWasRaised, Is.False, "A changed event should not be raised because the decrypted file is locked."); }
private LineReader OpenReadBufferStream(BufferInfo bufferInfo, out FileLock streamLock) { streamLock = null; if (bufferInfo.Read >= bufferInfo.Written) { return(null); } var bufferPath = GetBufferFilePath(bufferInfo); if (_buffer.StreamService.IsStreamExist(bufferPath) == false) { return(null); } streamLock = new FileLock(bufferPath); if (streamLock.Lock(TimeSpan.FromMilliseconds(OpenReadStreamTimeout)) == false) { streamLock = null; return(null); } try { var streamReader = _buffer.StreamService.OpenReadStream(streamLock.Path, FileMode.Open, FileShare.Write); var lineReader = new LineReader(streamReader); for (var i = 0; i < bufferInfo.Read; i++) { if (lineReader.ReadLine(out var line) == false) { if (line != null) { } else { break; } } } return(lineReader); } catch (IOException e) { _logger.LogError(e, "OpenReadBufferStream."); } finally { streamLock.Unlock(); } streamLock = null; return(null); }
public void ReleaseIfHeld_Relases_Lock_Successfully() { const string fileId = "testfile2"; var fileLock1 = new FileLock(fileId); (fileLock1.Lock(CancellationToken.None)).ShouldBeTrue(); var fileLock2 = new FileLock(fileId); (fileLock2.Lock(CancellationToken.None)).ShouldBeFalse(); (fileLock1.Lock(CancellationToken.None)).ShouldBeTrue(); fileLock1.ReleaseIfHeld(); (fileLock2.Lock(CancellationToken.None)).ShouldBeTrue(); }
public static void TestFileLockInvalidArguments() { IRuntimeFileInfo nullInfo = null; Assert.Throws <ArgumentNullException>(() => { FileLock.Lock(nullInfo); }); Assert.Throws <ArgumentNullException>(() => { FileLock.IsLocked(nullInfo); }); Assert.Throws <ArgumentNullException>(() => { FileLock.IsLocked(OS.Current.FileInfo(_fileExtPath), nullInfo); }); }
public static void TestFileLockDoubleDispose() { Assert.DoesNotThrow(() => { using (FileLock aLock = FileLock.Lock(OS.Current.FileInfo(_fileExtPath))) { aLock.Dispose(); } }); }
public static void TestFileLockMethods() { IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_fileExtPath); Assert.That(FileLock.IsLocked(fileInfo), Is.False, "There should be no lock for this file yet."); using (FileLock lock1 = FileLock.Lock(fileInfo)) { Assert.That(FileLock.IsLocked(fileInfo), Is.True, "There should be now be a lock for this file."); } Assert.That(FileLock.IsLocked(fileInfo), Is.False, "There should be no lock for this file again."); }
public static void TestFileLockCaseSensitivity() { IRuntimeFileInfo fileInfo1 = OS.Current.FileInfo(_fileExtPath); IRuntimeFileInfo fileInfo2 = OS.Current.FileInfo(_fileExtPath.ToUpper(CultureInfo.InvariantCulture)); Assert.That(FileLock.IsLocked(fileInfo1), Is.False, "There should be no lock for this file yet."); Assert.That(FileLock.IsLocked(fileInfo2), Is.False, "There should be no lock for this file yet."); using (FileLock lock1 = FileLock.Lock(fileInfo1)) { Assert.That(FileLock.IsLocked(fileInfo1), Is.True, "There should be now be a lock for this file."); Assert.That(FileLock.IsLocked(fileInfo2), Is.False, "There should be no lock for this file still."); } Assert.That(FileLock.IsLocked(fileInfo1), Is.False, "There should be no lock for this file again."); }
[System.Security.SecurityCritical] // auto-generated #endif public void Remove() { #if !FEATURE_LEGACYNETCF bool removedAll = true; FileLock groupLock = FileLock.GetFileLock(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_LockPathPrefix), IsolatedStorageFile.s_GroupPathPrefix + "-" + m_ObfuscatedId); try { groupLock.Lock(); foreach (string storeDir in Directory.UnsafeGetDirectories(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_StorePathPrefix), "*", SearchOption.TopDirectoryOnly)) { string groupFile = Path.Combine(storeDir, IsolatedStorageFile.s_GroupFileName); if (m_ObfuscatedId.Equals(File.UnsafeReadAllText(groupFile))) { IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreFromGroupAndStorePath(Group, storeDir); removedAll = removedAll & f.TryRemove(); } } IsolatedStorageFile.TouchFile(Path.Combine(m_GroupPath, IsolatedStorageFile.s_CleanupFileName)); if (removedAll) { IsolatedStorageAccountingInfo.RemoveAccountingInfo(m_GroupPath); File.UnsafeDelete(Path.Combine(m_GroupPath, IsolatedStorageFile.s_IdFileName)); File.UnsafeDelete(Path.Combine(m_GroupPath, IsolatedStorageFile.s_CleanupFileName)); Directory.UnsafeDelete(m_GroupPath, false); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } finally { groupLock.Unlock(); } #else // !FEATURE_LEGACYNETCF try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { isf.Remove(); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #endif // !FEATURE_LEGACYNETCF }
public static void TestFileLockWhenLocked() { IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_fileExtPath); Assert.That(FileLock.IsLocked(fileInfo), Is.False, "There should be no lock for this file to start with."); using (FileLock lock1 = FileLock.Lock(fileInfo)) { Assert.That(FileLock.IsLocked(fileInfo), Is.True, "There should be a lock for this file."); using (FileLock lock1a = FileLock.Lock(fileInfo)) { Assert.That(lock1a, Is.Null, "When trying to get a lock for a locked file, this should return null."); Assert.That(FileLock.IsLocked(fileInfo), Is.True, "There should still be a lock for this file."); } Assert.That(FileLock.IsLocked(fileInfo), Is.True, "There should still be a lock for this file."); } Assert.That(FileLock.IsLocked(fileInfo), Is.False, "There should be no lock for this file now."); }
public void ReleaseIfHeld_Does_Nothing_If_Lock_Was_Not_Held() { const string fileId = "testfile3"; var fileLock1 = new FileLock(fileId); (fileLock1.Lock(CancellationToken.None)).ShouldBeTrue(); var fileLock2 = new FileLock(fileId); (fileLock2.Lock(CancellationToken.None)).ShouldBeFalse(); fileLock2.ReleaseIfHeld(); var fileLock3 = new FileLock(fileId); fileLock3.ReleaseIfHeld(); (fileLock3.Lock(CancellationToken.None)).ShouldBeFalse(); }
private void CleanStalledBuffers(FileLock markLock) { try { markLock.Lock(); var oldBuffers = ReadMark(markLock).Buffers; var newBuffers = new List <BufferInfo>(); foreach (var bufferInfo in oldBuffers) { // TODO Investigate case when read is greater than written if (bufferInfo.WriteActive == false && bufferInfo.Read >= bufferInfo.Written) { var bufferFilePath = GetBufferFilePath(bufferInfo); if (_buffer.StreamService.IsStreamExist(bufferFilePath)) { try { DeleteBuffer(bufferFilePath); } catch (IOException) { newBuffers.Add(bufferInfo); } } } else { newBuffers.Add(bufferInfo); } } if (newBuffers.Count != oldBuffers.Count) { WriteMark(markLock, newBuffers); } } finally { markLock.Unlock(); } }
private static ActiveFile DecryptActiveFileDocument(IRuntimeFileInfo sourceFileInfo, IRuntimeFileInfo destinationFolderInfo, AxCryptDocument document, ProgressContext progress) { string destinationName = document.DocumentHeaders.FileName; string destinationPath = Path.Combine(destinationFolderInfo.FullName, destinationName); IRuntimeFileInfo destinationFileInfo = OS.Current.FileInfo(destinationPath); using (FileLock fileLock = FileLock.Lock(destinationFileInfo)) { AxCryptFile.Decrypt(document, destinationFileInfo, AxCryptOptions.SetFileTimes, progress); } ActiveFile destinationActiveFile = new ActiveFile(sourceFileInfo, destinationFileInfo, document.DocumentHeaders.KeyEncryptingKey, ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.IgnoreChange, null); if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("File decrypted from '{0}' to '{1}'".InvariantFormat(sourceFileInfo.FullName, destinationActiveFile.DecryptedFileInfo.FullName)); } return(destinationActiveFile); }
private StreamWriter OpenWriteBufferStream(out string bufferName, out FileLock streamLock) { bufferName = null; while (true) { try { _markLock.Lock(); var buffers = ReadMark(_markLock).Buffers; if (buffers.Count == 0) { buffers.Add(new BufferInfo(GetNextBufferName(), 0, 0, true, DateTime.MinValue, 0, DateTime.MinValue)); } var bufferInfo = buffers[buffers.Count - 1]; if (bufferInfo.WriteActive == false || bufferInfo.Written >= _fileBufferLimit) { bufferInfo = new BufferInfo(GetNextBufferName(), 0, 0, true, DateTime.MinValue, 0, DateTime.MinValue); buffers.Add(bufferInfo); } bufferName = bufferInfo.Name; streamLock = new FileLock(GetBufferFilePath(bufferInfo)); var streamWriter = _buffer.StreamService.OpenWriteStream(streamLock.Path, FileMode.Append, FileAccess.Write, FileShare.Read); streamWriter.AutoFlush = false; WriteMark(_markLock, buffers); return(streamWriter); } catch (IOException e) { _logger.LogWarning(e, $"OpenWriteBufferStream failed to open buffer '{bufferName}'."); } }
private bool IsReadBufferEmpty(FileLock markLock) { try { markLock.Lock(); var mark = ReadMark(markLock); return(mark.Read >= mark.Written); } catch (IOException e) { _logger.LogError(e, "IsReadBufferEmpty"); return(true); } finally { markLock.Unlock(); } }
private BufferInfo ModifyBuffer(FileLock markLock, string bufferName, Func <BufferInfo, BufferInfo> mutator) { try { markLock.Lock(); var buffers = ReadMark(markLock).Buffers; var buffer = GetBufferIndex(bufferName, buffers, out var index); var modifiedBuffer = mutator(buffer); buffers[index] = modifiedBuffer; WriteMark(markLock, buffers); return(modifiedBuffer); } finally { markLock.Unlock(); } }
public static async Task <bool> Invoke(ContextAdapter context) { context.Configuration.Validate(); var request = context.Request; var response = context.Response; var methodHandler = GetProtocolMethodHandler(context); if (methodHandler == null) { return(false); } if (!(methodHandler is OptionsHandler)) { var tusResumable = request.Headers.ContainsKey(HeaderConstants.TusResumable) ? request.Headers[HeaderConstants.TusResumable].FirstOrDefault() : null; if (tusResumable == null) { return(false); } if (tusResumable != HeaderConstants.TusResumableValue) { response.SetHeader(HeaderConstants.TusResumable, HeaderConstants.TusResumableValue); response.SetHeader(HeaderConstants.TusVersion, HeaderConstants.TusResumableValue); return(await response.Error(HttpStatusCode.PreconditionFailed, $"Tus version {tusResumable} is not supported. Supported versions: {HeaderConstants.TusResumableValue}")); } } FileLock fileLock = null; if (methodHandler.RequiresLock) { fileLock = new FileLock(context.GetFileId()); var hasLock = fileLock.Lock(context.CancellationToken); if (!hasLock) { return(await response.Error(HttpStatusCode.Conflict, $"File {context.GetFileId()} is currently being updated. Please try again later")); } } try { if (!await methodHandler.Validate(context)) { return(true); } return(await methodHandler.Handle(context)); } catch (TusStoreException storeException) { await context.Response.Error(HttpStatusCode.BadRequest, storeException.Message); return(true); } finally { fileLock?.ReleaseIfHeld(); } }
private LineReader OpenReadBufferStream(FileLock markLock, out string bufferName, out FileLock streamLock) { try { markLock.Lock(); bufferName = null; var buffers = ReadMark(markLock).Buffers; foreach (var buffer in buffers) { if (HasPauseTimeoutExpired(buffer) == false) { continue; } if (buffer.WriteActive && buffer.Written - buffer.Read < _readLimit && buffer.LastFlushTime + _flushTimeout > DateTime.Now) { continue; } var lineReader = OpenReadBufferStream(buffer, out streamLock); if (lineReader == null) { continue; } if (lineReader.ReadFinished) { try { if (buffer.WriteActive == false) { lineReader.Close(); DeleteBuffer(streamLock.Path); } } catch (Exception e) { _logger.LogError(e, $"Delete buffer '{streamLock.Path}' error."); } continue; } bufferName = buffer.Name; return(lineReader); } streamLock = null; return(null); } finally { markLock.Unlock(); } }
public static ValueTask <IDisposable> Lock(FilePath path, IEnumerable <TimeSpan>?retryIntervals = null, CancellationToken cancellationToken = default) { var fileLock = new FileLock(path, retryIntervals); return(fileLock.Lock(cancellationToken)); }
private void Flush(bool force, bool push = true) { try { List <TItem> flushBuffer; lock (_memoryBuffer) { flushBuffer = _memoryBuffer; var bufferCount = flushBuffer.Count; if (bufferCount == 0) { return; } if (force == false && bufferCount < _memoryBufferLimit) { return; } _memoryBuffer = RentMemoryBuffer(); } lock (_syncFlush) { try { if (_streamWriter == null) { _streamWriter = OpenWriteBufferStream(out _writeBuffer, out _writerLock); } var count = 0; try { _writerLock.Lock(); _streamWriter.BaseStream.Seek(0, SeekOrigin.End); foreach (var logEvent in flushBuffer) { WriteStartLine(_streamWriter); _buffer.FormatItem(logEvent, _streamWriter); WriteEndLine(_streamWriter); count++; } _streamWriter.Flush(); } finally { _writerLock.Unlock(); } ReleaseMemoryBuffer(flushBuffer); if (MarkWrite(_markLock, _writeBuffer, count).WriteActive == false) { _streamWriter.Close(); _streamWriter = null; _writerLock = null; } } catch (Exception e) { _logger.LogError(e, "Flush."); } } } finally { if (push) { PushBlock(); } } }
[System.Security.SecurityCritical] // auto-generated #endif public static IEnumerable <IsolatedStorageGroup> GetGroups() { List <IsolatedStorageGroup> groups = new List <IsolatedStorageGroup>(); #if !FEATURE_LEGACYNETCF try { foreach (string groupDir in Directory.UnsafeGetDirectories(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_GroupPathPrefix), "*", SearchOption.TopDirectoryOnly)) { string idFile = Path.Combine(groupDir, IsolatedStorageFile.s_IdFileName); string id; FileLock groupLock = FileLock.GetFileLock(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_LockPathPrefix), IsolatedStorageFile.s_GroupPathPrefix + "-" + DirectoryInfo.UnsafeCreateDirectoryInfo(groupDir).Name); try { groupLock.Lock(); if (!File.UnsafeExists(Path.Combine(groupDir, IsolatedStorageFile.s_CleanupFileName))) { id = File.UnsafeReadAllText(idFile); if (IsolatedStorageAccountingInfo.IsAccountingInfoValid(groupDir)) { using (IsolatedStorageAccountingInfo accountingInfo = new IsolatedStorageAccountingInfo(groupDir)) { groups.Add(new IsolatedStorageGroup(id, accountingInfo.Quota, accountingInfo.UsedSize, groupDir)); } } else { // In this case we've tried to deseriaize a group that doesn't have valid data. We'll try to remove it. try { new IsolatedStorageGroup(id, 0, 0, groupDir).Remove(); } catch (Exception) { // We couldn't remove the group for some reason. Ignore it and move on. } } } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } finally { groupLock.Unlock(); } } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #else try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { groups.Add(new IsolatedStorageGroup(isf.GroupName, Int64.MaxValue, 0, IsolatedStorageFile.IsolatedStorageRoot)); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (IsolatedStorageException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #endif return(groups); }