/// <summary>Load the snapshot diff section from fsimage.</summary> /// <exception cref="System.IO.IOException"/> public void LoadSnapshotDiffSection(InputStream @in) { IList <INodeReference> refList = parent.GetLoaderContext().GetRefList(); while (true) { FsImageProto.SnapshotDiffSection.DiffEntry entry = FsImageProto.SnapshotDiffSection.DiffEntry .ParseDelimitedFrom(@in); if (entry == null) { break; } long inodeId = entry.GetInodeId(); INode inode = fsDir.GetInode(inodeId); FsImageProto.SnapshotDiffSection.DiffEntry.Type type = entry.GetType(); switch (type) { case FsImageProto.SnapshotDiffSection.DiffEntry.Type.Filediff: { LoadFileDiffList(@in, inode.AsFile(), entry.GetNumOfDiff()); break; } case FsImageProto.SnapshotDiffSection.DiffEntry.Type.Directorydiff: { LoadDirectoryDiffList(@in, inode.AsDirectory(), entry.GetNumOfDiff(), refList); break; } } } }
/// <exception cref="System.IO.IOException"/> private void SerializeFileDiffList(INodeFile file, OutputStream @out) { FileWithSnapshotFeature sf = file.GetFileWithSnapshotFeature(); if (sf != null) { IList <FileDiff> diffList = sf.GetDiffs().AsList(); FsImageProto.SnapshotDiffSection.DiffEntry entry = ((FsImageProto.SnapshotDiffSection.DiffEntry )FsImageProto.SnapshotDiffSection.DiffEntry.NewBuilder().SetInodeId(file.GetId() ).SetType(FsImageProto.SnapshotDiffSection.DiffEntry.Type.Filediff).SetNumOfDiff (diffList.Count).Build()); entry.WriteDelimitedTo(@out); for (int i = diffList.Count - 1; i >= 0; i--) { FileDiff diff = diffList[i]; FsImageProto.SnapshotDiffSection.FileDiff.Builder fb = FsImageProto.SnapshotDiffSection.FileDiff .NewBuilder().SetSnapshotId(diff.GetSnapshotId()).SetFileSize(diff.GetFileSize() ); if (diff.GetBlocks() != null) { foreach (Block block in diff.GetBlocks()) { fb.AddBlocks(PBHelper.Convert(block)); } } INodeFileAttributes copy = diff.snapshotINode; if (copy != null) { fb.SetName(ByteString.CopyFrom(copy.GetLocalNameBytes())).SetSnapshotCopy(FSImageFormatPBINode.Saver.BuildINodeFile (copy, parent.GetSaverContext())); } ((FsImageProto.SnapshotDiffSection.FileDiff)fb.Build()).WriteDelimitedTo(@out); } } }
/// <exception cref="System.IO.IOException"/> private void SerializeDirDiffList(INodeDirectory dir, IList <INodeReference> refList , OutputStream @out) { DirectoryWithSnapshotFeature sf = dir.GetDirectoryWithSnapshotFeature(); if (sf != null) { IList <DirectoryWithSnapshotFeature.DirectoryDiff> diffList = sf.GetDiffs().AsList (); FsImageProto.SnapshotDiffSection.DiffEntry entry = ((FsImageProto.SnapshotDiffSection.DiffEntry )FsImageProto.SnapshotDiffSection.DiffEntry.NewBuilder().SetInodeId(dir.GetId()) .SetType(FsImageProto.SnapshotDiffSection.DiffEntry.Type.Directorydiff).SetNumOfDiff (diffList.Count).Build()); entry.WriteDelimitedTo(@out); for (int i = diffList.Count - 1; i >= 0; i--) { // reverse order! DirectoryWithSnapshotFeature.DirectoryDiff diff = diffList[i]; FsImageProto.SnapshotDiffSection.DirectoryDiff.Builder db = FsImageProto.SnapshotDiffSection.DirectoryDiff .NewBuilder().SetSnapshotId(diff.GetSnapshotId()).SetChildrenSize(diff.GetChildrenSize ()).SetIsSnapshotRoot(diff.IsSnapshotRoot()); INodeDirectoryAttributes copy = diff.snapshotINode; if (!diff.IsSnapshotRoot() && copy != null) { db.SetName(ByteString.CopyFrom(copy.GetLocalNameBytes())).SetSnapshotCopy(FSImageFormatPBINode.Saver.BuildINodeDirectory (copy, parent.GetSaverContext())); } // process created list and deleted list IList <INode> created = diff.GetChildrenDiff().GetList(Diff.ListType.Created); db.SetCreatedListSize(created.Count); IList <INode> deleted = diff.GetChildrenDiff().GetList(Diff.ListType.Deleted); foreach (INode d in deleted) { if (d.IsReference()) { refList.AddItem(d.AsReference()); db.AddDeletedINodeRef(refList.Count - 1); } else { db.AddDeletedINode(d.GetId()); } } ((FsImageProto.SnapshotDiffSection.DirectoryDiff)db.Build()).WriteDelimitedTo(@out ); SaveCreatedList(created, @out); } } }
/// <exception cref="System.IO.IOException"/> private void DumpSnapshotDiffSection(InputStream @in) { @out.Write("<SnapshotDiffSection>"); while (true) { FsImageProto.SnapshotDiffSection.DiffEntry e = FsImageProto.SnapshotDiffSection.DiffEntry .ParseDelimitedFrom(@in); if (e == null) { break; } @out.Write("<diff>"); O("inodeid", e.GetInodeId()); switch (e.GetType()) { case FsImageProto.SnapshotDiffSection.DiffEntry.Type.Filediff: { for (int i = 0; i < e.GetNumOfDiff(); ++i) { @out.Write("<filediff>"); FsImageProto.SnapshotDiffSection.FileDiff f = FsImageProto.SnapshotDiffSection.FileDiff .ParseDelimitedFrom(@in); O("snapshotId", f.GetSnapshotId()).O("size", f.GetFileSize()).O("name", f.GetName ().ToStringUtf8()); @out.Write("</filediff>\n"); } break; } case FsImageProto.SnapshotDiffSection.DiffEntry.Type.Directorydiff: { for (int i = 0; i < e.GetNumOfDiff(); ++i) { @out.Write("<dirdiff>"); FsImageProto.SnapshotDiffSection.DirectoryDiff d = FsImageProto.SnapshotDiffSection.DirectoryDiff .ParseDelimitedFrom(@in); O("snapshotId", d.GetSnapshotId()).O("isSnapshotroot", d.GetIsSnapshotRoot()).O("childrenSize" , d.GetChildrenSize()).O("name", d.GetName().ToStringUtf8()); for (int j = 0; j < d.GetCreatedListSize(); ++j) { FsImageProto.SnapshotDiffSection.CreatedListEntry ce = FsImageProto.SnapshotDiffSection.CreatedListEntry .ParseDelimitedFrom(@in); @out.Write("<created>"); O("name", ce.GetName().ToStringUtf8()); @out.Write("</created>\n"); } foreach (long did in d.GetDeletedINodeList()) { @out.Write("<deleted>"); O("inode", did); @out.Write("</deleted>\n"); } foreach (int dRefid in d.GetDeletedINodeRefList()) { @out.Write("<deleted>"); O("inodereference-index", dRefid); @out.Write("</deleted>\n"); } @out.Write("</dirdiff>\n"); } break; } default: { break; } } @out.Write("</diff>"); } @out.Write("</SnapshotDiffSection>\n"); }