1
/* Copyright (C) 2010 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
* Author: Barry Leslie
24
* Basic UNIX specific file I/O classes.
39
#include "CSStrUtil.h"
42
#define CS_MASK ((S_IRUSR | S_IWUSR) | (S_IRGRP | S_IWGRP) | (S_IROTH))
43
//=====================
45
//=====================
46
bool CSSysFile::isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
47
bool CSSysFile::isFileNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
48
bool CSSysFile::isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }
51
void CSSysFile::sf_open(const char *path, bool readonly, bool create)
55
flags = (readonly)?O_RDONLY:O_RDWR;
63
sf_path = CSString::newString(path);
65
sf_fh = open(path, flags, CS_MASK);
69
CSException::throwFileError(CS_CONTEXT, path, errno);
74
void CSSysFile::sf_close()
85
size_t CSSysFile::sf_pread(void *data, size_t size, off64_t offset)
89
read_size = pread(sf_fh, data, size, offset);
91
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);
97
void CSSysFile::sf_pwrite(const void *data, size_t size, off64_t offset)
101
write_size = pwrite(sf_fh, data, size, offset);
102
if (write_size != size)
103
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);
108
void CSSysFile::sf_setEOF(off64_t offset)
110
if (ftruncate(sf_fh, offset) == -1)
111
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);
115
off64_t CSSysFile::sf_getEOF()
119
if ((eof = lseek(sf_fh, 0, SEEK_END)) == ((off64_t)-1))
120
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);
126
void CSSysFile::sf_sync()
132
void CSSysFile::sf_lock(bool shared)
134
if (flock(sf_fh, (shared)?LOCK_SH:LOCK_EX) == -1)
135
CSException::throwOSError(CS_CONTEXT, errno);
139
void CSSysFile::sf_unlock()
141
if (flock(sf_fh, LOCK_UN) == -1)
142
CSException::throwOSError(CS_CONTEXT, errno);
145
//=====================
147
//=====================
149
bool CSSys::sys_exists(const char *path)
151
return (access(path, F_OK) != -1);
155
void CSSys::sys_makeDir(const char *path)
157
char super_path[PATH_MAX];
161
if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
162
CSException::throwFileError(CS_CONTEXT, path, errno);
164
// Set the access privileges.
165
ptr = cs_last_name_of_path(path);
167
strcpy(super_path, ".");
169
cs_strcpy(PATH_MAX, super_path, path);
171
if ((ptr = cs_last_name_of_path(super_path)))
175
if (stat(super_path, &stats) == -1)
176
CSException::throwFileError(CS_CONTEXT, path, errno);
178
if (chmod(path, stats.st_mode) == -1)
179
CSException::throwFileError(CS_CONTEXT, path, errno);
184
void CSSys::sys_removeDir(const char *path)
186
if (rmdir(path) == -1) {
190
CSException::throwFileError(CS_CONTEXT, path, err);
195
void CSSys::sys_removeFile(const char *path)
197
if (unlink(path) == -1) {
201
CSException::throwFileError(CS_CONTEXT, path, err);
208
void CSSys::sys_stat(const char *path, bool *is_dir, off64_t *size, CSTime *mod_time)
212
if (stat(path, &sb) == -1)
213
CSException::throwFileError(CS_CONTEXT, path, errno);
215
*is_dir = sb.st_mode & S_IFDIR;
220
/* This is the Linux version: */
221
mod_time->setUTC1970(sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
223
/* This is the Mac OS X version: */
224
mod_time->setUTC1970(sb.st_mtimespec.tv_sec, sb.st_mtimespec.tv_nsec);
229
bool CSSys::sys_isLink(const char *path)
233
if (lstat(path, &sb) == -1)
234
CSException::throwFileError(CS_CONTEXT, path, errno);
236
return S_ISLNK(sb.st_mode);
240
void CSSys::sys_rename(const char *old_path, const char *new_path)
242
if (rename(old_path, new_path) == -1)
243
CSException::throwFileError(CS_CONTEXT, old_path, errno);
247
void CSSys::sys_getcwd(char *path, size_t size)
249
if (getcwd(path, size) == NULL)
250
CSException::throwOSError(CS_CONTEXT, errno);
254
void CSSys::sys_setcwd(const char *path)
256
if (chdir(path) == -1)
257
CSException::throwFileError(CS_CONTEXT, path, errno);
261
uint32_t CSSys::sys_getpid()
267
bool CSSys::sys_isAlive(uint32_t pid)
269
return (kill(pid, 0) == 0);
272
//=====================
274
//=====================
275
CSSysDir::~CSSysDir()
282
sd_filter->release();
286
void CSSysDir::open()
289
if (!(sd_dir = opendir(sd_path->getCString())))
290
CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), errno);
295
void CSSysDir::close()
306
bool CSSysDir::next()
309
struct dirent *result;
313
err = readdir_r(sd_dir, &sd_entry, &result);
316
CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), err);
319
/* Filter out '.' and '..': */
320
if (sd_entry.d_name[0] == '.') {
321
if (sd_entry.d_name[1] == '.') {
322
if (sd_entry.d_name[2] == '\0')
326
if (sd_entry.d_name[1] == '\0')
332
return_(result ? true : false);
337
void CSSysDir::getEntryPath(char *path, size_t size)
339
cs_strcpy(size, path, sd_path->getCString());
340
cs_add_dir_char(size, path);
341
cs_strcat(size, path, sd_entry.d_name);
345
const char *CSSysDir::entryName()
347
return sd_entry.d_name;
351
bool CSSysDir::entryIsFile()
353
if (sd_entry.d_type & DT_DIR)
359
extern void unix_close(int h);
360
void unix_close(int h) {close(h);}