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.
34
#include "CSStrUtil.h"
37
#define CS_MASK ((S_IRUSR | S_IWUSR) | (S_IRGRP | S_IWGRP) | (S_IROTH))
38
//=====================
40
//=====================
41
static int get_win_error()
43
return (int) GetLastError();
46
bool CSSysFile::isDirNotFound(CSException *e) { return e->getErrorCode() == ERROR_PATH_NOT_FOUND; }
47
bool CSSysFile::isFileNotFound(CSException *e) { return e->getErrorCode() == ERROR_FILE_NOT_FOUND; }
48
bool CSSysFile::isDirExists(CSException *e) { return e->getErrorCode() == ERROR_ALREADY_EXISTS; }
52
void CSSysFile::sf_open(const char *path, bool readonly, bool create)
54
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), 0, 0 };
57
flags = (create)?OPEN_ALWAYS:OPEN_EXISTING;
59
if (sf_fh != INVALID_HANDLE_VALUE)
62
sf_path = CSString::newString(path);
66
readonly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE),
67
FILE_SHARE_READ | FILE_SHARE_WRITE,
70
FILE_FLAG_RANDOM_ACCESS,
73
if (sf_fh == INVALID_HANDLE_VALUE) {
76
CSException::throwFileError(CS_CONTEXT, path, get_win_error());
81
void CSSysFile::sf_close()
83
if (sf_fh != INVALID_HANDLE_VALUE) {
85
sf_fh = INVALID_HANDLE_VALUE;
92
size_t CSSysFile::sf_pread(void *data, size_t size, off64_t offset)
94
LARGE_INTEGER liDistanceToMove;
97
liDistanceToMove.QuadPart = offset;
98
if (!SetFilePointerEx(sf_fh, liDistanceToMove, NULL, FILE_BEGIN))
99
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
101
if (!ReadFile(sf_fh, data, size, &result, NULL))
102
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
104
return (size_t) result;
108
void CSSysFile::sf_pwrite(const void *data, size_t size, off64_t offset)
110
LARGE_INTEGER liDistanceToMove;
113
liDistanceToMove.QuadPart = offset;
114
if (!SetFilePointerEx(sf_fh, liDistanceToMove, NULL, FILE_BEGIN))
115
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
117
if (!WriteFile(sf_fh, data, size, &result, NULL))
118
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
121
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), ERROR_HANDLE_EOF);
126
void CSSysFile::sf_setEOF(off64_t offset)
128
LARGE_INTEGER liDistanceToMove;
130
liDistanceToMove.QuadPart = offset;
131
if (!SetFilePointerEx(sf_fh, liDistanceToMove, NULL, FILE_BEGIN))
132
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
134
if (!SetEndOfFile(sf_fh))
135
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
139
off64_t CSSysFile::sf_getEOF()
142
LARGE_INTEGER lpFileSize;
144
result = SetFilePointer(sf_fh, 0, NULL, FILE_END);
145
if (result == 0xFFFFFFFF)
146
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
148
if (!GetFileSizeEx(sf_fh, &lpFileSize))
149
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
151
return lpFileSize.QuadPart;
155
void CSSysFile::sf_sync()
157
if (!FlushFileBuffers(sf_fh))
158
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
162
void CSSysFile::sf_lock(bool shared)
164
OVERLAPPED overlap = {0};
166
if (!LockFileEx(sf_fh, (shared)? 0: LOCKFILE_EXCLUSIVE_LOCK, 0, 512, 0, &overlap))
167
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
171
void CSSysFile::sf_unlock()
173
OVERLAPPED overlap = {0};
175
if (!UnlockFileEx(sf_fh, 0, 512, 0, &overlap))
176
CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), get_win_error());
179
//=====================
181
//=====================
183
bool CSSys::sys_exists(const char *path)
185
return (access(path, 0) != -1);
189
void CSSys::sys_getcwd(char *path, size_t size)
193
len = GetCurrentDirectoryA(size, path);
195
CSException::throwFileError(CS_CONTEXT, "GetCurrentDirectory()" , get_win_error());
196
else if (len > (size -1))
197
CSException::throwFileError(CS_CONTEXT, "GetCurrentDirectory()overflow " , len);
202
void CSSys::sys_setcwd(const char *path)
204
if (!SetCurrentDirectoryA(path))
205
CSException::throwFileError(CS_CONTEXT, "SetCurrentDirectory()" , get_win_error());
209
void CSSys::sys_makeDir(const char *path)
211
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), 0, 0 };
213
if (!CreateDirectoryA(path, &sa))
214
CSException::throwFileError(CS_CONTEXT, path, get_win_error());
218
#define FILE_NOT_FOUND(x) ((x) == ERROR_FILE_NOT_FOUND || (x) == ERROR_PATH_NOT_FOUND)
220
void CSSys::sys_removeDir(const char *path)
222
if (!RemoveDirectoryA(path)) {
223
int err = get_win_error();
225
if (!FILE_NOT_FOUND(err))
226
CSException::throwFileError(CS_CONTEXT, path, err);
231
void CSSys::sys_removeFile(const char *path)
233
if (!DeleteFileA(path)) {
234
int err = get_win_error();
236
if (!FILE_NOT_FOUND(err))
237
CSException::throwFileError(CS_CONTEXT, path, err);
244
void CSSys::sys_stat(const char *path, bool *is_dir, off64_t *size, CSTime *mod_time)
247
BY_HANDLE_FILE_INFORMATION info;
248
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), 0, 0 };
256
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, // FILE_FLAG_BACKUP_SEMANTICS allows you to open directories.
259
if (fh == INVALID_HANDLE_VALUE) {
260
CSException::throwFileError(CS_CONTEXT, path, get_win_error());
263
if (!GetFileInformationByHandle(fh, &info)) {
265
CSException::throwFileError(CS_CONTEXT, path, get_win_error());
270
*is_dir = ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
273
*size = (off64_t) info.nFileSizeLow | (((off64_t) info.nFileSizeHigh) << 32);
277
FileTimeToSystemTime(&info.ftLastWriteTime, &st);
278
mod_time->setUTC(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds * 1000000);
284
bool CSSys::sys_isLink(const char *path)
286
CSException::throwFileError(CS_CONTEXT, "CSSys::sys_isLink() not implimented on windows.", -1);
291
void CSSys::sys_rename(const char *old_path, const char *new_path)
293
if (rename(old_path, new_path) == -1)
294
CSException::throwFileError(CS_CONTEXT, old_path, errno);
298
uint32_t CSSys::sys_getpid()
300
return GetCurrentProcessId();
304
bool CSSys::sys_isAlive(uint32_t pid)
307
bool isAlive = false;
310
h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
312
if (GetExitCodeProcess(h, &code) && (code == STILL_ACTIVE))
319
err = HRESULT_CODE(GetLastError());
320
if (err != ERROR_INVALID_PARAMETER)
323
fprintf(stderr, "ERROR CSSys::sys_isAlive(%d):OpenProcess %d\n", pid, err);
331
//=====================
333
//=====================
334
CSSysDir::~CSSysDir()
341
sd_filter->release();
345
void CSSysDir::open()
349
if (! CSSys::sys_exists(sd_path->getCString()))
350
CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), ERROR_PATH_NOT_FOUND);
352
sd_filter = new CSStringBuffer();
353
sd_filter->append(sd_path->getCString());
355
if (IS_DIR_CHAR(*(sd_filter->getBuffer(sd_filter->length()-1))))
356
sd_filter->append("*");
358
sd_filter->append(CS_DIR_DELIM"*");
364
void CSSysDir::close()
366
if (sd_dir != INVALID_HANDLE_VALUE) {
368
sd_dir = INVALID_HANDLE_VALUE;
373
bool CSSysDir::next()
378
if (sd_dir == INVALID_HANDLE_VALUE) {
379
sd_dir = FindFirstFileA(sd_filter->getCString(), &sd_entry);
380
if (sd_dir == INVALID_HANDLE_VALUE)
381
err = get_win_error();
384
if (!FindNextFileA(sd_dir, &sd_entry))
385
err = get_win_error();
389
if ((err != ERROR_NO_MORE_FILES) && (err != ERROR_FILE_NOT_FOUND)){
390
CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), err);
395
/* Filter out '.' and '..': */
396
if (sd_entry.cFileName[0] == '.') {
397
if (sd_entry.cFileName[1] == '.') {
398
if (sd_entry.cFileName[2] == '\0')
402
if (sd_entry.cFileName[1] == '\0')
414
void CSSysDir::getEntryPath(char *path, size_t size)
416
cs_strcpy(size, path, sd_path->getCString());
417
cs_add_dir_char(size, path);
418
cs_strcat(size, path, entryName());
422
const char *CSSysDir::entryName()
424
return (const char*) sd_entry.cFileName;
428
bool CSSysDir::entryIsFile()
430
if (sd_entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
435
///////////////////////////////////////////
436
// A windows version of gettimeofday() as taken from:
437
// http://www.suacommunity.com/dictionary/gettimeofday-entry.php
439
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
440
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
442
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
447
int tz_minuteswest; /* minutes W of Greenwich */
448
int tz_dsttime; /* type of dst correction */
451
// Definition of a gettimeofday function
453
int gettimeofday(struct timeval *tv, struct timezone *tz)
455
// Define a structure to receive the current Windows filetime
458
// Initialize the present time to 0 and the timezone to UTC
459
unsigned __int64 tmpres = 0;
460
static int tzflag = 0;
464
GetSystemTimeAsFileTime(&ft);
466
// The GetSystemTimeAsFileTime returns the number of 100 nanosecond
467
// intervals since Jan 1, 1601 in a structure. Copy the high bits to
468
// the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
469
tmpres |= ft.dwHighDateTime;
471
tmpres |= ft.dwLowDateTime;
473
// Convert to microseconds by dividing by 10
476
// The Unix epoch starts on Jan 1 1970. Need to subtract the difference
477
// in seconds from Jan 1 1601.
478
tmpres -= DELTA_EPOCH_IN_MICROSECS;
480
// Finally change microseconds to seconds and place in the seconds value.
481
// The modulus picks up the microseconds.
482
tv->tv_sec = (long)(tmpres / 1000000UL);
483
tv->tv_usec = (long)(tmpres % 1000000UL);
494
// Adjust for the timezone west of Greenwich
495
tz->tz_minuteswest = _timezone / 60;
496
tz->tz_dsttime = _daylight;