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

RestoreHandlerMetadataStorage.cs « Operation « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 131ed1f1bf17c9a0851cb562cf131a3dc8c72a04 (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
//  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.IO;
using Duplicati.Library.Utility;
using System.Collections.Generic;
namespace Duplicati.Library.Main.Operation
{
    public class RestoreHandlerMetadataStorage : IDisposable
    {
        private TempFile m_temp;
        private FileStream m_stream;
        private long m_entries;

        private long m_filepos;
        public RestoreHandlerMetadataStorage()
        {
            m_temp = new TempFile();
            m_stream = File.Open(m_temp, FileMode.Truncate, FileAccess.ReadWrite, FileShare.None);
        }

        public void Add(string path, Stream data)
        {
            var datalen = data.Length;

            if (datalen > Int32.MaxValue)
                throw new ArgumentOutOfRangeException("Metadata is larger than int32");

            var pathbytes = System.Text.Encoding.UTF8.GetBytes(path);
            var pathlen = BitConverter.GetBytes(pathbytes.LongLength);
            var entrylen = BitConverter.GetBytes(datalen);

            var totalsize = pathbytes.Length + pathlen.Length + entrylen.Length + datalen;

            m_stream.Position = m_filepos;
            m_stream.Write(pathlen, 0, pathlen.Length);
            m_stream.Write(pathbytes, 0, pathbytes.Length);
            m_stream.Write(entrylen, 0, entrylen.Length);
            data.CopyTo(m_stream);


            if (m_stream.Position != m_filepos + totalsize)
                throw new Exception("Bad file write!");

            m_filepos += totalsize;
            m_entries++;
        }

        private void CheckedRead(byte[] buffer, int offset, int count)
        {
            int r;
            while (count > 0 && (r = m_stream.Read(buffer, offset, count)) > 0)
            {
                offset += r;
                count -= r;
            }

            if (count != 0)
                throw new Exception("Bad file read");
        }

        public IEnumerable<KeyValuePair<string, Stream>> Records
        {
            get
            {
                long pos = 0;
                var bf = BitConverter.GetBytes(0L);
                var buf = new byte[8 * 1024];

                Logging.Log.WriteMessage(string.Format("The metadata storage file has {0} entries and takes up {1}", m_entries, Library.Utility.Utility.FormatSizeString(m_stream.Length)), Duplicati.Library.Logging.LogMessageType.Profiling);

                using(new Logging.Timer("Read metadata from file"))
                    for(var e = 0L; e < m_entries; e++)
                    {
                        m_stream.Position = pos;
                        CheckedRead(bf, 0, bf.Length);
                        var stringlen = BitConverter.ToInt64(bf, 0);

                        var strbuf = stringlen > buf.Length ? new byte[stringlen] : buf;
                        CheckedRead(strbuf, 0, (int)stringlen);
                        var path = System.Text.Encoding.UTF8.GetString(strbuf, 0, (int)stringlen);

                        CheckedRead(bf, 0, bf.Length);
                        var datalen = BitConverter.ToInt64(bf, 0);
                        if (datalen > Int32.MaxValue)
                            throw new ArgumentOutOfRangeException("Metadata is larger than int32");

                        var databuf = datalen > buf.Length ? new byte[datalen] : buf;
                        CheckedRead(databuf, 0, (int)datalen);

                        pos += datalen + stringlen + bf.Length + bf.Length;

                        yield return new KeyValuePair<string, Stream>(path, new MemoryStream(databuf, 0, (int)datalen));

                    }
            }
        }

        #region IDisposable implementation

        public void Dispose()
        {
            if (m_stream != null)
                try { m_stream.Dispose(); }
                catch { }
                finally { m_stream = null; }

            if (m_temp != null)
                try { m_temp.Dispose(); }
                catch { }
                finally { m_temp = null; }

        }

        #endregion
    }
}