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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/***************************************************************************
ringbuffer.cpp - description
-------------------
begin : Sun March 21 2004
copyright : (C) 2004 by Martin Witte
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "include/fileringbuffer.h"
#include <qstring.h>
#include <unistd.h>
#include <klocale.h>
FileRingBuffer::FileRingBuffer(const QString &filename, Q_UINT64 max_size)
{
m_BaseFileName = filename;
m_FileIdx = 0;
m_FileName = m_BaseFileName + "_" + QString::number(++m_FileIdx);
m_File = fopen(m_FileName.ascii(), "w+");
m_MaxSize = max_size;
m_RealSize = 0;
m_FillSize = 0;
m_Start = 0;
m_error = m_File == NULL;
m_errorString = m_File ? QString::null : i18n("cannot open buffer file %1").arg(filename);
}
FileRingBuffer::~FileRingBuffer()
{
if (m_File) {
fclose (m_File);
unlink (m_FileName.ascii());
}
m_File = NULL;
m_FileName = QString::null;
m_MaxSize = 0;
m_RealSize = 0;
m_FillSize = 0;
m_Start = 0;
m_error = false;
m_errorString = QString::null;
}
bool FileRingBuffer::resize(const QString &filename, Q_UINT64 new_max_size)
{
if (filename != m_BaseFileName) {
clear();
if (m_File) {
fclose (m_File);
unlink (m_FileName.ascii());
}
m_BaseFileName = filename;
m_FileName = m_BaseFileName + "_" + QString::number(++m_FileIdx);
m_File = fopen(m_FileName.ascii(), "w+");
m_error = m_File == NULL;
m_errorString = m_File ? QString::null : i18n("cannot open buffer file %1").arg(filename);
}
if (new_max_size >= m_RealSize) {
m_MaxSize = new_max_size;
}
else if (m_Start + m_FillSize < m_RealSize && new_max_size > m_Start + m_FillSize) {
ftruncate(fileno(m_File), new_max_size);
m_MaxSize = new_max_size;
}
else if (new_max_size >= m_FillSize) {
const size_t buffer_size = 65536;
char buffer[buffer_size];
QString tmp_file_name = m_BaseFileName + "_" + QString::number(++m_FileIdx);
FILE *tmp_file = fopen (tmp_file_name.ascii(), "w+");
Q_UINT64 newFill = 0;
if (tmp_file) {
while (!m_error && m_FillSize > 0) {
int tmp_size = takeData(buffer, buffer_size);
if (tmp_size > 0) {
if (fwrite (buffer, tmp_size, 1, tmp_file) > 0) {
newFill += tmp_size;
} else {
m_error = true;
m_errorString += i18n("FileRingbuffer::resize: Writing to tmpfile %1 failed. ").arg(tmp_file_name);
}
}
}
} else {
m_error = true;
m_errorString += i18n("FileRingbuffer::resize: Opening tmpfile %1 failed. ").arg(tmp_file_name);
}
if (!m_error) {
fclose (m_File);
m_FileName = tmp_file_name;
m_File = tmp_file;
m_FillSize = newFill;
m_Start = 0;
m_MaxSize = new_max_size;
m_RealSize = newFill;
}
return true;
}
return false;
}
size_t FileRingBuffer::addData (const char *src, size_t size)
{
size_t written = 0;
if (m_Start + m_FillSize <= m_RealSize) {
Q_UINT64 rest = m_MaxSize - (m_Start + m_FillSize);
if (rest > size)
rest = size;
fseek(m_File, m_Start + m_FillSize, SEEK_SET);
if (rest > 0 && fwrite(src, rest, 1, m_File) <= 0) {
m_error = true;
m_errorString += i18n("FileRingBuffer::addData: failed writing data to file %1.").arg(m_FileName);
} else {
m_FillSize += rest;
if (m_Start + m_FillSize > m_RealSize)
m_RealSize = m_Start + m_FillSize;
written += rest;
size -= rest;
src += rest;
}
}
if (!m_error && size > 0 && m_FillSize < m_RealSize) {
size_t rest = size;
if (rest > m_RealSize - m_FillSize)
rest = m_RealSize - m_FillSize;
fseek(m_File, m_Start + m_FillSize - m_RealSize, SEEK_SET);
if (fwrite(src, rest, 1, m_File) <= 0) {
m_error = true;
m_errorString += i18n("FileRingBuffer::addData: failed writing data to file %1.").arg(m_FileName);
} else {
m_FillSize += rest;
written += rest;
//fflush(m_File); // debug only
}
}
return written;
}
size_t FileRingBuffer::takeData(char *dst, size_t size)
{
size_t read = 0;
while (!m_error && m_FillSize > 0 && size > 0) {
size_t n = size;
if (n > m_FillSize)
n = m_FillSize;
if (n > m_RealSize - m_Start)
n = m_RealSize - m_Start;
fseek(m_File, m_Start, SEEK_SET);
if (fread(dst+read, n, 1, m_File) <= 0) {
m_error = true;
m_errorString += i18n("FileRingBuffer::takeData: failed reading data to file %1.").arg(m_FileName);
} else {
m_FillSize -= n;
m_Start += n;
read += n;
size -= n;
if (m_Start >= m_RealSize)
m_Start -= m_RealSize;
}
}
return read;
}
Q_UINT64 FileRingBuffer::getFreeSpace(Q_UINT64 &size)
{
if (m_FillSize == m_RealSize) {
size = 0;
return 0;
}
if (m_Start + m_FillSize >= m_RealSize) {
size = m_RealSize - m_FillSize;
return m_Start + m_FillSize - m_RealSize;
} else {
size = m_MaxSize - m_Start - m_FillSize;
return m_Start + m_FillSize;
}
}
Q_UINT64 FileRingBuffer::removeFreeSpace(Q_UINT64 size)
{
if (m_FillSize == m_RealSize)
return 0;
if (m_Start + m_FillSize >= m_RealSize) {
if (size > m_RealSize - m_FillSize)
size = m_RealSize - m_FillSize;
m_FillSize += size;
return size;
} else {
if (m_Start + m_FillSize + size >= m_MaxSize)
size = m_MaxSize - m_Start - m_FillSize;
m_FillSize += size;
return size;
}
}
Q_UINT64 FileRingBuffer::getData(Q_UINT64 &size)
{
if (m_Start + m_FillSize >= m_RealSize) {
size = m_RealSize - m_Start;
} else {
size = m_FillSize;
}
return m_Start;
}
Q_UINT64 FileRingBuffer::removeData(Q_UINT64 size)
{
if (size > m_FillSize)
size = m_FillSize;
if (m_Start + size >= m_RealSize) {
m_Start = m_Start + size - m_RealSize;
} else {
m_Start += size;
}
m_FillSize -= size;
return size;
}
void FileRingBuffer::clear()
{
if (!m_error) {
ftruncate(fileno(m_File), 0);
m_Start = 0;
m_FillSize = 0;
m_RealSize = 0;
}
}
|