|
bb0cedb…
|
drh
|
1 |
/* |
|
bb0cedb…
|
drh
|
2 |
** Copyright (c) 2006 D. Richard Hipp |
|
bb0cedb…
|
drh
|
3 |
** |
|
bb0cedb…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
bb0cedb…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
bb0cedb…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
bb0cedb…
|
drh
|
7 |
|
|
bb0cedb…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
bb0cedb…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
bb0cedb…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
bb0cedb…
|
drh
|
11 |
** |
|
bb0cedb…
|
drh
|
12 |
** Author contact information: |
|
bb0cedb…
|
drh
|
13 |
** [email protected] |
|
bb0cedb…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
bb0cedb…
|
drh
|
15 |
** |
|
bb0cedb…
|
drh
|
16 |
******************************************************************************* |
|
bb0cedb…
|
drh
|
17 |
** |
|
bb0cedb…
|
drh
|
18 |
** This file contains code for miscellaneous utility routines. |
|
bb0cedb…
|
drh
|
19 |
*/ |
|
bb0cedb…
|
drh
|
20 |
#include "config.h" |
|
bb0cedb…
|
drh
|
21 |
#include "util.h" |
|
d5ca538…
|
mistachkin
|
22 |
#if defined(USE_MMAN_H) |
|
d5ca538…
|
mistachkin
|
23 |
# include <sys/mman.h> |
|
d5ca538…
|
mistachkin
|
24 |
# include <unistd.h> |
|
d5ca538…
|
mistachkin
|
25 |
#endif |
|
931b97e…
|
drh
|
26 |
#include <math.h> |
|
e28433a…
|
stephan
|
27 |
|
|
e28433a…
|
stephan
|
28 |
/* |
|
e28433a…
|
stephan
|
29 |
** For the fossil_timer_xxx() family of functions... |
|
e28433a…
|
stephan
|
30 |
*/ |
|
e28433a…
|
stephan
|
31 |
#ifdef _WIN32 |
|
e28433a…
|
stephan
|
32 |
# include <windows.h> |
|
898a70c…
|
stephan
|
33 |
# include <io.h> |
|
e28433a…
|
stephan
|
34 |
#else |
|
e28433a…
|
stephan
|
35 |
# include <sys/time.h> |
|
e28433a…
|
stephan
|
36 |
# include <sys/resource.h> |
|
9396966…
|
drh
|
37 |
# include <sys/types.h> |
|
9396966…
|
drh
|
38 |
# include <sys/stat.h> |
|
4727ef4…
|
drh
|
39 |
# include <unistd.h> |
|
4727ef4…
|
drh
|
40 |
# include <fcntl.h> |
|
4727ef4…
|
drh
|
41 |
# include <errno.h> |
|
4727ef4…
|
drh
|
42 |
#endif |
|
898a70c…
|
stephan
|
43 |
|
|
898a70c…
|
stephan
|
44 |
/* |
|
898a70c…
|
stephan
|
45 |
** Returns the same as the platform's isatty() or _isatty() function. |
|
898a70c…
|
stephan
|
46 |
*/ |
|
898a70c…
|
stephan
|
47 |
int fossil_isatty(int fd){ |
|
898a70c…
|
stephan
|
48 |
#ifdef _WIN32 |
|
898a70c…
|
stephan
|
49 |
return _isatty(fd); |
|
898a70c…
|
stephan
|
50 |
#else |
|
898a70c…
|
stephan
|
51 |
return isatty(fd); |
|
898a70c…
|
stephan
|
52 |
#endif |
|
898a70c…
|
stephan
|
53 |
} |
|
898a70c…
|
stephan
|
54 |
|
|
898a70c…
|
stephan
|
55 |
/* |
|
898a70c…
|
stephan
|
56 |
** Returns the same as the platform's fileno() or _fileno() function. |
|
898a70c…
|
stephan
|
57 |
*/ |
|
898a70c…
|
stephan
|
58 |
int fossil_fileno(FILE *p){ |
|
898a70c…
|
stephan
|
59 |
#ifdef _WIN32 |
|
898a70c…
|
stephan
|
60 |
return _fileno(p); |
|
898a70c…
|
stephan
|
61 |
#else |
|
898a70c…
|
stephan
|
62 |
return fileno(p); |
|
898a70c…
|
stephan
|
63 |
#endif |
|
898a70c…
|
stephan
|
64 |
} |
|
bb0cedb…
|
drh
|
65 |
|
|
bb0cedb…
|
drh
|
66 |
/* |
|
bb0cedb…
|
drh
|
67 |
** Exit. Take care to close the database first. |
|
bb0cedb…
|
drh
|
68 |
*/ |
|
bb0cedb…
|
drh
|
69 |
NORETURN void fossil_exit(int rc){ |
|
bb0cedb…
|
drh
|
70 |
db_close(1); |
|
83b171b…
|
drh
|
71 |
#ifndef _WIN32 |
|
83b171b…
|
drh
|
72 |
if( g.fAnyTrace ){ |
|
83b171b…
|
drh
|
73 |
fprintf(stderr, "/***** Subprocess %d exit(%d) *****/\n", getpid(), rc); |
|
83b171b…
|
drh
|
74 |
fflush(stderr); |
|
83b171b…
|
drh
|
75 |
} |
|
83b171b…
|
drh
|
76 |
#endif |
|
bb0cedb…
|
drh
|
77 |
exit(rc); |
|
bb0cedb…
|
drh
|
78 |
} |
|
bb0cedb…
|
drh
|
79 |
|
|
bb0cedb…
|
drh
|
80 |
/* |
|
bb0cedb…
|
drh
|
81 |
** Malloc and free routines that cannot fail |
|
bb0cedb…
|
drh
|
82 |
*/ |
|
bb0cedb…
|
drh
|
83 |
void *fossil_malloc(size_t n){ |
|
bb0cedb…
|
drh
|
84 |
void *p = malloc(n==0 ? 1 : n); |
|
31c7bdb…
|
larrybr
|
85 |
if( p==0 ) fossil_fatal("out of memory"); |
|
9b4f2d8…
|
drh
|
86 |
return p; |
|
9b4f2d8…
|
drh
|
87 |
} |
|
9b4f2d8…
|
drh
|
88 |
void *fossil_malloc_zero(size_t n){ |
|
9b4f2d8…
|
drh
|
89 |
void *p = malloc(n==0 ? 1 : n); |
|
31c7bdb…
|
larrybr
|
90 |
if( p==0 ) fossil_fatal("out of memory"); |
|
9b4f2d8…
|
drh
|
91 |
memset(p, 0, n); |
|
bb0cedb…
|
drh
|
92 |
return p; |
|
bb0cedb…
|
drh
|
93 |
} |
|
bb0cedb…
|
drh
|
94 |
void fossil_free(void *p){ |
|
bb0cedb…
|
drh
|
95 |
free(p); |
|
bb0cedb…
|
drh
|
96 |
} |
|
bb0cedb…
|
drh
|
97 |
void *fossil_realloc(void *p, size_t n){ |
|
bb0cedb…
|
drh
|
98 |
p = realloc(p, n); |
|
31c7bdb…
|
larrybr
|
99 |
if( p==0 ) fossil_fatal("out of memory"); |
|
a8484dc…
|
mistachkin
|
100 |
return p; |
|
a8484dc…
|
mistachkin
|
101 |
} |
|
a8484dc…
|
mistachkin
|
102 |
void fossil_secure_zero(void *p, size_t n){ |
|
a8484dc…
|
mistachkin
|
103 |
volatile unsigned char *vp = (volatile unsigned char *)p; |
|
a8484dc…
|
mistachkin
|
104 |
size_t i; |
|
a8484dc…
|
mistachkin
|
105 |
|
|
a8484dc…
|
mistachkin
|
106 |
if( p==0 ) return; |
|
a8484dc…
|
mistachkin
|
107 |
assert( n>0 ); |
|
a8484dc…
|
mistachkin
|
108 |
if( n==0 ) return; |
|
a8484dc…
|
mistachkin
|
109 |
for(i=0; i<n; i++){ vp[i] ^= 0xFF; } |
|
a8484dc…
|
mistachkin
|
110 |
for(i=0; i<n; i++){ vp[i] ^= vp[i]; } |
|
a8484dc…
|
mistachkin
|
111 |
} |
|
a8484dc…
|
mistachkin
|
112 |
void fossil_get_page_size(size_t *piPageSize){ |
|
a8484dc…
|
mistachkin
|
113 |
#if defined(_WIN32) |
|
a8484dc…
|
mistachkin
|
114 |
SYSTEM_INFO sysInfo; |
|
a8484dc…
|
mistachkin
|
115 |
memset(&sysInfo, 0, sizeof(SYSTEM_INFO)); |
|
a8484dc…
|
mistachkin
|
116 |
GetSystemInfo(&sysInfo); |
|
a8484dc…
|
mistachkin
|
117 |
*piPageSize = (size_t)sysInfo.dwPageSize; |
|
d5ca538…
|
mistachkin
|
118 |
#elif defined(USE_MMAN_H) |
|
d5ca538…
|
mistachkin
|
119 |
*piPageSize = (size_t)sysconf(_SC_PAGE_SIZE); |
|
a8484dc…
|
mistachkin
|
120 |
#else |
|
a8484dc…
|
mistachkin
|
121 |
*piPageSize = 4096; /* FIXME: What for POSIX? */ |
|
a8484dc…
|
mistachkin
|
122 |
#endif |
|
a8484dc…
|
mistachkin
|
123 |
} |
|
a8484dc…
|
mistachkin
|
124 |
void *fossil_secure_alloc_page(size_t *pN){ |
|
a8484dc…
|
mistachkin
|
125 |
void *p; |
|
e2a90ed…
|
mistachkin
|
126 |
size_t pageSize = 0; |
|
a8484dc…
|
mistachkin
|
127 |
|
|
a8484dc…
|
mistachkin
|
128 |
fossil_get_page_size(&pageSize); |
|
a8484dc…
|
mistachkin
|
129 |
assert( pageSize>0 ); |
|
a8484dc…
|
mistachkin
|
130 |
assert( pageSize%2==0 ); |
|
a8484dc…
|
mistachkin
|
131 |
#if defined(_WIN32) |
|
a8484dc…
|
mistachkin
|
132 |
p = VirtualAlloc(NULL, pageSize, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); |
|
a8484dc…
|
mistachkin
|
133 |
if( p==NULL ){ |
|
31c7bdb…
|
larrybr
|
134 |
fossil_fatal("VirtualAlloc failed: %lu\n", GetLastError()); |
|
a8484dc…
|
mistachkin
|
135 |
} |
|
a8484dc…
|
mistachkin
|
136 |
if( !VirtualLock(p, pageSize) ){ |
|
31c7bdb…
|
larrybr
|
137 |
fossil_fatal("VirtualLock failed: %lu\n", GetLastError()); |
|
e2a90ed…
|
mistachkin
|
138 |
} |
|
d5ca538…
|
mistachkin
|
139 |
#elif defined(USE_MMAN_H) |
|
d5ca538…
|
mistachkin
|
140 |
p = mmap(0, pageSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
|
d5ca538…
|
mistachkin
|
141 |
if( p==MAP_FAILED ){ |
|
31c7bdb…
|
larrybr
|
142 |
fossil_fatal("mmap failed: %d\n", errno); |
|
d5ca538…
|
mistachkin
|
143 |
} |
|
d5ca538…
|
mistachkin
|
144 |
if( mlock(p, pageSize) ){ |
|
31c7bdb…
|
larrybr
|
145 |
fossil_fatal("mlock failed: %d\n", errno); |
|
d5ca538…
|
mistachkin
|
146 |
} |
|
a8484dc…
|
mistachkin
|
147 |
#else |
|
a8484dc…
|
mistachkin
|
148 |
p = fossil_malloc(pageSize); |
|
a8484dc…
|
mistachkin
|
149 |
#endif |
|
a8484dc…
|
mistachkin
|
150 |
fossil_secure_zero(p, pageSize); |
|
a8484dc…
|
mistachkin
|
151 |
if( pN ) *pN = pageSize; |
|
bb0cedb…
|
drh
|
152 |
return p; |
|
a8484dc…
|
mistachkin
|
153 |
} |
|
a8484dc…
|
mistachkin
|
154 |
void fossil_secure_free_page(void *p, size_t n){ |
|
a8484dc…
|
mistachkin
|
155 |
if( !p ) return; |
|
a8484dc…
|
mistachkin
|
156 |
assert( n>0 ); |
|
a8484dc…
|
mistachkin
|
157 |
fossil_secure_zero(p, n); |
|
a8484dc…
|
mistachkin
|
158 |
#if defined(_WIN32) |
|
a8484dc…
|
mistachkin
|
159 |
if( !VirtualUnlock(p, n) ){ |
|
3f5ab71…
|
drh
|
160 |
fossil_panic("VirtualUnlock failed: %lu\n", GetLastError()); |
|
a8484dc…
|
mistachkin
|
161 |
} |
|
a8484dc…
|
mistachkin
|
162 |
if( !VirtualFree(p, 0, MEM_RELEASE) ){ |
|
3f5ab71…
|
drh
|
163 |
fossil_panic("VirtualFree failed: %lu\n", GetLastError()); |
|
d5ca538…
|
mistachkin
|
164 |
} |
|
d5ca538…
|
mistachkin
|
165 |
#elif defined(USE_MMAN_H) |
|
d5ca538…
|
mistachkin
|
166 |
if( munlock(p, n) ){ |
|
3f5ab71…
|
drh
|
167 |
fossil_panic("munlock failed: %d\n", errno); |
|
d5ca538…
|
mistachkin
|
168 |
} |
|
d5ca538…
|
mistachkin
|
169 |
if( munmap(p, n) ){ |
|
3f5ab71…
|
drh
|
170 |
fossil_panic("munmap failed: %d\n", errno); |
|
a8484dc…
|
mistachkin
|
171 |
} |
|
a8484dc…
|
mistachkin
|
172 |
#else |
|
a8484dc…
|
mistachkin
|
173 |
fossil_free(p); |
|
a8484dc…
|
mistachkin
|
174 |
#endif |
|
3f5ab71…
|
drh
|
175 |
} |
|
3f5ab71…
|
drh
|
176 |
|
|
3f5ab71…
|
drh
|
177 |
/* |
|
0c9dff6…
|
drh
|
178 |
** Duplicate a string. |
|
0c9dff6…
|
drh
|
179 |
*/ |
|
0c9dff6…
|
drh
|
180 |
char *fossil_strndup(const char *zOrig, ssize_t len){ |
|
0c9dff6…
|
drh
|
181 |
char *z = 0; |
|
0c9dff6…
|
drh
|
182 |
if( zOrig ){ |
|
0c9dff6…
|
drh
|
183 |
if( len<0 ) len = strlen(zOrig); |
|
0c9dff6…
|
drh
|
184 |
z = fossil_malloc( len+1 ); |
|
0c9dff6…
|
drh
|
185 |
memcpy(z, zOrig, len); |
|
0c9dff6…
|
drh
|
186 |
z[len] = 0; |
|
0c9dff6…
|
drh
|
187 |
} |
|
0c9dff6…
|
drh
|
188 |
return z; |
|
0c9dff6…
|
drh
|
189 |
} |
|
0c9dff6…
|
drh
|
190 |
char *fossil_strdup(const char *zOrig){ |
|
0c9dff6…
|
drh
|
191 |
return fossil_strndup(zOrig, -1); |
|
0c9dff6…
|
drh
|
192 |
} |
|
0c9dff6…
|
drh
|
193 |
char *fossil_strdup_nn(const char *zOrig){ |
|
a04d181…
|
drh
|
194 |
if( zOrig==0 ) return fossil_strndup("", 0); |
|
0c9dff6…
|
drh
|
195 |
return fossil_strndup(zOrig, -1); |
|
0c9dff6…
|
drh
|
196 |
} |
|
0c9dff6…
|
drh
|
197 |
|
|
c0b9b44…
|
stephan
|
198 |
/* |
|
c0b9b44…
|
stephan
|
199 |
** strcpy() workalike to squelch an unwarranted warning from OpenBSD. |
|
c0b9b44…
|
stephan
|
200 |
*/ |
|
c0b9b44…
|
stephan
|
201 |
void fossil_strcpy(char *dest, const char *src){ |
|
c0b9b44…
|
stephan
|
202 |
while( (*(dest++) = *(src++))!=0 ){} |
|
c0b9b44…
|
stephan
|
203 |
} |
|
0c9dff6…
|
drh
|
204 |
|
|
0c9dff6…
|
drh
|
205 |
/* |
|
c00e912…
|
drh
|
206 |
** Translate every upper-case character in the input string into |
|
c00e912…
|
drh
|
207 |
** its equivalent lower-case. |
|
c00e912…
|
drh
|
208 |
*/ |
|
c00e912…
|
drh
|
209 |
char *fossil_strtolwr(char *zIn){ |
|
c00e912…
|
drh
|
210 |
char *zStart = zIn; |
|
c00e912…
|
drh
|
211 |
if( zIn ){ |
|
c00e912…
|
drh
|
212 |
while( *zIn ){ |
|
c00e912…
|
drh
|
213 |
*zIn = fossil_tolower(*zIn); |
|
c00e912…
|
drh
|
214 |
zIn++; |
|
c00e912…
|
drh
|
215 |
} |
|
c00e912…
|
drh
|
216 |
} |
|
c00e912…
|
drh
|
217 |
return zStart; |
|
a046f91…
|
drh
|
218 |
} |
|
a046f91…
|
drh
|
219 |
|
|
a046f91…
|
drh
|
220 |
/* |
|
7d4cf0e…
|
drh
|
221 |
** This local variable determines the behavior of |
|
7d4cf0e…
|
drh
|
222 |
** fossil_assert_safe_command_string(): |
|
7d4cf0e…
|
drh
|
223 |
** |
|
7d4cf0e…
|
drh
|
224 |
** 0 (default) fossil_panic() on an unsafe command string |
|
7d4cf0e…
|
drh
|
225 |
** |
|
7d4cf0e…
|
drh
|
226 |
** 1 Print an error but continue process. Used for |
|
7d4cf0e…
|
drh
|
227 |
** testing of fossil_assert_safe_command_string(). |
|
7d4cf0e…
|
drh
|
228 |
** |
|
7d4cf0e…
|
drh
|
229 |
** 2 No-op. Used to allow any arbitrary command string |
|
7d4cf0e…
|
drh
|
230 |
** through fossil_system(), such as when invoking |
|
7d4cf0e…
|
drh
|
231 |
** COMMAND in "fossil bisect run COMMAND". |
|
68b68ce…
|
drh
|
232 |
*/ |
|
68b68ce…
|
drh
|
233 |
static int safeCmdStrTest = 0; |
|
68b68ce…
|
drh
|
234 |
|
|
68b68ce…
|
drh
|
235 |
/* |
|
a046f91…
|
drh
|
236 |
** Check the input string to ensure that it is safe to pass into system(). |
|
68b68ce…
|
drh
|
237 |
** A string is unsafe for system() on unix if it contains any of the following: |
|
a046f91…
|
drh
|
238 |
** |
|
e2bdc10…
|
danield
|
239 |
** * Any occurrence of '$' or '`' except single-quoted or after \ |
|
84346a3…
|
drh
|
240 |
** * Any of the following characters, unquoted: ;|& or \n except |
|
84346a3…
|
drh
|
241 |
** these characters are allowed as the very last character in the |
|
84346a3…
|
drh
|
242 |
** string. |
|
a046f91…
|
drh
|
243 |
** * Unbalanced single or double quotes |
|
a046f91…
|
drh
|
244 |
** |
|
a046f91…
|
drh
|
245 |
** This routine is intended as a second line of defense against attack. |
|
a046f91…
|
drh
|
246 |
** It should never fail. Dangerous shell strings should be detected and |
|
a046f91…
|
drh
|
247 |
** fixed before calling fossil_system(). This routine serves only as a |
|
a046f91…
|
drh
|
248 |
** safety net in case of bugs elsewhere in the system. |
|
a046f91…
|
drh
|
249 |
** |
|
ce16c59…
|
drh
|
250 |
** If an unsafe string is seen, either abort (default) or print |
|
ce16c59…
|
drh
|
251 |
** a warning message (if safeCmdStrTest is true). |
|
a046f91…
|
drh
|
252 |
*/ |
|
ce16c59…
|
drh
|
253 |
static void fossil_assert_safe_command_string(const char *z){ |
|
68b68ce…
|
drh
|
254 |
int unsafe = 0; |
|
68b68ce…
|
drh
|
255 |
#ifndef _WIN32 |
|
68b68ce…
|
drh
|
256 |
/* Unix */ |
|
a046f91…
|
drh
|
257 |
int inQuote = 0; |
|
a046f91…
|
drh
|
258 |
int i, c; |
|
68b68ce…
|
drh
|
259 |
for(i=0; !unsafe && (c = z[i])!=0; i++){ |
|
a046f91…
|
drh
|
260 |
switch( c ){ |
|
a046f91…
|
drh
|
261 |
case '$': |
|
a046f91…
|
drh
|
262 |
case '`': { |
|
68b68ce…
|
drh
|
263 |
if( inQuote!='\'' ) unsafe = i+1; |
|
a046f91…
|
drh
|
264 |
break; |
|
a046f91…
|
drh
|
265 |
} |
|
a046f91…
|
drh
|
266 |
case ';': |
|
a046f91…
|
drh
|
267 |
case '|': |
|
a046f91…
|
drh
|
268 |
case '&': |
|
a046f91…
|
drh
|
269 |
case '\n': { |
|
68b68ce…
|
drh
|
270 |
if( inQuote!='\'' && z[i+1]!=0 ) unsafe = i+1; |
|
a046f91…
|
drh
|
271 |
break; |
|
a046f91…
|
drh
|
272 |
} |
|
a046f91…
|
drh
|
273 |
case '"': |
|
a046f91…
|
drh
|
274 |
case '\'': { |
|
a046f91…
|
drh
|
275 |
if( inQuote==0 ){ |
|
a046f91…
|
drh
|
276 |
inQuote = c; |
|
a046f91…
|
drh
|
277 |
}else if( inQuote==c ){ |
|
a046f91…
|
drh
|
278 |
inQuote = 0; |
|
a046f91…
|
drh
|
279 |
} |
|
a046f91…
|
drh
|
280 |
break; |
|
a046f91…
|
drh
|
281 |
} |
|
a046f91…
|
drh
|
282 |
case '\\': { |
|
a046f91…
|
drh
|
283 |
if( z[i+1]==0 ){ |
|
a046f91…
|
drh
|
284 |
unsafe = i+1; |
|
68b68ce…
|
drh
|
285 |
}else if( inQuote!='\'' ){ |
|
68b68ce…
|
drh
|
286 |
i++; |
|
68b68ce…
|
drh
|
287 |
} |
|
68b68ce…
|
drh
|
288 |
break; |
|
68b68ce…
|
drh
|
289 |
} |
|
68b68ce…
|
drh
|
290 |
} |
|
68b68ce…
|
drh
|
291 |
} |
|
68b68ce…
|
drh
|
292 |
if( inQuote ) unsafe = i; |
|
68b68ce…
|
drh
|
293 |
#else |
|
68b68ce…
|
drh
|
294 |
/* Windows */ |
|
68b68ce…
|
drh
|
295 |
int i, c; |
|
68b68ce…
|
drh
|
296 |
int inQuote = 0; |
|
68b68ce…
|
drh
|
297 |
for(i=0; !unsafe && (c = z[i])!=0; i++){ |
|
68b68ce…
|
drh
|
298 |
switch( c ){ |
|
ce16c59…
|
drh
|
299 |
case '>': |
|
ce16c59…
|
drh
|
300 |
case '<': |
|
68b68ce…
|
drh
|
301 |
case '|': |
|
68b68ce…
|
drh
|
302 |
case '&': |
|
68b68ce…
|
drh
|
303 |
case '\n': { |
|
68b68ce…
|
drh
|
304 |
if( inQuote==0 && z[i+1]!=0 ) unsafe = i+1; |
|
68b68ce…
|
drh
|
305 |
break; |
|
68b68ce…
|
drh
|
306 |
} |
|
68b68ce…
|
drh
|
307 |
case '"': { |
|
68b68ce…
|
drh
|
308 |
if( inQuote==c ){ |
|
68b68ce…
|
drh
|
309 |
inQuote = 0; |
|
a046f91…
|
drh
|
310 |
}else{ |
|
68b68ce…
|
drh
|
311 |
inQuote = c; |
|
68b68ce…
|
drh
|
312 |
} |
|
68b68ce…
|
drh
|
313 |
break; |
|
68b68ce…
|
drh
|
314 |
} |
|
ce16c59…
|
drh
|
315 |
case '^': { |
|
6d2e48b…
|
drh
|
316 |
if( !inQuote && z[i+1]!=0 ){ |
|
ce16c59…
|
drh
|
317 |
i++; |
|
ce16c59…
|
drh
|
318 |
} |
|
ce16c59…
|
drh
|
319 |
break; |
|
ce16c59…
|
drh
|
320 |
} |
|
a046f91…
|
drh
|
321 |
} |
|
a046f91…
|
drh
|
322 |
} |
|
68b68ce…
|
drh
|
323 |
if( inQuote ) unsafe = i; |
|
68b68ce…
|
drh
|
324 |
#endif |
|
7d4cf0e…
|
drh
|
325 |
if( unsafe && safeCmdStrTest<2 ){ |
|
68b68ce…
|
drh
|
326 |
char *zMsg = mprintf("Unsafe command string: %s\n%*shere ----^", |
|
68b68ce…
|
drh
|
327 |
z, unsafe+13, ""); |
|
68b68ce…
|
drh
|
328 |
if( safeCmdStrTest ){ |
|
68b68ce…
|
drh
|
329 |
fossil_print("%z\n", zMsg); |
|
7d4cf0e…
|
drh
|
330 |
fossil_free(zMsg); |
|
68b68ce…
|
drh
|
331 |
}else{ |
|
68b68ce…
|
drh
|
332 |
fossil_panic("%s", zMsg); |
|
68b68ce…
|
drh
|
333 |
} |
|
a046f91…
|
drh
|
334 |
} |
|
c00e912…
|
drh
|
335 |
} |
|
c00e912…
|
drh
|
336 |
|
|
c00e912…
|
drh
|
337 |
/* |
|
bb0cedb…
|
drh
|
338 |
** This function implements a cross-platform "system()" interface. |
|
bb0cedb…
|
drh
|
339 |
*/ |
|
bb0cedb…
|
drh
|
340 |
int fossil_system(const char *zOrigCmd){ |
|
bb0cedb…
|
drh
|
341 |
int rc; |
|
bb0cedb…
|
drh
|
342 |
#if defined(_WIN32) |
|
bb0cedb…
|
drh
|
343 |
/* On windows, we have to put double-quotes around the entire command. |
|
bb0cedb…
|
drh
|
344 |
** Who knows why - this is just the way windows works. |
|
bb0cedb…
|
drh
|
345 |
*/ |
|
bb0cedb…
|
drh
|
346 |
char *zNewCmd = mprintf("\"%s\"", zOrigCmd); |
|
32bb8c2…
|
jan.nijtmans
|
347 |
wchar_t *zUnicode = fossil_utf8_to_unicode(zNewCmd); |
|
bb0cedb…
|
drh
|
348 |
if( g.fSystemTrace ) { |
|
bb0cedb…
|
drh
|
349 |
fossil_trace("SYSTEM: %s\n", zNewCmd); |
|
bb0cedb…
|
drh
|
350 |
} |
|
ce16c59…
|
drh
|
351 |
fossil_assert_safe_command_string(zOrigCmd); |
|
ce16c59…
|
drh
|
352 |
rc = _wsystem(zUnicode); |
|
bb0cedb…
|
drh
|
353 |
fossil_unicode_free(zUnicode); |
|
bb0cedb…
|
drh
|
354 |
free(zNewCmd); |
|
bb0cedb…
|
drh
|
355 |
#else |
|
bb0cedb…
|
drh
|
356 |
/* On unix, evaluate the command directly. |
|
bb0cedb…
|
drh
|
357 |
*/ |
|
bb0cedb…
|
drh
|
358 |
if( g.fSystemTrace ) fprintf(stderr, "SYSTEM: %s\n", zOrigCmd); |
|
ce16c59…
|
drh
|
359 |
fossil_assert_safe_command_string(zOrigCmd); |
|
82e30c0…
|
drh
|
360 |
|
|
82e30c0…
|
drh
|
361 |
/* Unix systems should never shell-out while processing an HTTP request, |
|
82e30c0…
|
drh
|
362 |
** either via CGI, SCGI, or direct HTTP. The following assert verifies |
|
82e30c0…
|
drh
|
363 |
** this. And the following assert proves that Fossil is not vulnerable |
|
82e30c0…
|
drh
|
364 |
** to the ShellShock or BashDoor bug. |
|
82e30c0…
|
drh
|
365 |
*/ |
|
82e30c0…
|
drh
|
366 |
assert( g.cgiOutput==0 ); |
|
82e30c0…
|
drh
|
367 |
|
|
82e30c0…
|
drh
|
368 |
/* The regular system() call works to get a shell on unix */ |
|
3f193ba…
|
drh
|
369 |
fossil_limit_memory(0); |
|
bb0cedb…
|
drh
|
370 |
rc = system(zOrigCmd); |
|
3f193ba…
|
drh
|
371 |
fossil_limit_memory(1); |
|
bb0cedb…
|
drh
|
372 |
#endif |
|
7d4cf0e…
|
drh
|
373 |
return rc; |
|
7d4cf0e…
|
drh
|
374 |
} |
|
7d4cf0e…
|
drh
|
375 |
|
|
7d4cf0e…
|
drh
|
376 |
/* |
|
7d4cf0e…
|
drh
|
377 |
** Like "fossil_system()" but does not check the command-string for |
|
7d4cf0e…
|
drh
|
378 |
** potential security problems. |
|
7d4cf0e…
|
drh
|
379 |
*/ |
|
7d4cf0e…
|
drh
|
380 |
int fossil_unsafe_system(const char *zOrigCmd){ |
|
7d4cf0e…
|
drh
|
381 |
int rc; |
|
7d4cf0e…
|
drh
|
382 |
safeCmdStrTest = 2; |
|
7d4cf0e…
|
drh
|
383 |
rc = fossil_system(zOrigCmd); |
|
7d4cf0e…
|
drh
|
384 |
safeCmdStrTest = 0; |
|
f6b67e1…
|
drh
|
385 |
return rc; |
|
f6b67e1…
|
drh
|
386 |
} |
|
f6b67e1…
|
drh
|
387 |
|
|
f6b67e1…
|
drh
|
388 |
/* |
|
68b68ce…
|
drh
|
389 |
** COMMAND: test-fossil-system |
|
68b68ce…
|
drh
|
390 |
** |
|
68b68ce…
|
drh
|
391 |
** Read lines of input and send them to fossil_system() for evaluation. |
|
68b68ce…
|
drh
|
392 |
** Use this command to verify that fossil_system() will not run "unsafe" |
|
68b68ce…
|
drh
|
393 |
** commands. |
|
68b68ce…
|
drh
|
394 |
*/ |
|
68b68ce…
|
drh
|
395 |
void test_fossil_system_cmd(void){ |
|
68b68ce…
|
drh
|
396 |
char zLine[10000]; |
|
68b68ce…
|
drh
|
397 |
safeCmdStrTest = 1; |
|
68b68ce…
|
drh
|
398 |
while(1){ |
|
68b68ce…
|
drh
|
399 |
size_t n; |
|
f6b67e1…
|
drh
|
400 |
int rc; |
|
68b68ce…
|
drh
|
401 |
printf("system-test> "); |
|
68b68ce…
|
drh
|
402 |
fflush(stdout); |
|
68b68ce…
|
drh
|
403 |
if( !fgets(zLine, sizeof(zLine), stdin) ) break; |
|
68b68ce…
|
drh
|
404 |
n = strlen(zLine); |
|
68b68ce…
|
drh
|
405 |
while( n>0 && fossil_isspace(zLine[n-1]) ) n--; |
|
68b68ce…
|
drh
|
406 |
zLine[n] = 0; |
|
68b68ce…
|
drh
|
407 |
printf("cmd: [%s]\n", zLine); |
|
68b68ce…
|
drh
|
408 |
fflush(stdout); |
|
f6b67e1…
|
drh
|
409 |
rc = fossil_system(zLine); |
|
f6b67e1…
|
drh
|
410 |
printf("result: %d\n", rc); |
|
68b68ce…
|
drh
|
411 |
} |
|
72a4b61…
|
drh
|
412 |
} |
|
72a4b61…
|
drh
|
413 |
|
|
72a4b61…
|
drh
|
414 |
/* |
|
bb0cedb…
|
drh
|
415 |
** Like strcmp() except that it accepts NULL pointers. NULL sorts before |
|
bb0cedb…
|
drh
|
416 |
** all non-NULL string pointers. Also, this strcmp() is a binary comparison |
|
bb0cedb…
|
drh
|
417 |
** that does not consider locale. |
|
bb0cedb…
|
drh
|
418 |
*/ |
|
bb0cedb…
|
drh
|
419 |
int fossil_strcmp(const char *zA, const char *zB){ |
|
bb0cedb…
|
drh
|
420 |
if( zA==0 ){ |
|
bb0cedb…
|
drh
|
421 |
if( zB==0 ) return 0; |
|
bb0cedb…
|
drh
|
422 |
return -1; |
|
bb0cedb…
|
drh
|
423 |
}else if( zB==0 ){ |
|
bb0cedb…
|
drh
|
424 |
return +1; |
|
bb0cedb…
|
drh
|
425 |
}else{ |
|
72a4b61…
|
drh
|
426 |
return strcmp(zA,zB); |
|
bb0cedb…
|
drh
|
427 |
} |
|
bb0cedb…
|
drh
|
428 |
} |
|
bb0cedb…
|
drh
|
429 |
int fossil_strncmp(const char *zA, const char *zB, int nByte){ |
|
bb0cedb…
|
drh
|
430 |
if( zA==0 ){ |
|
bb0cedb…
|
drh
|
431 |
if( zB==0 ) return 0; |
|
bb0cedb…
|
drh
|
432 |
return -1; |
|
bb0cedb…
|
drh
|
433 |
}else if( zB==0 ){ |
|
bb0cedb…
|
drh
|
434 |
return +1; |
|
bb0cedb…
|
drh
|
435 |
}else if( nByte>0 ){ |
|
bb0cedb…
|
drh
|
436 |
int a, b; |
|
bb0cedb…
|
drh
|
437 |
do{ |
|
bb0cedb…
|
drh
|
438 |
a = *zA++; |
|
bb0cedb…
|
drh
|
439 |
b = *zB++; |
|
bb0cedb…
|
drh
|
440 |
}while( a==b && a!=0 && (--nByte)>0 ); |
|
bb0cedb…
|
drh
|
441 |
return ((unsigned char)a) - (unsigned char)b; |
|
bb0cedb…
|
drh
|
442 |
}else{ |
|
bb0cedb…
|
drh
|
443 |
return 0; |
|
bb0cedb…
|
drh
|
444 |
} |
|
bb0cedb…
|
drh
|
445 |
} |
|
bb0cedb…
|
drh
|
446 |
|
|
bb0cedb…
|
drh
|
447 |
/* |
|
bb0cedb…
|
drh
|
448 |
** Case insensitive string comparison. |
|
bb0cedb…
|
drh
|
449 |
*/ |
|
bb0cedb…
|
drh
|
450 |
int fossil_strnicmp(const char *zA, const char *zB, int nByte){ |
|
bb0cedb…
|
drh
|
451 |
if( zA==0 ){ |
|
bb0cedb…
|
drh
|
452 |
if( zB==0 ) return 0; |
|
bb0cedb…
|
drh
|
453 |
return -1; |
|
bb0cedb…
|
drh
|
454 |
}else if( zB==0 ){ |
|
bb0cedb…
|
drh
|
455 |
return +1; |
|
bb0cedb…
|
drh
|
456 |
} |
|
bb0cedb…
|
drh
|
457 |
if( nByte<0 ) nByte = strlen(zB); |
|
bb0cedb…
|
drh
|
458 |
return sqlite3_strnicmp(zA, zB, nByte); |
|
bb0cedb…
|
drh
|
459 |
} |
|
bb0cedb…
|
drh
|
460 |
int fossil_stricmp(const char *zA, const char *zB){ |
|
bb0cedb…
|
drh
|
461 |
int nByte; |
|
bb0cedb…
|
drh
|
462 |
int rc; |
|
bb0cedb…
|
drh
|
463 |
if( zA==0 ){ |
|
bb0cedb…
|
drh
|
464 |
if( zB==0 ) return 0; |
|
bb0cedb…
|
drh
|
465 |
return -1; |
|
bb0cedb…
|
drh
|
466 |
}else if( zB==0 ){ |
|
bb0cedb…
|
drh
|
467 |
return +1; |
|
bb0cedb…
|
drh
|
468 |
} |
|
bb0cedb…
|
drh
|
469 |
nByte = strlen(zB); |
|
bb0cedb…
|
drh
|
470 |
rc = sqlite3_strnicmp(zA, zB, nByte); |
|
bb0cedb…
|
drh
|
471 |
if( rc==0 && zA[nByte] ) rc = 1; |
|
bb0cedb…
|
drh
|
472 |
return rc; |
|
e28433a…
|
stephan
|
473 |
} |
|
e28433a…
|
stephan
|
474 |
|
|
e28433a…
|
stephan
|
475 |
/* |
|
e28433a…
|
stephan
|
476 |
** Get user and kernel times in microseconds. |
|
e28433a…
|
stephan
|
477 |
*/ |
|
e28433a…
|
stephan
|
478 |
void fossil_cpu_times(sqlite3_uint64 *piUser, sqlite3_uint64 *piKernel){ |
|
e28433a…
|
stephan
|
479 |
#ifdef _WIN32 |
|
e28433a…
|
stephan
|
480 |
FILETIME not_used; |
|
e28433a…
|
stephan
|
481 |
FILETIME kernel_time; |
|
e28433a…
|
stephan
|
482 |
FILETIME user_time; |
|
e28433a…
|
stephan
|
483 |
GetProcessTimes(GetCurrentProcess(), ¬_used, ¬_used, |
|
e28433a…
|
stephan
|
484 |
&kernel_time, &user_time); |
|
e28433a…
|
stephan
|
485 |
if( piUser ){ |
|
e28433a…
|
stephan
|
486 |
*piUser = ((((sqlite3_uint64)user_time.dwHighDateTime)<<32) + |
|
e28433a…
|
stephan
|
487 |
(sqlite3_uint64)user_time.dwLowDateTime + 5)/10; |
|
e28433a…
|
stephan
|
488 |
} |
|
e28433a…
|
stephan
|
489 |
if( piKernel ){ |
|
e28433a…
|
stephan
|
490 |
*piKernel = ((((sqlite3_uint64)kernel_time.dwHighDateTime)<<32) + |
|
e28433a…
|
stephan
|
491 |
(sqlite3_uint64)kernel_time.dwLowDateTime + 5)/10; |
|
e28433a…
|
stephan
|
492 |
} |
|
e28433a…
|
stephan
|
493 |
#else |
|
e28433a…
|
stephan
|
494 |
struct rusage s; |
|
e28433a…
|
stephan
|
495 |
getrusage(RUSAGE_SELF, &s); |
|
e28433a…
|
stephan
|
496 |
if( piUser ){ |
|
e28433a…
|
stephan
|
497 |
*piUser = ((sqlite3_uint64)s.ru_utime.tv_sec)*1000000 + s.ru_utime.tv_usec; |
|
e28433a…
|
stephan
|
498 |
} |
|
e28433a…
|
stephan
|
499 |
if( piKernel ){ |
|
55d7e2c…
|
jan.nijtmans
|
500 |
*piKernel = |
|
e28433a…
|
stephan
|
501 |
((sqlite3_uint64)s.ru_stime.tv_sec)*1000000 + s.ru_stime.tv_usec; |
|
e28433a…
|
stephan
|
502 |
} |
|
e28433a…
|
stephan
|
503 |
#endif |
|
e28433a…
|
stephan
|
504 |
} |
|
b00e20c…
|
drh
|
505 |
|
|
b00e20c…
|
drh
|
506 |
/* |
|
b00e20c…
|
drh
|
507 |
** Return the resident set size for this process |
|
b00e20c…
|
drh
|
508 |
*/ |
|
b00e20c…
|
drh
|
509 |
sqlite3_uint64 fossil_rss(void){ |
|
b00e20c…
|
drh
|
510 |
#ifdef _WIN32 |
|
b00e20c…
|
drh
|
511 |
return 0; |
|
b00e20c…
|
drh
|
512 |
#else |
|
b00e20c…
|
drh
|
513 |
struct rusage s; |
|
b00e20c…
|
drh
|
514 |
getrusage(RUSAGE_SELF, &s); |
|
b00e20c…
|
drh
|
515 |
return s.ru_maxrss*1024; |
|
b00e20c…
|
drh
|
516 |
#endif |
|
b00e20c…
|
drh
|
517 |
} |
|
b00e20c…
|
drh
|
518 |
|
|
3f5ab71…
|
drh
|
519 |
|
|
3f5ab71…
|
drh
|
520 |
/* |
|
e28433a…
|
stephan
|
521 |
** Internal helper type for fossil_timer_xxx(). |
|
e28433a…
|
stephan
|
522 |
*/ |
|
55d7e2c…
|
jan.nijtmans
|
523 |
enum FossilTimerEnum { |
|
55d7e2c…
|
jan.nijtmans
|
524 |
FOSSIL_TIMER_COUNT = 10 /* Number of timers we can track. */ |
|
55d7e2c…
|
jan.nijtmans
|
525 |
}; |
|
e28433a…
|
stephan
|
526 |
static struct FossilTimer { |
|
e28433a…
|
stephan
|
527 |
sqlite3_uint64 u; /* "User" CPU times */ |
|
e28433a…
|
stephan
|
528 |
sqlite3_uint64 s; /* "System" CPU times */ |
|
7994589…
|
stephan
|
529 |
int id; /* positive if allocated, else 0. */ |
|
55d7e2c…
|
jan.nijtmans
|
530 |
} fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}}; |
|
e28433a…
|
stephan
|
531 |
|
|
e28433a…
|
stephan
|
532 |
/* |
|
e28433a…
|
stephan
|
533 |
** Stores the current CPU times into the shared timer list |
|
e28433a…
|
stephan
|
534 |
** and returns that timer's internal ID. Pass that ID to |
|
e28433a…
|
stephan
|
535 |
** fossil_timer_fetch() to get the elapsed time for that |
|
e28433a…
|
stephan
|
536 |
** timer. |
|
e28433a…
|
stephan
|
537 |
** |
|
e28433a…
|
stephan
|
538 |
** The system has a fixed number of timers, and they can be |
|
e28433a…
|
stephan
|
539 |
** "deallocated" by passing this function's return value to |
|
e28433a…
|
stephan
|
540 |
** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of |
|
e28433a…
|
stephan
|
541 |
** available timers. |
|
e28433a…
|
stephan
|
542 |
** |
|
7994589…
|
stephan
|
543 |
** Returns 0 on error (no more timers available), with 1+ being valid |
|
7994589…
|
stephan
|
544 |
** timer IDs. |
|
e28433a…
|
stephan
|
545 |
*/ |
|
e28433a…
|
stephan
|
546 |
int fossil_timer_start(){ |
|
e28433a…
|
stephan
|
547 |
int i; |
|
e28433a…
|
stephan
|
548 |
for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){ |
|
e28433a…
|
stephan
|
549 |
struct FossilTimer * ft = &fossilTimerList[i]; |
|
7994589…
|
stephan
|
550 |
if(ft->id) continue; |
|
7994589…
|
stephan
|
551 |
ft->id = i+1; |
|
e28433a…
|
stephan
|
552 |
fossil_cpu_times( &ft->u, &ft->s ); |
|
e28433a…
|
stephan
|
553 |
break; |
|
e28433a…
|
stephan
|
554 |
} |
|
7994589…
|
stephan
|
555 |
return (i<FOSSIL_TIMER_COUNT) ? i+1 : 0; |
|
e28433a…
|
stephan
|
556 |
} |
|
e28433a…
|
stephan
|
557 |
|
|
e28433a…
|
stephan
|
558 |
/* |
|
e28433a…
|
stephan
|
559 |
** Returns the difference in CPU times in microseconds since |
|
e28433a…
|
stephan
|
560 |
** fossil_timer_start() was called and returned the given timer ID (or |
|
e28433a…
|
stephan
|
561 |
** since it was last reset). Returns 0 if timerId is out of range. |
|
e28433a…
|
stephan
|
562 |
*/ |
|
e28433a…
|
stephan
|
563 |
sqlite3_uint64 fossil_timer_fetch(int timerId){ |
|
6e1e4bb…
|
jan.nijtmans
|
564 |
if( timerId>0 && timerId<=FOSSIL_TIMER_COUNT ){ |
|
7994589…
|
stephan
|
565 |
struct FossilTimer * start = &fossilTimerList[timerId-1]; |
|
7994589…
|
stephan
|
566 |
if( !start->id ){ |
|
3f5ab71…
|
drh
|
567 |
fossil_panic("Invalid call to fetch a non-allocated " |
|
e28433a…
|
stephan
|
568 |
"timer (#%d)", timerId); |
|
e28433a…
|
stephan
|
569 |
/*NOTREACHED*/ |
|
e28433a…
|
stephan
|
570 |
}else{ |
|
e28433a…
|
stephan
|
571 |
sqlite3_uint64 eu = 0, es = 0; |
|
e28433a…
|
stephan
|
572 |
fossil_cpu_times( &eu, &es ); |
|
e28433a…
|
stephan
|
573 |
return (eu - start->u) + (es - start->s); |
|
e28433a…
|
stephan
|
574 |
} |
|
e28433a…
|
stephan
|
575 |
} |
|
6e1e4bb…
|
jan.nijtmans
|
576 |
return 0; |
|
e28433a…
|
stephan
|
577 |
} |
|
e28433a…
|
stephan
|
578 |
|
|
e28433a…
|
stephan
|
579 |
/* |
|
e28433a…
|
stephan
|
580 |
** Resets the timer associated with the given ID, as obtained via |
|
e28433a…
|
stephan
|
581 |
** fossil_timer_start(), to the current CPU time values. |
|
e28433a…
|
stephan
|
582 |
*/ |
|
e28433a…
|
stephan
|
583 |
sqlite3_uint64 fossil_timer_reset(int timerId){ |
|
6e1e4bb…
|
jan.nijtmans
|
584 |
if( timerId>0 && timerId<=FOSSIL_TIMER_COUNT ){ |
|
7994589…
|
stephan
|
585 |
struct FossilTimer * start = &fossilTimerList[timerId-1]; |
|
7994589…
|
stephan
|
586 |
if( !start->id ){ |
|
3f5ab71…
|
drh
|
587 |
fossil_panic("Invalid call to reset a non-allocated " |
|
e28433a…
|
stephan
|
588 |
"timer (#%d)", timerId); |
|
e28433a…
|
stephan
|
589 |
/*NOTREACHED*/ |
|
e28433a…
|
stephan
|
590 |
}else{ |
|
e28433a…
|
stephan
|
591 |
sqlite3_uint64 const rc = fossil_timer_fetch(timerId); |
|
e28433a…
|
stephan
|
592 |
fossil_cpu_times( &start->u, &start->s ); |
|
e28433a…
|
stephan
|
593 |
return rc; |
|
e28433a…
|
stephan
|
594 |
} |
|
e28433a…
|
stephan
|
595 |
} |
|
6e1e4bb…
|
jan.nijtmans
|
596 |
return 0; |
|
e28433a…
|
stephan
|
597 |
} |
|
e28433a…
|
stephan
|
598 |
|
|
e28433a…
|
stephan
|
599 |
/** |
|
e28433a…
|
stephan
|
600 |
"Deallocates" the fossil timer identified by the given timer ID. |
|
e28433a…
|
stephan
|
601 |
returns the difference (in uSec) between the last time that timer |
|
7994589…
|
stephan
|
602 |
was started or reset. Returns 0 if timerId is out of range (but |
|
7994589…
|
stephan
|
603 |
note that, due to system-level precision restrictions, this |
|
7994589…
|
stephan
|
604 |
function might return 0 on success, too!). It is not legal to |
|
7994589…
|
stephan
|
605 |
re-use the passed-in timerId after calling this until/unless it is |
|
7994589…
|
stephan
|
606 |
re-initialized using fossil_timer_start() (NOT |
|
e28433a…
|
stephan
|
607 |
fossil_timer_reset()). |
|
e28433a…
|
stephan
|
608 |
*/ |
|
e28433a…
|
stephan
|
609 |
sqlite3_uint64 fossil_timer_stop(int timerId){ |
|
7994589…
|
stephan
|
610 |
if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ |
|
e28433a…
|
stephan
|
611 |
return 0; |
|
e28433a…
|
stephan
|
612 |
}else{ |
|
e28433a…
|
stephan
|
613 |
sqlite3_uint64 const rc = fossil_timer_fetch(timerId); |
|
7994589…
|
stephan
|
614 |
struct FossilTimer * t = &fossilTimerList[timerId-1]; |
|
7994589…
|
stephan
|
615 |
t->id = 0; |
|
e28433a…
|
stephan
|
616 |
t->u = t->s = 0U; |
|
e28433a…
|
stephan
|
617 |
return rc; |
|
e28433a…
|
stephan
|
618 |
} |
|
e28433a…
|
stephan
|
619 |
} |
|
e28433a…
|
stephan
|
620 |
|
|
e28433a…
|
stephan
|
621 |
/* |
|
590406d…
|
stephan
|
622 |
** Returns true (non-0) if the given timer ID (as returned from |
|
590406d…
|
stephan
|
623 |
** fossil_timer_start() is currently active. |
|
590406d…
|
stephan
|
624 |
*/ |
|
590406d…
|
stephan
|
625 |
int fossil_timer_is_active( int timerId ){ |
|
7994589…
|
stephan
|
626 |
if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ |
|
590406d…
|
stephan
|
627 |
return 0; |
|
590406d…
|
stephan
|
628 |
}else{ |
|
4e18dba…
|
jan.nijtmans
|
629 |
const int rc = fossilTimerList[timerId-1].id; |
|
7994589…
|
stephan
|
630 |
assert(!rc || (rc == timerId)); |
|
7994589…
|
stephan
|
631 |
return fossilTimerList[timerId-1].id; |
|
7994589…
|
stephan
|
632 |
} |
|
4727ef4…
|
drh
|
633 |
} |
|
4727ef4…
|
drh
|
634 |
|
|
4727ef4…
|
drh
|
635 |
/* |
|
4727ef4…
|
drh
|
636 |
** Return TRUE if fd is a valid open file descriptor. This only |
|
4727ef4…
|
drh
|
637 |
** works on unix. The function always returns true on Windows. |
|
4727ef4…
|
drh
|
638 |
*/ |
|
4727ef4…
|
drh
|
639 |
int is_valid_fd(int fd){ |
|
4727ef4…
|
drh
|
640 |
#ifdef _WIN32 |
|
4727ef4…
|
drh
|
641 |
return 1; |
|
4727ef4…
|
drh
|
642 |
#else |
|
4727ef4…
|
drh
|
643 |
return fcntl(fd, F_GETFL)!=(-1) || errno!=EBADF; |
|
4727ef4…
|
drh
|
644 |
#endif |
|
b7ff537…
|
stephan
|
645 |
} |
|
b7ff537…
|
stephan
|
646 |
|
|
b7ff537…
|
stephan
|
647 |
/* |
|
323299c…
|
drh
|
648 |
** Returns TRUE if zSym is exactly HNAME_LEN_SHA1 or HNAME_LEN_K256 |
|
323299c…
|
drh
|
649 |
** bytes long and contains only lower-case ASCII hexadecimal values. |
|
ff78d6d…
|
drh
|
650 |
*/ |
|
8ad5e46…
|
wyoung
|
651 |
int fossil_is_artifact_hash(const char *zSym){ |
|
323299c…
|
drh
|
652 |
int sz = zSym ? (int)strlen(zSym) : 0; |
|
323299c…
|
drh
|
653 |
return (HNAME_LEN_SHA1==sz || HNAME_LEN_K256==sz) && validate16(zSym, sz); |
|
ff78d6d…
|
drh
|
654 |
} |
|
ff78d6d…
|
drh
|
655 |
|
|
ff78d6d…
|
drh
|
656 |
/* |
|
ff78d6d…
|
drh
|
657 |
** Return true if the input string is NULL or all whitespace. |
|
ff78d6d…
|
drh
|
658 |
** Return false if the input string contains text. |
|
ff78d6d…
|
drh
|
659 |
*/ |
|
ff78d6d…
|
drh
|
660 |
int fossil_all_whitespace(const char *z){ |
|
ff78d6d…
|
drh
|
661 |
if( z==0 ) return 1; |
|
ff78d6d…
|
drh
|
662 |
while( fossil_isspace(z[0]) ){ z++; } |
|
ff78d6d…
|
drh
|
663 |
return z[0]==0; |
|
1d6b678…
|
drh
|
664 |
} |
|
1d6b678…
|
drh
|
665 |
|
|
1d6b678…
|
drh
|
666 |
/* |
|
1d6b678…
|
drh
|
667 |
** Return the name of the users preferred text editor. Return NULL if |
|
1d6b678…
|
drh
|
668 |
** not found. |
|
1d6b678…
|
drh
|
669 |
** |
|
1d6b678…
|
drh
|
670 |
** Search algorithm: |
|
7675987…
|
drh
|
671 |
** (1) The value of the --editor command-line argument |
|
7675987…
|
drh
|
672 |
** (2) The local "editor" setting |
|
7675987…
|
drh
|
673 |
** (3) The global "editor" setting |
|
7675987…
|
drh
|
674 |
** (4) The VISUAL environment variable |
|
7675987…
|
drh
|
675 |
** (5) The EDITOR environment variable |
|
9b263d8…
|
drh
|
676 |
** (6) Any of several common editors that might be available, such as: |
|
9b263d8…
|
drh
|
677 |
** notepad, nano, pico, jove, edit, vi, vim, ed |
|
7675987…
|
drh
|
678 |
** |
|
7675987…
|
drh
|
679 |
** The search only occurs once, the first time this routine is called. |
|
7675987…
|
drh
|
680 |
** Second and subsequent invocations always return the same value. |
|
1d6b678…
|
drh
|
681 |
*/ |
|
1d6b678…
|
drh
|
682 |
const char *fossil_text_editor(void){ |
|
7675987…
|
drh
|
683 |
static const char *zEditor = 0; |
|
fc60e44…
|
drh
|
684 |
const char *azStdEd[] = { |
|
9b263d8…
|
drh
|
685 |
#ifdef _WIN32 |
|
9b263d8…
|
drh
|
686 |
"notepad", |
|
9b263d8…
|
drh
|
687 |
#endif |
|
9b263d8…
|
drh
|
688 |
"nano", "pico", "jove", "edit", "vi", "vim", "ed" |
|
fc60e44…
|
drh
|
689 |
}; |
|
fc60e44…
|
drh
|
690 |
int i = 0; |
|
7675987…
|
drh
|
691 |
if( zEditor==0 ){ |
|
7675987…
|
drh
|
692 |
zEditor = find_option("editor",0,1); |
|
7675987…
|
drh
|
693 |
} |
|
7675987…
|
drh
|
694 |
if( zEditor==0 ){ |
|
7675987…
|
drh
|
695 |
zEditor = db_get("editor", 0); |
|
7675987…
|
drh
|
696 |
} |
|
1d6b678…
|
drh
|
697 |
if( zEditor==0 ){ |
|
1d6b678…
|
drh
|
698 |
zEditor = fossil_getenv("VISUAL"); |
|
1d6b678…
|
drh
|
699 |
} |
|
1d6b678…
|
drh
|
700 |
if( zEditor==0 ){ |
|
1d6b678…
|
drh
|
701 |
zEditor = fossil_getenv("EDITOR"); |
|
1d6b678…
|
drh
|
702 |
} |
|
fc60e44…
|
drh
|
703 |
while( zEditor==0 && i<count(azStdEd) ){ |
|
fc60e44…
|
drh
|
704 |
if( fossil_app_on_path(azStdEd[i],0) ){ |
|
fc60e44…
|
drh
|
705 |
zEditor = azStdEd[i]; |
|
fc60e44…
|
drh
|
706 |
}else{ |
|
fc60e44…
|
drh
|
707 |
i++; |
|
fc60e44…
|
drh
|
708 |
} |
|
fc60e44…
|
drh
|
709 |
} |
|
cd03acb…
|
drh
|
710 |
if( zEditor && is_false(zEditor) ) zEditor = 0; |
|
1d6b678…
|
drh
|
711 |
return zEditor; |
|
1d6b678…
|
drh
|
712 |
} |
|
1d6b678…
|
drh
|
713 |
|
|
1d6b678…
|
drh
|
714 |
/* |
|
1d6b678…
|
drh
|
715 |
** Construct a temporary filename. |
|
1d6b678…
|
drh
|
716 |
** |
|
1d6b678…
|
drh
|
717 |
** The returned string is obtained from sqlite3_malloc() and must be |
|
1d6b678…
|
drh
|
718 |
** freed by the caller. |
|
1aff463…
|
drh
|
719 |
** |
|
1aff463…
|
drh
|
720 |
** See also: file_tempname() and file_time_timename(); |
|
1d6b678…
|
drh
|
721 |
*/ |
|
1d6b678…
|
drh
|
722 |
char *fossil_temp_filename(void){ |
|
1d6b678…
|
drh
|
723 |
char *zTFile = 0; |
|
9396966…
|
drh
|
724 |
const char *zDir; |
|
5434d2e…
|
drh
|
725 |
char cDirSep; |
|
5434d2e…
|
drh
|
726 |
char zSep[2]; |
|
5434d2e…
|
drh
|
727 |
size_t nDir; |
|
9396966…
|
drh
|
728 |
u64 r[2]; |
|
9396966…
|
drh
|
729 |
#ifdef _WIN32 |
|
f48e48f…
|
drh
|
730 |
char *zTempDirA = NULL; |
|
f48e48f…
|
drh
|
731 |
WCHAR zTempDirW[MAX_PATH+1]; |
|
f48e48f…
|
drh
|
732 |
const DWORD dwTempSizeW = sizeof(zTempDirW)/sizeof(zTempDirW[0]); |
|
f48e48f…
|
drh
|
733 |
DWORD dwTempLenW; |
|
9396966…
|
drh
|
734 |
#else |
|
e1996e2…
|
mistachkin
|
735 |
int i; |
|
9396966…
|
drh
|
736 |
static const char *azTmp[] = {"/var/tmp","/usr/tmp","/tmp"}; |
|
9396966…
|
drh
|
737 |
#endif |
|
1d6b678…
|
drh
|
738 |
if( g.db ){ |
|
9396966…
|
drh
|
739 |
sqlite3_file_control(g.db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile); |
|
9396966…
|
drh
|
740 |
if( zTFile ) return zTFile; |
|
9396966…
|
drh
|
741 |
} |
|
9396966…
|
drh
|
742 |
sqlite3_randomness(sizeof(r), &r); |
|
9396966…
|
drh
|
743 |
#if _WIN32 |
|
5434d2e…
|
drh
|
744 |
cDirSep = '\\'; |
|
f48e48f…
|
drh
|
745 |
dwTempLenW = GetTempPathW(dwTempSizeW, zTempDirW); |
|
f48e48f…
|
drh
|
746 |
if( dwTempLenW>0 && dwTempLenW<dwTempSizeW |
|
f48e48f…
|
drh
|
747 |
&& ( zTempDirA = fossil_path_to_utf8(zTempDirW) )){ |
|
f48e48f…
|
drh
|
748 |
zDir = zTempDirA; |
|
1d6b678…
|
drh
|
749 |
}else{ |
|
9396966…
|
drh
|
750 |
zDir = fossil_getenv("LOCALAPPDATA"); |
|
9396966…
|
drh
|
751 |
if( zDir==0 ) zDir = "."; |
|
9396966…
|
drh
|
752 |
} |
|
9396966…
|
drh
|
753 |
#else |
|
53db40e…
|
drh
|
754 |
for(i=0; i<(int)(sizeof(azTmp)/sizeof(azTmp[0])); i++){ |
|
9396966…
|
drh
|
755 |
struct stat buf; |
|
9396966…
|
drh
|
756 |
zDir = azTmp[i]; |
|
9396966…
|
drh
|
757 |
if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){ |
|
9396966…
|
drh
|
758 |
break; |
|
9396966…
|
drh
|
759 |
} |
|
1d6b678…
|
drh
|
760 |
} |
|
53db40e…
|
drh
|
761 |
if( i>=(int)(sizeof(azTmp)/sizeof(azTmp[0])) ) zDir = "."; |
|
5434d2e…
|
drh
|
762 |
cDirSep = '/'; |
|
5434d2e…
|
drh
|
763 |
#endif |
|
5434d2e…
|
drh
|
764 |
nDir = strlen(zDir); |
|
5434d2e…
|
drh
|
765 |
zSep[1] = 0; |
|
5434d2e…
|
drh
|
766 |
zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep; |
|
f48e48f…
|
drh
|
767 |
zTFile = sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]); |
|
f48e48f…
|
drh
|
768 |
#ifdef _WIN32 |
|
f48e48f…
|
drh
|
769 |
if( zTempDirA ) fossil_path_free(zTempDirA); |
|
f48e48f…
|
drh
|
770 |
#endif |
|
f48e48f…
|
drh
|
771 |
return zTFile; |
|
6e6e4b1…
|
drh
|
772 |
} |
|
6e6e4b1…
|
drh
|
773 |
|
|
6e6e4b1…
|
drh
|
774 |
/* |
|
3f193ba…
|
drh
|
775 |
** Turn memory limits for stack and heap on and off. The argument |
|
3f193ba…
|
drh
|
776 |
** is true to turn memory limits on and false to turn them off. |
|
3f193ba…
|
drh
|
777 |
** |
|
3f193ba…
|
drh
|
778 |
** Memory limits should be enabled at startup, but then turned off |
|
3f193ba…
|
drh
|
779 |
** before starting subprocesses. |
|
6e6e4b1…
|
drh
|
780 |
*/ |
|
3f193ba…
|
drh
|
781 |
void fossil_limit_memory(int onOff){ |
|
6e6e4b1…
|
drh
|
782 |
#if defined(__unix__) |
|
caf2681…
|
andygoth
|
783 |
static sqlite3_int64 origHeap = 10000000000LL; /* 10GB */ |
|
caf2681…
|
andygoth
|
784 |
static sqlite3_int64 origStack = 8000000 ; /* 8MB */ |
|
6e6e4b1…
|
drh
|
785 |
struct rlimit x; |
|
6e6e4b1…
|
drh
|
786 |
|
|
6e6e4b1…
|
drh
|
787 |
#if defined(RLIMIT_DATA) |
|
6e6e4b1…
|
drh
|
788 |
getrlimit(RLIMIT_DATA, &x); |
|
3f193ba…
|
drh
|
789 |
if( onOff ){ |
|
3f193ba…
|
drh
|
790 |
origHeap = x.rlim_cur; |
|
3f193ba…
|
drh
|
791 |
if( sizeof(void*)<8 || sizeof(x.rlim_cur)<8 ){ |
|
caf2681…
|
andygoth
|
792 |
x.rlim_cur = 1000000000 ; /* 1GB on 32-bit systems */ |
|
3f193ba…
|
drh
|
793 |
}else{ |
|
caf2681…
|
andygoth
|
794 |
x.rlim_cur = 10000000000LL; /* 10GB on 64-bit systems */ |
|
3f193ba…
|
drh
|
795 |
} |
|
3f193ba…
|
drh
|
796 |
}else{ |
|
3f193ba…
|
drh
|
797 |
x.rlim_cur = origHeap; |
|
6e6e4b1…
|
drh
|
798 |
} |
|
6e6e4b1…
|
drh
|
799 |
setrlimit(RLIMIT_DATA, &x); |
|
6e6e4b1…
|
drh
|
800 |
#endif /* defined(RLIMIT_DATA) */ |
|
6e6e4b1…
|
drh
|
801 |
#if defined(RLIMIT_STACK) |
|
6e6e4b1…
|
drh
|
802 |
getrlimit(RLIMIT_STACK, &x); |
|
3f193ba…
|
drh
|
803 |
if( onOff ){ |
|
3f193ba…
|
drh
|
804 |
origStack = x.rlim_cur; |
|
3f193ba…
|
drh
|
805 |
x.rlim_cur = 8000000; /* 8MB */ |
|
3f193ba…
|
drh
|
806 |
}else{ |
|
3f193ba…
|
drh
|
807 |
x.rlim_cur = origStack; |
|
6e6e4b1…
|
drh
|
808 |
} |
|
6e6e4b1…
|
drh
|
809 |
setrlimit(RLIMIT_STACK, &x); |
|
6e6e4b1…
|
drh
|
810 |
#endif /* defined(RLIMIT_STACK) */ |
|
6e6e4b1…
|
drh
|
811 |
#endif /* defined(__unix__) */ |
|
7b81a99…
|
drh
|
812 |
} |
|
7b81a99…
|
drh
|
813 |
|
|
64def88…
|
drh
|
814 |
#if defined(HAVE_PLEDGE) |
|
7b81a99…
|
drh
|
815 |
/* |
|
7b81a99…
|
drh
|
816 |
** Interface to pledge() on OpenBSD 5.9 and later. |
|
0e877b0…
|
andybradford
|
817 |
** |
|
8993c33…
|
andybradford
|
818 |
** On platforms that have pledge(), use this routine. |
|
8993c33…
|
andybradford
|
819 |
** On all other platforms, this routine does not exist, but instead |
|
8993c33…
|
andybradford
|
820 |
** a macro defined in config.h is used to provide a no-op. |
|
8993c33…
|
andybradford
|
821 |
*/ |
|
91ccde5…
|
drh
|
822 |
void fossil_pledge(const char *promises){ |
|
91ccde5…
|
drh
|
823 |
if( pledge(promises, 0) ){ |
|
3f5ab71…
|
drh
|
824 |
fossil_panic("pledge(\"%s\",NULL) fails with errno=%d", |
|
91ccde5…
|
drh
|
825 |
promises, (int)errno); |
|
64def88…
|
drh
|
826 |
} |
|
64def88…
|
drh
|
827 |
} |
|
64def88…
|
drh
|
828 |
#endif /* defined(HAVE_PLEDGE) */ |
|
23a9f9b…
|
drh
|
829 |
|
|
23a9f9b…
|
drh
|
830 |
/* |
|
23a9f9b…
|
drh
|
831 |
** Construct a random password and return it as a string. N is the |
|
23a9f9b…
|
drh
|
832 |
** recommended number of characters for the password. |
|
23a9f9b…
|
drh
|
833 |
** |
|
23a9f9b…
|
drh
|
834 |
** Space to hold the returned string is obtained from fossil_malloc() |
|
23a9f9b…
|
drh
|
835 |
** and should be freed by the caller. |
|
23a9f9b…
|
drh
|
836 |
*/ |
|
23a9f9b…
|
drh
|
837 |
char *fossil_random_password(int N){ |
|
23a9f9b…
|
drh
|
838 |
char zSrc[60]; |
|
23a9f9b…
|
drh
|
839 |
int nSrc; |
|
23a9f9b…
|
drh
|
840 |
int i; |
|
23a9f9b…
|
drh
|
841 |
char z[60]; |
|
23a9f9b…
|
drh
|
842 |
|
|
23a9f9b…
|
drh
|
843 |
/* Source characters for the password. Omit characters like "0", "O", |
|
23a9f9b…
|
drh
|
844 |
** "1" and "I" that might be easily confused */ |
|
5182a1b…
|
drh
|
845 |
static const char zAlphabet[] = |
|
23a9f9b…
|
drh
|
846 |
/* 0 1 2 3 4 5 */ |
|
23a9f9b…
|
drh
|
847 |
/* 123456789 123456789 123456789 123456789 123456789 123456 */ |
|
23a9f9b…
|
drh
|
848 |
"23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; |
|
23a9f9b…
|
drh
|
849 |
|
|
23a9f9b…
|
drh
|
850 |
if( N<8 ) N = 8; |
|
23a9f9b…
|
drh
|
851 |
nSrc = sizeof(zAlphabet) - 1; |
|
931b97e…
|
drh
|
852 |
if( N>nSrc ) N = nSrc; |
|
23a9f9b…
|
drh
|
853 |
memcpy(zSrc, zAlphabet, nSrc); |
|
23a9f9b…
|
drh
|
854 |
|
|
23a9f9b…
|
drh
|
855 |
for(i=0; i<N; i++){ |
|
23a9f9b…
|
drh
|
856 |
unsigned r; |
|
23a9f9b…
|
drh
|
857 |
sqlite3_randomness(sizeof(r), &r); |
|
23a9f9b…
|
drh
|
858 |
r %= nSrc; |
|
23a9f9b…
|
drh
|
859 |
z[i] = zSrc[r]; |
|
23a9f9b…
|
drh
|
860 |
zSrc[r] = zSrc[--nSrc]; |
|
23a9f9b…
|
drh
|
861 |
} |
|
23a9f9b…
|
drh
|
862 |
z[i] = 0; |
|
23a9f9b…
|
drh
|
863 |
return fossil_strdup(z); |
|
23a9f9b…
|
drh
|
864 |
} |
|
23a9f9b…
|
drh
|
865 |
|
|
23a9f9b…
|
drh
|
866 |
/* |
|
23a9f9b…
|
drh
|
867 |
** COMMAND: test-random-password |
|
23a9f9b…
|
drh
|
868 |
** |
|
931b97e…
|
drh
|
869 |
** Usage: %fossil test-random-password [N] [--entropy] |
|
23a9f9b…
|
drh
|
870 |
** |
|
23a9f9b…
|
drh
|
871 |
** Generate a random password string of approximately N characters in length. |
|
931b97e…
|
drh
|
872 |
** If N is omitted, use 12. Values of N less than 8 are changed to 8 |
|
931b97e…
|
drh
|
873 |
** and greater than 57 and changed to 57. |
|
931b97e…
|
drh
|
874 |
** |
|
931b97e…
|
drh
|
875 |
** If the --entropy flag is included, the number of bits of entropy in |
|
931b97e…
|
drh
|
876 |
** the password is show as well. |
|
23a9f9b…
|
drh
|
877 |
*/ |
|
23a9f9b…
|
drh
|
878 |
void test_random_password(void){ |
|
931b97e…
|
drh
|
879 |
int N = 12; |
|
931b97e…
|
drh
|
880 |
int showEntropy = 0; |
|
931b97e…
|
drh
|
881 |
int i; |
|
931b97e…
|
drh
|
882 |
char *zPassword; |
|
931b97e…
|
drh
|
883 |
for(i=2; i<g.argc; i++){ |
|
931b97e…
|
drh
|
884 |
const char *z = g.argv[i]; |
|
931b97e…
|
drh
|
885 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
931b97e…
|
drh
|
886 |
if( strcmp(z,"-entropy")==0 ){ |
|
931b97e…
|
drh
|
887 |
showEntropy = 1; |
|
931b97e…
|
drh
|
888 |
}else if( fossil_isdigit(z[0]) ){ |
|
931b97e…
|
drh
|
889 |
N = atoi(z); |
|
931b97e…
|
drh
|
890 |
if( N<8 ) N = 8; |
|
931b97e…
|
drh
|
891 |
if( N>57 ) N = 57; |
|
931b97e…
|
drh
|
892 |
}else{ |
|
931b97e…
|
drh
|
893 |
usage("[N] [--entropy]"); |
|
931b97e…
|
drh
|
894 |
} |
|
931b97e…
|
drh
|
895 |
} |
|
931b97e…
|
drh
|
896 |
zPassword = fossil_random_password(N); |
|
931b97e…
|
drh
|
897 |
if( showEntropy ){ |
|
931b97e…
|
drh
|
898 |
double et = 57.0; |
|
931b97e…
|
drh
|
899 |
for(i=1; i<N; i++) et *= 57-i; |
|
931b97e…
|
drh
|
900 |
fossil_print("%s (%d bits of entropy)\n", zPassword, |
|
931b97e…
|
drh
|
901 |
(int)(log(et)/log(2.0))); |
|
931b97e…
|
drh
|
902 |
}else{ |
|
931b97e…
|
drh
|
903 |
fossil_print("%s\n", zPassword); |
|
931b97e…
|
drh
|
904 |
} |
|
931b97e…
|
drh
|
905 |
fossil_free(zPassword); |
|
931b97e…
|
drh
|
906 |
} |
|
931b97e…
|
drh
|
907 |
|
|
931b97e…
|
drh
|
908 |
/* |
|
062bb67…
|
danield
|
909 |
** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4). |
|
062bb67…
|
danield
|
910 |
** |
|
062bb67…
|
danield
|
911 |
** Format: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx |
|
062bb67…
|
danield
|
912 |
** where M=4 and N=8, 9, a, or b (this leaves 122 random bits) |
|
062bb67…
|
danield
|
913 |
*/ |
|
062bb67…
|
danield
|
914 |
char* fossil_generate_uuid() { |
|
062bb67…
|
danield
|
915 |
static const char zDigits[] = "0123456789abcdef"; |
|
062bb67…
|
danield
|
916 |
unsigned char aBlob[16]; |
|
062bb67…
|
danield
|
917 |
unsigned char zStr[37]; |
|
062bb67…
|
danield
|
918 |
unsigned char *p = zStr; |
|
062bb67…
|
danield
|
919 |
int i, k; |
|
062bb67…
|
danield
|
920 |
|
|
062bb67…
|
danield
|
921 |
sqlite3_randomness(16, aBlob); |
|
524474c…
|
danield
|
922 |
aBlob[6] = (aBlob[6]&0x0f) | 0x40; /* Version byte: 0100 xxxx */ |
|
524474c…
|
danield
|
923 |
aBlob[8] = (aBlob[8]&0x3f) | 0x80; /* Variant byte: 10xx xxxx */ |
|
062bb67…
|
danield
|
924 |
|
|
062bb67…
|
danield
|
925 |
for(i=0, k=0x550; i<16; i++, k=k>>1){ |
|
062bb67…
|
danield
|
926 |
if( k&1 ){ |
|
062bb67…
|
danield
|
927 |
*p++ = '-'; /* Add a dash after byte 4, 6, 8, and 12 */ |
|
062bb67…
|
danield
|
928 |
} |
|
062bb67…
|
danield
|
929 |
*p++ = zDigits[aBlob[i]>>4]; |
|
062bb67…
|
danield
|
930 |
*p++ = zDigits[aBlob[i]&0xf]; |
|
062bb67…
|
danield
|
931 |
} |
|
062bb67…
|
danield
|
932 |
*p = 0; |
|
062bb67…
|
danield
|
933 |
|
|
a8ef11b…
|
danield
|
934 |
return fossil_strdup((char*)zStr); |
|
062bb67…
|
danield
|
935 |
} |
|
062bb67…
|
danield
|
936 |
|
|
062bb67…
|
danield
|
937 |
/* |
|
062bb67…
|
danield
|
938 |
** COMMAND: test-generate-uuid |
|
062bb67…
|
danield
|
939 |
** |
|
062bb67…
|
danield
|
940 |
** Usage: %fossil test-generate-uuid |
|
062bb67…
|
danield
|
941 |
** |
|
062bb67…
|
danield
|
942 |
** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4): |
|
062bb67…
|
danield
|
943 |
** |
|
062bb67…
|
danield
|
944 |
** xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx - where M=4 and N=8, 9, a, or b |
|
062bb67…
|
danield
|
945 |
*/ |
|
062bb67…
|
danield
|
946 |
void test_generate_uuid(void){ |
|
062bb67…
|
danield
|
947 |
fossil_print("%s\n", fossil_generate_uuid()); |
|
062bb67…
|
danield
|
948 |
} |
|
062bb67…
|
danield
|
949 |
|
|
062bb67…
|
danield
|
950 |
/* |
|
5182a1b…
|
drh
|
951 |
** Return the number of decimal digits in a nonnegative integer. This is useful |
|
5182a1b…
|
drh
|
952 |
** when formatting text. |
|
5182a1b…
|
drh
|
953 |
*/ |
|
5182a1b…
|
drh
|
954 |
int fossil_num_digits(int n){ |
|
5182a1b…
|
drh
|
955 |
return n< 10 ? 1 : n< 100 ? 2 : n< 1000 ? 3 |
|
5182a1b…
|
drh
|
956 |
: n< 10000 ? 4 : n< 100000 ? 5 : n< 1000000 ? 6 |
|
5182a1b…
|
drh
|
957 |
: n<10000000 ? 7 : n<100000000 ? 8 : n<1000000000 ? 9 : 10; |
|
f2fcdbc…
|
drh
|
958 |
} |
|
f2fcdbc…
|
drh
|
959 |
|
|
f2fcdbc…
|
drh
|
960 |
/* |
|
f2fcdbc…
|
drh
|
961 |
** Search for an executable on the PATH environment variable. |
|
f2fcdbc…
|
drh
|
962 |
** Return true (1) if found and false (0) if not found. |
|
fc60e44…
|
drh
|
963 |
** |
|
fc60e44…
|
drh
|
964 |
** Print the full pathname of the first location if ePrint==1. Print |
|
fc60e44…
|
drh
|
965 |
** all pathnames for the executable if ePrint==2 or more. |
|
f2fcdbc…
|
drh
|
966 |
*/ |
|
fc60e44…
|
drh
|
967 |
int fossil_app_on_path(const char *zBinary, int ePrint){ |
|
f2fcdbc…
|
drh
|
968 |
const char *zPath = fossil_getenv("PATH"); |
|
f2fcdbc…
|
drh
|
969 |
char *zFull; |
|
f2fcdbc…
|
drh
|
970 |
int i; |
|
f2fcdbc…
|
drh
|
971 |
int bExists; |
|
fc60e44…
|
drh
|
972 |
int bFound = 0; |
|
f2fcdbc…
|
drh
|
973 |
while( zPath && zPath[0] ){ |
|
fc60e44…
|
drh
|
974 |
#ifdef _WIN32 |
|
fc60e44…
|
drh
|
975 |
while( zPath[0]==';' ) zPath++; |
|
fc60e44…
|
drh
|
976 |
for(i=0; zPath[i] && zPath[i]!=';'; i++){} |
|
fc60e44…
|
drh
|
977 |
zFull = mprintf("%.*s\\%s.exe", i, zPath, zBinary); |
|
fc60e44…
|
drh
|
978 |
bExists = file_access(zFull, R_OK); |
|
fc60e44…
|
drh
|
979 |
if( bExists!=0 ){ |
|
fc60e44…
|
drh
|
980 |
fossil_free(zFull); |
|
fc60e44…
|
drh
|
981 |
zFull = mprintf("%.*s\\%s.bat", i, zPath, zBinary); |
|
fc60e44…
|
drh
|
982 |
bExists = file_access(zFull, R_OK); |
|
fc60e44…
|
drh
|
983 |
} |
|
fc60e44…
|
drh
|
984 |
#else |
|
f2fcdbc…
|
drh
|
985 |
while( zPath[0]==':' ) zPath++; |
|
f2fcdbc…
|
drh
|
986 |
for(i=0; zPath[i] && zPath[i]!=':'; i++){} |
|
f2fcdbc…
|
drh
|
987 |
zFull = mprintf("%.*s/%s", i, zPath, zBinary); |
|
f2fcdbc…
|
drh
|
988 |
bExists = file_access(zFull, X_OK); |
|
fc60e44…
|
drh
|
989 |
#endif |
|
fc60e44…
|
drh
|
990 |
if( bExists==0 && ePrint ){ |
|
fc60e44…
|
drh
|
991 |
fossil_print("%s\n", zFull); |
|
fc60e44…
|
drh
|
992 |
} |
|
f2fcdbc…
|
drh
|
993 |
fossil_free(zFull); |
|
fc60e44…
|
drh
|
994 |
if( bExists==0 ){ |
|
fc60e44…
|
drh
|
995 |
if( ePrint<2 ) return 1; |
|
fc60e44…
|
drh
|
996 |
bFound = 1; |
|
fc60e44…
|
drh
|
997 |
} |
|
f2fcdbc…
|
drh
|
998 |
zPath += i; |
|
f2fcdbc…
|
drh
|
999 |
} |
|
fc60e44…
|
drh
|
1000 |
return bFound; |
|
f2fcdbc…
|
drh
|
1001 |
} |
|
f2fcdbc…
|
drh
|
1002 |
|
|
f2fcdbc…
|
drh
|
1003 |
/* |
|
f2fcdbc…
|
drh
|
1004 |
** Return the name of a command that will launch a web-browser. |
|
f2fcdbc…
|
drh
|
1005 |
*/ |
|
f2fcdbc…
|
drh
|
1006 |
const char *fossil_web_browser(void){ |
|
f2fcdbc…
|
drh
|
1007 |
const char *zBrowser = 0; |
|
f2fcdbc…
|
drh
|
1008 |
#if defined(_WIN32) |
|
0d8c928…
|
drh
|
1009 |
zBrowser = db_get("web-browser", "start \"\""); |
|
f2fcdbc…
|
drh
|
1010 |
#elif defined(__DARWIN__) || defined(__APPLE__) || defined(__HAIKU__) |
|
f2fcdbc…
|
drh
|
1011 |
zBrowser = db_get("web-browser", "open"); |
|
f2fcdbc…
|
drh
|
1012 |
#else |
|
f2fcdbc…
|
drh
|
1013 |
zBrowser = db_get("web-browser", 0); |
|
f2fcdbc…
|
drh
|
1014 |
if( zBrowser==0 ){ |
|
f2fcdbc…
|
drh
|
1015 |
static const char *const azBrowserProg[] = |
|
f2fcdbc…
|
drh
|
1016 |
{ "xdg-open", "gnome-open", "firefox", "google-chrome" }; |
|
f2fcdbc…
|
drh
|
1017 |
int i; |
|
f2fcdbc…
|
drh
|
1018 |
zBrowser = "echo"; |
|
f2fcdbc…
|
drh
|
1019 |
for(i=0; i<count(azBrowserProg); i++){ |
|
fc60e44…
|
drh
|
1020 |
if( fossil_app_on_path(azBrowserProg[i],0) ){ |
|
f2fcdbc…
|
drh
|
1021 |
zBrowser = azBrowserProg[i]; |
|
f2fcdbc…
|
drh
|
1022 |
break; |
|
f2fcdbc…
|
drh
|
1023 |
} |
|
f2fcdbc…
|
drh
|
1024 |
} |
|
36bd542…
|
drh
|
1025 |
zBrowser = mprintf("%s 2>/dev/null", zBrowser); |
|
f2fcdbc…
|
drh
|
1026 |
} |
|
f2fcdbc…
|
drh
|
1027 |
#endif |
|
f2fcdbc…
|
drh
|
1028 |
return zBrowser; |
|
9413395…
|
drh
|
1029 |
} |
|
9413395…
|
drh
|
1030 |
|
|
9413395…
|
drh
|
1031 |
/* |
|
9413395…
|
drh
|
1032 |
** On non-Windows systems, calls nice(2) with the given level. Errors |
|
9413395…
|
drh
|
1033 |
** are ignored. On Windows this is a no-op. |
|
9413395…
|
drh
|
1034 |
*/ |
|
9413395…
|
drh
|
1035 |
void fossil_nice(int level){ |
|
9413395…
|
drh
|
1036 |
#ifndef _WIN32 |
|
7425082…
|
mgagnon
|
1037 |
/* dummy if() condition to avoid nuisance warning about unused result on |
|
7425082…
|
mgagnon
|
1038 |
certain compiler */ |
|
7425082…
|
mgagnon
|
1039 |
if( nice(level) ){ /*ignored*/ } |
|
9413395…
|
drh
|
1040 |
#else |
|
9413395…
|
drh
|
1041 |
(void)level; |
|
9413395…
|
drh
|
1042 |
#endif |
|
9413395…
|
drh
|
1043 |
} |
|
9413395…
|
drh
|
1044 |
|
|
9413395…
|
drh
|
1045 |
/* |
|
9413395…
|
drh
|
1046 |
** Calls fossil_nice() with a default level. |
|
9413395…
|
drh
|
1047 |
*/ |
|
9413395…
|
drh
|
1048 |
void fossil_nice_default(void){ |
|
2c89202…
|
stephan
|
1049 |
fossil_nice(19); |
|
23a9f9b…
|
drh
|
1050 |
} |