Exemplo n.º 1
0
 public static byte[] SerializeToBytes(TreeChunk instance)
 {
     using (MemoryStream ms = new MemoryStream()) {
         Serialize(ms, instance);
         return(ms.ToArray());
     }
 }
Exemplo n.º 2
0
 public static void Serialize(Stream stream, TreeChunk instance)
 {
     if (instance.Directories != null)
     {
         foreach (Whisper.ChunkGenerator.TreeFile i1 in instance.Directories)
         {
             ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(1, Wire.LengthDelimited));
             using (MemoryStream ms1 = new MemoryStream()) {
                 Whisper.ChunkGenerator.TreeFile.Serialize(ms1, i1);
                 ProtocolParser.WriteBytes(stream, ms1.ToArray());
             }
         }
     }
     if (instance.Files != null)
     {
         foreach (Whisper.ChunkGenerator.TreeFile i2 in instance.Files)
         {
             ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(2, Wire.LengthDelimited));
             using (MemoryStream ms2 = new MemoryStream()) {
                 Whisper.ChunkGenerator.TreeFile.Serialize(ms2, i2);
                 ProtocolParser.WriteBytes(stream, ms2.ToArray());
             }
         }
     }
 }
Exemplo n.º 3
0
        public static TreeChunk Deserialize(Stream stream)
        {
            TreeChunk instance = new TreeChunk();

            Deserialize(stream, instance);
            return(instance);
        }
Exemplo n.º 4
0
        public static ChunkHash GenerateChunk(string path, Repo repo)
        {
            string fullPath = Path.GetFullPath (path);
            TreeChunk tree = new TreeChunk ();

            //Subdirectories
            string[] dirs = Directory.GetDirectories (fullPath);
            foreach (string d in dirs) {
                TreeFile df = new TreeFile ();
                df.Name = Path.GetFileName (d);
                df.TreeChunkHash = TreeChunk.GenerateChunk (d, repo).bytes;
                tree.Directories.Add (df);
            }

            //Files
            string[] files = Directory.GetFiles (fullPath);
            foreach (string f in files) {
                TreeFile ff = new TreeFile ();
                ff.Name = Path.GetFileName (f);
                ff.TreeChunkHash = StreamChunk.GenerateChunk (f, repo).bytes;
                tree.Files.Add (ff);
            }

            Chunk treeChunk = new Chunk (TreeChunk.SerializeToBytes (tree));
            ChunkHash ch = repo.WriteChunk (treeChunk);

            return ch;
        }
Exemplo n.º 5
0
        public static ChunkHash GenerateChunk(string path, Repo repo)
        {
            string    fullPath = Path.GetFullPath(path);
            TreeChunk tree     = new TreeChunk();

            //Subdirectories
            string[] dirs = Directory.GetDirectories(fullPath);
            foreach (string d in dirs)
            {
                TreeFile df = new TreeFile();
                df.Name          = Path.GetFileName(d);
                df.TreeChunkHash = TreeChunk.GenerateChunk(d, repo).bytes;
                tree.Directories.Add(df);
            }

            //Files
            string[] files = Directory.GetFiles(fullPath);
            foreach (string f in files)
            {
                TreeFile ff = new TreeFile();
                ff.Name          = Path.GetFileName(f);
                ff.TreeChunkHash = StreamChunk.GenerateChunk(f, repo).bytes;
                tree.Files.Add(ff);
            }

            Chunk     treeChunk = new Chunk(TreeChunk.SerializeToBytes(tree));
            ChunkHash ch        = repo.WriteChunk(treeChunk);

            return(ch);
        }
Exemplo n.º 6
0
        public static void Extract(Repo store, ChunkHash cid, string targetPath)
        {
            Directory.CreateDirectory(targetPath);

            Chunk     c    = store.ReadChunk(cid);
            TreeChunk tree = TreeChunk.Deserialize(c.Data);

            foreach (TreeFile file in tree.Files)
            {
                StreamChunk.Extract(store, ChunkHash.FromHashBytes(file.TreeChunkHash), Path.Combine(targetPath, file.Name));
            }

            foreach (TreeFile subdir in tree.Directories)
            {
                TreeChunk.Extract(store, ChunkHash.FromHashBytes(subdir.TreeChunkHash), Path.Combine(targetPath, subdir.Name));
            }
        }