Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / PithosWorkflow.cs @ cfed7823

History | View | Annotate | Download (3 kB)

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

    
13
namespace Pithos.Core
14
{
15
    [Export(typeof(IPithosWorkflow))]
16
    public class PithosWorkflow:IPithosWorkflow
17
    {
18
        [Import]
19
        public IPithosSettings Settings { get; set; }
20

    
21
        [Import]
22
        public IStatusKeeper StatusKeeper { get; set; }
23

    
24
        public FileStatus SetFileStatus(string path, FileStatus status)
25
        {
26
            Debug.Assert(!path.Contains("fragments"));
27
            Debug.Assert(!path.EndsWith(".ignore",StringComparison.InvariantCultureIgnoreCase));
28

    
29
            if (String.IsNullOrWhiteSpace(path))
30
                throw new ArgumentNullException("path", "The path parameter must not be emtpy");
31

    
32
            var oldStatus=StatusKeeper.GetFileStatus(path);
33

    
34
            if (oldStatus == status)
35
                return oldStatus;
36

    
37
            switch(oldStatus)
38
            {
39
                case FileStatus.Unchanged :
40
                    break;
41
                case FileStatus.Created:
42
                    if (status == FileStatus.Modified)
43
                        return oldStatus;
44
                    break;
45
                case FileStatus.Modified:
46
                case FileStatus.Renamed:
47
                    if (status == FileStatus.Created)
48
                        return oldStatus;
49
                    break;
50
                case FileStatus.Deleted:
51
                    return oldStatus;                    
52
            }
53
            StatusKeeper.SetFileStatus(path, status);
54
            return status;
55
        }
56

    
57
        public void ClearFileStatus(string path)
58
        {
59
            if (String.IsNullOrWhiteSpace(path))
60
                throw new ArgumentNullException("path", "The path parameter must not be emtpy");
61

    
62
            StatusKeeper.ClearFileStatus(path.ToLower());
63
        }
64
       
65

    
66
        public Task<FileStream> OpenStreamWithWaiting(string path)
67
        {
68
            if (String.IsNullOrWhiteSpace(path))
69
                throw new ArgumentNullException("path","The path parameter must not be emtpy");
70

    
71
            if (!File.Exists(path))
72
                throw new FileNotFoundException("The specified file or path does not exist", path);
73

    
74
            return new Task<FileStream>(() =>
75
                    {
76
                        int counter = 0;
77
                        while (true)
78
                        {
79
                            try
80
                            {
81
                                var stream=File.OpenRead(path);
82
                                return stream;                                
83
                            }
84
                            catch 
85
                            {
86
                                Thread.Sleep(500);
87
                                if (++counter > 10)
88
                                    throw;
89
                            }
90
                        }
91
                    });
92
        }
93
    }
94
}