sedna 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGES → CHANGES.rdoc} +9 -0
- data/{README → README.rdoc} +23 -25
- data/Rakefile +32 -9
- data/ext/{extconf.rb → sedna/extconf.rb} +33 -21
- data/ext/{sedna.c → sedna/sedna.c} +48 -40
- data/test/sedna_test.rb +9 -9
- data/vendor/sedna/AUTHORS +18 -0
- data/vendor/sedna/COPYRIGHT +90 -0
- data/vendor/sedna/LICENSE +202 -0
- data/vendor/sedna/Makefile.include +423 -0
- data/vendor/sedna/Makefile.platform +31 -0
- data/vendor/sedna/depend.sed +48 -0
- data/vendor/sedna/driver/c/Makefile +98 -0
- data/vendor/sedna/driver/c/libsedna.c +1998 -0
- data/vendor/sedna/driver/c/libsedna.h +199 -0
- data/vendor/sedna/driver/c/sednamt.def +21 -0
- data/vendor/sedna/driver/c/sp_defs.h +186 -0
- data/vendor/sedna/kernel/common/FastXptrHash.cpp +101 -0
- data/vendor/sedna/kernel/common/IntHash.h +314 -0
- data/vendor/sedna/kernel/common/IntList.h +224 -0
- data/vendor/sedna/kernel/common/Makefile +30 -0
- data/vendor/sedna/kernel/common/SSMMsg.cpp +459 -0
- data/vendor/sedna/kernel/common/SSMMsg.h +142 -0
- data/vendor/sedna/kernel/common/XptrHash.h +435 -0
- data/vendor/sedna/kernel/common/argtable.c +972 -0
- data/vendor/sedna/kernel/common/argtable.h +896 -0
- data/vendor/sedna/kernel/common/base.cpp +339 -0
- data/vendor/sedna/kernel/common/base.h +226 -0
- data/vendor/sedna/kernel/common/bit_set.cpp +157 -0
- data/vendor/sedna/kernel/common/bit_set.h +55 -0
- data/vendor/sedna/kernel/common/commutil.h +67 -0
- data/vendor/sedna/kernel/common/config.h +62 -0
- data/vendor/sedna/kernel/common/counted_ptr.h +74 -0
- data/vendor/sedna/kernel/common/errdbg/ErrorCodes.java +1056 -0
- data/vendor/sedna/kernel/common/errdbg/Makefile +34 -0
- data/vendor/sedna/kernel/common/errdbg/assert.c +133 -0
- data/vendor/sedna/kernel/common/errdbg/d_printf.c +150 -0
- data/vendor/sedna/kernel/common/errdbg/d_printf.h +91 -0
- data/vendor/sedna/kernel/common/errdbg/error.codes +1743 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes.c +531 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes.h +549 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes_scm.scm +527 -0
- data/vendor/sedna/kernel/common/errdbg/event_log.c +956 -0
- data/vendor/sedna/kernel/common/errdbg/event_log.h +226 -0
- data/vendor/sedna/kernel/common/errdbg/exceptions.cpp +155 -0
- data/vendor/sedna/kernel/common/errdbg/exceptions.h +559 -0
- data/vendor/sedna/kernel/common/errdbg/gen_error_codes +0 -0
- data/vendor/sedna/kernel/common/errdbg/gen_error_codes.c +345 -0
- data/vendor/sedna/kernel/common/gmm.cpp +192 -0
- data/vendor/sedna/kernel/common/gmm.h +29 -0
- data/vendor/sedna/kernel/common/ipc_ops.cpp +435 -0
- data/vendor/sedna/kernel/common/ipc_ops.h +51 -0
- data/vendor/sedna/kernel/common/lfsGlobals.h +12 -0
- data/vendor/sedna/kernel/common/lm_base.h +90 -0
- data/vendor/sedna/kernel/common/mmgr/Makefile +11 -0
- data/vendor/sedna/kernel/common/mmgr/aset.c +1185 -0
- data/vendor/sedna/kernel/common/mmgr/mcxt.c +741 -0
- data/vendor/sedna/kernel/common/mmgr/memnodes.h +70 -0
- data/vendor/sedna/kernel/common/mmgr/memutils.h +145 -0
- data/vendor/sedna/kernel/common/mmgr/se_alloc.h +321 -0
- data/vendor/sedna/kernel/common/mmgr/track.c +214 -0
- data/vendor/sedna/kernel/common/pping.cpp +672 -0
- data/vendor/sedna/kernel/common/pping.h +119 -0
- data/vendor/sedna/kernel/common/rcv_test.cpp +273 -0
- data/vendor/sedna/kernel/common/rcv_test.h +19 -0
- data/vendor/sedna/kernel/common/sedna.c +128 -0
- data/vendor/sedna/kernel/common/sedna.h +49 -0
- data/vendor/sedna/kernel/common/sedna_ef.h +52 -0
- data/vendor/sedna/kernel/common/sm_vmm_data.h +144 -0
- data/vendor/sedna/kernel/common/sp.c +93 -0
- data/vendor/sedna/kernel/common/sp.h +36 -0
- data/vendor/sedna/kernel/common/st/Makefile +20 -0
- data/vendor/sedna/kernel/common/st/os_linux/stacktrace.c +213 -0
- data/vendor/sedna/kernel/common/st/os_nt/stacktrace.c +338 -0
- data/vendor/sedna/kernel/common/st/os_other/stacktrace.c +39 -0
- data/vendor/sedna/kernel/common/st/stacktrace.h +72 -0
- data/vendor/sedna/kernel/common/st/stacktrfmt.c +64 -0
- data/vendor/sedna/kernel/common/tr_debug.cpp +112 -0
- data/vendor/sedna/kernel/common/tr_debug.h +22 -0
- data/vendor/sedna/kernel/common/u/Makefile +14 -0
- data/vendor/sedna/kernel/common/u/u.c +268 -0
- data/vendor/sedna/kernel/common/u/u.h +715 -0
- data/vendor/sedna/kernel/common/u/uatomic.h +12 -0
- data/vendor/sedna/kernel/common/u/udl.h +31 -0
- data/vendor/sedna/kernel/common/u/uevent.c +406 -0
- data/vendor/sedna/kernel/common/u/uevent.h +71 -0
- data/vendor/sedna/kernel/common/u/ugnames.cpp +330 -0
- data/vendor/sedna/kernel/common/u/ugnames.h +134 -0
- data/vendor/sedna/kernel/common/u/uhash_map.h +77 -0
- data/vendor/sedna/kernel/common/u/uhdd.c +1018 -0
- data/vendor/sedna/kernel/common/u/uhdd.h +206 -0
- data/vendor/sedna/kernel/common/u/ummap.cpp +268 -0
- data/vendor/sedna/kernel/common/u/ummap.h +60 -0
- data/vendor/sedna/kernel/common/u/umutex.c +145 -0
- data/vendor/sedna/kernel/common/u/umutex.h +65 -0
- data/vendor/sedna/kernel/common/u/upipe.cpp +244 -0
- data/vendor/sedna/kernel/common/u/upipe.h +74 -0
- data/vendor/sedna/kernel/common/u/uprocess.c +767 -0
- data/vendor/sedna/kernel/common/u/uprocess.h +91 -0
- data/vendor/sedna/kernel/common/u/usafesync.h +41 -0
- data/vendor/sedna/kernel/common/u/usecurity.c +150 -0
- data/vendor/sedna/kernel/common/u/usecurity.h +55 -0
- data/vendor/sedna/kernel/common/u/usem.c +891 -0
- data/vendor/sedna/kernel/common/u/usem.h +83 -0
- data/vendor/sedna/kernel/common/u/ushm.c +222 -0
- data/vendor/sedna/kernel/common/u/ushm.h +46 -0
- data/vendor/sedna/kernel/common/u/usocket.c +541 -0
- data/vendor/sedna/kernel/common/u/usocket.h +118 -0
- data/vendor/sedna/kernel/common/u/usystem.c +57 -0
- data/vendor/sedna/kernel/common/u/usystem.h +46 -0
- data/vendor/sedna/kernel/common/u/uthread.c +259 -0
- data/vendor/sedna/kernel/common/u/uthread.h +95 -0
- data/vendor/sedna/kernel/common/u/utime.c +65 -0
- data/vendor/sedna/kernel/common/u/utime.h +40 -0
- data/vendor/sedna/kernel/common/u/uutils.c +142 -0
- data/vendor/sedna/kernel/common/u/uutils.h +65 -0
- data/vendor/sedna/kernel/common/ugc.cpp +156 -0
- data/vendor/sedna/kernel/common/ugc.h +15 -0
- data/vendor/sedna/kernel/common/utils.cpp +156 -0
- data/vendor/sedna/kernel/common/utils.h +133 -0
- data/vendor/sedna/kernel/common/version.c +16 -0
- data/vendor/sedna/kernel/common/version.h +21 -0
- data/vendor/sedna/kernel/common/wustructures.h +18 -0
- data/vendor/sedna/kernel/common/wutypes.h +34 -0
- data/vendor/sedna/kernel/common/xptr.cpp +17 -0
- data/vendor/sedna/kernel/common/xptr.h +211 -0
- data/vendor/sedna/ver +1 -0
- metadata +142 -14
@@ -0,0 +1,77 @@
|
|
1
|
+
/* Unfortunately hash_map is incompatible across
|
2
|
+
* windows and linux. The differenece is subtle
|
3
|
+
* (header file, namespace and the way to setup
|
4
|
+
* custom hash function) so we can hide these details
|
5
|
+
* with conditional defines.
|
6
|
+
*
|
7
|
+
* We define the following macroses:
|
8
|
+
*
|
9
|
+
* U_HASH_MAP(KEY_TYPE, VALUE_TYPE) - hash_map template instantiation
|
10
|
+
* with selected type parameters
|
11
|
+
*
|
12
|
+
* U_HASH_MAP_W_CUSTOM_HASH_FN(KEY_TYPE, VALUE_TYPE, HASH_FUNCTION) -
|
13
|
+
* hash_map template instantiation
|
14
|
+
* with selected type parameters
|
15
|
+
* and custom hash function;
|
16
|
+
* HASH_FUNCTION must be a class
|
17
|
+
* with overloaded operator
|
18
|
+
*
|
19
|
+
* size_t operator() (const KEY_TYPE &) const
|
20
|
+
*
|
21
|
+
* which computes a hash function of the
|
22
|
+
* given key
|
23
|
+
*
|
24
|
+
*/
|
25
|
+
|
26
|
+
#if (_MSC_VER > 1000)
|
27
|
+
#pragma once
|
28
|
+
#endif
|
29
|
+
|
30
|
+
#ifndef UHASH_MAP_H_INCLUDED
|
31
|
+
#define UHASH_MAP_H_INCLUDED
|
32
|
+
|
33
|
+
#ifdef _WIN32
|
34
|
+
|
35
|
+
#include <hash_map>
|
36
|
+
|
37
|
+
namespace __sedna_U
|
38
|
+
{
|
39
|
+
template<typename key_t,
|
40
|
+
typename hash_fn_t>
|
41
|
+
class hash_fn_2_hash_compare_adaptor:
|
42
|
+
public stdext::hash_compare<key_t>
|
43
|
+
{
|
44
|
+
hash_fn_t m_hash_fn;
|
45
|
+
public:
|
46
|
+
bool operator() (const key_t& kvala, const key_t& kvalb) const
|
47
|
+
{
|
48
|
+
return stdext::hash_compare<key_t>::operator() (kvala, kvalb);
|
49
|
+
}
|
50
|
+
|
51
|
+
size_t operator() (const key_t& kval) const
|
52
|
+
{
|
53
|
+
return m_hash_fn.operator () (kval);
|
54
|
+
}
|
55
|
+
};
|
56
|
+
};
|
57
|
+
|
58
|
+
#define U_HASH_MAP(KEY_TYPE, VALUE_TYPE) \
|
59
|
+
stdext::hash_map<KEY_TYPE, VALUE_TYPE>
|
60
|
+
|
61
|
+
#define U_HASH_MAP_W_CUSTOM_HASH_FN(KEY_TYPE, VALUE_TYPE, HASH_FUNCTION) \
|
62
|
+
stdext::hash_map<KEY_TYPE, VALUE_TYPE, __sedna_U:: \
|
63
|
+
hash_fn_2_hash_compare_adaptor<KEY_TYPE, HASH_FUNCTION> >
|
64
|
+
|
65
|
+
#else
|
66
|
+
|
67
|
+
#include <ext/hash_map>
|
68
|
+
|
69
|
+
#define U_HASH_MAP(KEY_TYPE, VALUE_TYPE) \
|
70
|
+
__gnu_cxx::hash_map<KEY_TYPE, VALUE_TYPE>
|
71
|
+
|
72
|
+
#define U_HASH_MAP_W_CUSTOM_HASH_FN(KEY_TYPE, VALUE_TYPE, HASH_FUNCTION) \
|
73
|
+
__gnu_cxx::hash_map<KEY_TYPE, VALUE_TYPE, HASH_FUNCTION>
|
74
|
+
|
75
|
+
#endif
|
76
|
+
|
77
|
+
#endif
|
@@ -0,0 +1,1018 @@
|
|
1
|
+
/*
|
2
|
+
* File: uhdd.cpp
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
#if (defined(__linux__))
|
7
|
+
#define _LARGEFILE64_SOURCE
|
8
|
+
#include <fcntl.h>
|
9
|
+
#endif
|
10
|
+
|
11
|
+
#if (defined(__cygwin__))
|
12
|
+
#define HAVE_DEFAULT_LARGEFILE_FUNCTIONS
|
13
|
+
#endif
|
14
|
+
|
15
|
+
#include "common/u/uhdd.h"
|
16
|
+
#include "common/errdbg/d_printf.h"
|
17
|
+
#include "common/u/uutils.h"
|
18
|
+
|
19
|
+
#ifdef _WIN32
|
20
|
+
#include <direct.h>
|
21
|
+
#else
|
22
|
+
#ifdef LINUX
|
23
|
+
#define _LINUX_CONFIG_H
|
24
|
+
#include <linux/unistd.h>
|
25
|
+
#include <linux/fs.h>
|
26
|
+
#endif
|
27
|
+
#include <fcntl.h>
|
28
|
+
#include <dirent.h>
|
29
|
+
#include <errno.h>
|
30
|
+
#include <libgen.h>
|
31
|
+
#endif
|
32
|
+
|
33
|
+
|
34
|
+
#ifdef _WIN32
|
35
|
+
#if (_MSC_VER <= 1200)
|
36
|
+
|
37
|
+
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
|
38
|
+
|
39
|
+
WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
|
40
|
+
|
41
|
+
WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
|
42
|
+
|
43
|
+
#endif
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#ifdef HAVE_DEFAULT_LARGEFILE_FUNCTIONS
|
47
|
+
#define O_LARGEFILE 0
|
48
|
+
#define lseek64 lseek
|
49
|
+
#define fstat64 fstat
|
50
|
+
#define ftruncate64 ftruncate
|
51
|
+
#define __off64_t off_t
|
52
|
+
#define stat64 stat
|
53
|
+
|
54
|
+
#include <machine/param.h>
|
55
|
+
|
56
|
+
#endif
|
57
|
+
|
58
|
+
#ifdef SunOS
|
59
|
+
#define __off64_t off64_t
|
60
|
+
#endif
|
61
|
+
|
62
|
+
|
63
|
+
UFile uCreateFile(const char *name, UShareMode share, UAccess accs, UFlag attr, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
64
|
+
{
|
65
|
+
#ifdef _WIN32
|
66
|
+
UFile fd = CreateFile(name, accs, share, sa, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | attr, NULL);
|
67
|
+
if (fd == U_INVALID_FD)
|
68
|
+
sys_call_error("CreateFile");
|
69
|
+
return fd;
|
70
|
+
#else
|
71
|
+
int fd;
|
72
|
+
USECURITY_ATTRIBUTES file_access_mode = U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK;
|
73
|
+
if (sa) file_access_mode = *sa;
|
74
|
+
fd = open(name, accs | O_CREAT | O_EXCL | O_LARGEFILE | attr, file_access_mode);
|
75
|
+
if (fd == -1)
|
76
|
+
{
|
77
|
+
sys_call_error("open");
|
78
|
+
return -1;
|
79
|
+
}
|
80
|
+
else if (fchmod(fd, file_access_mode) == -1)
|
81
|
+
{
|
82
|
+
sys_call_error("fchmod");
|
83
|
+
return -1;
|
84
|
+
}
|
85
|
+
|
86
|
+
return fd;
|
87
|
+
#endif
|
88
|
+
}
|
89
|
+
|
90
|
+
UFile uOpenFile(const char *name, UShareMode share, UAccess accs, UFlag attr, sys_call_error_fun fun)
|
91
|
+
{
|
92
|
+
#ifdef _WIN32
|
93
|
+
UFile fd = CreateFile(name, accs, share, NULL, OPEN_EXISTING, attr, NULL);
|
94
|
+
if (fd == U_INVALID_FD)
|
95
|
+
sys_call_error("CreateFile");
|
96
|
+
return fd;
|
97
|
+
#else
|
98
|
+
UFile fd = open(name, accs | O_LARGEFILE | attr, U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK);
|
99
|
+
if (fd == U_INVALID_FD)
|
100
|
+
sys_call_error("open");
|
101
|
+
return fd;
|
102
|
+
#endif
|
103
|
+
}
|
104
|
+
|
105
|
+
int uCloseFile(UFile fd, sys_call_error_fun fun)
|
106
|
+
{
|
107
|
+
#ifdef _WIN32
|
108
|
+
int res = CloseHandle(fd);
|
109
|
+
if (res == 0)
|
110
|
+
sys_call_error("CloseHandle");
|
111
|
+
return res;
|
112
|
+
#else
|
113
|
+
int res = close(fd);
|
114
|
+
if (res == -1)
|
115
|
+
sys_call_error("close");
|
116
|
+
return (res == -1 ? 0 : 1);
|
117
|
+
#endif
|
118
|
+
}
|
119
|
+
|
120
|
+
int uDeleteFile(const char *name, sys_call_error_fun fun)
|
121
|
+
{
|
122
|
+
#ifdef _WIN32
|
123
|
+
int res = DeleteFile(name);
|
124
|
+
if (res == 0)
|
125
|
+
sys_call_error("DeleteFile");
|
126
|
+
return res;
|
127
|
+
|
128
|
+
#else
|
129
|
+
int res = remove(name);
|
130
|
+
if (res == -1)
|
131
|
+
sys_call_error("remove");
|
132
|
+
return (res == -1 ? 0 : 1);
|
133
|
+
#endif
|
134
|
+
}
|
135
|
+
|
136
|
+
int uMoveFile(const char* old_name, const char* new_name, sys_call_error_fun fun)
|
137
|
+
{
|
138
|
+
#ifdef _WIN32
|
139
|
+
BOOL res = MoveFile(old_name, new_name);
|
140
|
+
|
141
|
+
if (res == 0)
|
142
|
+
sys_call_error("MoveFile");
|
143
|
+
|
144
|
+
return res;
|
145
|
+
#else
|
146
|
+
int res;
|
147
|
+
res = rename(old_name, new_name);
|
148
|
+
if (res != 0)
|
149
|
+
{
|
150
|
+
sys_call_error("rename");
|
151
|
+
return 0;
|
152
|
+
}
|
153
|
+
|
154
|
+
return 1;
|
155
|
+
#endif
|
156
|
+
}
|
157
|
+
|
158
|
+
int uDelDir(const char *dir, sys_call_error_fun fun)
|
159
|
+
{
|
160
|
+
#ifdef _WIN32
|
161
|
+
int res = _rmdir(dir);
|
162
|
+
if (res != 0)
|
163
|
+
sys_call_error("_rmdir");
|
164
|
+
return (res != 0 ? 0 : 1);
|
165
|
+
#else
|
166
|
+
int res = rmdir(dir);
|
167
|
+
if (res != 0)
|
168
|
+
sys_call_error("rmdir");
|
169
|
+
return (res != 0 ? 0 : 1);
|
170
|
+
#endif
|
171
|
+
}
|
172
|
+
|
173
|
+
/* If the function succeeds, the return value is nonzero. If the return value */
|
174
|
+
/* is nonzero and the number of bytes read is zero, the file pointer was beyond */
|
175
|
+
/* the current end of the file at the time of the read operation.*/
|
176
|
+
/* If the function fails, the return value is zero*/
|
177
|
+
int uReadFile(UFile fd, void *buf, int to_read, int *already_read, sys_call_error_fun fun)
|
178
|
+
{
|
179
|
+
#ifdef _WIN32
|
180
|
+
BOOL res = ReadFile(fd, buf, to_read, (LPDWORD) already_read, NULL);
|
181
|
+
if (res == 0)
|
182
|
+
sys_call_error("ReadFile");
|
183
|
+
return res;
|
184
|
+
#else
|
185
|
+
int res = read(fd, buf, to_read);
|
186
|
+
if (res == -1)
|
187
|
+
sys_call_error("read");
|
188
|
+
else
|
189
|
+
*already_read = res;
|
190
|
+
return (res == -1 ? 0 : 1);
|
191
|
+
#endif
|
192
|
+
}
|
193
|
+
|
194
|
+
/* If the function succeeds, the return value is nonzero.*/
|
195
|
+
/* If the function fails, the return value is zero.*/
|
196
|
+
int uWriteFile(UFile fd, const void *buf, int to_write, int *already_written, sys_call_error_fun fun)
|
197
|
+
{
|
198
|
+
#ifdef _WIN32
|
199
|
+
int written = 0;
|
200
|
+
BOOL res = 0;
|
201
|
+
|
202
|
+
if(to_write == 0)
|
203
|
+
{
|
204
|
+
*already_written = 0;
|
205
|
+
return 1;
|
206
|
+
}
|
207
|
+
while(written < to_write)
|
208
|
+
{
|
209
|
+
res = WriteFile(fd, (char*)buf + written, to_write - written, (LPDWORD) already_written, NULL);
|
210
|
+
if (res == 0)
|
211
|
+
{
|
212
|
+
sys_call_error("WriteFile");
|
213
|
+
return 0;
|
214
|
+
}
|
215
|
+
else
|
216
|
+
written += (*already_written);
|
217
|
+
}
|
218
|
+
*already_written = written;
|
219
|
+
return written;
|
220
|
+
#else
|
221
|
+
int res = 0, written = 0;
|
222
|
+
|
223
|
+
if(to_write == 0)
|
224
|
+
{
|
225
|
+
*already_written = 0;
|
226
|
+
return 1;
|
227
|
+
}
|
228
|
+
while(written < to_write)
|
229
|
+
{
|
230
|
+
res = write(fd, (char*)buf + written, to_write - written);
|
231
|
+
if (res == -1 || res == 0)
|
232
|
+
if (res == -1 && errno == EINTR)
|
233
|
+
continue;
|
234
|
+
else
|
235
|
+
{
|
236
|
+
sys_call_error(res == 0 ? "write, res=0" : "write");
|
237
|
+
return 0;
|
238
|
+
}
|
239
|
+
else
|
240
|
+
written += res;
|
241
|
+
}
|
242
|
+
*already_written = written;
|
243
|
+
return written;
|
244
|
+
#endif
|
245
|
+
}
|
246
|
+
|
247
|
+
/* If the function succeeds, the return value is nonzero.*/
|
248
|
+
/* If the function fails, the return value is zero.*/
|
249
|
+
int uSetFilePointer(UFile fd, __int64 offs, __int64 * res_pos, UFlag meth, sys_call_error_fun fun)
|
250
|
+
{
|
251
|
+
#ifdef _WIN32
|
252
|
+
LARGE_INTEGER _offs, _res_pos;
|
253
|
+
BOOL res;
|
254
|
+
_offs.QuadPart = offs;
|
255
|
+
res = SetFilePointerEx(fd, _offs, &_res_pos, meth);
|
256
|
+
if (res == 0)
|
257
|
+
sys_call_error("SetFilePointerEx");
|
258
|
+
if (res_pos)
|
259
|
+
*res_pos = _res_pos.QuadPart;
|
260
|
+
return res;
|
261
|
+
#else
|
262
|
+
__int64 _res_pos = lseek64(fd, offs, meth);
|
263
|
+
if (res_pos)
|
264
|
+
*res_pos = _res_pos;
|
265
|
+
if (_res_pos == (__off64_t) -1)
|
266
|
+
{
|
267
|
+
sys_call_error("lseek64");
|
268
|
+
return 0;
|
269
|
+
}
|
270
|
+
return 1;
|
271
|
+
#endif
|
272
|
+
}
|
273
|
+
|
274
|
+
/* If the function succeeds, the return value is nonzero.*/
|
275
|
+
/* If the function fails, the return value is zero.*/
|
276
|
+
int uSetEndOfFile(UFile fd, __int64 offs, UFlag meth, sys_call_error_fun fun)
|
277
|
+
{
|
278
|
+
#ifdef _WIN32
|
279
|
+
LARGE_INTEGER _offs, _res_pos;
|
280
|
+
BOOL res;
|
281
|
+
_offs.QuadPart = offs;
|
282
|
+
res = SetFilePointerEx(fd, _offs, &_res_pos, meth);
|
283
|
+
if (res == 0)
|
284
|
+
sys_call_error("SetFilePointerEx");
|
285
|
+
else
|
286
|
+
{
|
287
|
+
res = SetEndOfFile(fd);
|
288
|
+
if (res == 0)
|
289
|
+
sys_call_error("SetEndOfFile");
|
290
|
+
}
|
291
|
+
|
292
|
+
return res;
|
293
|
+
#else
|
294
|
+
if (meth == U_FILE_BEGIN)
|
295
|
+
{
|
296
|
+
if (ftruncate64(fd, offs) == -1)
|
297
|
+
{
|
298
|
+
sys_call_error("ftruncate64");
|
299
|
+
return 0;
|
300
|
+
}
|
301
|
+
}
|
302
|
+
else if (meth == U_FILE_END)
|
303
|
+
{
|
304
|
+
struct stat64 buf;
|
305
|
+
if (fstat64(fd, &buf) == -1)
|
306
|
+
{
|
307
|
+
sys_call_error("fstat64");
|
308
|
+
return 0;
|
309
|
+
}
|
310
|
+
if (ftruncate64(fd, buf.st_size + offs) == -1)
|
311
|
+
{
|
312
|
+
sys_call_error("ftruncate64");
|
313
|
+
return 0;
|
314
|
+
}
|
315
|
+
}
|
316
|
+
else if (meth == U_FILE_CURRENT)
|
317
|
+
{
|
318
|
+
__int64 cur_pos = lseek64(fd, offs, U_FILE_CURRENT);
|
319
|
+
if (cur_pos == (__off64_t) - 1)
|
320
|
+
{
|
321
|
+
sys_call_error("lseek64");
|
322
|
+
return 0;
|
323
|
+
}
|
324
|
+
|
325
|
+
if (ftruncate64(fd, cur_pos + offs) == -1)
|
326
|
+
{
|
327
|
+
sys_call_error("ftruncate64");
|
328
|
+
return 0;
|
329
|
+
}
|
330
|
+
}
|
331
|
+
else return 0;
|
332
|
+
|
333
|
+
return 1;
|
334
|
+
#endif
|
335
|
+
}
|
336
|
+
|
337
|
+
int uMkDir(const char *name, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
338
|
+
{
|
339
|
+
int res;
|
340
|
+
#ifdef _WIN32
|
341
|
+
res = CreateDirectory(name, NULL); /// WinAPI equivalent of what was here, namely "_mkdir(name)". The goal was not to use errno.
|
342
|
+
if (0 == res && GetLastError() != ERROR_ALREADY_EXISTS)
|
343
|
+
{
|
344
|
+
sys_call_error("CreateDirectory");
|
345
|
+
return 0;
|
346
|
+
}
|
347
|
+
return 1;
|
348
|
+
#else
|
349
|
+
USECURITY_ATTRIBUTES dir_access_mode = U_SEDNA_DIRECTORY_ACCESS_PERMISSIONS_MASK;
|
350
|
+
if (sa) dir_access_mode = *sa;
|
351
|
+
|
352
|
+
res = mkdir(name, dir_access_mode);
|
353
|
+
if (res == -1 && errno != EEXIST)
|
354
|
+
{
|
355
|
+
sys_call_error("mkdir");
|
356
|
+
return 0;
|
357
|
+
}
|
358
|
+
return 1;
|
359
|
+
#endif
|
360
|
+
}
|
361
|
+
|
362
|
+
int uIsFileExist(const char *name, sys_call_error_fun fun)
|
363
|
+
{
|
364
|
+
#ifdef _WIN32
|
365
|
+
if (GetFileAttributes(name) == INVALID_FILE_ATTRIBUTES)
|
366
|
+
{
|
367
|
+
sys_call_error("GetFileAttributes");
|
368
|
+
return 0;
|
369
|
+
}
|
370
|
+
return 1;
|
371
|
+
#else
|
372
|
+
struct stat64 buf;
|
373
|
+
if (stat64(name, &buf) == -1)
|
374
|
+
{
|
375
|
+
sys_call_error("stat64");
|
376
|
+
return 0;
|
377
|
+
}
|
378
|
+
return 1;
|
379
|
+
#endif
|
380
|
+
}
|
381
|
+
|
382
|
+
int uCopyFile(const char *existing_file, const char *new_file, int fail_if_exists, sys_call_error_fun fun)
|
383
|
+
{
|
384
|
+
#ifdef _WIN32
|
385
|
+
BOOL res = CopyFile(existing_file, new_file, fail_if_exists);
|
386
|
+
if (res == 0)
|
387
|
+
sys_call_error("CopyFile");
|
388
|
+
return res;
|
389
|
+
#else
|
390
|
+
|
391
|
+
#define BUFFLEN 32768
|
392
|
+
|
393
|
+
int c = 0;
|
394
|
+
int des = 0;
|
395
|
+
int src = 0;
|
396
|
+
char buf[BUFFLEN];
|
397
|
+
__int64 src_file_size = (__int64) 0;
|
398
|
+
|
399
|
+
if (fail_if_exists)
|
400
|
+
des = open(new_file, O_CREAT | O_EXCL | O_LARGEFILE | O_WRONLY | O_SYNC | O_TRUNC, U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK);
|
401
|
+
else
|
402
|
+
des = open(new_file, O_CREAT | O_LARGEFILE | O_WRONLY | O_SYNC | O_TRUNC, U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK);
|
403
|
+
|
404
|
+
if (des == -1)
|
405
|
+
{
|
406
|
+
sys_call_error("open");
|
407
|
+
return 0;
|
408
|
+
}
|
409
|
+
|
410
|
+
src = open(existing_file, O_LARGEFILE | O_RDONLY, U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK);
|
411
|
+
if (src == -1)
|
412
|
+
{
|
413
|
+
sys_call_error("open");
|
414
|
+
return 0;
|
415
|
+
}
|
416
|
+
|
417
|
+
if (uGetFileSize(src, &src_file_size, __sys_call_error) == 0)
|
418
|
+
return 0;
|
419
|
+
if (uSetEndOfFile(des, src_file_size, U_FILE_BEGIN, __sys_call_error) == 0)
|
420
|
+
return 0;
|
421
|
+
|
422
|
+
while ((c = read(src, buf, BUFFLEN)) > 0)
|
423
|
+
{
|
424
|
+
if (write(des, buf, c) != c)
|
425
|
+
{
|
426
|
+
sys_call_error("write");
|
427
|
+
return 0;
|
428
|
+
}
|
429
|
+
}
|
430
|
+
|
431
|
+
if (c < 0)
|
432
|
+
{
|
433
|
+
sys_call_error("read");
|
434
|
+
return 0;
|
435
|
+
}
|
436
|
+
return 1;
|
437
|
+
#endif
|
438
|
+
}
|
439
|
+
|
440
|
+
int uGetFileSize(UFile fd, __int64 * file_size, sys_call_error_fun fun)
|
441
|
+
{
|
442
|
+
#ifdef _WIN32
|
443
|
+
LARGE_INTEGER size;
|
444
|
+
BOOL res = GetFileSizeEx(fd, &size);
|
445
|
+
if (res == 0)
|
446
|
+
sys_call_error("GetFileSizeEx");
|
447
|
+
else
|
448
|
+
*file_size = size.QuadPart;
|
449
|
+
return res;
|
450
|
+
#else
|
451
|
+
struct stat64 buf;
|
452
|
+
if (fstat64(fd, &buf) == -1)
|
453
|
+
{
|
454
|
+
sys_call_error("fstat64");
|
455
|
+
return 0;
|
456
|
+
}
|
457
|
+
*file_size = buf.st_size;
|
458
|
+
return 1;
|
459
|
+
#endif
|
460
|
+
}
|
461
|
+
|
462
|
+
/* If the function succeeds, the return value is nonzero.*/
|
463
|
+
/* If the function fails, the return value is zero.*/
|
464
|
+
int uGetFileSizeByName(const char* name, __int64 * file_size, sys_call_error_fun fun)
|
465
|
+
{
|
466
|
+
int res;
|
467
|
+
UFile fd;
|
468
|
+
|
469
|
+
fd = uOpenFile(name,
|
470
|
+
U_SHARE_READ,
|
471
|
+
U_READ,
|
472
|
+
0,
|
473
|
+
__sys_call_error);
|
474
|
+
|
475
|
+
if (fd == U_INVALID_FD) return 0;
|
476
|
+
|
477
|
+
res = uGetFileSize(fd, file_size, __sys_call_error);
|
478
|
+
|
479
|
+
if (res == 0)
|
480
|
+
{
|
481
|
+
uCloseFile(fd, __sys_call_error);
|
482
|
+
return 0;
|
483
|
+
}
|
484
|
+
|
485
|
+
if (uCloseFile(fd, __sys_call_error) == 0) return 0;
|
486
|
+
|
487
|
+
return 1;
|
488
|
+
}
|
489
|
+
|
490
|
+
|
491
|
+
/* Retrieves information about the specified disk's sector size.
|
492
|
+
* sector_size - pointer to a variable that receives
|
493
|
+
* the number of bytes per sector.
|
494
|
+
* path - The directory of the disk for which information is to be returned.
|
495
|
+
* On Windows it must include drive specification (for example, C:\).
|
496
|
+
*/
|
497
|
+
int
|
498
|
+
uGetDiskSectorSize(int *sector_size, /* out */
|
499
|
+
const char *path, /* in */
|
500
|
+
sys_call_error_fun fun)
|
501
|
+
{
|
502
|
+
#ifdef _WIN32
|
503
|
+
|
504
|
+
char dbuf[4]; /* <letter><colon><slash><nul> */
|
505
|
+
BOOL res = 1;
|
506
|
+
|
507
|
+
/* To find the sector size, we call GetDiskFreeSpace, which expects a
|
508
|
+
* drive name like "d:\\".
|
509
|
+
*/
|
510
|
+
if (path != NULL && path[0] != '\0' && path[1] == ':') {
|
511
|
+
sprintf(dbuf, "%c:\\", path[0]);
|
512
|
+
|
513
|
+
res = GetDiskFreeSpace(dbuf, NULL, (LPDWORD) sector_size, NULL, NULL);
|
514
|
+
|
515
|
+
if (res == 0)
|
516
|
+
sys_call_error("GetDiskFreeSpace");
|
517
|
+
|
518
|
+
} else {
|
519
|
+
res = 0;
|
520
|
+
u_call_error("Can't get disk sector size. NULL, UNC or relative path was given.");
|
521
|
+
}
|
522
|
+
|
523
|
+
return res;
|
524
|
+
|
525
|
+
#else /* !_WIN32 */
|
526
|
+
|
527
|
+
#ifdef PREDEFINED_DISK_SECTOR_SIZE
|
528
|
+
*sector_size = PREDEFINED_DISK_SECTOR_SIZE;
|
529
|
+
return 1;
|
530
|
+
#else
|
531
|
+
#define DSS_BUF_SIZE 100
|
532
|
+
int fd;
|
533
|
+
struct stat path_buf, dev_buf;
|
534
|
+
struct dirent **dir;
|
535
|
+
int n = 0, i = 0;
|
536
|
+
char buf[DSS_BUF_SIZE];
|
537
|
+
|
538
|
+
if (geteuid() != 0)
|
539
|
+
{
|
540
|
+
*sector_size = PREDEFINED_DISK_SECTOR_SIZE;
|
541
|
+
return 1;
|
542
|
+
}
|
543
|
+
|
544
|
+
if (lstat(path, &path_buf) == -1)
|
545
|
+
{
|
546
|
+
sys_call_error("lstat");
|
547
|
+
return 0;
|
548
|
+
}
|
549
|
+
|
550
|
+
n = scandir("/dev", &dir, 0, alphasort);
|
551
|
+
if (n == -1)
|
552
|
+
{
|
553
|
+
sys_call_error("lstat");
|
554
|
+
return 0;
|
555
|
+
}
|
556
|
+
|
557
|
+
memset(buf, '\0', DSS_BUF_SIZE);
|
558
|
+
strcpy(buf, "/dev/");
|
559
|
+
|
560
|
+
for (i = 0; i < n; i++)
|
561
|
+
{
|
562
|
+
memset(buf + 5, '\0', DSS_BUF_SIZE - 5);
|
563
|
+
if (strlen(dir[i]->d_name) > DSS_BUF_SIZE - 6)
|
564
|
+
{
|
565
|
+
sys_call_error("<buffer overflow error>");
|
566
|
+
|
567
|
+
for (i = 0; i < n; i++)
|
568
|
+
free(dir[i]);
|
569
|
+
free(dir);
|
570
|
+
|
571
|
+
return 0;
|
572
|
+
}
|
573
|
+
strcpy(buf + 5, dir[i]->d_name);
|
574
|
+
if (lstat(buf, &dev_buf) == -1)
|
575
|
+
{
|
576
|
+
sys_call_error("lstat");
|
577
|
+
|
578
|
+
for (i = 0; i < n; i++)
|
579
|
+
free(dir[i]);
|
580
|
+
free(dir);
|
581
|
+
|
582
|
+
return 0;
|
583
|
+
}
|
584
|
+
|
585
|
+
if ((major(path_buf.st_dev) == major(dev_buf.st_rdev)) &&
|
586
|
+
(minor(path_buf.st_dev) == minor(dev_buf.st_rdev)) &&
|
587
|
+
(!S_ISCHR(dev_buf.st_mode)) &&
|
588
|
+
(strlen(dir[i]->d_name) > 1) &&
|
589
|
+
(dir[i]->d_name[0] == 'h') &&
|
590
|
+
(dir[i]->d_name[1] == 'd'))
|
591
|
+
break;
|
592
|
+
}
|
593
|
+
|
594
|
+
for (i = 0; i < n; i++)
|
595
|
+
free(dir[i]);
|
596
|
+
free(dir);
|
597
|
+
|
598
|
+
fd = open(buf, 0);
|
599
|
+
if (fd == -1)
|
600
|
+
{
|
601
|
+
sys_call_error("open");
|
602
|
+
return 0;
|
603
|
+
}
|
604
|
+
|
605
|
+
if (ioctl(fd, BLKSSZGET, sector_size) == -1)
|
606
|
+
{
|
607
|
+
sys_call_error("ioctl");
|
608
|
+
return 0;
|
609
|
+
}
|
610
|
+
|
611
|
+
return 1;
|
612
|
+
|
613
|
+
#endif /* PREDEFINED_DISK_SECTOR_SIZE */
|
614
|
+
#endif /* _WIN32 */
|
615
|
+
}
|
616
|
+
|
617
|
+
/*
|
618
|
+
* Creates a file with unique name.
|
619
|
+
* If the function succeeds, it returns nonzero. Else it returns 0.
|
620
|
+
* template we use:
|
621
|
+
*
|
622
|
+
* tmp.SESSION.NUMBER - under Windows, SESSION - session id, NUMBER - counter.
|
623
|
+
* tmpXXXXXX - under *nix. We use mkstemp() call to generate unique file.
|
624
|
+
* SE_MAX_TMP_FILE_NAME - defines maximum length of the temporary file name.
|
625
|
+
*
|
626
|
+
* Note! This function is session safe, but not thread safe.
|
627
|
+
* Note! Files generated by this function are not persistent and can be
|
628
|
+
* cleaned up when SM starts.
|
629
|
+
*/
|
630
|
+
int
|
631
|
+
uGetUniqueFileStruct(const char *dir, /* in */
|
632
|
+
struct file_struct *fs, /* out */
|
633
|
+
int sid, /* in */
|
634
|
+
sys_call_error_fun fun) /* in */
|
635
|
+
{
|
636
|
+
#ifdef _WIN32
|
637
|
+
char template[U_MAX_PATH];
|
638
|
+
WIN32_FIND_DATA find_data;
|
639
|
+
HANDLE handle;
|
640
|
+
USECURITY_ATTRIBUTES *sa;
|
641
|
+
uint8_t last = 0;
|
642
|
+
int tmp;
|
643
|
+
|
644
|
+
/* Check length of the total path ( + 1 for \ path delimiter) */
|
645
|
+
if (strlen(dir) + SE_MAX_TMP_FILE_NAME + 1 >= U_MAX_PATH) {
|
646
|
+
u_call_error("Too long temporary file path");
|
647
|
+
return 0;
|
648
|
+
}
|
649
|
+
|
650
|
+
sprintf(template,
|
651
|
+
"%s\\tmp.%.8"PRIx32".*",
|
652
|
+
dir,
|
653
|
+
(uint32_t)sid);
|
654
|
+
|
655
|
+
/* Try to find the first file */
|
656
|
+
handle = FindFirstFile(template, &find_data);
|
657
|
+
|
658
|
+
if (INVALID_HANDLE_VALUE != handle) {
|
659
|
+
/* Find the last file for this session ...
|
660
|
+
* (FAT): Since files are written one-by-one in
|
661
|
+
* alphabetical order, we expect that FindNextFile
|
662
|
+
* will return them in alphabetical order.
|
663
|
+
* (NTFS): Alphabetical order is guaranteed.
|
664
|
+
*/
|
665
|
+
do {
|
666
|
+
/* Save cFileName since we cannot expect
|
667
|
+
* FindNextFile doesn't change find_data.cFileName
|
668
|
+
* in case it returns 0 */
|
669
|
+
strcpy(template, find_data.cFileName);
|
670
|
+
|
671
|
+
} while (FindNextFile(handle, &find_data) != 0);
|
672
|
+
|
673
|
+
/* "tmp." + 8 symbols for session in hex + "." = 13 */
|
674
|
+
tmp = atoi(template + 13);
|
675
|
+
|
676
|
+
if(0 == FindClose(handle)) {
|
677
|
+
sys_call_error("FindClose");
|
678
|
+
return 0;
|
679
|
+
}
|
680
|
+
|
681
|
+
if (tmp >= UINT8_MAX) {
|
682
|
+
u_call_error("Temporary files limit number is exceeded");
|
683
|
+
return 0;
|
684
|
+
}
|
685
|
+
|
686
|
+
last = (uint8_t)tmp;
|
687
|
+
}
|
688
|
+
|
689
|
+
/* Generate the file name: tmp.SESSION.(LAST+1) */
|
690
|
+
sprintf(fs->name,
|
691
|
+
"%s\\tmp.%.8"PRIx32".%.3"PRIu8"",
|
692
|
+
dir,
|
693
|
+
(uint32_t)sid,
|
694
|
+
(last + 1));
|
695
|
+
|
696
|
+
if (uCreateSA(&sa, U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK, 0, __sys_call_error) != 0)
|
697
|
+
return 0;
|
698
|
+
|
699
|
+
/* Create and open the file. Just like mkstemp on *NIX does. */
|
700
|
+
fs->f = uCreateFile(fs->name, 0, U_READ_WRITE, 0, sa, __sys_call_error);
|
701
|
+
|
702
|
+
if (0 != uReleaseSA(sa, __sys_call_error) ||
|
703
|
+
INVALID_HANDLE_VALUE == fs->f)
|
704
|
+
return 0;
|
705
|
+
|
706
|
+
return 1;
|
707
|
+
#else /* !_WIN32 */
|
708
|
+
char template[U_MAX_PATH];
|
709
|
+
|
710
|
+
/* Check length of the total path ( + 1 for / symbol) */
|
711
|
+
if (strlen(dir) + SE_MAX_TMP_FILE_NAME + 1 >= U_MAX_PATH) {
|
712
|
+
u_call_error("Too long temporary file path");
|
713
|
+
return 0;
|
714
|
+
}
|
715
|
+
|
716
|
+
strcpy(template, dir);
|
717
|
+
strcat(template, "/tmpXXXXXX");
|
718
|
+
fs->f = mkstemp(template);
|
719
|
+
|
720
|
+
if (fs->f == -1) {
|
721
|
+
sys_call_error("mkstemp");
|
722
|
+
return 0;
|
723
|
+
}
|
724
|
+
|
725
|
+
strcpy(fs->name, template);
|
726
|
+
|
727
|
+
return 1;
|
728
|
+
#endif /*_WIN32 */
|
729
|
+
}
|
730
|
+
|
731
|
+
/*
|
732
|
+
* Cleanups files created by uGetUniqueFileStruct
|
733
|
+
* If the function succeeds, it returns nonzero. Else it returns 0.
|
734
|
+
* template we use:
|
735
|
+
*
|
736
|
+
* For simplicity function removes all "/tmp*" files located in
|
737
|
+
* the "dir" folder.
|
738
|
+
*/
|
739
|
+
int
|
740
|
+
uCleanupUniqueFileStructs(const char *dir, /* in */
|
741
|
+
sys_call_error_fun fun) /* in */
|
742
|
+
{
|
743
|
+
#ifdef _WIN32
|
744
|
+
char template[U_MAX_PATH];
|
745
|
+
WIN32_FIND_DATA find_data;
|
746
|
+
HANDLE handle;
|
747
|
+
|
748
|
+
/* Check length of the path ( + 1 for \ path delimiter) */
|
749
|
+
if (strlen(dir) + SE_MAX_TMP_FILE_NAME + 1 >= U_MAX_PATH) {
|
750
|
+
u_call_error("Too long temporary files path");
|
751
|
+
return 0;
|
752
|
+
}
|
753
|
+
|
754
|
+
sprintf(template, "%s\\tmp.*", dir);
|
755
|
+
|
756
|
+
/* Find the first file */
|
757
|
+
handle = FindFirstFile(template, &find_data);
|
758
|
+
|
759
|
+
if (INVALID_HANDLE_VALUE != handle) {
|
760
|
+
|
761
|
+
/* Delete the first and other files */
|
762
|
+
do
|
763
|
+
{
|
764
|
+
if(strlen(find_data.cFileName) <= SE_MAX_TMP_FILE_NAME)
|
765
|
+
{
|
766
|
+
sprintf(template, "%s\\%s", dir, find_data.cFileName);
|
767
|
+
|
768
|
+
if (DeleteFile(template) == 0) {
|
769
|
+
sys_call_error("DeleteFile");
|
770
|
+
FindClose(handle);
|
771
|
+
return 0;
|
772
|
+
}
|
773
|
+
}
|
774
|
+
} while (FindNextFile(handle, &find_data) != 0);
|
775
|
+
|
776
|
+
if(FindClose(handle) == 0) {
|
777
|
+
sys_call_error("FindClose");
|
778
|
+
return 0;
|
779
|
+
}
|
780
|
+
}
|
781
|
+
|
782
|
+
return 1;
|
783
|
+
#else /* !_WIN32 */
|
784
|
+
char name[U_MAX_PATH]; //buffer for absolute path
|
785
|
+
DIR* dd = NULL; //directory descriptor
|
786
|
+
struct dirent *dent;
|
787
|
+
|
788
|
+
/* Check length of the path ( + 1 for / path delimiter) */
|
789
|
+
if (strlen(dir) + SE_MAX_TMP_FILE_NAME + 1 >= U_MAX_PATH) {
|
790
|
+
u_call_error("Too long temporary files path");
|
791
|
+
return 0;
|
792
|
+
}
|
793
|
+
|
794
|
+
/* Open target directory */
|
795
|
+
if ((dd = opendir(dir)) == NULL) {
|
796
|
+
sys_call_error("opendir");
|
797
|
+
return 0;
|
798
|
+
}
|
799
|
+
|
800
|
+
while ((dent = readdir(dd)) != NULL)
|
801
|
+
{
|
802
|
+
/* Check that file is a temporary ... */
|
803
|
+
if (strlen(dent->d_name) > 3 &&
|
804
|
+
strlen(dent->d_name) <= SE_MAX_TMP_FILE_NAME &&
|
805
|
+
't' == dent->d_name[0] &&
|
806
|
+
'm' == dent->d_name[1] &&
|
807
|
+
'p' == dent->d_name[2])
|
808
|
+
{
|
809
|
+
/* ... and delete it */
|
810
|
+
sprintf(name, "%s/%s", dir, dent->d_name);
|
811
|
+
if (remove(name) == -1) {
|
812
|
+
sys_call_error("remove");
|
813
|
+
closedir(dd);
|
814
|
+
return 0;
|
815
|
+
}
|
816
|
+
}
|
817
|
+
}
|
818
|
+
|
819
|
+
if (closedir(dd) != 0) {
|
820
|
+
sys_call_error("closedir");
|
821
|
+
return 0;
|
822
|
+
}
|
823
|
+
|
824
|
+
return 1;
|
825
|
+
#endif /*_WIN32 */
|
826
|
+
}
|
827
|
+
|
828
|
+
|
829
|
+
|
830
|
+
char *uGetAbsoluteFilePath(const char *relPath, char *absPath, int maxLength, sys_call_error_fun fun)
|
831
|
+
{
|
832
|
+
#ifdef _WIN32
|
833
|
+
char *p = _fullpath(absPath, relPath, maxLength);
|
834
|
+
if (p == NULL)
|
835
|
+
sys_call_error("_fullpath");
|
836
|
+
return p;
|
837
|
+
#else
|
838
|
+
char *p = realpath(relPath, absPath);
|
839
|
+
if (p == NULL)
|
840
|
+
sys_call_error("realpath");
|
841
|
+
return p;
|
842
|
+
#endif
|
843
|
+
}
|
844
|
+
|
845
|
+
char *uGetCurrentWorkingDirectory(char *buf, int maxLength, sys_call_error_fun fun)
|
846
|
+
{
|
847
|
+
#ifdef _WIN32
|
848
|
+
char *p = _getcwd(buf, maxLength);
|
849
|
+
if (p == NULL)
|
850
|
+
sys_call_error("_getcwd");
|
851
|
+
return p;
|
852
|
+
#else
|
853
|
+
char *p = getcwd(buf, maxLength);
|
854
|
+
if (p == NULL)
|
855
|
+
sys_call_error("getcwd");
|
856
|
+
return p;
|
857
|
+
|
858
|
+
#endif
|
859
|
+
}
|
860
|
+
|
861
|
+
int uChangeWorkingDirectory(const char *path, sys_call_error_fun fun)
|
862
|
+
{
|
863
|
+
#ifdef _WIN32
|
864
|
+
int res = _chdir(path);
|
865
|
+
if (res != 0)
|
866
|
+
sys_call_error("_chdir");
|
867
|
+
return res;
|
868
|
+
#else
|
869
|
+
int res = chdir(path);
|
870
|
+
if (res != 0)
|
871
|
+
sys_call_error("chdir");
|
872
|
+
return res;
|
873
|
+
#endif
|
874
|
+
}
|
875
|
+
|
876
|
+
char *uGetDirectoryFromFilePath(const char *path, char *buf, int buf_len, sys_call_error_fun fun)
|
877
|
+
{
|
878
|
+
#ifdef _WIN32
|
879
|
+
char drive[_MAX_DRIVE];
|
880
|
+
char dir[_MAX_DIR];
|
881
|
+
|
882
|
+
_splitpath(path, drive, dir, NULL, NULL);
|
883
|
+
if (strlen(drive) + strlen(dir) + 1 > buf_len)
|
884
|
+
{
|
885
|
+
return NULL;
|
886
|
+
}
|
887
|
+
strcpy(buf, drive);
|
888
|
+
strcat(buf, dir);
|
889
|
+
|
890
|
+
return buf;
|
891
|
+
#else
|
892
|
+
char *tmp_path = NULL, *dir_name = NULL;
|
893
|
+
tmp_path = strdup(path);
|
894
|
+
dir_name = dirname(tmp_path);
|
895
|
+
if (strlen(dir_name) + 1 > buf_len)
|
896
|
+
{
|
897
|
+
free(tmp_path);
|
898
|
+
return NULL;
|
899
|
+
}
|
900
|
+
strcpy(buf, dir_name);
|
901
|
+
free(tmp_path);
|
902
|
+
|
903
|
+
return buf;
|
904
|
+
#endif
|
905
|
+
}
|
906
|
+
|
907
|
+
char *uGetFileNameFromFilePath(const char *path, char *buf, int buf_len, sys_call_error_fun fun)
|
908
|
+
{
|
909
|
+
#ifdef _WIN32
|
910
|
+
char fname[_MAX_FNAME];
|
911
|
+
char ext[_MAX_EXT];
|
912
|
+
|
913
|
+
_splitpath(path, NULL, NULL, fname, ext);
|
914
|
+
if (strlen(fname) + strlen(ext) + 1 > buf_len)
|
915
|
+
{
|
916
|
+
return NULL;
|
917
|
+
}
|
918
|
+
strcpy(buf, fname);
|
919
|
+
strcat(buf, ext);
|
920
|
+
|
921
|
+
return buf;
|
922
|
+
#else
|
923
|
+
char *tmp_path = NULL, *file_name = NULL;
|
924
|
+
tmp_path = strdup(path);
|
925
|
+
file_name = basename(tmp_path);
|
926
|
+
if (strlen(file_name) + 1 > buf_len)
|
927
|
+
{
|
928
|
+
free(tmp_path);
|
929
|
+
return NULL;
|
930
|
+
}
|
931
|
+
strcpy(buf, file_name);
|
932
|
+
free(tmp_path);
|
933
|
+
|
934
|
+
return buf;
|
935
|
+
#endif
|
936
|
+
}
|
937
|
+
|
938
|
+
UDir uFindFirstFile(const char* dir_name, struct UFindDataStruct* find_data, sys_call_error_fun fun)
|
939
|
+
{
|
940
|
+
#ifdef _WIN32
|
941
|
+
HANDLE res;
|
942
|
+
WIN32_FIND_DATA _find_data;
|
943
|
+
char buf[U_MAX_PATH + 3];
|
944
|
+
strcpy(buf, dir_name);
|
945
|
+
strcat(buf, "\\*");
|
946
|
+
res = FindFirstFile(buf, &_find_data);
|
947
|
+
if (res == INVALID_HANDLE_VALUE)
|
948
|
+
{
|
949
|
+
sys_call_error("FindFirstFile");
|
950
|
+
return res;
|
951
|
+
}
|
952
|
+
else
|
953
|
+
{
|
954
|
+
strcpy(find_data->fname, _find_data.cFileName);
|
955
|
+
return res;
|
956
|
+
}
|
957
|
+
#else
|
958
|
+
DIR* dir;
|
959
|
+
|
960
|
+
if ((dir = opendir(dir_name)) != NULL)
|
961
|
+
{
|
962
|
+
struct dirent *dent;
|
963
|
+
dent = readdir(dir);
|
964
|
+
if (dent == NULL)
|
965
|
+
{
|
966
|
+
sys_call_error("readdir");
|
967
|
+
return NULL;
|
968
|
+
}
|
969
|
+
strcpy(find_data->fname, dent->d_name);
|
970
|
+
}
|
971
|
+
|
972
|
+
return dir;
|
973
|
+
#endif
|
974
|
+
}
|
975
|
+
|
976
|
+
// returns 0 if there is no more files in directory
|
977
|
+
// returns 1 if find_data is fulfilled by next file information
|
978
|
+
// returns -1 in case of errors
|
979
|
+
int uFindNextFile(UDir dir, struct UFindDataStruct* find_data, sys_call_error_fun fun)
|
980
|
+
{
|
981
|
+
#ifdef _WIN32
|
982
|
+
BOOL res;
|
983
|
+
WIN32_FIND_DATA _find_data;
|
984
|
+
res = FindNextFile(dir, &_find_data);
|
985
|
+
if (res != 0)
|
986
|
+
{
|
987
|
+
strcpy(find_data->fname, _find_data.cFileName);
|
988
|
+
return 1;
|
989
|
+
}
|
990
|
+
else
|
991
|
+
{
|
992
|
+
if (GetLastError() == ERROR_NO_MORE_FILES) return 0;
|
993
|
+
else return -1;
|
994
|
+
}
|
995
|
+
#else
|
996
|
+
struct dirent *dent;
|
997
|
+
dent = readdir(dir);
|
998
|
+
if (dent == NULL) return 0;
|
999
|
+
|
1000
|
+
strcpy(find_data->fname, dent->d_name);
|
1001
|
+
return 1;
|
1002
|
+
#endif
|
1003
|
+
}
|
1004
|
+
|
1005
|
+
//1 - success, 0 - fail
|
1006
|
+
int uFindClose(UDir dir, sys_call_error_fun fun)
|
1007
|
+
{
|
1008
|
+
#ifdef _WIN32
|
1009
|
+
BOOL res;
|
1010
|
+
res = FindClose(dir);
|
1011
|
+
if (res != 0) return 1;
|
1012
|
+
else return 0;
|
1013
|
+
#else
|
1014
|
+
int res;
|
1015
|
+
res = closedir(dir);
|
1016
|
+
return (res == 0)? 1: 0;
|
1017
|
+
#endif
|
1018
|
+
}
|