Renamed Others to others-shared
[pithos-ms-client] / trunk / Pithos.Core / PithosWorkflow.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Linq;
5 using System.Runtime.InteropServices;
6 using System.Text;
7 using System.Threading;
8 using System.Threading.Tasks;
9 using Pithos.Interfaces;
10 using System.IO;
11 using System.Diagnostics;
12 using Pithos.Network;
13
14 namespace Pithos.Core
15 {
16     [Export(typeof(IPithosWorkflow))]
17     public class PithosWorkflow:IPithosWorkflow
18     {
19         [Import]
20         public IPithosSettings Settings { get; set; }
21
22         [Import]
23         public IStatusKeeper StatusKeeper { get; set; }
24
25         public FileStatus SetFileStatus(string path, FileStatus status)
26         {
27             Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
28             Debug.Assert(!path.EndsWith(".ignore",StringComparison.InvariantCultureIgnoreCase));
29
30             if (String.IsNullOrWhiteSpace(path))
31                 throw new ArgumentNullException("path", "The path parameter must not be emtpy");
32
33             var oldStatus=StatusKeeper.GetFileStatus(path);
34
35             if (oldStatus == status)
36                 return oldStatus;
37
38             switch(oldStatus)
39             {
40                 case FileStatus.Unchanged :
41                     break;
42                 case FileStatus.Created:
43                     if (status == FileStatus.Modified)
44                         return oldStatus;
45                     break;
46                 case FileStatus.Modified:
47                 case FileStatus.Renamed:
48                     if (status == FileStatus.Created)
49                         return oldStatus;
50                     break;
51                 case FileStatus.Deleted:
52                     return oldStatus;                    
53             }
54             StatusKeeper.SetFileStatus(path, status);
55             return status;
56         }
57
58         public void ClearFileStatus(string path)
59         {
60             if (String.IsNullOrWhiteSpace(path))
61                 throw new ArgumentNullException("path", "The path parameter must not be emtpy");
62
63             StatusKeeper.ClearFileStatus(path.ToLower());
64         }
65        
66
67         public Task<FileStream> OpenStreamWithWaiting(string path)
68         {
69             if (String.IsNullOrWhiteSpace(path))
70                 throw new ArgumentNullException("path","The path parameter must not be emtpy");
71
72             if (!File.Exists(path))
73                 throw new FileNotFoundException("The specified file or path does not exist", path);
74
75             return new Task<FileStream>(() =>
76                     {
77                         int counter = 0;
78                         while (true)
79                         {
80                             try
81                             {
82                                 var stream=File.OpenRead(path);
83                                 return stream;                                
84                             }
85                             catch 
86                             {
87                                 Thread.Sleep(500);
88                                 if (++counter > 10)
89                                     throw;
90                             }
91                         }
92                     });
93         }
94     }
95 }