Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 5172d45f4d2b575008f757a7cbd1743f052da7d6 0 parents
@hbons authored
2  .gitignore
@@ -0,0 +1,2 @@
+bin
+.DS_Store
111 Chunker.cs
@@ -0,0 +1,111 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+using System.IO;
+
+namespace Rainbows {
+
+ public class Chunker {
+
+ public string OutputDirectory;
+
+ public delegate void ChunkCreatedHandler (string chunk_file_path, int chunk_size, string chunk_hash);
+ public event ChunkCreatedHandler ChunkCreated;
+
+ private Cryptographer cryptographer;
+
+
+ public Chunker (string output_directory)
+ {
+ this.cryptographer = null;
+ this.Init (output_directory);
+ }
+
+
+ public Chunker (string output_directory, Cryptographer cryptographer)
+ {
+ this.cryptographer = cryptographer;
+ this.Init (output_directory);
+ }
+
+
+ private void Init (string output_directory)
+ {
+ OutputDirectory = Path.Combine (output_directory, "objects");
+
+ if (!Directory.Exists (OutputDirectory))
+ Directory.CreateDirectory (OutputDirectory);
+ }
+
+
+ public void FileToChunks (string source_file_path)
+ {
+ int chunk_size = 1024 * 1024 * 4;
+
+ using (FileStream stream = File.OpenRead (source_file_path))
+ {
+ int current_chunk_size = 0;
+ var buffer = new byte [chunk_size];
+ int chunk_number = 1;
+
+ while ((current_chunk_size = stream.Read (buffer, 0, buffer.Length)) > 0)
+ {
+ string hash = Cryptographer.SHA1 (buffer);
+ string chunk_file_name = hash;
+ string chunk_container = chunk_file_name.Substring (6, 2);
+ string chunk_container_path = Path.Combine (OutputDirectory, chunk_container);
+ string chunk_file_path = Path.Combine (chunk_container_path, chunk_file_name);
+
+ if (!File.Exists (chunk_file_path)) {
+ if (!Directory.Exists (chunk_container_path))
+ Directory.CreateDirectory (chunk_container_path);
+
+ if (this.cryptographer != null)
+ buffer = this.cryptographer.Encrypt (buffer);
+
+ File.WriteAllBytes (chunk_file_path, buffer);
+ }
+
+ if (ChunkCreated != null)
+ ChunkCreated (chunk_file_path, current_chunk_size, hash);
+
+ chunk_number++;
+ }
+ }
+ }
+
+
+ public void ChunksToFile (string [] chunk_file_paths, string target_file_path)
+ {
+ using (FileStream stream = File.OpenWrite (target_file_path))
+ {
+ Console.WriteLine ("Creating file from chunks...");
+ byte [] buffer;
+
+ foreach (string chunk_path in chunk_file_paths) {
+ buffer = File.ReadAllBytes (chunk_path);
+
+ if (this.cryptographer != null)
+ buffer = this.cryptographer.Decrypt (buffer);
+
+ stream.Write (buffer, 0, buffer.Length);
+ }
+ }
+ }
+ }
+}
63 Cryptographer.cs
@@ -0,0 +1,63 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Rainbows {
+
+ public class Cryptographer {
+
+ private RijndaelManaged aes = new RijndaelManaged () {
+ KeySize = 256,
+ BlockSize = 128,
+ Mode = CipherMode.CBC,
+ Padding = PaddingMode.PKCS7
+ };
+
+
+ public Cryptographer (string password)
+ {
+ this.aes.IV = Convert.FromBase64String (password);
+ this.aes.Key = Convert.FromBase64String (password);
+ }
+
+
+ public byte [] Encrypt (byte [] buffer)
+ {
+ ICryptoTransform crypto = this.aes.CreateEncryptor ();
+ return crypto.TransformFinalBlock (buffer, 0, buffer.Length);
+ }
+
+
+ public byte [] Decrypt (byte [] buffer)
+ {
+ ICryptoTransform crypto = this.aes.CreateDecryptor ();
+ return crypto.TransformFinalBlock (buffer, 0, buffer.Length);
+ }
+
+
+ // 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 ("-", "");
+ }
+ }
+}
60 Main.cs
@@ -0,0 +1,60 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Text;
+using System.Xml;
+
+namespace Rainbows
+{
+ public class Rainbows
+ {
+ public static void Main (string [] args)
+ {
+ //
+ // PLAYING GROUND
+ //
+
+ Stopwatch s = new Stopwatch ();
+ s.Start ();
+
+ Chunker chunker = new Chunker ("/Users/hbons/SparkleShare/Rainbows/.sparkleshare",
+ new Cryptographer ("cGFzc3dvcmQAAAAAAAAAAA=="));
+
+ List<string> chunk_paths = new List<string> ();
+ List<string> hashes = new List<string> ();
+
+ chunker.ChunkCreated += delegate (string chunk_file_path, int chunk_size, string chunk_hash) {
+ Console.WriteLine ("Created: " + chunk_file_path + " (" + chunk_size + " bytes)");
+ chunk_paths.Add (chunk_file_path);
+ hashes.Add (chunk_hash);
+ // Upload files
+ };
+
+ chunker.FileToChunks ("/Users/hbons/SparkleShare/Rainbows/hp.avi");
+
+ Blobs blobs = new Blobs ("Users/hbons/SparkleShare/Rainbows/.sparkleshare");
+ blobs.Store ("hp.avi", hashes.ToArray ());
+
+ s.Stop ();
+ Console.WriteLine ("Total time: " + (int) s.Elapsed.TotalSeconds + " seconds");
+ }
+ }
+}
77 Objects.cs
@@ -0,0 +1,77 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+using System.Collections;
+
+namespace Rainbows {
+
+ public class Commit {
+
+ public string Hash;
+ public string ParentHash;
+
+ public string UserName;
+ public string UserEmail;
+ public DateTime Timestamp;
+
+ public Tree Tree;
+ }
+
+
+ public class Tree {
+
+ public string Hash;
+ public string Path;
+
+
+ // Key: blob hash
+ // Value: file name
+ public Hashtable Blobs {
+ get {
+ return null;
+ }
+ }
+
+
+ // Key: tree hash
+ // Value: folder name
+ public Hashtable Trees {
+ get {
+ return null;
+ }
+ }
+ }
+
+
+ public class Blob {
+
+ public string Hash;
+
+ public Chunk [] Chunks {
+ get {
+ return null;
+ }
+ }
+ }
+
+
+ public class Chunk {
+
+ public string Hash;
+ }
+}
47 Rainbows.csproj
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{FDEC5172-D787-434C-94C7-B64EECC21E6E}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>Rainbows</RootNamespace>
+ <AssemblyName>Rainbows</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <PlatformTarget>x86</PlatformTarget>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <PlatformTarget>x86</PlatformTarget>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Compile Include="Main.cs" />
+ <Compile Include="Cryptographer.cs" />
+ <Compile Include="Chunker.cs" />
+ <Compile Include="Objects.cs" />
+ <Compile Include="Repository.cs" />
+ <Compile Include="TransferManager.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+</Project>
20 Rainbows.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rainbows", "Rainbows.csproj", "{FDEC5172-D787-434C-94C7-B64EECC21E6E}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {FDEC5172-D787-434C-94C7-B64EECC21E6E}.Debug|x86.ActiveCfg = Debug|x86
+ {FDEC5172-D787-434C-94C7-B64EECC21E6E}.Debug|x86.Build.0 = Debug|x86
+ {FDEC5172-D787-434C-94C7-B64EECC21E6E}.Release|x86.ActiveCfg = Release|x86
+ {FDEC5172-D787-434C-94C7-B64EECC21E6E}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
+ GlobalSection(MonoDevelopProperties) = preSolution
+ StartupItem = Rainbows.csproj
+ EndGlobalSection
+EndGlobal
91 Repository.cs
@@ -0,0 +1,91 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+using System.Collections;
+
+namespace Rainbows {
+
+ public class Repository {
+
+ public Repository (string path)
+ {
+
+ }
+
+
+ public void Commit ()
+ {
+ // - Walk the new tree and create blobs, trees
+ // - Create new commit object: 'current' file is ParentHash,
+ // - Update 'current' file
+ }
+
+
+ public void Push ()
+ {
+
+ }
+
+
+ public void Pull ()
+ {
+
+ }
+ }
+
+ // TODO: integrate into Repository
+ public class Blobs {
+
+ public readonly string OutputDirectory;
+
+
+ public Blobs (string output_directory)
+ {
+ OutputDirectory = Path.Combine (output_directory, "objects");
+
+ if (!Directory.Exists (output_directory))
+ Directory.CreateDirectory (output_directory);
+ }
+
+
+ public void Store (string file_name, string [] chunk_hashes)
+ {
+ string hash = string.Join ("", chunk_hashes);
+ string file_store_name = Cryptographer.SHA1 (Encoding.ASCII.GetBytes (hash));
+
+ string file_store_container = file_store_name.Substring (5, 2);
+ string file_store_container_path = Path.Combine (OutputDirectory, file_store_container);
+ string file_store_path = Path.Combine (file_store_container_path, file_store_name);
+
+ if (!File.Exists (file_store_path)) {
+ if (!Directory.Exists (file_store_container_path))
+ Directory.CreateDirectory (file_store_container_path);
+
+ using (FileStream stream = File.OpenWrite (file_store_path))
+ {
+ foreach (string chunk_hash in chunk_hashes) {
+ byte [] buffer = Encoding.ASCII.GetBytes (chunk_hash + "\n");
+ stream.Write (buffer, 0, buffer.Length);
+ }
+ Console.WriteLine ("Created: " + file_store_path);
+ }
+ }
+ }
+ }
+
+}
29 TransferManager.cs
@@ -0,0 +1,29 @@
+// Rainbows, an experimental backend for SparkleShare
+// Copyright (C) 2011 Hylke Bons <hylkebons@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+using System;
+
+namespace Rainbows {
+
+ public class TransferManager {
+
+ public TransferManager (string path)
+ {
+
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.