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
36
#include "CSException.h"
42
class CSFile : public CSSysFile, public CSRefObject {
46
static const int DEFAULT = 0; // Open for read/write, error if it does not exist.
47
static const int READONLY = 1; // Open for readonly
48
static const int CREATE = 2; // Create if it does not exist
49
static const int TRUNCATE = 4; // After open, set EOF to zero
51
CSFile(): myFilePath(NULL), iMode(-1), iLocked(0) { }
55
CSOutputStream *getOutputStream();
56
CSOutputStream *getOutputStream(off64_t offset);
58
CSInputStream *getInputStream();
59
CSInputStream *getInputStream(off64_t offset);
62
* Open the file in the specified
65
virtual void open(int mode);
67
/* Lock the file. The file will be unlocked
72
virtual void unlock();
80
* Calculate the Md5 digest for the file.
82
void md5Digest(Md5Digest *digest);
85
* Move the current position to
86
* the end of the file.
88
virtual off64_t getEOF();
90
virtual void setEOF(off64_t offset);
93
* Read a given number of bytes. This function
94
* throws an error if the number of bytes read
95
* ius less than 'min_size'.
97
virtual size_t read(void *data, off64_t offset, size_t size, size_t min_size);
100
* Write the given number of bytes.
101
* Throws IOException if an error occurs.
103
virtual void write(const void *data, off64_t offset, size_t size);
106
* Flush the data written.
108
virtual void flush();
110
/* Flush the OS buffers: */
111
virtual void sync() ;
113
/* Resets access and modification times of the file: */
114
virtual void touch();
117
* Return a platform specific prefered
118
* line ending for text files.
120
virtual const char *getEOL() { return "\n"; };
122
virtual const char *getPathString() { return myFilePath->getCString(); }
124
bool exists() { return myFilePath->exists(); }
126
friend class CSReadBufferedFile;
132
virtual void openFile(int mode);
133
bool try_CreateAndOpen(CSThread *self, int mode, bool retry);
136
void streamOut(CSOutputStream *dst_stream, off64_t src_offset, off64_t size, char *buffer, size_t buffer_size);
137
void streamIn(CSInputStream *src_stream, off64_t dst_offset, off64_t size, char *buffer, size_t buffer_size);
139
static bool isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
140
static bool isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
142
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);
144
static CSFile *newFile(CSPath *path);
146
static CSFile *newFile(const char *path);
148
static CSFile *newFile(const char *dir_str, const char *path_str);
152
// This stuff needs to be retought.
155
#define SC_DEFAULT_FILE_BUFFER_SIZE 127
157
#define SC_DEFAULT_FILE_BUFFER_SIZE (64 * 1024)
160
class CSReadBufferedFile : public CSRefObject {
163
CSReadBufferedFile();
165
~CSReadBufferedFile();
167
void setFile(CSFile *file) {myFile = file;}
169
const char *getPathString() { return myFile->getPathString(); }
170
void open(int mode) {myFile->open(mode); }
176
void setEOF(off64_t offset);
178
size_t read(void *data, off64_t offset, size_t size, size_t min_size);
180
void write(const void *data, off64_t offset, size_t size);
186
const char *getEOL();
191
char iFileBuffer[SC_DEFAULT_FILE_BUFFER_SIZE];
192
off64_t iFileBufferOffset;
193
size_t iBufferDataLen;
195
virtual void openFile(int mode);