Permalink
Browse files

implement object writing

  • Loading branch information...
1 parent a00a6ca commit 0b3f6ac79e34c83dc927a1c7dedae1b308af9c6d @hbons committed Sep 10, 2011
Showing with 114 additions and 54 deletions.
  1. +2 −1 Rainbows/Chunker.cs
  2. +2 −2 Rainbows/Index.cs
  3. +95 −50 Rainbows/Objects.cs
  4. +4 −0 Rainbows/Rainbows.csproj
  5. +11 −1 Rainbows/Utils.cs
View
@@ -111,8 +111,9 @@ public string FileToChunks (string source_file_path, int chunk_size)
} finally {
stream.Unlock (0, stream.Length);
- return ""; // Full hash
}
+
+ return ""; // Full hash
}
}
View
@@ -72,7 +72,7 @@ public void Commit ()
chunker.NameChunk = delegate (string chunk_file_name) {
string hash = chunk_file_name;
string chunk_container = hash.Substring (0, 2);
- string chunk_file_name = hash.Substring (2);
+ chunk_file_name = hash.Substring (2);
return Path.Combine (chunker.OutputDirectory, chunk_container, chunk_file_name);
};
@@ -83,7 +83,7 @@ public void Commit ()
chunker.ChunkCreated += delegate (string chunk_file_path, int chunk_size,
string chunk_hash) {
// add hash to "todo" transfer list
- transfer_manager.UploadObject (chunk_file_path);
+ //transfer_manager.UploadObject (chunk_file_path);
// remove hash to "todo" transfer list
};
View
@@ -43,26 +43,36 @@ public HashObject (string hash)
return File.ReadAllBytes (file_path);
} catch (IOException) {
+ Utils.Log ("Could not read object " + hash);
return null;
}
}
- protected void WriteHashObject (string hash, string lines)
+ public static void WriteHashObject (string hash, byte [] buffer)
{
+ string file_path = Path.Combine (DatabasePath, "objects",
+ hash.Substring (0, 2), hash.Substring (2));
+
+ try {
+ File.WriteAllBytes (file_path, buffer);
+
+ } catch (IOException) {
+ Utils.Log ("Could not write object " + hash);
+ }
}
- protected string [] ToLines (byte [] buffer)
+ public static string [] ToLines (byte [] buffer)
{
string line = ASCIIEncoding.ASCII.GetString (buffer);
return line.Split ("\n".ToCharArray (), StringSplitOptions.RemoveEmptyEntries);
}
- protected byte [] ToBytes (string line)
+ public static byte [] ToBytes (string line)
{
- return ASCIIEncoding.ASCII.GetBytes (line);
+ return UnicodeEncoding.Unicode.GetBytes (line);
}
}
@@ -80,22 +90,35 @@ public Commit (string hash) : base (hash)
}
- public Tree Tree {
+ public Tree Root {
get {
- return null;
- }
+ byte [] buffer = ReadHashObject (Hash);
+ string line = ToLines (buffer) [0];
+ string hash = line.Substring (0, line.IndexOf (" "));
- set {
- Tree tree = value;
- // WriteHashObject
+ return new Tree (hash);
}
}
+
+
+ public static Commit Write (User author, DateTime timestamp,
+ Commit parent, Tree root)
+ {
+ int seconds_since_epoch = (int) (timestamp - new DateTime (1970, 1, 1)).TotalSeconds;
+
+ string line = root.Hash + " " + seconds_since_epoch + " " +
+ author.Name + " " + "<" + author.Email + ">" + "\n";
+
+ string hash = Utils.SHA1 (line);
+ HashObject.WriteHashObject (hash, ToBytes (line));
+
+ return new Commit (hash);
+ }
}
public class Tree : HashObject {
-
public Tree (string hash) : base (hash)
{
}
@@ -110,52 +133,66 @@ public Tree (string hash) : base (hash)
foreach (string line in lines) {
string [] columns = line.Split (" ".ToCharArray ());
- string hash = columns [0];
- string name = columns [1];
+ string name = columns [1];
- if (name.EndsWith ("/"))
+ if (name.EndsWith ("/")) {
+ string hash = columns [0];
trees.Add (name, new Tree (hash));
+ }
}
- if (trees.Count > 0)
- return trees;
- else
- return null;
+ return trees;
}
+ }
- set {
- IDictionaryEnumerator dictionary = value.GetEnumerator ();
- List<string> names = new List<string> ();
- List<Tree> trees = new List<Tree> ();
- while (dictionary.MoveNext ()) {
- string name = (string) dictionary.Key;
- Tree tree = (Tree) dictionary.Value;
+ public Hashtable Blobs {
+ get {
+ Hashtable blobs = new Hashtable ();
- names.Add (name);
- trees.Add (tree);
+ byte [] buffer = ReadHashObject (Hash);
+ string [] lines = ToLines (buffer);
+
+ foreach (string line in lines) {
+ string [] columns = line.Split (" ".ToCharArray ());
+ string name = columns [1];
+
+ if (!name.EndsWith ("/")) {
+ string hash = columns [0];
+ blobs.Add (name, new Blob (hash));
+ }
}
- byte [] buffer = ReadHashObject (Hash);
- List<string> lines;
+ return blobs;
+ }
+ }
- if (buffer == null) {
- lines = new List<string> ();
- // write trees
+ public static Tree Write (Hashtable trees_and_blobs)
+ {
+ IDictionaryEnumerator dictionary = trees_and_blobs.GetEnumerator ();
+ string tree_lines = "";
+ string blob_lines = "";
- } else {
- lines = new List<string> (ToLines (buffer));
+ while (dictionary.MoveNext ()) {
+ string name = (string) dictionary.Key;
- for (int i = 0; i < names.Count; i++) {
- string line = trees [i].Hash + " " + names [i] + "\n";
- lines.Add (line);
- }
- // append trees
- }
+ if (name.EndsWith ("/")) {
+ Tree tree = (Tree) dictionary.Value;
+ tree_lines += tree.Hash + " " + name + "\n";
- WriteHashObject (Hash, string.Join ("", lines.ToArray ()));
+ } else {
+ Blob blob = (Blob) dictionary.Value;
+ blob_lines += blob.Hash + " " + name + "\n";
+ }
}
+
+ string lines = tree_lines + blob_lines;
+
+ string hash = Utils.SHA1 (lines);
+ HashObject.WriteHashObject (hash, ToBytes (lines));
+
+ return new Tree (hash);
}
}
@@ -178,16 +215,20 @@ public Blob (string hash) : base (hash)
return chunks.ToArray ();
}
+ }
- set {
- Chunk [] chunks = value;
- string lines = "";
- foreach (Chunk chunk in chunks)
- lines += chunk + "\n";
+ public static Blob Write (Chunk [] chunks)
+ {
+ string lines = "";
- WriteHashObject (Hash, lines);
- }
+ foreach (Chunk chunk in chunks)
+ lines += chunk.Hash + "\n";
+
+ string hash = Utils.SHA1 (lines);
+ HashObject.WriteHashObject (hash, ToBytes (lines));
+
+ return new Blob (hash);
}
}
@@ -201,8 +242,12 @@ public Chunk (string hash) : base (hash)
public byte [] Bytes {
get {
- // TODO: return null if file doesn't exist so it can be downloaded
- return ReadHashObject (Hash);
+ byte [] buffer = ReadHashObject (Hash);
+
+ if (buffer != null)
+ return buffer;
+ else
+ return null;
}
set {
View
@@ -48,6 +48,10 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="Renci.SshNet, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\lib\Renci.SshNet.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Plugins\" />
View
@@ -32,11 +32,21 @@ public static void Log (string message)
}
- // Creates a SHA-1 hash of input
public static string SHA1 (byte [] buffer)
{
SHA1 sha1 = new SHA1CryptoServiceProvider ();
byte [] encoded_bytes = sha1.ComputeHash (buffer);
+
+ return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
+ }
+
+
+ public static string SHA1 (string s)
+ {
+ byte [] buffer = UnicodeEncoding.UTF8.GetBytes (s);
+ SHA1 sha1 = new SHA1CryptoServiceProvider ();
+ byte [] encoded_bytes = sha1.ComputeHash (buffer);
+
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
}
}

0 comments on commit 0b3f6ac

Please sign in to comment.