Annotation of parser3/src/main/pa_common.C, revision 1.143.2.2
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly functions.
3:
1.143 paf 4: Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.101 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111 paf 6: */
1.16 paf 7:
1.143.2.2! paf 8: static const char* IDENT_COMMON_C="$Date: 2003/01/23 17:05:34 $";
1.1 paf 9:
10: #include "pa_common.h"
1.4 paf 11: #include "pa_exception.h"
1.14 paf 12: #include "pa_globals.h"
1.126 paf 13: #include "pa_hash.h"
1.1 paf 14:
1.98 paf 15: #ifdef WIN32
16: # include <windows.h>
1.126 paf 17: #else
18: # define closesocket close
1.98 paf 19: #endif
20:
1.93 paf 21: // some maybe-undefined constants
22:
1.82 paf 23: #ifndef _O_TEXT
24: # define _O_TEXT 0
25: #endif
26: #ifndef _O_BINARY
27: # define _O_BINARY 0
1.47 paf 28: #endif
1.80 paf 29:
1.138 paf 30: #ifdef HAVE_FTRUNCATE
31: # define PA_O_TRUNC 0
32: #else
33: # ifdef _O_TRUNC
34: # define PA_O_TRUNC _O_TRUNC
35: # else
36: # error you must have either ftruncate function or _O_TRUNC bit declared
37: # endif
38: #endif
39:
1.93 paf 40: // locking constants
41:
1.99 paf 42: #ifdef HAVE_FLOCK
43:
44: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
45: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
46: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
47: static int unlock(int fd) { return flock(fd, LOCK_UN); }
48:
1.98 paf 49: #else
1.99 paf 50: #ifdef HAVE__LOCKING
1.98 paf 51:
1.126 paf 52: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1)
1.99 paf 53: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
54: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
55: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
56: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93 paf 57:
1.99 paf 58: #else
59: #ifdef HAVE_FCNTL
1.93 paf 60:
1.126 paf 61: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls)
1.99 paf 62: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
63: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
64: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
65: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93 paf 66:
67: #else
68: #ifdef HAVE_LOCKF
1.99 paf 69:
1.126 paf 70: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1)
1.99 paf 71: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
72: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
73: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
74: static int unlock(int fd) { FLOCK(F_TLOCK); }
75:
1.93 paf 76: #else
1.99 paf 77:
78: #error unable to find file locking func
79:
80: #endif
1.93 paf 81: #endif
82: #endif
83: #endif
84:
1.127 paf 85: #define DEFAULT_USER_AGENT "parser3"
86:
87:
1.126 paf 88: void fix_line_breaks(char* buf, size_t& size) {
1.139 paf 89: if(size==0)
90: return;
91:
1.87 paf 92: //_asm int 3;
1.126 paf 93: const char* const eob=buf+size;
94: char* dest=buf;
1.72 parser 95: // fix DOS: \r\n -> \n
96: // fix Macintosh: \r -> \n
1.126 paf 97: char* bol=buf;
1.137 paf 98: while(char* eol=(char*)memchr(bol, '\r', eob -bol)) {
1.72 parser 99: size_t len=eol-bol;
100: if(dest!=bol)
1.126 paf 101: memcpy(dest, bol, len);
1.72 parser 102: dest+=len;
1.126 paf 103: *dest++='\n';
1.72 parser 104:
1.126 paf 105: if(&eol[1]<eob && eol[1]=='\n') { // \r, \n = DOS
1.72 parser 106: bol=eol+2;
1.126 paf 107: size--;
108: } else // \r, not \n = Macintosh
1.72 parser 109: bol=eol+1;
110: }
111: // last piece without \r, including terminating 0
112: if(dest!=bol)
1.126 paf 113: memcpy(dest, bol, eob-bol);
1.72 parser 114: }
1.18 paf 115:
1.143.2.1 paf 116: char* file_read_text(Pool& pool,
117: ConstStringPtr file_spec,
1.126 paf 118: bool fail_on_read_problem,
1.143.2.1 paf 119: StringHash *params, StringHash** out_fields) {
1.72 parser 120: void *result; size_t size;
1.143.2.1 paf 121: return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)?
122: (char *)result:0;
1.126 paf 123: }
124:
125: //http request stuff
126: /* ************************ http stuff *********************** */
127:
128: static bool set_addr(struct sockaddr_in *addr, const char* host, const short port){
129: memset(addr, 0, sizeof(*addr));
130: addr->sin_family=AF_INET;
131: addr->sin_port=htons(port);
132: if(host) {
133: if(struct hostent *hostIP=gethostbyname(host))
134: memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length);
135: else
136: return false;
137: } else
138: addr->sin_addr.s_addr=INADDR_ANY;
139: return true;
140: }
141:
1.143.2.1 paf 142: static int http_read_response(Pool& pool, String& response, int sock, bool fail_on_status_ne_200){
1.143.2.2! paf 143: int result=0;
1.130 paf 144: ssize_t EOLat=0;
1.126 paf 145: while(true) {
1.143.2.1 paf 146: char *buf=(char *)pool.malloc(MAX_STRING);
1.126 paf 147: ssize_t size=recv(sock, buf, MAX_STRING, 0);
148: if(size<=0)
149: break;
1.130 paf 150: response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0);
1.143.2.2! paf 151: if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response
1.142 paf 152: const String& status_line=response.mid(0, (size_t)EOLat);
153: Array astatus(response.pool());
154: size_t pos_after_ref=0; status_line.split(astatus, &pos_after_ref, " ", 1);
1.143.2.2! paf 155: result=astatus.get_string(1)->as_int();
1.142 paf 156:
157: if(fail_on_status_ne_200 && *status_code!="200")
158: throw Exception("http.status",
159: status_code,
160: "invalid HTTP response status");
161: }
162: }
1.143.2.2! paf 163: if(result)
! 164: return result;
1.142 paf 165: else
166: throw Exception("http.response",
167: 0,
168: "bad response from host - no status found (size=%lu)", response.size());
1.126 paf 169: }
170:
171: /* ********************** request *************************** */
172:
173: #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP)
174: # define WE_CAN_USE_ALARM
175: #endif
176:
177: #ifdef WE_CAN_USE_ALARM
178: static sigjmp_buf timeout_env;
179: static void timeout_handler(int sig){
180: siglongjmp(timeout_env, 1);
181: }
182: #endif
183:
1.142 paf 184: static int http_request(String& response,
1.126 paf 185: const String *origin_string,
186: const char* host, int port,
187: const char* request,
1.142 paf 188: int timeout,
189: bool fail_on_status_ne_200){
1.126 paf 190: if(!host)
191: throw Exception("http.host",
192: origin_string,
193: "zero hostname"); //never
194:
195: #ifdef WE_CAN_USE_ALARM
196: signal(SIGALRM, timeout_handler);
197: #endif
198: int sock=-1;
199: try {
1.142 paf 200: int result;
1.126 paf 201: #ifdef WE_CAN_USE_ALARM
202: if(sigsetjmp(timeout_env, 1))
203: throw Exception("http.timeout",
204: origin_string,
205: "timeout occured while retrieving document");
206: else {
207: alarm(timeout);
208: #endif
209: struct sockaddr_in dest;
210:
211: if(!set_addr(&dest, host, port))
212: throw Exception("http.host",
213: origin_string,
1.127 paf 214: "can not resolve hostname \"%s\"", host);
1.126 paf 215:
216: if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0)
217: throw Exception("http.connect",
218: origin_string,
1.127 paf 219: "can not make socket: %s (%d)", strerror(errno), errno);
1.126 paf 220: if(connect(sock, (struct sockaddr *)&dest, sizeof(dest)))
221: throw Exception("http.connect",
222: origin_string,
1.127 paf 223: "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno);
1.126 paf 224: size_t request_size=strlen(request);
225: if(send(sock, request, request_size, 0)!=(ssize_t)request_size)
226: throw Exception("http.connect",
227: origin_string,
1.127 paf 228: "error sending request: %s (%d)", strerror(errno), errno);
1.126 paf 229:
1.142 paf 230: result=http_read_response(response, sock, fail_on_status_ne_200);
231: closesocket(sock);
1.126 paf 232: #ifdef WE_CAN_USE_ALARM
1.142 paf 233: alarm(0);
1.126 paf 234: }
235: #endif
1.142 paf 236: return result;
1.126 paf 237: } catch(...) {
238: if(sock>=0)
239: closesocket(sock);
240: #ifdef WE_CAN_USE_ALARM
241: alarm(0);
242: #endif
243: /*re*/throw;
244: }
245: }
246:
1.127 paf 247: #ifndef DOXYGEN
248: struct Http_pass_header_info {
249: String* request;
250: bool user_agent_specified;
251: };
252: #endif
253: static void http_pass_header(const Hash::Key& key, Hash::Val *value, void *info)
1.126 paf 254: {
1.127 paf 255: Http_pass_header_info& i=*static_cast<Http_pass_header_info *>(info);
256: Pool& pool=i.request->pool();
257:
1.135 paf 258: *(i.request)<<key<<": "
1.136 paf 259: << attributed_meaning_to_string(*static_cast<Value *>(value), String::UL_HTTP_HEADER, false)
1.135 paf 260: <<"\n";
261:
1.127 paf 262: if(key.change_case(pool, String::CC_UPPER)=="USER-AGENT")
263: i.user_agent_specified=true;
1.126 paf 264: }
265: static void file_read_http(Pool& pool, const String& file_spec,
266: void*& data, size_t& data_size,
1.127 paf 267: Hash *options=0, Hash** out_fields=0) {
1.126 paf 268: char host[MAX_STRING];
1.129 paf 269: const char* uri;
1.126 paf 270: int port;
271: const char* method="GET";
272: int timeout=2;
1.142 paf 273: bool fail_on_status_ne_200=true;
1.127 paf 274: Value *vheaders=0;
1.126 paf 275:
1.133 paf 276: String connect_string(pool);
277: // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one
278: connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces
279:
280: char* connect_string_cstr=connect_string.cstr(String::UL_UNSPECIFIED);
281: if(strncmp(connect_string_cstr, "http://", 7)!=0)
1.126 paf 282: throw Exception(0,
1.133 paf 283: &connect_string,
1.126 paf 284: "does not start with http://"); //never
1.133 paf 285: connect_string_cstr+=7;
1.126 paf 286:
1.133 paf 287: strncpy(host, connect_string_cstr, sizeof(host)-1); host[sizeof(host)-1]=0;
1.126 paf 288: char* host_uri=lsplit(host, '/');
1.133 paf 289: uri=host_uri?connect_string_cstr+(host_uri-1-host):"/";
1.126 paf 290: char* port_cstr=lsplit(host, ':');
291: char* error_pos=0;
292: port=port_cstr?strtol(port_cstr, &error_pos, 0):80;
293:
1.127 paf 294: if(options) {
295: int valid_options=0;
296: if(Value *vmethod=static_cast<Value *>(options->get(*http_method_name))) {
297: valid_options++;
298: method=vmethod->as_string().cstr();
299: }
300: if(Value *vtimeout=static_cast<Value *>(options->get(*http_timeout_name))) {
301: valid_options++;
302: timeout=vtimeout->as_int();
303: }
304: if(vheaders=static_cast<Value *>(options->get(*http_headers_name))) {
305: valid_options++;
306: }
1.142 paf 307: if(Value *vany_status=static_cast<Value *>(options->get(*http_any_status_name))) {
308: valid_options++;
309: fail_on_status_ne_200=!vany_status->as_bool();
310: }
311:
1.127 paf 312: if(valid_options!=options->size())
313: throw Exception("parser.runtime",
314: 0,
315: "invalid option passed");
1.133 paf 316: }
1.126 paf 317:
318: //making request
1.127 paf 319: String request(pool);
1.126 paf 320: request<< method <<" "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n";
1.127 paf 321: bool user_agent_specified=false;
322: if(vheaders && !vheaders->is_string()) { // allow empty
1.133 paf 323: if(Hash *headers=vheaders->get_hash(&connect_string)) {
1.127 paf 324: Http_pass_header_info info={&request};
325: headers->for_each(http_pass_header, &info);
326: user_agent_specified=info.user_agent_specified;
327: } else
328: throw Exception("parser.runtime",
1.133 paf 329: &connect_string,
1.127 paf 330: "headers param must be hash");
331: };
332: if(!user_agent_specified) // defaulting
333: request << "user-agent: " DEFAULT_USER_AGENT "\n";
1.126 paf 334: request<<"\n";
335:
336: //sending request
337: String response(pool);
1.142 paf 338: int status_code=http_request(response,
339: &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED),
340: timeout, fail_on_status_ne_200);
1.126 paf 341:
342: //processing results
343: int pos=response.pos("\r\n\r\n", 4);
344: if(pos<1){
345: throw Exception("http.response",
1.133 paf 346: &connect_string,
1.126 paf 347: "bad response from host - no headers found");
348: }
349: String header_block=response.mid(0, pos);
350: String body=response.mid(pos+4, response.size());
351:
352: Array aheaders(pool);
353: Hash& headers=*new(pool) Hash(pool);
354: size_t pos_after_ref=0;
355: header_block.split(aheaders, &pos_after_ref, "\r\n", 2);
356:
357: //processing headers
358: for(int i=1;i<aheaders.size();i++) {
359: if(const String *line=aheaders.get_string(i)) {
360: pos=line->pos(": ", 2);
361: if(pos<1)
362: throw Exception("http.response",
1.133 paf 363: &connect_string,
1.126 paf 364: "bad response from host - bad header \"%s\"", line->cstr());
365:
1.128 paf 366: headers.put(
367: line->mid(0, pos).change_case(pool, String::CC_UPPER),
368: new(pool) VString(line->mid(pos+2, line->size())));
1.126 paf 369: } else
370: throw Exception("http.response",
1.133 paf 371: &connect_string,
1.126 paf 372: "bad response from host - bad headers \"%s\"", header_block.cstr());
373: }
374:
375: // output response
376: data=body.cstr(); data_size=body.size();
1.141 paf 377: if(out_fields) {
1.142 paf 378: headers.put(*file_status_name, new(pool) VInt(pool, status_code));
1.126 paf 379: *out_fields=&headers;
1.141 paf 380: }
1.34 paf 381: }
1.123 paf 382:
383: #ifndef DOXYGEN
384: struct File_read_action_info {
385: void **data; size_t *data_size;
1.126 paf 386: };
1.123 paf 387: #endif
1.143.2.1 paf 388: static void file_read_action(
1.126 paf 389: struct stat& finfo,
1.123 paf 390: int f,
1.126 paf 391: const String& file_spec, const char* fname, bool as_text,
1.123 paf 392: void *context) {
1.126 paf 393: File_read_action_info& info=*static_cast<File_read_action_info *>(context);
1.123 paf 394: if(size_t to_read_size=(size_t)finfo.st_size) {
1.126 paf 395: *info.data=pool.malloc(to_read_size+(as_text?1:0), 3);
396: *info.data_size=(size_t)read(f, *info.data, to_read_size);
1.123 paf 397:
398: if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)
1.126 paf 399: throw Exception(0,
1.123 paf 400: &file_spec,
401: "read failed: actually read %lu bytes count not in [0..%lu] valid range",
1.126 paf 402: *info.data_size, to_read_size);
1.123 paf 403: } else { // empty file
404: if(as_text) {
1.126 paf 405: *info.data=pool.malloc(1);
1.123 paf 406: *(char*)(*info.data)=0;
407: } else
408: *info.data=0;
409: *info.data_size=0;
410: return;
411: }
1.126 paf 412: }
1.143.2.1 paf 413: bool file_read(ConstStringPtr file_spec,
1.126 paf 414: void*& data, size_t& data_size,
415: bool as_text, Hash *params, Hash** out_fields,
416: bool fail_on_read_problem) {
417: bool result;
418: if(file_spec.starts_with("http://", 7)) {
419: // fail on read problem
420: file_read_http(pool, file_spec, data, data_size, params, out_fields);
421: result=true;
422: } else {
423: File_read_action_info info={&data, &data_size};
424: result=file_read_action_under_lock(pool, file_spec,
425: "read", file_read_action, &info,
426: as_text, fail_on_read_problem);
427: }
1.123 paf 428:
1.126 paf 429: if(result && as_text) {
1.131 paf 430: // UTF-8 signature: EF BB BF
431: if(data_size>=3) {
432: char *in=(char *)data;
433: if((in[0] == '\xEF') && (in[1] == '\xBB') &&
434: (in[2] == '\xBF')) {
435: data=in+3; data_size-=3;// skip prefix
436: }
437: }
438:
1.126 paf 439: fix_line_breaks((char *)(data), data_size);
1.123 paf 440: // note: after fixing
1.126 paf 441: ((char*&)(data))[data_size]=0;
1.123 paf 442: }
1.126 paf 443:
444: return result;
1.123 paf 445: }
446:
1.143.2.1 paf 447: bool file_read_action_under_lock(const String& file_spec,
1.126 paf 448: const char* action_name, File_read_action action, void *context,
449: bool as_text,
1.123 paf 450: bool fail_on_read_problem) {
1.126 paf 451: const char* fname=file_spec.cstr(String::UL_FILE_SPEC);
1.33 paf 452: int f;
453:
454: // first open, next stat:
1.45 paf 455: // directory update of NTFS hard links performed on open.
1.33 paf 456: // ex:
457: // a.html:^test[] and b.html hardlink to a.html
458: // user inserts ! before ^test in a.html
1.126 paf 459: // directory entry of b.html in NTFS not updated at once,
1.35 paf 460: // they delay update till open, so we would receive "!^test[" string
461: // if would do stat, next open.
1.123 paf 462: // later: it seems, even this does not help sometimes
1.98 paf 463: if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.123 paf 464: try {
465: if(lock_shared_blocking(f)!=0)
1.126 paf 466: throw Exception("file.lock",
1.123 paf 467: &file_spec,
468: "shared lock failed: %s (%d), actual filename '%s'",
1.126 paf 469: strerror(errno), errno, fname);
1.123 paf 470:
1.124 paf 471: struct stat finfo;
472: if(stat(fname, &finfo)!=0)
473: throw Exception("file.missing", // hardly possible: we just opened it OK
474: &file_spec,
475: "stat failed: %s (%d), actual filename '%s'",
1.126 paf 476: strerror(errno), errno, fname);
1.124 paf 477:
1.140 paf 478: #ifdef PA_SAFE_MODE
479: if(finfo.st_uid/*foreign?*/!=geteuid()
480: && finfo.st_gid/*foreign?*/!=getegid())
1.126 paf 481: throw Exception("parser.runtime",
482: &file_spec,
1.140 paf 483: "parser is in safe mode: reading files of foreign group and user disabled [recompile parser with --disable-safe-mode configure option], actual filename '%s'",
1.126 paf 484: fname);
1.105 paf 485: #endif
1.32 paf 486:
1.126 paf 487: action(pool, finfo, f, file_spec, fname, as_text, context);
1.123 paf 488: } catch(...) {
1.126 paf 489: unlock(f);close(f);
1.123 paf 490: if(fail_on_read_problem)
491: /*re*/throw;
492: return false;
493: }
1.87 paf 494:
1.126 paf 495: unlock(f);close(f);
1.72 parser 496: return true;
1.118 paf 497: } else {
498: if(fail_on_read_problem)
1.126 paf 499: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.118 paf 500: &file_spec,
1.123 paf 501: "%s failed: %s (%d), actual filename '%s'",
1.126 paf 502: action_name, strerror(errno), errno, fname);
1.118 paf 503: return false;
504: }
1.8 paf 505: }
506:
1.63 parser 507: static void create_dir_for_file(const String& file_spec) {
508: size_t pos_after=1;
509: int pos_before;
510: while((pos_before=file_spec.pos("/", 1, pos_after))>=0) {
1.126 paf 511: mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775);
1.63 parser 512: pos_after=pos_before+1;
513: }
514: }
515:
1.98 paf 516: bool file_write_action_under_lock(
1.28 paf 517: const String& file_spec,
1.126 paf 518: const char* action_name, File_write_action action, void *context,
519: bool as_text,
520: bool do_append,
521: bool do_block,
1.110 paf 522: bool fail_on_lock_problem) {
1.126 paf 523: const char* fname=file_spec.cstr(String::UL_FILE_SPEC);
1.28 paf 524: int f;
1.80 paf 525: if(access(fname, W_OK)!=0) // no
1.126 paf 526: create_dir_for_file(file_spec);
1.50 paf 527:
1.80 paf 528: if((f=open(fname,
529: O_CREAT|O_RDWR
530: |(as_text?_O_TEXT:_O_BINARY)
1.138 paf 531: |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) {
1.99 paf 532: if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.126 paf 533: Exception e("file.lock",
1.110 paf 534: &file_spec,
535: "shared lock failed: %s (%d), actual filename '%s'",
1.126 paf 536: strerror(errno), errno, fname);
537: close(f);
1.110 paf 538: if(fail_on_lock_problem)
539: throw e;
1.98 paf 540: return false;
541: }
1.96 paf 542:
543: try {
1.126 paf 544: action(f, context);
1.96 paf 545: } catch(...) {
1.138 paf 546: #ifdef HAVE_FTRUNCATE
1.104 paf 547: if(!do_append)
1.125 paf 548: ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower
1.138 paf 549: #endif
1.126 paf 550: unlock(f);close(f);
1.96 paf 551: /*re*/throw;
552: }
1.80 paf 553:
1.138 paf 554: #ifdef HAVE_FTRUNCATE
1.104 paf 555: if(!do_append)
1.125 paf 556: ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.138 paf 557: #endif
1.126 paf 558: unlock(f);close(f);
1.98 paf 559: return true;
1.80 paf 560: } else
1.126 paf 561: throw Exception(errno==EACCES?"file.access":0,
1.80 paf 562: &file_spec,
1.96 paf 563: "%s failed: %s (%d), actual filename '%s'",
1.126 paf 564: action_name, strerror(errno), errno, fname);
1.96 paf 565: // here should be nothing, see rethrow above
566: }
567:
568: #ifndef DOXYGEN
569: struct File_write_action_info {
570: const void *data; size_t size;
1.126 paf 571: };
1.96 paf 572: #endif
573: static void file_write_action(int f, void *context) {
1.126 paf 574: File_write_action_info& info=*static_cast<File_write_action_info *>(context);
1.113 paf 575: if(info.size) {
1.126 paf 576: int written=write(f, info.data, info.size);
1.116 paf 577: if(written<0)
1.126 paf 578: throw Exception(0,
579: 0,
580: "write failed: %s (%d)", strerror(errno), errno);
1.113 paf 581: }
1.96 paf 582: }
583: void file_write(
584: const String& file_spec,
585: const void *data, size_t size,
1.126 paf 586: bool as_text,
1.96 paf 587: bool do_append) {
1.126 paf 588: File_write_action_info info={data, size};
1.98 paf 589: file_write_action_under_lock(
1.96 paf 590: file_spec,
1.126 paf 591: "write", file_write_action, &info,
592: as_text,
593: do_append);
1.30 paf 594: }
595:
1.63 parser 596: // throws nothing! [this is required in file_move & file_delete]
1.50 paf 597: static void rmdir(const String& file_spec, size_t pos_after) {
598: int pos_before;
599: if((pos_before=file_spec.pos("/", 1, pos_after))>=0)
1.126 paf 600: rmdir(file_spec, pos_before+1);
1.50 paf 601:
1.126 paf 602: rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC));
1.50 paf 603: }
1.95 paf 604: bool file_delete(const String& file_spec, bool fail_on_read_problem) {
1.126 paf 605: const char* fname=file_spec.cstr(String::UL_FILE_SPEC);
1.54 parser 606: if(unlink(fname)!=0)
1.93 paf 607: if(fail_on_read_problem)
1.126 paf 608: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.93 paf 609: &file_spec,
610: "unlink failed: %s (%d), actual filename '%s'",
1.126 paf 611: strerror(errno), errno, fname);
1.93 paf 612: else
613: return false;
1.50 paf 614:
1.126 paf 615: rmdir(file_spec, 1);
1.93 paf 616: return true;
1.60 parser 617: }
1.95 paf 618: void file_move(const String& old_spec, const String& new_spec) {
1.126 paf 619: const char* old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC);
620: const char* new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC);
1.63 parser 621:
1.126 paf 622: create_dir_for_file(new_spec);
1.63 parser 623:
1.60 parser 624: if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126 paf 625: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.60 parser 626: &old_spec,
627: "rename failed: %s (%d), actual filename '%s' to '%s'",
1.126 paf 628: strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63 parser 629:
1.126 paf 630: rmdir(old_spec, 1);
1.31 paf 631: }
632:
1.51 paf 633:
1.126 paf 634: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118 paf 635: struct stat lfinfo;
636: bool result=stat(fname, &lfinfo)==0;
637: if(afinfo)
638: *afinfo=lfinfo;
639: return result;
1.119 paf 640: }
641:
642: bool entry_exists(const String& file_spec) {
1.126 paf 643: const char* fname=file_spec.cstr(String::UL_FILE_SPEC);
644: return entry_exists(fname, 0);
1.118 paf 645: }
646:
1.51 paf 647: static bool entry_readable(const String& file_spec, bool need_dir) {
1.126 paf 648: char* fname=file_spec.cstr(String::UL_FILE_SPEC);
1.120 paf 649: if(need_dir) {
1.126 paf 650: size_t size=strlen(fname);
1.120 paf 651: while(size) {
1.126 paf 652: char c=fname[size-1];
1.120 paf 653: if(c=='/' || c=='\\')
654: fname[--size]=0;
655: else
656: break;
657: }
658: }
1.51 paf 659: struct stat finfo;
1.118 paf 660: if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109 paf 661: bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51 paf 662: return is_dir==need_dir;
663: }
664: return false;
665: }
1.31 paf 666: bool file_readable(const String& file_spec) {
1.126 paf 667: return entry_readable(file_spec, false);
1.51 paf 668: }
669: bool dir_readable(const String& file_spec) {
1.126 paf 670: return entry_readable(file_spec, true);
1.65 parser 671: }
672: String *file_readable(const String& path, const String& name) {
1.126 paf 673: String *result=new(path.pool()) String(path);
674: *result << "/";
1.65 parser 675: *result << name;
676: return file_readable(*result)?result:0;
1.43 paf 677: }
678: bool file_executable(const String& file_spec) {
1.64 parser 679: return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44 paf 680: }
681:
1.64 parser 682: bool file_stat(const String& file_spec,
1.58 parser 683: size_t& rsize,
1.126 paf 684: time_t& ratime,
685: time_t& rmtime,
686: time_t& rctime,
1.64 parser 687: bool fail_on_read_problem) {
1.126 paf 688: Pool& pool=file_spec.pool();
689: const char* fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44 paf 690: struct stat finfo;
691: if(stat(fname, &finfo)!=0)
1.64 parser 692: if(fail_on_read_problem)
1.126 paf 693: throw Exception("file.missing",
1.67 parser 694: &file_spec,
695: "getting file size failed: %s (%d), real filename '%s'",
1.126 paf 696: strerror(errno), errno, fname);
1.64 parser 697: else
698: return false;
1.58 parser 699: rsize=finfo.st_size;
700: ratime=finfo.st_atime;
701: rmtime=finfo.st_mtime;
702: rctime=finfo.st_ctime;
1.64 parser 703: return true;
1.18 paf 704: }
705:
1.126 paf 706: char* getrow(char* *row_ref, char delim) {
707: char* result=*row_ref;
1.8 paf 708: if(result) {
1.126 paf 709: *row_ref=strchr(result, delim);
1.8 paf 710: if(*row_ref)
711: *((*row_ref)++)=0;
712: else if(!*result)
713: return 0;
714: }
715: return result;
716: }
717:
1.126 paf 718: char* lsplit(char* string, char delim) {
1.23 paf 719: if(string) {
1.126 paf 720: char* v=strchr(string, delim);
1.8 paf 721: if(v) {
722: *v=0;
723: return v+1;
724: }
725: }
726: return 0;
727: }
728:
1.126 paf 729: char* lsplit(char* *string_ref, char delim) {
730: char* result=*string_ref;
731: char* next=lsplit(*string_ref, delim);
1.8 paf 732: *string_ref=next;
733: return result;
1.9 paf 734: }
735:
1.126 paf 736: char* rsplit(char* string, char delim) {
1.18 paf 737: if(string) {
1.126 paf 738: char* v=strrchr(string, delim);
1.18 paf 739: if(v) {
1.9 paf 740: *v=0;
741: return v+1;
742: }
743: }
744: return NULL;
1.10 paf 745: }
746:
1.37 paf 747: /// @todo less stupid type detection
1.143.2.1 paf 748: char* format(double value, char* fmt) {
1.126 paf 749: char local_buf[MAX_NUMBER];
1.108 paf 750: size_t size;
751:
1.10 paf 752: if(fmt)
753: if(strpbrk(fmt, "diouxX"))
754: if(strpbrk(fmt, "ouxX"))
1.126 paf 755: size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10 paf 756: else
1.126 paf 757: size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10 paf 758: else
1.126 paf 759: size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10 paf 760: else
1.126 paf 761: size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10 paf 762:
1.126 paf 763: char* pool_buf=(char *)pool.malloc(size+1, 4);
764: memcpy(pool_buf, local_buf, size+1);
1.108 paf 765: return pool_buf;
1.12 paf 766: }
767:
1.36 paf 768: size_t stdout_write(const void *buf, size_t size) {
1.12 paf 769: #ifdef WIN32
770: do{
1.126 paf 771: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
1.12 paf 772: if(chunk_written<=0)
773: break;
774: size-=chunk_written;
1.36 paf 775: buf=((const char*)buf)+chunk_written;
1.126 paf 776: } while(size>0);
1.12 paf 777:
778: return size;
779: #else
1.126 paf 780: return fwrite(buf, 1, size, stdout);
1.12 paf 781: #endif
1.2 paf 782: }
1.14 paf 783:
1.143.2.1 paf 784: char* unescape_chars(const char* cp, int len) {
1.126 paf 785: char* s=(char *)pool.malloc(len + 1, 5);
1.14 paf 786: enum EscapeState {
1.33 paf 787: EscapeRest,
788: EscapeFirst,
1.14 paf 789: EscapeSecond
790: } escapeState=EscapeRest;
791: int escapedValue=0;
792: int srcPos=0;
793: int dstPos=0;
794: while(srcPos < len) {
1.126 paf 795: int ch=cp[srcPos];
1.14 paf 796: switch(escapeState) {
797: case EscapeRest:
798: if(ch=='%') {
799: escapeState=EscapeFirst;
800: } else if(ch=='+') {
1.126 paf 801: s[dstPos++]=' ';
1.14 paf 802: } else {
803: s[dstPos++]=ch;
804: }
805: break;
806: case EscapeFirst:
807: escapedValue=hex_value[ch] << 4;
808: escapeState=EscapeSecond;
809: break;
810: case EscapeSecond:
1.126 paf 811: escapedValue +=hex_value[ch];
1.14 paf 812: s[dstPos++]=escapedValue;
813: escapeState=EscapeRest;
814: break;
815: }
1.126 paf 816: srcPos++;
1.14 paf 817: }
818: s[dstPos]=0;
819: return s;
1.24 paf 820: }
821:
822: #ifdef WIN32
1.126 paf 823: void back_slashes_to_slashes(char* s) {
1.24 paf 824: if(s)
825: for(; *s; s++)
826: if(*s=='\\')
1.126 paf 827: *s='/';
1.24 paf 828: }
1.42 paf 829: /*
1.126 paf 830: void slashes_to_back_slashes(char* s) {
1.42 paf 831: if(s)
832: for(; *s; s++)
833: if(*s=='/')
1.126 paf 834: *s='\\';
1.42 paf 835: }
836: */
1.24 paf 837: #endif
1.41 paf 838:
1.126 paf 839: bool StrEqNc(const char* s1, const char* s2, bool strict) {
1.41 paf 840: while(true) {
841: if(!(*s1)) {
842: if(!(*s2))
843: return true;
844: else
845: return !strict;
846: } else if(!(*s2))
847: return !strict;
848: if(isalpha(*s1)) {
849: if(tolower(*s1) !=tolower(*s2))
850: return false;
851: } else if((*s1) !=(*s2))
852: return false;
1.126 paf 853: s1++;
854: s2++;
1.41 paf 855: }
1.57 parser 856: }
857:
1.84 paf 858: static bool isLeap(int year) {
1.57 parser 859: return !(
860: (year % 4) || ((year % 400) && !(year % 100))
1.126 paf 861: );
1.57 parser 862: }
863:
864: int getMonthDays(int year, int month) {
865: int monthDays[]={
1.126 paf 866: 31,
867: isLeap(year) ? 29 : 28,
868: 31,
869: 30,
870: 31,
871: 30,
872: 31,
873: 31,
874: 30,
875: 31,
876: 30,
1.57 parser 877: 31
1.126 paf 878: };
879: return monthDays[month];
1.41 paf 880: }
1.69 parser 881:
1.126 paf 882: void remove_crlf(char* start, char* end) {
883: for(char* p=start; p<end; p++)
1.69 parser 884: switch(*p) {
1.126 paf 885: case '\n': *p='|'; break;
886: case '\r': *p=' '; break;
1.69 parser 887: }
1.91 paf 888: }
889:
890:
891: /// must be last in this file
892: #undef vsnprintf
1.126 paf 893: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91 paf 894: if(!s)
895: return 0;
896:
897: int r;
898: // note: on win32& maybe somewhere else
899: // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
900: --s;
901: #if _MSC_VER
902: /*
903: win32:
904: mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
905:
906: if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
907: */
1.126 paf 908: r=_vsnprintf(b, s, f, l);
1.91 paf 909: if(r<0)
910: r=s;
911: #else
1.126 paf 912: r=vsnprintf(b, s, f, l);
1.91 paf 913: /*
914: solaris:
915: man vsnprintf
916:
917: The snprintf() function returns the number of characters
918: formatted, that is, the number of characters that would have
919: been written to the buffer if it were large enough. If the
920: value of n is 0 on a call to snprintf(), an unspecified
921: value less than 1 is returned.
922: */
923:
924: if(r<0)
925: r=0;
926: else if(r>s)
927: r=s;
928: #endif
929: b[r]=0;
930: return r;
931: }
932:
1.126 paf 933: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91 paf 934: va_list l;
1.126 paf 935: va_start(l, f);
936: int r=__vsnprintf(b, s, f, l);
937: va_end(l);
1.91 paf 938: return r;
1.98 paf 939: }
940:
941: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.126 paf 942: for (; usecs >= 1000000; ++secs, usecs -= 1000000);
1.98 paf 943:
944: #ifdef WIN32
1.126 paf 945: Sleep(secs * 1000 + usecs / 1000);
1.98 paf 946: return 0;
947: #else
948: struct timeval t;
949: t.tv_sec = secs;
950: t.tv_usec = usecs;
1.126 paf 951: return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
1.98 paf 952: #endif
1.135 paf 953: }
954:
955:
E-mail: