aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/microbdb/file.cpp
blob: 599a7301a0ff3f15a630be0fb8185c337fcec52d (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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "file.h"

#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>

#ifdef _win32_
#define S_ISREG(x) !!(x & S_IFREG)
#endif

TFileManipBase::TFileManipBase()
    : FileBased(true)
{
}

i64 TFileManipBase::DoSeek(i64 offset, int whence, bool isStreamOpen) {
    if (!isStreamOpen)
        return -1;
    VerifyRandomAccess();
    return File.Seek(offset, (SeekDir)whence);
}

int TFileManipBase::DoFileOpen(const TFile& file) {
    File = file;
    SetFileBased(IsFileBased());
    return (File.IsOpen()) ? 0 : MBDB_OPEN_ERROR;
}

int TFileManipBase::DoFileClose() {
    if (File.IsOpen()) {
        File.Close();
        return MBDB_ALREADY_INITIALIZED;
    }
    return 0;
}

int TFileManipBase::IsFileBased() const {
    bool fileBased = true;
#if defined(_win_)
#elif defined(_unix_)
    FHANDLE h = File.GetHandle();
    struct stat sb;
    fileBased = false;
    if (h != INVALID_FHANDLE && !::fstat(h, &sb) && S_ISREG(sb.st_mode)) {
        fileBased = true;
    }
#else
#error
#endif
    return fileBased;
}

TInputFileManip::TInputFileManip()
    : InputStream(nullptr)
{
}

int TInputFileManip::Open(const char* fname, bool direct) {
    int ret;
    return (ret = DoClose()) ? ret : DoStreamOpen(TFile(fname, RdOnly | (direct ? DirectAligned : EOpenMode())));
}

int TInputFileManip::Open(IInputStream& input) {
    int ret;
    return (ret = DoClose()) ? ret : DoStreamOpen(&input);
}

int TInputFileManip::Open(TAutoPtr<IInputStream> input) {
    int ret;
    return (ret = DoClose()) ? ret : DoStreamOpen(input.Release());
}

int TInputFileManip::Init(const TFile& file) {
    int ret;
    if (ret = DoClose())
        return ret;
    DoStreamOpen(file);
    return 0;
}

int TInputFileManip::Close() {
    DoClose();
    return 0;
}

ssize_t TInputFileManip::Read(void* buf, unsigned len) {
    if (!IsStreamOpen())
        return -1;
    return InputStream->Load(buf, len);
}

IInputStream* TInputFileManip::CreateStream(const TFile& file) {
    return new TUnbufferedFileInput(file);
}

TMappedInputPageFile::TMappedInputPageFile()
    : Pagesize(0)
    , Error(0)
    , Pagenum(0)
    , Recordsig(0)
    , Open(false)
{
    Term();
}

TMappedInputPageFile::~TMappedInputPageFile() {
    Term();
}

int TMappedInputPageFile::Init(const char* fname, ui32 recsig, ui32* gotRecordSig, bool) {
    Mappedfile.init(fname);
    Open = true;

    TDatMetaPage* meta = (TDatMetaPage*)Mappedfile.getData();
    if (gotRecordSig)
        *gotRecordSig = meta->RecordSig;

    if (meta->MetaSig != METASIG)
        Error = MBDB_BAD_METAPAGE;
    else if (meta->RecordSig != recsig)
        Error = MBDB_BAD_RECORDSIG;

    if (Error) {
        Mappedfile.term();
        return Error;
    }

    size_t fsize = Mappedfile.getSize();
    if (fsize < METASIZE)
        return Error = MBDB_BAD_FILE_SIZE;
    fsize -= METASIZE;
    if (fsize % meta->PageSize)
        return Error = MBDB_BAD_FILE_SIZE;
    Pagenum = (int)(fsize / meta->PageSize);
    Pagesize = meta->PageSize;
    Recordsig = meta->RecordSig;
    Error = 0;
    return Error;
}

int TMappedInputPageFile::Term() {
    Mappedfile.term();
    Open = false;
    return 0;
}

TOutputFileManip::TOutputFileManip()
    : OutputStream(nullptr)
{
}

int TOutputFileManip::Open(const char* fname, EOpenMode mode) {
    if (IsStreamOpen()) {
        return MBDB_ALREADY_INITIALIZED; // should it be closed as TInputFileManip
    }

    try {
        if (unlink(fname) && errno != ENOENT) {
            if (strncmp(fname, "/dev/std", 8))
                return MBDB_OPEN_ERROR;
        }
        TFile file(fname, mode);
        DoStreamOpen(file);
    } catch (const TFileError&) {
        return MBDB_OPEN_ERROR;
    }
    return 0;
}

int TOutputFileManip::Open(IOutputStream& output) {
    if (IsStreamOpen())
        return MBDB_ALREADY_INITIALIZED;
    DoStreamOpen(&output);
    return 0;
}

int TOutputFileManip::Open(TAutoPtr<IOutputStream> output) {
    if (IsStreamOpen())
        return MBDB_ALREADY_INITIALIZED;
    DoStreamOpen(output.Release());
    return 0;
}

int TOutputFileManip::Init(const TFile& file) {
    if (IsStreamOpen())
        return MBDB_ALREADY_INITIALIZED; // should it be closed as TInputFileManip
    DoStreamOpen(file);
    return 0;
}

int TOutputFileManip::Rotate(const char* newfname) {
    if (!IsStreamOpen()) {
        return MBDB_NOT_INITIALIZED;
    }

    try {
        TFile file(newfname, WrOnly | OpenAlways | TruncExisting | ARW | AWOther);
        DoClose();
        DoStreamOpen(file);
    } catch (const TFileError&) {
        return MBDB_OPEN_ERROR;
    }
    return 0;
}

int TOutputFileManip::Close() {
    DoClose();
    return 0;
}

int TOutputFileManip::Write(const void* buf, unsigned len) {
    if (!IsStreamOpen())
        return -1;
    OutputStream->Write(buf, len);
    return len;
}

IOutputStream* TOutputFileManip::CreateStream(const TFile& file) {
    return new TUnbufferedFileOutput(file);
}