1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
37
#include "CSException.h"
43
class CSFile : public CSSysFile, public CSRefObject {
47
static const int DEFAULT = 0; // Open for read/write, error if it does not exist.
48
static const int READONLY = 1; // Open for readonly
49
static const int CREATE = 2; // Create if it does not exist
50
static const int TRUNCATE = 4; // After open, set EOF to zero
52
CSFile(): myFilePath(NULL), iMode(-1), iLocked(0) { }
56
CSOutputStream *getOutputStream();
57
CSOutputStream *getOutputStream(off64_t offset);
59
CSInputStream *getInputStream();
60
CSInputStream *getInputStream(off64_t offset);
63
* Open the file in the specified
66
virtual void open(int mode);
68
/* Lock the file. The file will be unlocked
73
virtual void unlock();
81
* Calculate the Md5 digest for the file.
83
void md5Digest(Md5Digest *digest);
86
* Move the current position to
87
* the end of the file.
89
virtual off64_t getEOF();
91
virtual void setEOF(off64_t offset);
94
* Read a given number of bytes. This function
95
* throws an error if the number of bytes read
96
* ius less than 'min_size'.
98
virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
101
* Write the given number of bytes.
102
* Throws IOException if an error occurs.
104
virtual void write(const void *data, off64_t offset, size_t size);
107
* Flush the data written.
109
virtual void flush();
111
/* Flush the OS buffers: */
112
virtual void sync() ;
114
/* Resets access and modification times of the file: */
115
virtual void touch();
118
* Return a platform specific prefered
119
* line ending for text files.
121
virtual const char *getEOL() { return "\n"; };
123
virtual const char *getPathString() { return myFilePath->getCString(); }
125
bool exists() { return myFilePath->exists(); }
127
friend class CSReadBufferedFile;
133
virtual void openFile(int mode);
134
bool try_CreateAndOpen(CSThread *self, int mode, bool retry);
137
void streamOut(CSOutputStream *dst_stream, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
138
void streamIn(CSInputStream *src_stream, off64_t dst_offset, off64_t size, char *buffer, size_t buffer_size);
140
static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
141
static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
143
static bool transfer(CSFile *dst_file, off64_t dst_offset, CSFile *src_file, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
145
static CSFile *newFile(CSPath *path);
147
static CSFile *newFile(const char *path);
149
static CSFile *newFile(const char *dir_str, const char *path_str);
153
// This stuff needs to be retought.
156
#define SC_DEFAULT_FILE_BUFFER_SIZE 127
158
#define SC_DEFAULT_FILE_BUFFER_SIZE (64 * 1024)
161
class CSReadBufferedFile : public CSRefObject {
164
CSReadBufferedFile();
166
~CSReadBufferedFile();
168
void setFile(CSFile *file) {myFile = file;}
170
const char *getPathString() { return myFile->getPathString(); }
171
void open(int mode) {myFile->open(mode); }
177
void setEOF(off64_t offset);
179
size_t read(void *data, off64_t offset, size_t size, size_t min_size);
181
void write(const void *data, off64_t offset, size_t size);
187
const char *getEOL();
192
char iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
193
off64_t iFileBufferOffset;
194
size_t iBufferDataLen;
196
virtual void openFile(int mode);