public string UserName { get; set; }
public string ApiKey { get; set; }
- private Network.AccountInfo _accountInfo;
+ private AccountInfo _accountInfo;
private static readonly ILog Log = LogManager.GetLogger(typeof(PithosMonitor));
public string RootPath
{
get { return _rootPath; }
- set
+ set
{
- _rootPath = value.ToLower();
+ _rootPath = String.IsNullOrWhiteSpace(value)
+ ? String.Empty
+ : value.ToLower();
}
}
CancellationTokenSource _cancellationSource;
- private bool _isInitialized;
+ private bool _started;
public void Start()
{
Contract.EndContractBlock();
StatusNotification.NotifyChange("Starting");
- if (_isInitialized)
+ if (_started)
{
if (!_cancellationSource.IsCancellationRequested)
return;
}
_cancellationSource = new CancellationTokenSource();
+ _started = true;
CloudClient=new CloudFilesClient(UserName,ApiKey);
var proxyUri = ProxyFromSettings();
_accountInfo.AccountPath = RootPath;
+ var pithosFolder = Path.Combine(RootPath, FolderConstants.PithosContainer);
+ if (!Directory.Exists(pithosFolder))
+ Directory.CreateDirectory(pithosFolder);
+ //Create the cache folder and ensure it is hidden
+ CreateHiddenFolder(RootPath, FolderConstants.CacheFolder);
+
+ var policy=CloudClient.GetAccountPolicies(_accountInfo);
+
+ StatusNotification.NotifyAccount(policy);
EnsurePithosContainers();
StatusKeeper.BlockHash = _blockHash;
StartNetworkAgent();
StartWorkflowAgent();
- WorkflowAgent.RestartInterruptedFiles(_accountInfo);
- _isInitialized = true;
+ WorkflowAgent.RestartInterruptedFiles(_accountInfo);
}
private void EnsurePithosContainers()
Log.Info("START");
try
{
- var fragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
+ var cachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
var directory = new DirectoryInfo(RootPath);
var files =
from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
- where !file.FullName.StartsWith(fragmentsPath, StringComparison.InvariantCultureIgnoreCase) &&
+ where !file.FullName.StartsWith(cachePath, StringComparison.InvariantCultureIgnoreCase) &&
!file.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase)
select file;
StatusKeeper.ProcessExistingFiles(files);
NetworkAgent.ProcessRemoteFiles();
}
- //Make sure a hidden fragments folder exists to store partial downloads
+ //Make sure a hidden cache folder exists to store partial downloads
private static string CreateHiddenFolder(string rootPath, string folderName)
{
if (String.IsNullOrWhiteSpace(rootPath))
var info = Directory.CreateDirectory(folder);
info.Attributes |= FileAttributes.Hidden;
- Log.InfoFormat("Created Fragments Folder: {0}", folder);
+ Log.InfoFormat("Created cache Folder: {0}", folder);
+ }
+ else
+ {
+ var info = new DirectoryInfo(folder);
+ if ((info.Attributes & FileAttributes.Hidden) == 0)
+ {
+ info.Attributes |= FileAttributes.Hidden;
+ Log.InfoFormat("Reset cache folder to hidden: {0}", folder);
+ }
}
return folder;
}
FileAgent.StatusKeeper = StatusKeeper;
FileAgent.Workflow = Workflow;
- FileAgent.FragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
+ FileAgent.CachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
FileAgent.Start(_accountInfo, RootPath);
}
Directory.Delete(removedPath,true);
}
}
+
+ public IEnumerable<string> GetRootFolders()
+ {
+ var dirs = from container in CloudClient.ListContainers(UserName)
+ from dir in CloudClient.ListObjects(UserName, container.Name, "")
+ select dir.Name;
+ return dirs;
+ }
+
+ public ObjectInfo GetObjectInfo(string filePath)
+ {
+ if (String.IsNullOrWhiteSpace(filePath))
+ throw new ArgumentNullException("filePath");
+ Contract.EndContractBlock();
+
+ var file=new FileInfo(filePath);
+ string relativeUrl;//=file.AsRelativeUrlTo(this.RootPath);
+ var relativePath = file.AsRelativeTo(RootPath);
+
+ string accountName,container;
+
+ var parts=relativePath.Split('\\');
+
+ var accountInfo = _accountInfo;
+ if (relativePath.StartsWith(FolderConstants.OthersFolder))
+ {
+ accountName = parts[1];
+ container = parts[2];
+ relativeUrl = String.Join("/", parts.Splice(3));
+ //Create the root URL for the target account
+ var oldName = UserName;
+ var absoluteUri = _accountInfo.StorageUri.AbsoluteUri;
+ var nameIndex=absoluteUri.IndexOf(oldName);
+ var root=absoluteUri.Substring(0, nameIndex);
+
+ accountInfo = new AccountInfo
+ {
+ UserName = accountName,
+ AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
+ StorageUri = new Uri(root + accountName),
+ BlockHash=accountInfo.BlockHash,
+ BlockSize=accountInfo.BlockSize,
+ Token=accountInfo.Token
+ };
+ }
+ else
+ {
+ accountName = this.UserName;
+ container = parts[0];
+ relativeUrl = String.Join("/", parts.Splice(1));
+ }
+
+ var client = new CloudFilesClient(accountInfo);
+ var objectInfo=client.GetObjectInfo(accountName, container, relativeUrl);
+ return objectInfo;
+ }
+
+ public ContainerInfo GetContainerInfo(string filePath)
+ {
+ if (String.IsNullOrWhiteSpace(filePath))
+ throw new ArgumentNullException("filePath");
+ Contract.EndContractBlock();
+
+ var file=new FileInfo(filePath);
+ var relativePath = file.AsRelativeTo(RootPath);
+
+ string accountName,container;
+
+ var parts=relativePath.Split('\\');
+
+ var accountInfo = _accountInfo;
+ if (relativePath.StartsWith(FolderConstants.OthersFolder))
+ {
+ accountName = parts[1];
+ container = parts[2];
+ //Create the root URL for the target account
+ var oldName = UserName;
+ var absoluteUri = _accountInfo.StorageUri.AbsoluteUri;
+ var nameIndex=absoluteUri.IndexOf(oldName);
+ var root=absoluteUri.Substring(0, nameIndex);
+
+ accountInfo = new AccountInfo
+ {
+ UserName = accountName,
+ AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
+ StorageUri = new Uri(root + accountName),
+ BlockHash=accountInfo.BlockHash,
+ BlockSize=accountInfo.BlockSize,
+ Token=accountInfo.Token
+ };
+ }
+ else
+ {
+ accountName = UserName;
+ container = parts[0];
+ }
+
+ var client = new CloudFilesClient(accountInfo);
+ var containerInfo=client.GetContainerInfo(accountName, container);
+ return containerInfo;
+ }
}
+
public interface IStatusNotification
{
void NotifyChange(string status,TraceLevel level=TraceLevel.Info);
void NotifyChangedFile(string filePath);
+ void NotifyAccount(AccountInfo policy);
}
}