source: box/trunk/lib/backupstore/BackupStoreFilename.cpp @ 2945

Revision 2945, 7.8 KB checked in by chris, 13 months ago (diff)

Major refactoring to make lib/backupclient depend on lib/backupstore rather
than the other way around. This is needed to allow clients to have all the
code that they'd need to implement local backups (using the Local protocol)
in subsequent commits.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    BackupStoreFilename.cpp
5//              Purpose: Filename for the backup store
6//              Created: 2003/08/26
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11#include "BackupStoreFilename.h"
12#include "Protocol.h"
13#include "BackupStoreException.h"
14#include "IOStream.h"
15#include "Guards.h"
16
17#include "MemLeakFindOn.h"
18
19// --------------------------------------------------------------------------
20//
21// Function
22//              Name:    BackupStoreFilename::BackupStoreFilename()
23//              Purpose: Default constructor -- creates an invalid filename
24//              Created: 2003/08/26
25//
26// --------------------------------------------------------------------------
27BackupStoreFilename::BackupStoreFilename()
28{
29}
30
31// --------------------------------------------------------------------------
32//
33// Function
34//              Name:    BackupStoreFilename::BackupStoreFilename(const BackupStoreFilename &)
35//              Purpose: Copy constructor
36//              Created: 2003/08/26
37//
38// --------------------------------------------------------------------------
39BackupStoreFilename::BackupStoreFilename(const BackupStoreFilename &rToCopy)
40        : mEncryptedName(rToCopy.mEncryptedName)
41{
42}
43
44// --------------------------------------------------------------------------
45//
46// Function
47//              Name:    BackupStoreFilename::~BackupStoreFilename()
48//              Purpose: Destructor
49//              Created: 2003/08/26
50//
51// --------------------------------------------------------------------------
52BackupStoreFilename::~BackupStoreFilename()
53{
54}
55
56// --------------------------------------------------------------------------
57//
58// Function
59//              Name:    BackupStoreFilename::CheckValid(bool)
60//              Purpose: Checks the encoded filename for validity
61//              Created: 2003/08/26
62//
63// --------------------------------------------------------------------------
64bool BackupStoreFilename::CheckValid(bool ExceptionIfInvalid) const
65{
66        bool ok = true;
67       
68        if(mEncryptedName.size() < 2)
69        {
70                // Isn't long enough to have a header
71                ok = false;
72        }
73        else
74        {
75                // Check size is consistent
76                unsigned int dsize = BACKUPSTOREFILENAME_GET_SIZE(this->mEncryptedName);
77                if(dsize != mEncryptedName.size())
78                {
79                        ok = false;
80                }
81               
82                // And encoding is an accepted value
83                unsigned int encoding = BACKUPSTOREFILENAME_GET_ENCODING(this->mEncryptedName);
84                if(encoding < Encoding_Min || encoding > Encoding_Max)
85                {
86                        ok = false;
87                }
88        }
89       
90        // Exception?
91        if(!ok && ExceptionIfInvalid)
92        {
93                THROW_EXCEPTION(BackupStoreException, InvalidBackupStoreFilename)
94        }
95
96        return ok;
97}
98
99
100// --------------------------------------------------------------------------
101//
102// Function
103//              Name:    BackupStoreFilename::ReadFromProtocol(Protocol &)
104//              Purpose: Reads the filename from the protocol object
105//              Created: 2003/08/26
106//
107// --------------------------------------------------------------------------
108void BackupStoreFilename::ReadFromProtocol(Protocol &rProtocol)
109{
110        // Read the header
111        char hdr[2];
112        rProtocol.Read(hdr, 2);
113       
114        // How big is it?
115        int dsize = BACKUPSTOREFILENAME_GET_SIZE(hdr);
116       
117        // Fetch rest of data, relying on the Protocol to error on stupidly large sizes for us
118        std::string data;
119        rProtocol.Read(data, dsize - 2);
120       
121        // assign to this string, storing the header and the extra data
122        mEncryptedName.assign(hdr, 2);
123        mEncryptedName.append(data.c_str(), data.size());
124       
125        // Check it
126        CheckValid();
127       
128        // Alert derived classes
129        EncodedFilenameChanged();
130}
131
132// --------------------------------------------------------------------------
133//
134// Function
135//              Name:    BackupStoreFilename::WriteToProtocol(Protocol &)
136//              Purpose: Writes the filename to the protocol object
137//              Created: 2003/08/26
138//
139// --------------------------------------------------------------------------
140void BackupStoreFilename::WriteToProtocol(Protocol &rProtocol) const
141{
142        CheckValid();
143       
144        rProtocol.Write(mEncryptedName.c_str(), mEncryptedName.size());
145}
146
147// --------------------------------------------------------------------------
148//
149// Function
150//              Name:    BackupStoreFilename::ReadFromStream(IOStream &)
151//              Purpose: Reads the filename from a stream
152//              Created: 2003/08/26
153//
154// --------------------------------------------------------------------------
155void BackupStoreFilename::ReadFromStream(IOStream &rStream, int Timeout)
156{
157        // Read the header
158        char hdr[2];
159        if(!rStream.ReadFullBuffer(hdr, 2, 0 /* not interested in bytes read if this fails */, Timeout))
160        {
161                THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
162        }
163       
164        // How big is it?
165        unsigned int dsize = BACKUPSTOREFILENAME_GET_SIZE(hdr);
166       
167        // Assume most filenames are small
168        char buf[256];
169        if(dsize < sizeof(buf))
170        {
171                // Fetch rest of data, relying on the Protocol to error on stupidly large sizes for us
172                if(!rStream.ReadFullBuffer(buf + 2, dsize - 2, 0 /* not interested in bytes read if this fails */, Timeout))
173                {
174                        THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
175                }
176                // Copy in header
177                buf[0] = hdr[0]; buf[1] = hdr[1];
178
179                // assign to this string, storing the header and the extra data
180                mEncryptedName.assign(buf, dsize);
181        }
182        else
183        {
184                // Block of memory to hold it
185                MemoryBlockGuard<char*> dataB(dsize+2);
186                char *data = dataB;
187
188                // Fetch rest of data, relying on the Protocol to error on stupidly large sizes for us
189                if(!rStream.ReadFullBuffer(data + 2, dsize - 2, 0 /* not interested in bytes read if this fails */, Timeout))
190                {
191                        THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
192                }
193                // Copy in header
194                data[0] = hdr[0]; data[1] = hdr[1];
195
196                // assign to this string, storing the header and the extra data
197                mEncryptedName.assign(data, dsize);
198        }
199       
200        // Check it
201        CheckValid();
202       
203        // Alert derived classes
204        EncodedFilenameChanged();
205}
206
207// --------------------------------------------------------------------------
208//
209// Function
210//              Name:    BackupStoreFilename::WriteToStream(IOStream &)
211//              Purpose: Writes the filename to a stream
212//              Created: 2003/08/26
213//
214// --------------------------------------------------------------------------
215void BackupStoreFilename::WriteToStream(IOStream &rStream) const
216{
217        CheckValid();
218       
219        rStream.Write(mEncryptedName.c_str(), mEncryptedName.size());
220}
221
222// --------------------------------------------------------------------------
223//
224// Function
225//              Name:    BackupStoreFilename::EncodedFilenameChanged()
226//              Purpose: The encoded filename stored has changed
227//              Created: 2003/08/26
228//
229// --------------------------------------------------------------------------
230void BackupStoreFilename::EncodedFilenameChanged()
231{
232}
233
234
235// --------------------------------------------------------------------------
236//
237// Function
238//              Name:    BackupStoreFilename::IsEncrypted()
239//              Purpose: Returns true if the filename is stored using an encrypting encoding
240//              Created: 1/12/03
241//
242// --------------------------------------------------------------------------
243bool BackupStoreFilename::IsEncrypted() const
244{
245        return BACKUPSTOREFILENAME_GET_ENCODING(this->mEncryptedName) !=
246                Encoding_Clear;
247}
248
249
250// --------------------------------------------------------------------------
251//
252// Function
253//              Name:    BackupStoreFilename::SetAsClearFilename(const char *)
254//              Purpose: Sets this object to be a valid filename, but with a
255//                       filename in the clear. Used on the server to create
256//                       filenames when there's no way of encrypting it.
257//              Created: 22/4/04
258//
259// --------------------------------------------------------------------------
260void BackupStoreFilename::SetAsClearFilename(const char *Clear)
261{
262        // Make std::string from the clear name
263        std::string toEncode(Clear);
264
265        // Make an encoded string
266        char hdr[2];
267        BACKUPSTOREFILENAME_MAKE_HDR(hdr, toEncode.size()+2, Encoding_Clear);
268        std::string encoded(hdr, 2);
269        encoded += toEncode;
270        ASSERT(encoded.size() == toEncode.size() + 2);
271       
272        // Store the encoded string
273        mEncryptedName.assign(encoded);
274       
275        // Stuff which must be done
276        EncodedFilenameChanged();
277        CheckValid(false);
278}
279
280
281
Note: See TracBrowser for help on using the repository browser.