Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ListChangesHandler.cs « Operation « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e43a3b988c2e17aa15cd2d38fd650d19d4102ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//  Copyright (C) 2015, The Duplicati Team

//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Linq;

namespace Duplicati.Library.Main.Operation
{
    internal class ListChangesHandler
    {
        private string m_backendurl;
        private Options m_options;
        private ListChangesResults m_result;

        public ListChangesHandler(string backend, Options options, ListChangesResults result)
        {
            m_backendurl = backend;
            m_options = options;
            m_result = result;
        }
        
        private static Tuple<long, DateTime, T> SelectTime<T>(string value, IEnumerable<Tuple<long, DateTime, T>> list, out long index, out DateTime time, out T el)
        {
            long indexValue;
            Tuple<long, DateTime, T> res;
            if (!long.TryParse(value, out indexValue))
            {
                var t = Library.Utility.Timeparser.ParseTimeInterval(value, DateTime.Now, true);
                res = list.OrderBy(x => Math.Abs((x.Item2 - t).Ticks)).First();
            }
            else
            {
                res = list.OrderBy(x => Math.Abs(x.Item1 - indexValue)).First();
            }
            
            index = res.Item1;
            time = res.Item2;
            el = res.Item3;
            return res;
        }

        public void Run(string baseVersion, string compareVersion, IEnumerable<string> filterstrings = null, Library.Utility.IFilter compositefilter = null)
        {
            var filter = Library.Utility.JoinedFilterExpression.Join(new Library.Utility.FilterExpression(filterstrings), compositefilter);
            
            var useLocalDb = !m_options.NoLocalDb && System.IO.File.Exists(m_options.Dbpath);
            baseVersion = string.IsNullOrEmpty(baseVersion) ? "1" : baseVersion;
            compareVersion = string.IsNullOrEmpty(compareVersion) ? "0" : compareVersion;
            
            long baseVersionIndex = -1;
            long compareVersionIndex = -1;
            
            DateTime baseVersionTime = new DateTime(0);
            DateTime compareVersionTime = new DateTime(0);
            
            using(var tmpdb = useLocalDb ? null : new Library.Utility.TempFile())
            using(var db = new Database.LocalListChangesDatabase(useLocalDb ? m_options.Dbpath : (string)tmpdb))
            using(var backend = new BackendManager(m_backendurl, m_options, m_result.BackendWriter, db))
            using(var storageKeeper = db.CreateStorageHelper())
            {
                m_result.SetDatabase(db);
                
                if (useLocalDb)
                {
                    var dbtimes = db.FilesetTimes.ToList();
                    if (dbtimes.Count < 2)
                        throw new Exception(string.Format("Need at least two backups to show differences, database contains {0} backups", dbtimes.Count));
                    
                    long baseVersionId;
                    long compareVersionId;
                    
                    var times = dbtimes.Zip(Enumerable.Range(0, dbtimes.Count), (a, b) => new Tuple<long, DateTime, long>(b, a.Value, a.Key)).ToList();
                    var bt = SelectTime(baseVersion, times, out baseVersionIndex, out baseVersionTime, out baseVersionId);
                    times.Remove(bt);
                    SelectTime(compareVersion, times, out compareVersionIndex, out compareVersionTime, out compareVersionId);
                                            
                    storageKeeper.AddFromDb(baseVersionId, false, filter);
                    storageKeeper.AddFromDb(compareVersionId, true, filter);
                }
                else
                {
                    m_result.AddMessage("No local database, accessing remote store");
                    
                    var parsedlist = (from n in backend.List()
                                let p = Volumes.VolumeBase.ParseFilename(n)
                                where p != null && p.FileType == RemoteVolumeType.Files
                                orderby p.Time descending
                                select p).ToArray();
                                
                    var numberedList = parsedlist.Zip(Enumerable.Range(0, parsedlist.Length), (a, b) => new Tuple<long, DateTime, Volumes.IParsedVolume>(b, a.Time, a)).ToList();
                    if (numberedList.Count < 2)
                        throw new Exception(string.Format("Need at least two backups to show differences, database contains {0} backups", numberedList.Count));

                    Volumes.IParsedVolume baseFile;
                    Volumes.IParsedVolume compareFile;
                    
                    var bt = SelectTime(baseVersion, numberedList, out baseVersionIndex, out baseVersionTime, out baseFile);
                    numberedList.Remove(bt);
                    SelectTime(compareVersion, numberedList, out compareVersionIndex, out compareVersionTime, out compareFile);
                    
                    Func<FilelistEntryType, Library.Interface.ListChangesElementType> conv = (x) => {
                        switch (x)
                        {
                            case FilelistEntryType.File:
                                return Library.Interface.ListChangesElementType.File;
                            case FilelistEntryType.Folder:
                                return Library.Interface.ListChangesElementType.Folder;
                            case FilelistEntryType.Symlink:
                                return Library.Interface.ListChangesElementType.Symlink;
                            default:
                                return (Library.Interface.ListChangesElementType)(-1);
                        }
                    };
                    
                    if (m_result.TaskControlRendevouz() == TaskControlState.Stop)
                        return;
                        
                    using(var tmpfile = backend.Get(baseFile.File.Name, baseFile.File.Size, null))
                    using(var rd = new Volumes.FilesetVolumeReader(RestoreHandler.GetCompressionModule(baseFile.File.Name), tmpfile, m_options))
                        foreach(var f in rd.Files)
                            if (Library.Utility.FilterExpression.Matches(filter, f.Path))
                                storageKeeper.AddElement(f.Path, f.Hash, f.Metahash, f.Size, conv(f.Type), false);
                                
                    if (m_result.TaskControlRendevouz() == TaskControlState.Stop)
                        return;
                    
                    using(var tmpfile = backend.Get(compareFile.File.Name, compareFile.File.Size, null))
                    using(var rd = new Volumes.FilesetVolumeReader(RestoreHandler.GetCompressionModule(compareFile.File.Name), tmpfile, m_options))
                        foreach(var f in rd.Files)
                            if (Library.Utility.FilterExpression.Matches(filter, f.Path))
                                storageKeeper.AddElement(f.Path, f.Hash, f.Metahash, f.Size, conv(f.Type), true);
                }
                
                var changes = storageKeeper.CreateChangeCountReport();
                var sizes = storageKeeper.CreateChangeSizeReport();
                
                m_result.SetResult(
                    baseVersionTime, baseVersionIndex, compareVersionTime, compareVersionIndex,
                    changes.AddedFolders, changes.AddedSymlinks, changes.AddedFiles,
                    changes.DeletedFolders, changes.DeletedSymlinks, changes.DeletedFiles,
                    changes.ModifiedFolders, changes.ModifiedSymlinks, changes.ModifiedFiles,
                    sizes.AddedSize, sizes.DeletedSize, sizes.PreviousSize, sizes.CurrentSize,
                    
                    m_options.Verbose ? 
                        (from n in storageKeeper.CreateChangedFileReport()
                        select n).ToArray() : null
                );

                return;                                
            }      
        }
    }
}