public void WriteFile(JungleDiskBucket bucket, string path, Stream file) { string directory = path.Remove(path.LastIndexOf('/')); string filepart = path.Substring(path.LastIndexOf('/') + 1); SetBucket(bucket); // Setup the file pointer object string parentMarker = GetMarkerForPath(bucket, directory); string selfMarker = GetNewMarker(); int blockSize = 0; // for now string attributes = ""; string[] parts = new string[] { parentMarker, selfMarker, "file", FilterFileNameWrite(filepart, selfMarker), file.Length.ToString(), blockSize.ToString(), attributes }; string filePointer = string.Join("/", parts); s3.PutObject(filePointer, new byte[0], null); // empty object // Setup the file object itself try { parts = new string[] { "FILES", selfMarker, 0.ToString() }; string fileObject = string.Join("/", parts); s3.PutObject(fileObject, file, encryptNewFiles ? encryptionKey : null); } catch (Exception ex) { // If we failed to create the actual file object, delete the pointer we created. s3.DeleteObject(filePointer); throw ex; } }
public void CreateBucket(JungleDiskBucket bucket, string bucketPassword, bool newEncryptFilenames) { // Essentially we just create the 0.dir file (and 0.key if necessary). If 0.dir already exists, throw. // SetBucket(bucket); -- Don't call SetBucket() here; it will be called by BucketExists(). bool s3BucketExists; if (BucketExists(bucket, out s3BucketExists)) { throw new Exception("Bucket already exists"); } if (!s3BucketExists) { s3.CreateBucket(bucket.S3Bucket); // Make sure the bucket is visible before we continue: for (int retries = 0; retries < 20; retries++) { if (s3.BucketExists(bucket.S3Bucket)) { break; } Thread.Sleep(5000); //need to wait some period of time for the bucket to be visible? } SetBucket(bucket); // Call it now because if the base S3 bucket did not exist in BucketExists, SetBucket() did not get called. } if (bucketPassword != null) { // This will establish the 0.key file. ChangeBucketPassword(bucket, bucketPassword, newEncryptFilenames); } s3.PutObject("0.dir", new byte[] { }, bucketPassword); }
// Important: Caller must close the returned stream! public Stream ReadFile(JungleDiskBucket bucket, string path) { SetBucket(bucket); string objPath = "FILES/" + GetMarkerForPath(bucket, path) + "/0"; return(s3.GetObject(objPath)); }
public bool BucketExists(JungleDiskBucket bucket, out bool s3BucketExists) { s3BucketExists = true; try { // Check that the S3 bucket exists at all before we bother with SetBucket(). // Because SetBucket() tries to fetch objects from the bucket, if the S3 bucket does not exist it could create a negative cache entry for this bucket // and cause a delay until it is 'found' again. if (!s3.BucketExists(bucket.S3Bucket)) { s3BucketExists = false; return(false); } SetBucket(bucket); } catch (CryptographicException) { return(true); } catch (Exception) { } try { // We do this expecting them to throw: s3.GetObjectAsString("0.dir"); return(true); } catch (S3Exception ex) { if (ex.Code != "NoSuchKey" && ex.Code != "NoSuchBucket") { throw ex; } } catch (CryptographicException) { return(true); } try { s3.GetObjectAsString("0.key"); return(true); } catch (S3Exception ex) { if (ex.Code != "NoSuchKey" && ex.Code != "NoSuchBucket") { throw ex; } } catch (CryptographicException) { return(true); } return(false); }
protected void SetBucket(JungleDiskBucket bucket) { if (bucket.TypeOfBucket != BucketType.Advanced) { throw new NotSupportedException("Only Advanced buckets are presently supported."); } if (s3.SetBucket(bucket.S3Bucket, bucket.DisplayName, bucket.S3Bucket.EndsWith("-us"))) { ReadKeyFile(); } }
protected string GetMarkerForPath(JungleDiskBucket bucket, string path) { DirectoryItem item = GetDirectoryItemForPath(bucket, path); if (item == null) { return("ROOT"); } else { return(item.Marker); } }
public DirectoryContents GetDirectoryListing(JungleDiskBucket bucket, string path) { string parentMarker = ""; path = path.Trim(new char[] { '/', '\\' }); if (path.Length == 0) { parentMarker = "ROOT"; } else { parentMarker = GetMarkerForPath(bucket, path); } return(GetDirectoryListingForMarker(bucket, parentMarker)); }
protected void ChangeBucketPassword(JungleDiskBucket bucket, string newPassword, bool newEncryptFilenames) { SetBucket(bucket); string keyFileRaw = null; try { keyFileRaw = s3.GetObjectAsString("0.key"); XmlNode node; XmlDocument doc = new XmlDocument(); // Parse the XML to make sure the key file is valid. If not, an exception will be thrown. doc.LoadXml(keyFileRaw); // Note: encryptfilenames cannot be changed after bucket creation, but we will perform a sanity check here anyway. node = doc.SelectSingleNode("//encryptfilenames"); if ((node.InnerText == "1" ? true : false) != newEncryptFilenames) { throw new Exception("ChangeBucketPassword: newEncryptFilenames, " + newEncryptFilenames + " != existing value, " + node.InnerText); } node = doc.SelectSingleNode("//encryptnewfiles"); node.InnerText = (newPassword != null) ? "1" : "0"; keyFileRaw = doc.InnerXml; } catch (S3Exception ex) { if (ex.Code == "NoSuchKey") { // There is no 0.key file. We must create one. keyFileRaw = CreateKeyFile((newPassword != null), newEncryptFilenames); // We might like to just ReadKeyFile() to update the flags later on, but since S3 doesn't guarantee that it will be immediately available, we'll just set them by hand: encryptNewFiles = (newPassword != null); encryptFilenames = newEncryptFilenames; encryptionKey = newPassword; } else { throw ex; } } s3.PutObject("0.key", Encoding.Default.GetBytes(keyFileRaw), newPassword); AddKey(newPassword); }
// Returns null if the path is the root path. protected DirectoryItem GetDirectoryItemForPath(JungleDiskBucket bucket, string path) { DirectoryItem item = null; string[] pathParts = path.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); if (pathParts.Length == 0) { return(null); } DirectoryContents contents = GetDirectoryListing(bucket, ""); foreach (string pathPart in pathParts) { item = contents.ItemFor(pathPart); if (item.IsDirectory == false) { return(item); } contents = GetDirectoryListingForMarker(bucket, item.Marker); } return(item); }
protected DirectoryContents GetDirectoryListingForMarker(JungleDiskBucket bucket, string parentMarker) { DirectoryContents output = new DirectoryContents(); string resource = parentMarker + "/"; SetBucket(bucket); string marker = ""; // Different kind of marker for continuing listings. List <string> markers = new List <string>(); Regex filePointerRegex = new Regex("^([a-f0-9]{32})/file/(.+?)/(\\d+)/(\\d+)/(.*)$"); // Does not contain parent marker since it has been stripped as part of the prefix. ([a-f0-9]{32}|ROOT)/ Regex dirPointerRegex = new Regex("^([a-f0-9]{32})/dir/(.+?)(/(.+))?$"); S3Connection.ReceiveContentsDelegate markerDelegate = delegate(string key) { Match m; m = filePointerRegex.Match(key); if (m.Success) { string selfMarker = m.Groups[1].Value; string name = FilterFileNameRead(m.Groups[2].Value, selfMarker); int size = Convert.ToInt32(m.Groups[3].Value); string attributes = m.Groups[5].Value; output.Add(new DirectoryItem(name, selfMarker, parentMarker, false, size, attributes, resource + key, "FILES/" + selfMarker + "/0")); } m = dirPointerRegex.Match(key); if (m.Success) { string selfMarker = m.Groups[1].Value; string name = FilterFileNameRead(m.Groups[2].Value, selfMarker); string attributes = m.Groups[3].Success ? m.Groups[4].Value : ""; // this may not work output.Add(new DirectoryItem(name, selfMarker, parentMarker, true, 0, attributes, resource + key, null)); } }; s3.GetObjectList(resource, ref marker, 0, null, markerDelegate, null); return(output); }
// Returns null if the path is the root path. protected DirectoryItem GetDirectoryItemForPath(JungleDiskBucket bucket, string path) { DirectoryItem item = null; string[] pathParts = path.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); if (pathParts.Length == 0) return null; DirectoryContents contents = GetDirectoryListing(bucket, ""); foreach (string pathPart in pathParts) { item = contents.ItemFor(pathPart); if (item.IsDirectory == false) return item; contents = GetDirectoryListingForMarker(bucket, item.Marker); } return item; }
protected string GetMarkerForPath(JungleDiskBucket bucket, string path) { DirectoryItem item = GetDirectoryItemForPath(bucket, path); if (item == null) return "ROOT"; else return item.Marker; }
public void DeleteFile(JungleDiskBucket bucket, DirectoryItem item) { s3.DeleteObject(item.PointerKey); s3.DeleteObject(item.FileKey); }
protected void SetBucket(JungleDiskBucket bucket) { if (bucket.TypeOfBucket != BucketType.Advanced) throw new NotSupportedException("Only Advanced buckets are presently supported."); if(s3.SetBucket(bucket.S3Bucket, bucket.DisplayName, bucket.S3Bucket.EndsWith("-us"))) ReadKeyFile(); }
public void DeleteFile(JungleDiskBucket bucket, string path) { DeleteFile(bucket, GetDirectoryItemForPath(bucket, path)); }
protected void ChangeBucketPassword(JungleDiskBucket bucket, string newPassword, bool newEncryptFilenames) { SetBucket(bucket); string keyFileRaw = null; try { keyFileRaw = s3.GetObjectAsString("0.key"); XmlNode node; XmlDocument doc = new XmlDocument(); // Parse the XML to make sure the key file is valid. If not, an exception will be thrown. doc.LoadXml(keyFileRaw); // Note: encryptfilenames cannot be changed after bucket creation, but we will perform a sanity check here anyway. node = doc.SelectSingleNode("//encryptfilenames"); if ((node.InnerText == "1" ? true : false) != newEncryptFilenames) throw new Exception("ChangeBucketPassword: newEncryptFilenames, "+newEncryptFilenames+" != existing value, " + node.InnerText); node = doc.SelectSingleNode("//encryptnewfiles"); node.InnerText = (newPassword != null) ? "1" : "0"; keyFileRaw = doc.InnerXml; } catch (S3Exception ex) { if (ex.Code == "NoSuchKey") { // There is no 0.key file. We must create one. keyFileRaw = CreateKeyFile((newPassword != null), newEncryptFilenames); // We might like to just ReadKeyFile() to update the flags later on, but since S3 doesn't guarantee that it will be immediately available, we'll just set them by hand: encryptNewFiles = (newPassword != null); encryptFilenames = newEncryptFilenames; encryptionKey = newPassword; } else throw ex; } s3.PutObject("0.key", Encoding.Default.GetBytes(keyFileRaw), newPassword); AddKey(newPassword); }
public void CreateBucket(JungleDiskBucket bucket, string bucketPassword, bool newEncryptFilenames) { // Essentially we just create the 0.dir file (and 0.key if necessary). If 0.dir already exists, throw. // SetBucket(bucket); -- Don't call SetBucket() here; it will be called by BucketExists(). bool s3BucketExists; if (BucketExists(bucket, out s3BucketExists)) throw new Exception("Bucket already exists"); if (!s3BucketExists) { s3.CreateBucket(bucket.S3Bucket); // Make sure the bucket is visible before we continue: for (int retries = 0; retries < 20; retries++) { if (s3.BucketExists(bucket.S3Bucket)) break; Thread.Sleep(5000); //need to wait some period of time for the bucket to be visible? } SetBucket(bucket); // Call it now because if the base S3 bucket did not exist in BucketExists, SetBucket() did not get called. } if (bucketPassword != null) { // This will establish the 0.key file. ChangeBucketPassword(bucket, bucketPassword, newEncryptFilenames); } s3.PutObject("0.dir", new byte[] { }, bucketPassword); }
public void ChangeBucketPassword(JungleDiskBucket bucket, string newPassword) { SetBucket(bucket); ChangeBucketPassword(bucket, newPassword, EncryptFilenames); }
// Creates the S3 bucket which will contain the JD2.0 buckets. public void PrepareBucket() { string s3BucketName = JungleDiskBucket.S3BucketFromAccessKey(AccessKey); s3.CreateBucket(s3BucketName); }
public bool BucketExists(JungleDiskBucket bucket) { bool s3BucketExists; return BucketExists(bucket, out s3BucketExists); }
public DirectoryContents GetDirectoryListing(JungleDiskBucket bucket, string path) { string parentMarker = ""; path = path.Trim(new char[] { '/', '\\' }); if (path.Length == 0) parentMarker = "ROOT"; else parentMarker = GetMarkerForPath(bucket, path); return GetDirectoryListingForMarker(bucket, parentMarker); }
// Important: Caller must close the returned stream! public Stream ReadFile(JungleDiskBucket bucket, string path) { SetBucket(bucket); string objPath = "FILES/" + GetMarkerForPath(bucket, path) + "/0"; return s3.GetObject(objPath); }
protected DirectoryContents GetDirectoryListingForMarker(JungleDiskBucket bucket, string parentMarker) { DirectoryContents output = new DirectoryContents(); string resource = parentMarker + "/"; SetBucket(bucket); string marker = ""; // Different kind of marker for continuing listings. List<string> markers = new List<string>(); Regex filePointerRegex = new Regex("^([a-f0-9]{32})/file/(.+?)/(\\d+)/(\\d+)/(.*)$"); // Does not contain parent marker since it has been stripped as part of the prefix. ([a-f0-9]{32}|ROOT)/ Regex dirPointerRegex = new Regex("^([a-f0-9]{32})/dir/(.+?)(/(.+))?$"); S3Connection.ReceiveContentsDelegate markerDelegate = delegate(string key) { Match m; m = filePointerRegex.Match(key); if (m.Success) { string selfMarker = m.Groups[1].Value; string name = FilterFileNameRead(m.Groups[2].Value, selfMarker); int size = Convert.ToInt32(m.Groups[3].Value); string attributes = m.Groups[5].Value; output.Add(new DirectoryItem(name, selfMarker, parentMarker, false, size, attributes, resource + key, "FILES/" + selfMarker + "/0")); } m = dirPointerRegex.Match(key); if (m.Success) { string selfMarker = m.Groups[1].Value; string name = FilterFileNameRead(m.Groups[2].Value, selfMarker); string attributes = m.Groups[3].Success ? m.Groups[4].Value : ""; // this may not work output.Add(new DirectoryItem(name, selfMarker, parentMarker, true, 0, attributes, resource + key, null)); } }; s3.GetObjectList(resource, ref marker, 0, null, markerDelegate, null); return output; }
public bool BucketExists(JungleDiskBucket bucket, out bool s3BucketExists) { s3BucketExists = true; try { // Check that the S3 bucket exists at all before we bother with SetBucket(). // Because SetBucket() tries to fetch objects from the bucket, if the S3 bucket does not exist it could create a negative cache entry for this bucket // and cause a delay until it is 'found' again. if (!s3.BucketExists(bucket.S3Bucket)) { s3BucketExists = false; return false; } SetBucket(bucket); } catch (CryptographicException) { return true; } catch (Exception) { } try { // We do this expecting them to throw: s3.GetObjectAsString("0.dir"); return true; } catch (S3Exception ex) { if (ex.Code != "NoSuchKey" && ex.Code != "NoSuchBucket") throw ex; } catch (CryptographicException) { return true; } try { s3.GetObjectAsString("0.key"); return true; } catch (S3Exception ex) { if (ex.Code != "NoSuchKey" && ex.Code != "NoSuchBucket") throw ex; } catch (CryptographicException) { return true; } return false; }
public bool BucketExists(JungleDiskBucket bucket) { bool s3BucketExists; return(BucketExists(bucket, out s3BucketExists)); }