1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
22
|***************************************************************************/
34
/// Special Node types that sync deals with.
36
public enum SyncNodeType : byte
39
/// This is a generic node.
43
/// This node represents a file in the FS.
47
/// This node represents a directory in the FS.
51
/// This node represents a deleted node.
61
/// class to represent the minimal information that the sync code needs
62
/// to know about a node to determine if it needs to be synced.
64
public class SyncNodeInfo: IComparable
73
/// The local incarnation for the node.
75
public ulong LocalIncarnation;
77
/// The Master incarnation for this node.
79
public ulong MasterIncarnation;
81
/// The base type of this node.
83
public SyncNodeType NodeType;
85
/// The SyncOperation to perform on this node.
87
public SyncOperation Operation;
89
/// The size of this instance serialized.
90
/// If fields are added this needs to be updated.
92
public static int InstanceSize = 34;
101
public SyncNodeInfo()
106
/// Constructs a SyncNodeInfo from a ShallowNode.
108
/// <param name="collection"></param>
109
/// <param name="sn"></param>
110
public SyncNodeInfo(Collection collection, ShallowNode sn) :
111
this (new Node(collection, sn))
116
/// Construct a SyncNodeInfo from a stream.
118
/// <param name="reader"></param>
119
internal SyncNodeInfo(BinaryReader reader)
121
this.ID = new Guid(reader.ReadBytes(16)).ToString();
122
this.LocalIncarnation = reader.ReadUInt64();
123
this.MasterIncarnation = reader.ReadUInt64();
124
this.NodeType = (SyncNodeType)reader.ReadByte();
125
this.Operation = (SyncOperation)reader.ReadByte();
129
/// Construct a SyncNodeStamp from a Node.
131
/// <param name="node">the node to use.</param>
132
internal SyncNodeInfo(Node node)
135
this.LocalIncarnation = node.LocalIncarnation;
136
this.MasterIncarnation = node.MasterIncarnation;
137
this.NodeType = GetSyncNodeType(node.Type);
138
this.Operation = node.Type == NodeTypes.TombstoneType ? SyncOperation.Delete : SyncOperation.Unknown;
142
/// Consturct a SyncNodeStamp from a ChangeLogRecord.
144
/// <param name="record">The record to use.</param>
145
internal SyncNodeInfo(ChangeLogRecord record)
147
this.ID = record.EventID;
148
this.LocalIncarnation = record.SlaveRev;
149
this.MasterIncarnation = record.MasterRev;
150
this.NodeType = GetSyncNodeType(record.Type.ToString());
151
switch (record.Operation)
153
case ChangeLogRecord.ChangeLogOp.Changed:
154
this.Operation = SyncOperation.Change;
156
case ChangeLogRecord.ChangeLogOp.Created:
157
this.Operation = SyncOperation.Create;
159
case ChangeLogRecord.ChangeLogOp.Deleted:
160
this.Operation = SyncOperation.Delete;
162
case ChangeLogRecord.ChangeLogOp.Renamed:
163
this.Operation = SyncOperation.Rename;
166
this.Operation = SyncOperation.Unknown;
176
/// Serializes this instance into a stream.
178
/// <param name="writer">The stream to serialize to.</param>
179
internal void Serialize(BinaryWriter writer)
181
writer.Write(new Guid(ID).ToByteArray());
182
writer.Write(LocalIncarnation);
183
writer.Write(MasterIncarnation);
184
writer.Write((byte)NodeType);
185
writer.Write((byte)Operation);
189
/// <summary> implement some convenient operator overloads </summary>
190
public int CompareTo(object obj)
192
return ID.CompareTo(((SyncNodeInfo)obj).ID);
200
/// Converts the base type string into a SyncNodeType
202
/// <param name="baseType">The base type.</param>
203
/// <returns>the SyncNodeType.</returns>
204
private SyncNodeType GetSyncNodeType(string baseType)
206
if (baseType == NodeTypes.DirNodeType)
208
return SyncNodeType.Directory;
210
else if (baseType == NodeTypes.FileNodeType || baseType == NodeTypes.StoreFileNodeType)
212
return SyncNodeType.File;
216
return SyncNodeType.Generic;
228
/// This is the object that is used to sync a node.
230
public class SyncNode : SyncNodeInfo
235
/// The node as an XML string.
240
/// The size of this instance serialized.
242
public new int InstanceSize
244
get { return SyncNodeInfo.InstanceSize + node.Length; }
259
/// Create a SyncNode from a Node.
261
/// <param name="node">The node used to create the sync node.</param>
262
internal SyncNode(Node node) :
265
this.node = node.Properties.ToString(true);
269
/// Create a SyncNode from a stream.
271
/// <param name="reader">The stream containing the SyncNode.</param>
272
internal SyncNode(BinaryReader reader) :
275
this.MasterIncarnation = reader.ReadUInt64();
276
node = reader.ReadString();
284
/// Serialize the SyncNode to the stream.
286
/// <param name="writer"></param>
287
internal new void Serialize(BinaryWriter writer)
289
base.Serialize(writer);
290
writer.Write(MasterIncarnation);
301
/// Used to report the status of a sync.
303
public class SyncNodeStatus
308
/// The ID of the node.
310
public string nodeID;
312
/// The status of the sync.
314
public SyncStatus status;
317
/// The size of this instance serialized.
319
public int InstanceSize = 17;
324
/// Constructs a SyncNodeStatus object.
326
internal SyncNodeStatus()
328
status = SyncStatus.Error;
332
/// Create a SyncNodeStatus from a stream.
334
/// <param name="reader">The stream containing the SyncNode.</param>
335
internal SyncNodeStatus(BinaryReader reader)
337
this.nodeID = new Guid(reader.ReadBytes(16)).ToString();
338
this.status = (SyncStatus)reader.ReadByte();
342
/// Serialize the SyncNodeStatus to the stream.
344
/// <param name="writer"></param>
345
internal void Serialize(BinaryWriter writer)
347
writer.Write(new Guid(nodeID).ToByteArray());
348
writer.Write((byte)status);