Multiple changes:
[pithos-ms-client] / trunk / Pithos.Core / Agents / PollAgent.cs
1 #region\r
2 /* -----------------------------------------------------------------------\r
3  * <copyright file="PollAgent.cs" company="GRNet">\r
4  * \r
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or\r
8  * without modification, are permitted provided that the following\r
9  * conditions are met:\r
10  *\r
11  *   1. Redistributions of source code must retain the above\r
12  *      copyright notice, this list of conditions and the following\r
13  *      disclaimer.\r
14  *\r
15  *   2. Redistributions in binary form must reproduce the above\r
16  *      copyright notice, this list of conditions and the following\r
17  *      disclaimer in the documentation and/or other materials\r
18  *      provided with the distribution.\r
19  *\r
20  *\r
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
32  * POSSIBILITY OF SUCH DAMAGE.\r
33  *\r
34  * The views and conclusions contained in the software and\r
35  * documentation are those of the authors and should not be\r
36  * interpreted as representing official policies, either expressed\r
37  * or implied, of GRNET S.A.\r
38  * </copyright>\r
39  * -----------------------------------------------------------------------\r
40  */\r
41 #endregion\r
42 \r
43 using System.Collections.Concurrent;\r
44 using System.ComponentModel.Composition;\r
45 using System.Diagnostics;\r
46 using System.Diagnostics.Contracts;\r
47 using System.IO;\r
48 using System.Reflection;\r
49 using System.Threading;\r
50 using System.Threading.Tasks;\r
51 using Castle.ActiveRecord;\r
52 using Pithos.Interfaces;\r
53 using Pithos.Network;\r
54 using log4net;\r
55 \r
56 namespace Pithos.Core.Agents\r
57 {\r
58     using System;\r
59     using System.Collections.Generic;\r
60     using System.Linq;\r
61 \r
62     /// <summary>\r
63     /// PollAgent periodically polls the server to detect object changes. The agent retrieves a listing of all\r
64     /// objects and compares it with a previously cached version to detect differences. \r
65     /// New files are downloaded, missing files are deleted from the local file system and common files are compared\r
66     /// to determine the appropriate action\r
67     /// </summary>\r
68     [Export]\r
69     public class PollAgent\r
70     {\r
71         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);\r
72 \r
73         [System.ComponentModel.Composition.Import]\r
74         public IStatusKeeper StatusKeeper { get; set; }\r
75 \r
76         [System.ComponentModel.Composition.Import]\r
77         public IPithosSettings Settings { get; set; }\r
78 \r
79         [System.ComponentModel.Composition.Import]\r
80         public NetworkAgent NetworkAgent { get; set; }\r
81 \r
82         public IStatusNotification StatusNotification { get; set; }\r
83 \r
84         private bool _firstPoll = true;\r
85 \r
86         //The Sync Event signals a manual synchronisation\r
87         private readonly AsyncManualResetEvent _syncEvent = new AsyncManualResetEvent();\r
88 \r
89         private readonly ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>();\r
90         private readonly ConcurrentDictionary<string, AccountInfo> _accounts = new ConcurrentDictionary<string,AccountInfo>();\r
91 \r
92 \r
93         /// <summary>\r
94         /// Start a manual synchronization\r
95         /// </summary>\r
96         public void SynchNow()\r
97         {            \r
98             _syncEvent.Set();\r
99         }\r
100 \r
101         /// <summary>\r
102         /// Remote files are polled periodically. Any changes are processed\r
103         /// </summary>\r
104         /// <param name="since"></param>\r
105         /// <returns></returns>\r
106         public async Task PollRemoteFiles(DateTime? since = null)\r
107         {\r
108             if (Log.IsDebugEnabled)\r
109                 Log.DebugFormat("Polling changes after [{0}]",since);\r
110 \r
111             Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");\r
112             \r
113 \r
114             using (ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))\r
115             {\r
116                 //If this poll fails, we will retry with the same since value\r
117                 var nextSince = since;\r
118                 try\r
119                 {\r
120                     UpdateStatus(PithosStatus.PollSyncing);\r
121 \r
122                     //Next time we will check for all changes since the current check minus 1 second\r
123                     //This is done to ensure there are no discrepancies due to clock differences\r
124                     var current = DateTime.Now.AddSeconds(-1);\r
125 \r
126                     var tasks = from accountInfo in _accounts.Values\r
127                                 select ProcessAccountFiles(accountInfo, since);\r
128 \r
129                     await TaskEx.WhenAll(tasks.ToList());\r
130 \r
131                     _firstPoll = false;\r
132                     //Reschedule the poll with the current timestamp as a "since" value\r
133                     nextSince = current;\r
134                 }\r
135                 catch (Exception ex)\r
136                 {\r
137                     Log.ErrorFormat("Error while processing accounts\r\n{0}", ex);\r
138                     //In case of failure retry with the same "since" value\r
139                 }\r
140 \r
141                 UpdateStatus(PithosStatus.PollComplete);\r
142                 //The multiple try blocks are required because we can't have an await call\r
143                 //inside a finally block\r
144                 //TODO: Find a more elegant solution for reschedulling in the event of an exception\r
145                 try\r
146                 {\r
147                     //Wait for the polling interval to pass or the Sync event to be signalled\r
148                     nextSince = await WaitForScheduledOrManualPoll(nextSince);\r
149                 }\r
150                 finally\r
151                 {\r
152                     //Ensure polling is scheduled even in case of error\r
153                     TaskEx.Run(() => PollRemoteFiles(nextSince));                        \r
154                 }\r
155             }\r
156         }\r
157 \r
158         /// <summary>\r
159         /// Wait for the polling period to expire or a manual sync request\r
160         /// </summary>\r
161         /// <param name="since"></param>\r
162         /// <returns></returns>\r
163         private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since)\r
164         {\r
165             var sync = _syncEvent.WaitAsync();\r
166             var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken);\r
167             var signaledTask = await TaskEx.WhenAny(sync, wait);\r
168 \r
169             //Wait for network processing to finish before polling\r
170             var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();\r
171             await TaskEx.WhenAll(signaledTask, pauseTask);\r
172 \r
173             //If polling is signalled by SynchNow, ignore the since tag\r
174             if (sync.IsCompleted)\r
175             {\r
176                 //TODO: Must convert to AutoReset\r
177                 _syncEvent.Reset();\r
178                 return null;\r
179             }\r
180             return since;\r
181         }\r
182 \r
183         public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null)\r
184         {\r
185             if (accountInfo == null)\r
186                 throw new ArgumentNullException("accountInfo");\r
187             if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))\r
188                 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");\r
189             Contract.EndContractBlock();\r
190 \r
191 \r
192             using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))\r
193             {\r
194                 await NetworkAgent.GetDeleteAwaiter();\r
195 \r
196                 Log.Info("Scheduled");\r
197                 var client = new CloudFilesClient(accountInfo);\r
198 \r
199                 //We don't need to check the trash container\r
200                 var containers = client.ListContainers(accountInfo.UserName)\r
201                     .Where(c=>c.Name!="trash")\r
202                     .ToList();\r
203 \r
204 \r
205                 CreateContainerFolders(accountInfo, containers);\r
206 \r
207                 try\r
208                 {\r
209                     //Wait for any deletions to finish\r
210                     await NetworkAgent.GetDeleteAwaiter();\r
211                     //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted\r
212                     //than delete a file that was created while we were executing the poll                    \r
213 \r
214                     //Get the list of server objects changed since the last check\r
215                     //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step\r
216                     var listObjects = (from container in containers\r
217                                        select Task<IList<ObjectInfo>>.Factory.StartNew(_ =>\r
218                                              client.ListObjects(accountInfo.UserName, container.Name, since), container.Name)).ToList();\r
219 \r
220                     var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => \r
221                         client.ListSharedObjects(since), "shared");\r
222                     listObjects.Add(listShared);\r
223                     var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());\r
224 \r
225                     using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))\r
226                     {\r
227                         var dict = listTasks.ToDictionary(t => t.AsyncState);\r
228 \r
229                         //Get all non-trash objects. Remember, the container name is stored in AsyncState\r
230                         var remoteObjects = from objectList in listTasks\r
231                                             where (string)objectList.AsyncState != "trash"\r
232                                             from obj in objectList.Result\r
233                                             select obj;\r
234 \r
235                         var sharedObjects = dict["shared"].Result;\r
236 \r
237                         //DON'T process trashed files\r
238                         //If some files are deleted and added again to a folder, they will be deleted\r
239                         //even though they are new.\r
240                         //We would have to check file dates and hashes to ensure that a trashed file\r
241                         //can be deleted safely from the local hard drive.\r
242                         /*\r
243                         //Items with the same name, hash may be both in the container and the trash\r
244                         //Don't delete items that exist in the container\r
245                         var realTrash = from trash in trashObjects\r
246                                         where\r
247                                             !remoteObjects.Any(\r
248                                                 info => info.Name == trash.Name && info.Hash == trash.Hash)\r
249                                         select trash;\r
250                         ProcessTrashedFiles(accountInfo, realTrash);\r
251 */\r
252 \r
253                         var cleanRemotes = (from info in remoteObjects.Union(sharedObjects)\r
254                                             let name = info.Name??""\r
255                                             where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&\r
256                                                   !name.StartsWith(FolderConstants.CacheFolder + "/",\r
257                                                                    StringComparison.InvariantCultureIgnoreCase)\r
258                                             select info).ToList();\r
259 \r
260                         var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);\r
261 \r
262                         ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterDirectlyBelow(SelectiveUris));\r
263 \r
264                         // @@@ NEED To add previous state here as well, To compare with previous hash\r
265 \r
266                         \r
267 \r
268                         //Create a list of actions from the remote files\r
269                         var allActions = MovesToActions(accountInfo,differencer.Moved.FilterDirectlyBelow(SelectiveUris))\r
270                                         .Union(\r
271                                         ChangesToActions(accountInfo, differencer.Changed.FilterDirectlyBelow(SelectiveUris)))\r
272                                         .Union(\r
273                                         CreatesToActions(accountInfo, differencer.Created.FilterDirectlyBelow(SelectiveUris)));\r
274 \r
275                         //And remove those that are already being processed by the agent\r
276                         var distinctActions = allActions\r
277                             .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())\r
278                             .ToList();\r
279 \r
280                         //Queue all the actions\r
281                         foreach (var message in distinctActions)\r
282                         {\r
283                             NetworkAgent.Post(message);\r
284                         }\r
285 \r
286                         Log.Info("[LISTENER] End Processing");\r
287                     }\r
288                 }\r
289                 catch (Exception ex)\r
290                 {\r
291                     Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);\r
292                     return;\r
293                 }\r
294 \r
295                 Log.Info("[LISTENER] Finished");\r
296 \r
297             }\r
298         }\r
299 \r
300         readonly AccountsDifferencer _differencer = new AccountsDifferencer();\r
301         private List<Uri> _selectiveUris=new List<Uri>();\r
302 \r
303         /// <summary>\r
304         /// Deletes local files that are not found in the list of cloud files\r
305         /// </summary>\r
306         /// <param name="accountInfo"></param>\r
307         /// <param name="cloudFiles"></param>\r
308         private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles)\r
309         {\r
310             if (accountInfo == null)\r
311                 throw new ArgumentNullException("accountInfo");\r
312             if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))\r
313                 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");\r
314             if (cloudFiles == null)\r
315                 throw new ArgumentNullException("cloudFiles");\r
316             Contract.EndContractBlock();\r
317 \r
318             //On the first run\r
319             if (_firstPoll)\r
320             {\r
321                 //Only consider files that are not being modified, ie they are in the Unchanged state            \r
322                 var deleteCandidates = FileState.Queryable.Where(state =>\r
323                     state.FilePath.StartsWith(accountInfo.AccountPath)\r
324                     && state.FileStatus == FileStatus.Unchanged).ToList();\r
325 \r
326 \r
327                 //TODO: filesToDelete must take into account the Others container            \r
328                 var filesToDelete = (from deleteCandidate in deleteCandidates\r
329                                      let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath)\r
330                                      let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath)\r
331                                      where\r
332                                          !cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath)\r
333                                      select localFile).ToList();\r
334 \r
335 \r
336 \r
337                 //Set the status of missing files to Conflict\r
338                 foreach (var item in filesToDelete)\r
339                 {\r
340                     //Try to acquire a gate on the file, to take into account files that have been dequeued\r
341                     //and are being processed\r
342                     using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting))\r
343                     {\r
344                         if (gate.Failed)\r
345                             continue;\r
346                         StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted);\r
347                     }\r
348                 }\r
349                 UpdateStatus(PithosStatus.HasConflicts);\r
350                 StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count));\r
351                 StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info);\r
352             }\r
353             else\r
354             {\r
355                 var deletedFiles = new List<FileSystemInfo>();\r
356                 foreach (var objectInfo in cloudFiles)\r
357                 {\r
358                     Log.DebugFormat("Handle deleted [{0}]",objectInfo.Uri);\r
359                     var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);\r
360                     var item = FileAgent.GetFileAgent(accountInfo).GetFileSystemInfo(relativePath);\r
361                     Log.DebugFormat("Will delete [{0}] for [{1}]", item.FullName,objectInfo.Uri);\r
362                     if (item.Exists)\r
363                     {\r
364                         if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)\r
365                         {\r
366                             item.Attributes = item.Attributes & ~FileAttributes.ReadOnly;\r
367 \r
368                         }\r
369                         var directory = item as DirectoryInfo;\r
370                         if (directory!=null)\r
371                             directory.Delete(true);\r
372                         else\r
373                             item.Delete();\r
374                         Log.DebugFormat("Deleted [{0}] for [{1}]", item.FullName, objectInfo.Uri);\r
375                         DateTime lastDate;\r
376                         _lastSeen.TryRemove(item.FullName, out lastDate);\r
377                         deletedFiles.Add(item);\r
378                     }\r
379                     StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);\r
380                 }\r
381                 Log.InfoFormat("[{0}] files were deleted",deletedFiles.Count);\r
382                 StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);\r
383             }\r
384 \r
385         }\r
386 \r
387         /// <summary>\r
388         /// Creates a Sync action for each changed server file\r
389         /// </summary>\r
390         /// <param name="accountInfo"></param>\r
391         /// <param name="changes"></param>\r
392         /// <returns></returns>\r
393         private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)\r
394         {\r
395             if (changes == null)\r
396                 throw new ArgumentNullException();\r
397             Contract.EndContractBlock();\r
398             var fileAgent = FileAgent.GetFileAgent(accountInfo);\r
399 \r
400             //In order to avoid multiple iterations over the files, we iterate only once\r
401             //over the remote files\r
402             foreach (var objectInfo in changes)\r
403             {\r
404                 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);\r
405                 //If a directory object already exists, we may need to sync it\r
406                 if (fileAgent.Exists(relativePath))\r
407                 {\r
408                     var localFile = fileAgent.GetFileSystemInfo(relativePath);\r
409                     //We don't need to sync directories\r
410                     if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)\r
411                         continue;\r
412                     using (new SessionScope(FlushAction.Never))\r
413                     {\r
414                         var state = StatusKeeper.GetStateByFilePath(localFile.FullName);\r
415                         _lastSeen[localFile.FullName] = DateTime.Now;\r
416                         //Common files should be checked on a per-case basis to detect differences, which is newer\r
417 \r
418                         yield return new CloudAction(accountInfo, CloudActionType.MustSynch,\r
419                                                      localFile, objectInfo, state, accountInfo.BlockSize,\r
420                                                      accountInfo.BlockHash);\r
421                     }\r
422                 }\r
423                 else\r
424                 {\r
425                     //Remote files should be downloaded\r
426                     yield return new CloudDownloadAction(accountInfo, objectInfo);\r
427                 }\r
428             }\r
429         }\r
430 \r
431         /// <summary>\r
432         /// Creates a Local Move action for each moved server file\r
433         /// </summary>\r
434         /// <param name="accountInfo"></param>\r
435         /// <param name="moves"></param>\r
436         /// <returns></returns>\r
437         private IEnumerable<CloudAction> MovesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> moves)\r
438         {\r
439             if (moves == null)\r
440                 throw new ArgumentNullException();\r
441             Contract.EndContractBlock();\r
442             var fileAgent = FileAgent.GetFileAgent(accountInfo);\r
443 \r
444             //In order to avoid multiple iterations over the files, we iterate only once\r
445             //over the remote files\r
446             foreach (var objectInfo in moves)\r
447             {\r
448                 var previousRelativepath = objectInfo.Previous.RelativeUrlToFilePath(accountInfo.UserName);\r
449                 //If the previous file already exists, we can execute a Move operation\r
450                 if (fileAgent.Exists(previousRelativepath))\r
451                 {\r
452                     var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);\r
453                     using (new SessionScope(FlushAction.Never))\r
454                     {\r
455                         var state = StatusKeeper.GetStateByFilePath(previousFile.FullName);\r
456                         _lastSeen[previousFile.FullName] = DateTime.Now;\r
457 \r
458                         //For each moved object we need to move both the local file and update                                                \r
459                         yield return new CloudAction(accountInfo, CloudActionType.RenameLocal,\r
460                                                      previousFile, objectInfo, state, accountInfo.BlockSize,\r
461                                                      accountInfo.BlockHash);\r
462                         //For modified files, we need to download the changes as well\r
463                         if (objectInfo.Hash!=objectInfo.PreviousHash)\r
464                             yield return new CloudDownloadAction(accountInfo,objectInfo);\r
465                     }\r
466                 }\r
467                 //If the previous file does not exist, we need to download it in the new location\r
468                 else\r
469                 {\r
470                     //Remote files should be downloaded\r
471                     yield return new CloudDownloadAction(accountInfo, objectInfo);\r
472                 }\r
473             }\r
474         }\r
475 \r
476 \r
477         /// <summary>\r
478         /// Creates a download action for each new server file\r
479         /// </summary>\r
480         /// <param name="accountInfo"></param>\r
481         /// <param name="creates"></param>\r
482         /// <returns></returns>\r
483         private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)\r
484         {\r
485             if (creates == null)\r
486                 throw new ArgumentNullException();\r
487             Contract.EndContractBlock();\r
488             var fileAgent = FileAgent.GetFileAgent(accountInfo);\r
489 \r
490             //In order to avoid multiple iterations over the files, we iterate only once\r
491             //over the remote files\r
492             foreach (var objectInfo in creates)\r
493             {\r
494                 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);\r
495                 //If the object already exists, we probably have a conflict\r
496                 if (fileAgent.Exists(relativePath))\r
497                 {\r
498                     //If a directory object already exists, we don't need to perform any other action                    \r
499                     var localFile = fileAgent.GetFileSystemInfo(relativePath);\r
500                     StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);\r
501                 }\r
502                 else\r
503                 {\r
504                     //Remote files should be downloaded\r
505                     yield return new CloudDownloadAction(accountInfo, objectInfo);\r
506                 }\r
507             }\r
508         }\r
509 \r
510         /// <summary>\r
511         /// Notify the UI to update the visual status\r
512         /// </summary>\r
513         /// <param name="status"></param>\r
514         private void UpdateStatus(PithosStatus status)\r
515         {\r
516             try\r
517             {\r
518                 StatusNotification.SetPithosStatus(status);\r
519                 //StatusNotification.Notify(new Notification());\r
520             }\r
521             catch (Exception exc)\r
522             {\r
523                 //Failure is not critical, just log it\r
524                 Log.Warn("Error while updating status", exc);\r
525             }\r
526         }\r
527 \r
528         private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)\r
529         {\r
530             var containerPaths = from container in containers\r
531                                  let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)\r
532                                  where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)\r
533                                  select containerPath;\r
534 \r
535             foreach (var path in containerPaths)\r
536             {\r
537                 Directory.CreateDirectory(path);\r
538             }\r
539         }\r
540 \r
541         public void SetSyncUris(Uri[] uris)\r
542         {            \r
543             SelectiveUris=uris.ToList();\r
544         }\r
545 \r
546         protected List<Uri> SelectiveUris\r
547         {\r
548             get { return _selectiveUris;}\r
549             set { _selectiveUris = value; }\r
550         }\r
551 \r
552         public void AddAccount(AccountInfo accountInfo)\r
553         {\r
554             //Avoid adding a duplicate accountInfo\r
555             _accounts.TryAdd(accountInfo.UserName, accountInfo);\r
556         }\r
557 \r
558         public void RemoveAccount(AccountInfo accountInfo)\r
559         {\r
560             AccountInfo account;\r
561             _accounts.TryRemove(accountInfo.UserName,out account);\r
562         }\r
563     }\r
564 }\r