Permalink
Browse files

chunker: lock file when chunking

  • Loading branch information...
1 parent 0282d98 commit 8c12347d9f4f8c662c6bb8dcffff2742836013f5 @hbons committed Aug 2, 2011
Showing with 49 additions and 34 deletions.
  1. +49 −34 Chunker.cs
View
@@ -62,47 +62,62 @@ public void FileToChunks (string [] source_file_paths)
int chunk_size = 1024 * 1024 * 4;
List <string> chunk_paths = new List<string> ();
- // TODO: threadpool
+ // TODO: threadpool and block
foreach (string source_file_path in source_file_paths) {
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.Substring (2);
- string chunk_container = hash.Substring (0, 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) {
- byte [] crypto_buffer = this.cryptographer.Encrypt (buffer);
- File.WriteAllBytes (chunk_file_path, crypto_buffer);
-
+ stream.Lock (0, stream.Length);
+ List<string> new_chunk_paths = new List<string> ();
+
+ try {
+ 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.Substring (2);
+ string chunk_container = hash.Substring (0, 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) {
+ byte [] crypto_buffer = this.cryptographer.Encrypt (buffer);
+ File.WriteAllBytes (chunk_file_path, crypto_buffer);
+
+ } else {
+ File.WriteAllBytes (chunk_file_path, buffer);
+ new_chunk_paths.Add (chunk_file_path);
+ }
+
+ chunk_paths.Add (chunk_file_path);
+
+ if (ChunkCreated != null)
+ ChunkCreated (chunk_file_path, current_chunk_size, hash);
+
+ Console.WriteLine ("Chunk " + hash + " created");
+
} else {
- File.WriteAllBytes (chunk_file_path, buffer);
+ Console.WriteLine ("Chunk " + hash + " exists");
}
-
- chunk_paths.Add (chunk_file_path);
-
- if (ChunkCreated != null)
- ChunkCreated (chunk_file_path, current_chunk_size, hash);
-
- Console.WriteLine ("Chunk " + hash + " created");
-
- } else {
- Console.WriteLine ("Chunk " + hash + " exists");
+
+ chunk_number++;
}
-
- chunk_number++;
+
+ } catch (IOException exception) {
+ foreach (string new_chunk_path in new_chunk_paths) {
+ if (File.Exists (new_chunk_path))
+ File.Delete (new_chunk_path); // TODO: what to do with ongoing transfers?
+ }
+
+ } finally {
+ stream.Unlock (0, stream.Length);
}
}
}

0 comments on commit 8c12347

Please sign in to comment.