diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-04-02 17:55:19 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-04-02 17:55:19 -0400 |
| commit | 0200911afc125051b4f5e652ef1bebeca984b571 (patch) | |
| tree | d6f00cd6b521dd2232bf0ac1f9d110773e3abe7e /MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs | |
| parent | dcf2e70f039a79dd93ac4f6f3cd5b2c8c9a4dcc8 (diff) | |
add latest translations
Diffstat (limited to 'MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs index 5b65a60c8..09e0e91f5 100644 --- a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs +++ b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs @@ -417,6 +417,11 @@ namespace MediaBrowser.Server.Implementations.IO _affectedPaths.AddOrUpdate(path, path, (key, oldValue) => affectedPath); } + RestartTimer(); + } + + private void RestartTimer() + { lock (_timerLock) { if (_updateTimer == null) @@ -436,6 +441,14 @@ namespace MediaBrowser.Server.Implementations.IO /// <param name="stateInfo">The state info.</param> private async void TimerStopped(object stateInfo) { + // Extend the timer as long as any of the paths are still being written to. + if (_affectedPaths.Any(p => IsFileLocked(p.Key))) + { + Logger.Info("Timer extended."); + RestartTimer(); + return; + } + Logger.Debug("Timer stopped."); DisposeTimer(); @@ -453,6 +466,70 @@ namespace MediaBrowser.Server.Implementations.IO } } + private bool IsFileLocked(string path) + { + try + { + var data = _fileSystem.GetFileSystemInfo(path); + + if (!data.Exists + || data.Attributes.HasFlag(FileAttributes.Directory) + + // Opening a writable stream will fail with readonly files + || data.Attributes.HasFlag(FileAttributes.ReadOnly)) + { + return false; + } + } + catch (IOException) + { + return false; + } + catch (Exception ex) + { + Logger.ErrorException("Error getting file system info for: {0}", ex, path); + return false; + } + + try + { + using (_fileSystem.GetFileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) + { + if (_updateTimer != null) + { + //file is not locked + return false; + } + } + } + catch (DirectoryNotFoundException) + { + // File may have been deleted + return false; + } + catch (FileNotFoundException) + { + // File may have been deleted + return false; + } + catch (IOException) + { + //the file is unavailable because it is: + //still being written to + //or being processed by another thread + //or does not exist (has already been processed) + Logger.Debug("{0} is locked.", path); + return true; + } + catch (Exception ex) + { + Logger.ErrorException("Error determining if file is locked: {0}", ex, path); + return false; + } + + return false; + } + private void DisposeTimer() { lock (_timerLock) |
