source: box/trunk/lib/backupclient/BackupStoreObjectDump.cpp @ 2481

Revision 2481, 7.0 KB checked in by chris, 3 years ago (diff)

Change type of BackupStoreFilename? not to derive from std::string, so
it can't accidentally be used as one.

Fix use of encrypted filename in deleted file message, thanks to Kenny
Millington for reporting.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    BackupStoreObjectDump.cpp
5//              Purpose: Implementations of dumping objects to stdout/TRACE
6//              Created: 3/5/04
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#include <stdio.h>
13#include <stdarg.h>
14#include <map>
15
16#include "BackupStoreDirectory.h"
17#include "BackupStoreFile.h"
18#include "BackupStoreFileWire.h"
19#include "autogen_BackupStoreException.h"
20#include "BackupStoreFilename.h"
21#include "BackupClientFileAttributes.h"
22#include "BackupStoreObjectMagic.h"
23
24#include "MemLeakFindOn.h"
25
26
27// --------------------------------------------------------------------------
28//
29// Function
30//              Name:    static void OutputLine(FILE *, bool, const char *, ...)
31//              Purpose: Output a line for the object dumping, to file and/or trace...
32//              Created: 3/5/04
33//
34// --------------------------------------------------------------------------
35static void OutputLine(FILE *file, bool ToTrace, const char *format, ...)
36{
37        char text[512];
38        int r = 0;
39        va_list ap;
40        va_start(ap, format);
41        r = vsnprintf(text, sizeof(text), format, ap);
42        va_end(ap);
43
44        if(file != 0)
45        {
46                ::fprintf(file, "%s", text);           
47        }
48        if(ToTrace)
49        {
50                BOX_TRACE(text);
51        }
52}
53
54
55// --------------------------------------------------------------------------
56//
57// Function
58//              Name:    BackupStoreDirectory::Dump(void *clibFileHandle, bool ToTrace)
59//              Purpose: (first arg is FILE *, but avoid including stdio.h everywhere)
60//                               Dump the contents to a file, or trace.
61//              Created: 3/5/04
62//
63// --------------------------------------------------------------------------
64void BackupStoreDirectory::Dump(void *clibFileHandle, bool ToTrace)
65{
66        FILE *file = (FILE*)clibFileHandle;
67
68        OutputLine(file, ToTrace, "Directory object.\nObject ID: %llx\nContainer ID: %llx\nNumber entries: %d\n"\
69                "Attributes mod time: %llx\nAttributes size: %d\n", mObjectID, mContainerID, mEntries.size(),
70                mAttributesModTime, mAttributes.GetSize());
71
72        // So repeated filenames can be illustrated, even though they can't be decoded
73        std::map<std::string, int> nameNum;
74        int nameNumI = 0;
75
76        // Dump items
77        OutputLine(file, ToTrace, "Items:\nID     Size AttrHash         AtSz NSz NIdx Flags\n");
78        for(std::vector<Entry*>::const_iterator i(mEntries.begin()); i != mEntries.end(); ++i)
79        {
80                // Choose file name index number for this file
81                std::map<std::string, int>::iterator nn(nameNum.find((*i)->GetName().GetEncodedFilename()));
82                int ni = nameNumI;
83                if(nn != nameNum.end())
84                {
85                        ni = nn->second;
86                }
87                else
88                {
89                        nameNum[(*i)->GetName().GetEncodedFilename()] = nameNumI;
90                        ++nameNumI;
91                }
92               
93                // Do dependencies
94                char depends[128];
95                depends[0] = '\0';
96                int depends_l = 0;
97                if((*i)->GetDependsNewer() != 0)
98                {
99#ifdef _MSC_VER
100                        depends_l += ::sprintf(depends + depends_l, " depNew(%I64x)", (*i)->GetDependsNewer());
101#else
102                        depends_l += ::sprintf(depends + depends_l, " depNew(%llx)", (long long)((*i)->GetDependsNewer()));
103#endif
104                }
105                if((*i)->GetDependsOlder() != 0)
106                {
107#ifdef _MSC_VER
108                        depends_l += ::sprintf(depends + depends_l, " depOld(%I64x)", (*i)->GetDependsOlder());
109#else
110                        depends_l += ::sprintf(depends + depends_l, " depOld(%llx)", (long long)((*i)->GetDependsOlder()));
111#endif
112                }
113
114                // Output item
115                int16_t f = (*i)->GetFlags();
116#ifdef WIN32
117                OutputLine(file, ToTrace, 
118                        "%06I64x %4I64d %016I64x %4d %3d %4d%s%s%s%s%s%s\n",
119#else
120                OutputLine(file, ToTrace, 
121                        "%06llx %4lld %016llx %4d %3d %4d%s%s%s%s%s%s\n",
122#endif
123                        (*i)->GetObjectID(),
124                        (*i)->GetSizeInBlocks(),
125                        (*i)->GetAttributesHash(),
126                        (*i)->GetAttributes().GetSize(),
127                        (*i)->GetName().GetEncodedFilename().size(),
128                        ni,
129                        ((f & BackupStoreDirectory::Entry::Flags_File)?" file":""),
130                        ((f & BackupStoreDirectory::Entry::Flags_Dir)?" dir":""),
131                        ((f & BackupStoreDirectory::Entry::Flags_Deleted)?" del":""),
132                        ((f & BackupStoreDirectory::Entry::Flags_OldVersion)?" old":""),
133                        ((f & BackupStoreDirectory::Entry::Flags_RemoveASAP)?" removeASAP":""),
134                        depends);
135        }
136}
137
138// --------------------------------------------------------------------------
139//
140// Function
141//              Name:    BackupStoreFile::DumpFile(void *, bool, IOStream &)
142//              Purpose: (first arg is FILE *, but avoid including stdio.h everywhere)
143//                               Dump the contents to a file, or trace.
144//              Created: 4/5/04
145//
146// --------------------------------------------------------------------------
147void BackupStoreFile::DumpFile(void *clibFileHandle, bool ToTrace, IOStream &rFile)
148{
149        FILE *file = (FILE*)clibFileHandle;
150
151        // Read header
152        file_StreamFormat hdr;
153        if(!rFile.ReadFullBuffer(&hdr, sizeof(hdr),
154                0 /* not interested in bytes read if this fails */, IOStream::TimeOutInfinite))
155        {
156                // Couldn't read header
157                THROW_EXCEPTION(BackupStoreException, WhenDecodingExpectedToReadButCouldnt)
158        }
159
160        // Check and output header info
161        if(hdr.mMagicValue != (int32_t)htonl(OBJECTMAGIC_FILE_MAGIC_VALUE_V1)
162                && hdr.mMagicValue != (int32_t)htonl(OBJECTMAGIC_FILE_MAGIC_VALUE_V0))
163        {
164                OutputLine(file, ToTrace, "File header doesn't have the correct magic, aborting dump\n");
165                return;
166        }
167
168        OutputLine(file, ToTrace, "File object.\nContainer ID: %llx\nModification time: %llx\n"\
169                "Max block clear size: %d\nOptions: %08x\nNum blocks: %d\n", box_ntoh64(hdr.mContainerID),
170                        box_ntoh64(hdr.mModificationTime), ntohl(hdr.mMaxBlockClearSize), ntohl(hdr.mOptions),
171                        box_ntoh64(hdr.mNumBlocks));
172
173        // Read the next two objects
174        BackupStoreFilename fn;
175        fn.ReadFromStream(rFile, IOStream::TimeOutInfinite);
176        OutputLine(file, ToTrace, "Filename size: %d\n",
177                fn.GetEncodedFilename().size());
178       
179        BackupClientFileAttributes attr;
180        attr.ReadFromStream(rFile, IOStream::TimeOutInfinite);
181        OutputLine(file, ToTrace, "Attributes size: %d\n", attr.GetSize());
182       
183        // Dump the blocks
184        rFile.Seek(0, IOStream::SeekType_Absolute);
185        BackupStoreFile::MoveStreamPositionToBlockIndex(rFile);
186
187        // Read in header
188        file_BlockIndexHeader bhdr;
189        rFile.ReadFullBuffer(&bhdr, sizeof(bhdr), 0);
190        if(bhdr.mMagicValue != (int32_t)htonl(OBJECTMAGIC_FILE_BLOCKS_MAGIC_VALUE_V1)
191                && bhdr.mMagicValue != (int32_t)htonl(OBJECTMAGIC_FILE_BLOCKS_MAGIC_VALUE_V0))
192        {
193                OutputLine(file, ToTrace, "WARNING: Block header doesn't have the correct magic\n");
194        }
195        // number of blocks
196        int64_t nblocks = box_ntoh64(bhdr.mNumBlocks);
197        OutputLine(file, ToTrace, "Other file ID (for block refs): %llx\nNum blocks (in blk hdr): %lld\n",
198                box_ntoh64(bhdr.mOtherFileID), nblocks);
199
200        // Dump info about each block
201        OutputLine(file, ToTrace, "======== ===== ==========\n   Index Where  EncSz/Idx\n");
202        int64_t nnew = 0, nold = 0;
203        for(int64_t b = 0; b < nblocks; ++b)
204        {
205                file_BlockIndexEntry en;
206                if(!rFile.ReadFullBuffer(&en, sizeof(en), 0))
207                {
208                        OutputLine(file, ToTrace, "Didn't manage to read block %lld from file\n", b);
209                        continue;
210                }
211                int64_t s = box_ntoh64(en.mEncodedSize);
212                if(s > 0)
213                {
214                        nnew++;
215                        BOX_TRACE(std::setw(8) << b << " this  s=" <<
216                                std::setw(8) << s);
217                }
218                else
219                {
220                        nold++;
221                        BOX_TRACE(std::setw(8) << b << " other i=" <<
222                                std::setw(8) << 0 - s);
223                }
224        }
225        BOX_TRACE("======== ===== ==========");
226}
227
Note: See TracBrowser for help on using the repository browser.