blob: 5212db355575c1528deefb8861f9ed58dd703d0e (
plain) (
blame)
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "file_with_header.h"
#include "file_settings.h"
#include "filesizecalculator.h"
#include <vespa/fastos/file.h>
#include <vespa/vespalib/util/size_literals.h>
#include <cassert>
namespace search {
namespace {
bool
extract_file_size(const vespalib::GenericHeader& header,
FastOS_FileInterface& file, uint64_t& file_size)
{
file_size = file.getSize();
return FileSizeCalculator::extractFileSize(header, header.getSize(),file.GetFileName(), file_size);
}
}
FileWithHeader::FileWithHeader(std::unique_ptr<FastOS_FileInterface> file_in)
: _file(std::move(file_in)),
_header(FileSettings::DIRECTIO_ALIGNMENT),
_header_len(0),
_file_size(0)
{
if (valid()) {
_header_len = _header.readFile(*_file);
_file->SetPosition(_header_len);
if (!extract_file_size(_header, *_file, _file_size)) {
bool close_ok = _file->Close();
assert(close_ok);
}
}
}
FileWithHeader::~FileWithHeader() = default;
bool
FileWithHeader::valid() const
{
return _file && _file->IsOpened();
}
void
FileWithHeader::rewind()
{
_file->SetPosition(_header_len);
}
void
FileWithHeader::close()
{
bool close_ok = _file->Close();
assert(close_ok);
}
}
|