private void LockStreamWithRetry() { MapiStream mapiStream = this.oofHistoryStream as MapiStream; if (mapiStream == null) { return; } for (int i = 0; i < 6; i++) { try { mapiStream.LockRegion(0L, 1L, 1); this.streamLocked = true; this.Trace("Oof history stream locked."); break; } catch (MapiExceptionLockViolation argument) { if (i == 5) { throw; } this.context.TraceError <int, MapiExceptionLockViolation>("Failed to lock on attempt {0}. Exception encountered is {1}.", i, argument); Thread.Sleep(100); } } }
private static MapiStream OpenAndLockStream(MapiFolder oofHistoryFolder, byte[] initialBytes, long hashCode, out bool isNew) { MapiStream mapiStream = null; isNew = false; OpenPropertyFlags openPropertyFlags = OpenPropertyFlags.Modify | OpenPropertyFlags.DeferredErrors; try { mapiStream = oofHistoryFolder.OpenStream(PropTag.OofHistory, openPropertyFlags); OofHistory.LockStreamWithRetry(mapiStream, hashCode); } catch (MapiExceptionNotFound) { if (mapiStream != null) { mapiStream.Dispose(); mapiStream = null; } OofHistory.Tracer.TraceDebug(hashCode, "OOF history property does not exist, trying to open with create flag."); openPropertyFlags |= OpenPropertyFlags.Create; mapiStream = oofHistoryFolder.OpenStream(PropTag.OofHistory, openPropertyFlags); OofHistory.LockStreamWithRetry(mapiStream, hashCode); OofHistory.ResetOofHistoryProperty(mapiStream, initialBytes, hashCode); isNew = true; } return(mapiStream); }
private static T ReadObjectFromMessage(MapiMessage mapiMessage, bool throwOnDeserializationError) { T result; using (MapiStream mapiStream = mapiMessage.OpenStream(PropTag.Body, OpenPropertyFlags.BestAccess)) { result = MoveObjectInfo <T> .DeserializeFromStream(mapiStream, throwOnDeserializationError); } return(result); }
public List <T> ReadObjectChunks(ReadObjectFlags flags) { if ((flags & ReadObjectFlags.Refresh) != ReadObjectFlags.None) { this.message.Dispose(); this.message = null; } if (this.message == null && !this.OpenMessage()) { return(null); } List <T> list = new List <T>(); T t; if ((flags & ReadObjectFlags.LastChunkOnly) == ReadObjectFlags.None) { using (MapiTable attachmentTable = this.message.GetAttachmentTable()) { if (attachmentTable != null) { PropValue[][] array = attachmentTable.QueryAllRows(null, MoveObjectInfo <T> .AttachmentTagsToLoad); foreach (PropValue[] array3 in array) { int @int = array3[0].GetInt(); using (MapiAttach mapiAttach = this.message.OpenAttach(@int)) { using (MapiStream mapiStream = mapiAttach.OpenStream(PropTag.AttachDataBin, OpenPropertyFlags.BestAccess)) { t = MoveObjectInfo <T> .DeserializeFromStream(mapiStream, (flags & ReadObjectFlags.DontThrowOnCorruptData) == ReadObjectFlags.None); } if (t != null) { list.Add(t); } } } } } } t = MoveObjectInfo <T> .ReadObjectFromMessage(this.message, (flags & ReadObjectFlags.DontThrowOnCorruptData) == ReadObjectFlags.None); if (t != null) { list.Add(t); } if (list.Count <= 0) { return(null); } return(list); }
internal MapiStreamWrapper(MapiStream stream, StoreSession session) { using (DisposeGuard disposeGuard = this.Guard()) { this.disposeTracker = this.GetDisposeTracker(); this.session = session; if (stream == null) { throw new ArgumentNullException("stream"); } this.mapiStream = stream; disposeGuard.Success(); } }
private void CloseHistoryStream() { try { if (this.streamLocked) { MapiStream mapiStream = this.oofHistoryStream as MapiStream; mapiStream.UnlockRegion(0L, 1L, 1); } } finally { this.oofHistoryStream.Dispose(); this.oofHistoryStream = null; } }
private static void LockStreamWithRetry(MapiStream oofHistoryStream, long hashCode) { for (int i = 0; i < 6; i++) { try { oofHistoryStream.LockRegion(0L, 1L, 1); break; } catch (MapiExceptionLockViolation arg) { if (i == 5) { throw; } OofHistory.Tracer.TraceDebug <int, int, MapiExceptionLockViolation>(hashCode, "Failed to lock on attempt {0}, sleeping {1} milliseconds. Exception encountered is {2}.", i, 100, arg); Thread.Sleep(100); } } }
private uint CreateMsgWithAttachement(out byte[] entryId) { ASCIIEncoding asciiencoding = new ASCIIEncoding(); uint @int; using (MapiMessage mapiMessage = this.testFolder.CreateMessage()) { PropValue[] props = new PropValue[] { new PropValue(PropTag.Subject, string.Format("CITestSearch: {0}.", this.searchString)), new PropValue(PropTag.Body, string.Format("The unique search string in the body is: {0}.", this.searchString)), new PropValue(PropTag.MessageDeliveryTime, (DateTime)ExDateTime.Now) }; this.threadExit.CheckStop(); mapiMessage.SetProps(props); int num; using (MapiAttach mapiAttach = mapiMessage.CreateAttach(out num)) { string s = string.Format("This is a test msg created by test-search task (MSExchangeSearch {0}).It will be deleted soon...", this.searchString); byte[] bytes = asciiencoding.GetBytes(s); using (MapiStream mapiStream = mapiAttach.OpenStream(PropTag.AttachDataBin, OpenPropertyFlags.Create)) { mapiStream.Write(bytes, 0, bytes.Length); mapiStream.Flush(); this.threadExit.CheckStop(); } props = new PropValue[] { new PropValue(PropTag.AttachFileName, "CITestSearch.txt"), new PropValue(PropTag.AttachMethod, AttachMethods.ByValue) }; mapiAttach.SetProps(props); mapiAttach.SaveChanges(); } this.threadExit.CheckStop(); mapiMessage.SaveChanges(); entryId = mapiMessage.GetProp(PropTag.EntryId).GetBytes(); @int = (uint)mapiMessage.GetProp(PropTag.DocumentId).GetInt(); } return(@int); }
private static void ResetOofHistoryProperty(MapiStream oofHistoryStream, byte[] initialBytes, long hashCode) { OofHistory.Tracer.TraceDebug(hashCode, "Resetting OOF history to initial values."); if (!oofHistoryStream.CanWrite) { throw new InvalidOperationException("OOF history property stream is not writable."); } if (!oofHistoryStream.CanSeek) { throw new InvalidOperationException("OOF history property stream is not seekable."); } if (0L != oofHistoryStream.Position) { oofHistoryStream.Seek(0L, SeekOrigin.Begin); } oofHistoryStream.Write(initialBytes, 0, 6); if (oofHistoryStream.Length > 6L) { oofHistoryStream.SetLength(6L); } }
internal static void RemoveOofHistoryEntriesWithProperty(MailboxSession itemStore, bool oofEnabled, OofHistory.PropId propId, byte[] propValue) { MapiFolder mapiFolder = null; MapiFolder mapiFolder2 = null; MapiStream mapiStream = null; bool flag = false; byte[] initialBytes = oofEnabled ? OofHistory.InitialBytesOofStateOn : OofHistory.InitialBytesOofStateOff; try { if (OofHistory.TryOpenFolder(itemStore, out mapiFolder, out mapiFolder2)) { long hashCode = (long)itemStore.GetHashCode(); bool flag2; mapiStream = OofHistory.OpenAndLockStream(mapiFolder2, initialBytes, hashCode, out flag2); flag = true; if (!flag2) { int num; if (OofHistory.TryGetStreamLengthForEntryRemoval(hashCode, mapiStream, out num)) { byte[] buffer; if (OofHistory.TryReadAllFromStream(hashCode, mapiStream, num, out buffer)) { int num2 = OofHistory.RemoveEntriesWithProperty(num, buffer, propId, propValue, hashCode); if (num2 < num) { mapiStream.Seek(0L, SeekOrigin.Begin); mapiStream.Write(buffer, 0, num2); mapiStream.SetLength((long)num2); } } } } } } finally { if (mapiStream != null) { try { if (flag) { mapiStream.UnlockRegion(0L, 1L, 1); } } finally { mapiStream.Dispose(); } } if (mapiFolder != null) { mapiFolder.Dispose(); mapiFolder = null; } if (mapiFolder2 != null) { mapiFolder2.Dispose(); mapiFolder2 = null; } } }
internal static void ClearOofHistory(MailboxSession itemStore, bool oofEnabled) { MapiFolder mapiFolder = null; MapiFolder mapiFolder2 = null; MapiStream mapiStream = null; bool flag = false; byte[] initialBytes = oofEnabled ? OofHistory.InitialBytesOofStateOn : OofHistory.InitialBytesOofStateOff; try { if (OofHistory.TryOpenFolder(itemStore, out mapiFolder, out mapiFolder2)) { long num = (long)itemStore.GetHashCode(); bool flag2; mapiStream = OofHistory.OpenAndLockStream(mapiFolder2, initialBytes, num, out flag2); flag = true; if (!flag2) { byte[] array = new byte[2]; if (2 != mapiStream.Read(array, 0, 2)) { OofHistory.ResetOofHistoryProperty(mapiStream, initialBytes, num); } else { bool flag3 = 0 != array[1]; if (flag3 == oofEnabled) { OofHistory.Tracer.TraceDebug <bool>(num, "Current OOF state {0} matches in OOF history and mailbox properties, clear is not executed.", oofEnabled); } else { OofHistory.ResetOofHistoryProperty(mapiStream, initialBytes, num); } } } } } finally { if (mapiStream != null) { try { if (flag) { mapiStream.UnlockRegion(0L, 1L, 1); } } finally { mapiStream.Dispose(); } } if (mapiFolder != null) { mapiFolder.Dispose(); mapiFolder = null; } if (mapiFolder2 != null) { mapiFolder2.Dispose(); mapiFolder2 = null; } } }
public void SaveObjectChunks(List <T> chunks, int maxChunks, MoveObjectInfo <T> .GetAdditionalProperties getAdditionalPropertiesCallback) { if (chunks.Count > maxChunks) { MrsTracer.Common.Warning("Too many chunks supplied, truncating", new object[0]); chunks.RemoveRange(0, chunks.Count - maxChunks); } bool flag = false; if (this.message == null) { using (MapiFolder mapiFolder = MapiUtils.OpenFolderUnderRoot(this.store, this.folderName, true)) { this.FolderId = mapiFolder.GetProp(PropTag.EntryId).GetBytes(); this.message = mapiFolder.CreateMessage(); } this.message.SetProps(new PropValue[] { new PropValue(PropTag.MessageClass, this.messageClass), new PropValue(PropTag.Subject, this.subject), new PropValue(PropTag.ReplyTemplateID, this.searchKey) }); flag = true; } if (chunks.Count > 1) { using (MapiTable attachmentTable = this.message.GetAttachmentTable()) { if (attachmentTable != null) { int num = attachmentTable.GetRowCount() - (maxChunks - chunks.Count); if (num > 0) { attachmentTable.SetColumns(MoveObjectInfo <T> .AttachmentTagsToLoad); PropValue[][] array = attachmentTable.QueryRows(num); for (int i = 0; i < num; i++) { this.message.DeleteAttach(array[i][0].GetInt()); } } } } for (int j = 0; j < chunks.Count - 1; j++) { int num2; using (MapiAttach mapiAttach = this.message.CreateAttach(out num2)) { using (MapiStream mapiStream = mapiAttach.OpenStream(PropTag.AttachDataBin, OpenPropertyFlags.Create)) { MoveObjectInfo <T> .SerializeToStream(chunks[j], mapiStream); } mapiAttach.SetProps(new PropValue[] { new PropValue(PropTag.AttachFileName, string.Format("MOI_Chunk_{0:yyyymmdd_HHmmssfff}", DateTime.UtcNow)), new PropValue(PropTag.AttachMethod, AttachMethods.ByValue) }); mapiAttach.SaveChanges(); } } } T obj = chunks[chunks.Count - 1]; if (getAdditionalPropertiesCallback != null) { this.message.SetProps(getAdditionalPropertiesCallback(this.store)); } using (MapiStream mapiStream2 = this.message.OpenStream(PropTag.Body, OpenPropertyFlags.Create)) { MoveObjectInfo <T> .SerializeToStream(obj, mapiStream2); } this.message.SaveChanges(); if (flag) { this.MessageId = this.message.GetProp(PropTag.EntryId).GetBytes(); } }