public SyncIndexInput(SyncDirectory directory, string name) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } _name = name; _syncDirectory = directory; #if FULLDEBUG Trace.WriteLine($"opening {_name} "); #endif _fileMutex = SyncMutexManager.GrabMutex(_syncDirectory, _name); _fileMutex.WaitOne(); try { var fileName = _name; var fFileNeeded = false; if (!CacheDirectory.FileExists(fileName)) { fFileNeeded = true; } else { long cachedLength = CacheDirectory.FileLength(fileName); long masterLength = MasterDirectory.FileLength(fileName); if (cachedLength != masterLength) { fFileNeeded = true; } else { long cacheDate = CacheDirectory.FileModified(fileName); long masterDate = MasterDirectory.FileModified(fileName); //we need to compare to the second instead of by ticks because when we call //TouchFile in SyncIndexOutput this won't set the files to be identical DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); var cachedLastModifiedUTC = start.AddMilliseconds(cacheDate).ToUniversalTime(); var masterLastModifiedUTC = start.AddMilliseconds(masterDate).ToUniversalTime(); if (cachedLastModifiedUTC != masterLastModifiedUTC) { var timeSpan = masterLastModifiedUTC.Subtract(cachedLastModifiedUTC); //NOTE: This heavily depends on TouchFile in SyncIndexOutput which sets both the //master and slave files to be 'Now', in theory this operation shouldn't //make the file times any bigger than 1 second if (timeSpan.TotalSeconds > 2) { fFileNeeded = true; } else { #if FULLDEBUG Debug.WriteLine(timeSpan.TotalSeconds); #endif // file not needed } } } } // if the file does not exist // or if it exists and it is older then the lastmodified time in the blobproperties (which always comes from the blob storage) if (fFileNeeded) { //get the master file stream using (var masterStream = new StreamInput(MasterDirectory.OpenInput(fileName))) using (var cacheStream = _syncDirectory.CreateCachedOutputAsStream(fileName)) { //copy this to the cached file stream masterStream.CopyTo(cacheStream); cacheStream.Flush(); Debug.WriteLine($"GET {_name} RETREIVED {cacheStream.Length} bytes"); } // and open it as an input _indexInput = CacheDirectory.OpenInput(fileName); } else { #if FULLDEBUG Debug.WriteLine($"Using cached file for {_name}"); #endif // open the file in read only mode _indexInput = CacheDirectory.OpenInput(fileName); } } finally { _fileMutex.ReleaseMutex(); } }