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

ListBrokenFilesHandler.cs « Operation « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1083f61651a8034112a10a32044a5d2de045548c (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//  Copyright (C) 2017, 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;
using System.Collections.Generic;
using System.Linq;
using Duplicati.Library.Interface;

namespace Duplicati.Library.Main.Operation
{
    internal class ListBrokenFilesHandler
    {
        protected string m_backendurl;
        protected Options m_options;
        protected ListBrokenFilesResults m_result;

        public ListBrokenFilesHandler(string backend, Options options, ListBrokenFilesResults result)
        {
            m_backendurl = backend;
            m_options = options;
            m_result = result;
        }

        public void Run(Library.Utility.IFilter filter, Func<long, DateTime, long, string, long, bool> callbackhandler = null)
        {
            if (!System.IO.File.Exists(m_options.Dbpath))
                throw new UserInformationException(string.Format("Database file does not exist: {0}", m_options.Dbpath));

            using (var db = new Database.LocalListBrokenFilesDatabase(m_options.Dbpath))
            using (var tr = db.BeginTransaction())
                DoRun(db, tr, filter, callbackhandler);
        }

        public static Tuple<DateTime, long, long>[] GetBrokenFilesetsFromRemote(string backendurl, BasicResults result, Database.LocalListBrokenFilesDatabase db, System.Data.IDbTransaction transaction, Options options, out List<Database.RemoteVolumeEntry> missing)
        {
            missing = null;
            var brokensets = db.GetBrokenFilesets(options.Time, options.Version, transaction).ToArray();

            if (brokensets.Length == 0)
            {
                if (db.RepairInProgress)
                    throw new UserInformationException("Cannot continue because the database is marked as being under repair, but does not have broken files.");

                result.AddMessage("No broken filesets found in database, checking for missing remote files");

                using (var backend = new BackendManager(backendurl, options, result.BackendWriter, db))
                {
                    var remotestate = FilelistProcessor.RemoteListAnalysis(backend, options, db, result.BackendWriter, null);
                    if (!remotestate.ParsedVolumes.Any())
                        throw new UserInformationException("No remote volumes were found, refusing purge");

                    missing = remotestate.MissingVolumes.ToList();
                    if (missing.Count == 0)
                    {
                        result.AddMessage("Skipping operation because no files were found to be missing, and no filesets were recorded as broken.");
                        return null;
                    }

                    // Mark all volumes as disposable
                    foreach (var f in missing)
                        db.UpdateRemoteVolume(f.Name, RemoteVolumeState.Deleting, f.Size, f.Hash, transaction);

                    result.AddMessage(string.Format("Marked {0} remote files for deletion", missing.Count));

                    // Drop all content from tables
                    db.RemoveMissingBlocks(missing.Select(x => x.Name), transaction);
                }
                brokensets = db.GetBrokenFilesets(options.Time, options.Version, transaction).ToArray();
            }

            return brokensets;
        }

        private void DoRun(Database.LocalListBrokenFilesDatabase db, System.Data.IDbTransaction transaction, Library.Utility.IFilter filter, Func<long, DateTime, long, string, long, bool> callbackhandler)
        {
            if (filter != null && !filter.Empty)
                throw new UserInformationException("Filters are not supported for this operation");

            if (db.PartiallyRecreated)
                throw new UserInformationException("The command does not work on partially recreated databases");

            List<Database.RemoteVolumeEntry> missing;
            var brokensets = GetBrokenFilesetsFromRemote(m_backendurl, m_result, db, transaction, m_options, out missing);
            if (brokensets == null)
                return;

            if (brokensets.Length == 0)
            {
                m_result.BrokenFiles = new Tuple<long, DateTime, IEnumerable<Tuple<string, long>>>[0];
                m_result.AddMessage("No broken filesets found");
                return;
            }

            var fstimes = db.FilesetTimes.ToList();

            var brokenfilesets = 
                brokensets.Select(x => new 
                {
                    Version = fstimes.FindIndex(y => y.Key == x.Item2),
                    Timestamp = x.Item1,
                    FilesetID = x.Item2,
                    BrokenCount = x.Item3
                }
              )
              .ToArray();


            m_result.BrokenFiles =
                brokenfilesets.Select(
                    x => new Tuple<long, DateTime, IEnumerable<Tuple<string, long>>>(
                                x.Version, 
                                x.Timestamp, 
                                callbackhandler == null && !m_options.ListSetsOnly
                                    ? db.GetBrokenFilenames(x.FilesetID, transaction).ToArray().AsEnumerable()
                                    : new MockList<Tuple<string, long>>((int)x.BrokenCount)
                    ))
                .ToArray();


            if (callbackhandler != null)
                foreach (var bs in brokenfilesets)
                    foreach (var fe in db.GetBrokenFilenames(bs.FilesetID, transaction))
                        if (!callbackhandler(bs.Version, bs.Timestamp, bs.BrokenCount, fe.Item1, fe.Item2))
                            break;
        }

        private class MockList<T> : IList<T>
        {
            public MockList(int count)
            {
                Count = count;
            }

            public T this[int index]
            {
                get { throw new NotImplementedException(); }
                set { throw new NotImplementedException(); }
            }

            public int Count { get; private set; }

            public bool IsReadOnly { get { return true; }}

            public void Add(T item)
            {
                throw new NotImplementedException();
            }

            public void Clear()
            {
                throw new NotImplementedException();
            }

            public bool Contains(T item)
            {
                throw new NotImplementedException();
            }

            public void CopyTo(T[] array, int arrayIndex)
            {
                throw new NotImplementedException();
            }

            public IEnumerator<T> GetEnumerator()
            {
                throw new NotImplementedException();
            }

            public int IndexOf(T item)
            {
                throw new NotImplementedException();
            }

            public void Insert(int index, T item)
            {
                throw new NotImplementedException();
            }

            public bool Remove(T item)
            {
                throw new NotImplementedException();
            }

            public void RemoveAt(int index)
            {
                throw new NotImplementedException();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
        }
    }
}