~drizzle-trunk/drizzle/development

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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Copyright (c) 2010 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Author: Barry Leslie
 *
 * 2010-02-05
 *
 * CORE SYSTEM:
 * Basic UNIX specific file I/O classes.
 *
 */

#include "CSConfig.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/file.h>
#include <unistd.h>
#include <signal.h>

#include "CSGlobal.h"
#include "CSDefs.h"
#include "CSStrUtil.h"
#include "CSSys.h"

#define CS_MASK		((S_IRUSR | S_IWUSR) | (S_IRGRP | S_IWGRP) | (S_IROTH))
//=====================
// CSSysFile
//=====================
bool CSSysFile::isDirNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
bool CSSysFile::isFileNotFound(CSException *e) { return e->getErrorCode() == ENOENT; }
bool CSSysFile::isDirExists(CSException *e) { return e->getErrorCode() == EEXIST; }

//--------------
void CSSysFile::sf_open(const char *path, bool readonly, bool create)
{
	int flags;
	
	flags = (readonly)?O_RDONLY:O_RDWR;
	
	if (create)
		flags |= O_CREAT;
		
	if (sf_fh != -1)
		sf_close();
	
	sf_path = CSString::newString(path);
	
	sf_fh = open(path, flags, CS_MASK);
	if (sf_fh == -1) {
		sf_path->release();
		sf_path = NULL;
		CSException::throwFileError(CS_CONTEXT, path, errno);
	}
}

//--------------
void CSSysFile::sf_close()
{
	if (sf_fh != -1) {
		close(sf_fh);
		sf_fh = -1;
		sf_path->release();
		sf_path = NULL;
	}	
}

//--------------
size_t CSSysFile::sf_pread(void *data, size_t size, off64_t offset)
{
	ssize_t read_size;
	
	read_size = pread(sf_fh, data, size, offset);
	if (read_size ==  -1)
		CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);

	return read_size;
}

//--------------
void CSSysFile::sf_pwrite(const void *data, size_t size, off64_t offset)
{
	size_t write_size;
	
	write_size = pwrite(sf_fh, data, size, offset);
	if (write_size != size)
		CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);

}

//--------------
void CSSysFile::sf_setEOF(off64_t offset)
{
	if (ftruncate(sf_fh, offset) == -1)
		CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);
}

//--------------
off64_t CSSysFile::sf_getEOF()
{
	off64_t eof;

	if ((eof = lseek(sf_fh, 0, SEEK_END)) == ((off64_t)-1))
		CSException::throwFileError(CS_CONTEXT, sf_path->getCString(), errno);

     return eof;
}

//--------------
void CSSysFile::sf_sync()
{
	fsync(sf_fh);
}

//--------------
void CSSysFile::sf_lock(bool shared)
{
	if (flock(sf_fh, (shared)?LOCK_SH:LOCK_EX) == -1)
		CSException::throwOSError(CS_CONTEXT, errno);
}

//--------------
void CSSysFile::sf_unlock()
{
	if (flock(sf_fh, LOCK_UN) == -1)
		CSException::throwOSError(CS_CONTEXT, errno);
}

//=====================
// CSSys
//=====================
//--------------
bool CSSys::sys_exists(const char *path)
{
	 return (access(path, F_OK) != -1);
}

//--------------
void CSSys::sys_makeDir(const char *path)
{
	char		super_path[PATH_MAX];
	struct stat	stats;
	char		*ptr;

	if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);

	// Set the access privileges.
	ptr = cs_last_name_of_path(path);
	if (ptr == path) 
		strcpy(super_path, ".");
	else {
		cs_strcpy(PATH_MAX, super_path, path);

		if ((ptr = cs_last_name_of_path(super_path)))
			*ptr = 0;
	}
	
	if (stat(super_path, &stats) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);

	if (chmod(path, stats.st_mode) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);

}

//--------------
void CSSys::sys_removeDir(const char *path)
{
	if (rmdir(path) == -1) {
		int err = errno;

		if (err != ENOENT)
			CSException::throwFileError(CS_CONTEXT, path, err);
	}
}

//--------------
void CSSys::sys_removeFile(const char *path)
{
	if (unlink(path) == -1) {
		int err = errno;

		if (err != ENOENT)
			CSException::throwFileError(CS_CONTEXT, path, err);
	}
}


//--------------

void CSSys::sys_stat(const char *path, bool *is_dir, off64_t *size, CSTime *mod_time)
{
	struct stat sb;

	if (stat(path, &sb) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);
	if (is_dir)
		*is_dir = sb.st_mode & S_IFDIR;
	if (size)
		*size = sb.st_size;
	if (mod_time)
#ifdef __USE_MISC
		/* This is the Linux version: */
		mod_time->setUTC1970(sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
#else
		/* This is the Mac OS X version: */
		mod_time->setUTC1970(sb.st_mtimespec.tv_sec, sb.st_mtimespec.tv_nsec);
#endif
}

//--------------
bool CSSys::sys_isLink(const char *path)
{
	struct stat sb;

	if (lstat(path, &sb) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);
		
	return S_ISLNK(sb.st_mode);
}

//--------------
void CSSys::sys_rename(const char *old_path, const char *new_path)
{
	 if (rename(old_path, new_path) == -1)
		CSException::throwFileError(CS_CONTEXT, old_path, errno);
}

//--------------
void CSSys::sys_getcwd(char *path, size_t size)
{
	if (getcwd(path, size) == NULL)
		CSException::throwOSError(CS_CONTEXT, errno);
}

//--------------
void CSSys::sys_setcwd(const char *path)
{
	if (chdir(path) == -1)
		CSException::throwFileError(CS_CONTEXT, path, errno);
}

//--------------
uint32_t CSSys::sys_getpid()
{
	return getpid();
}

//--------------
bool CSSys::sys_isAlive(uint32_t pid)
{
	return (kill(pid, 0) == 0);
}

//=====================
// CSSysDir
//=====================
CSSysDir::~CSSysDir()
{
	close();
	if (sd_path)
		sd_path->release();
		
	if (sd_filter)
		sd_filter->release();
}

//--------------
void CSSysDir::open()
{
	enter_();
	if (!(sd_dir = opendir(sd_path->getCString())))
		CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), errno);
	exit_();
}

//--------------
void CSSysDir::close()
{
	enter_();
	if (sd_dir) {
		closedir(sd_dir);
		sd_dir = NULL;
	}
	exit_();
}

//--------------
bool CSSysDir::next()
{
	int				err;
	struct dirent	*result;

	enter_();
	for (;;) {
		err = readdir_r(sd_dir, &sd_entry, &result);
		self->interrupted();
		if (err)
			CSException::throwFileError(CS_CONTEXT, sd_path->getCString(), err);
		if (!result)
			break;
		/* Filter out '.' and '..': */
		if (sd_entry.d_name[0] == '.') {
			if (sd_entry.d_name[1] == '.') {
				if (sd_entry.d_name[2] == '\0')
					continue;
			}
			else {
				if (sd_entry.d_name[1] == '\0')
					continue;
			}
		}
		break;
	}
	return_(result ? true : false);
}


//--------------
void CSSysDir::getEntryPath(char *path, size_t size)
{
	cs_strcpy(size, path, sd_path->getCString());
	cs_add_dir_char(size, path);
	cs_strcat(size, path, sd_entry.d_name);
}

//--------------
const char *CSSysDir::entryName()
{
	return sd_entry.d_name;
}

//--------------
bool CSSysDir::entryIsFile()
{
	if (sd_entry.d_type & DT_DIR)
		return false;
	return true;
}

//--------------
extern void unix_close(int h);
void unix_close(int h) {close(h);}