File: index_file.cc

package info (click to toggle)
zbackup 1.5-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: cpp: 6,957; ansic: 468; python: 207; makefile: 10
file content (78 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download | duplicates (5)
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
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE

#include <string.h>

#include "bundle.hh"
#include "encryption.hh"
#include "index_file.hh"
#include "message.hh"

namespace IndexFile {

enum
{
  FileFormatVersion = 1
};

Writer::Writer( EncryptionKey const & key, string const & fileName ):
  stream( fileName.c_str(), key, Encryption::ZeroIv )
{
  stream.writeRandomIv();
  FileHeader header;
  header.set_version( FileFormatVersion );
  Message::serialize( header, stream );
}

void Writer::add( BundleInfo const & info, Bundle::Id const & bundleId )
{
  IndexBundleHeader header;
  header.set_id( &bundleId, sizeof( bundleId ) );

  Message::serialize( header, stream );
  Message::serialize( info, stream );
}

Writer::~Writer()
{
  // Final record which does not have a bundle id
  IndexBundleHeader header;
  Message::serialize( header, stream );
  stream.writeAdler32();
}

Reader::Reader( EncryptionKey const & key, string const & fileName ):
  stream( fileName.c_str(), key, Encryption::ZeroIv )
{
  stream.consumeRandomIv();

  FileHeader header;
  Message::parse( header, stream );

  if ( header.version() != FileFormatVersion )
    throw exUnsupportedVersion();
}

bool Reader::readNextRecord( BundleInfo & info, Bundle::Id & bundleId )
{
  IndexBundleHeader header;
  Message::parse( header, stream );

  if ( header.has_id() )
  {
    if ( header.id().size() != sizeof( bundleId ) )
      throw exIncorrectBundleIdSize();

    memcpy( &bundleId, header.id().data(), sizeof( bundleId ) );

    Message::parse( info, stream );
    return true;
  }
  else
  {
    stream.checkAdler32();
    return false;
  }
}

}