1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
/*
* file.c: system specific functions for reading/writing files
*
* See file.h for formal definitions of what these functions do
*
* Rob McCool
*/
#include "base/file.h"
#ifdef BSD_RLIMIT
#include <sys/time.h>
#include <sys/resource.h>
#else
#include <stdlib.h>
#include <signal.h>
#endif
#ifdef XP_WIN32
#include <time.h> /* time */
#include <sys/stat.h> /* stat */
#include <errno.h>
#include <direct.h>
#include <base/nterr.h>
/* Removed for ns security integration
#include <xp_error.h>
*/
#endif
#include <prerror.h>
#include "private/pprio.h"
#include "prlock.h"
extern "C" char *nscperror_lookup(int err);
/* --- globals -------------------------------------------------------------*/
/* PRFileDesc * SYS_ERROR_FD = NULL; */
const int errbuf_size = 256;
const unsigned int LOCKFILERANGE=0x7FFFFFFF;
PRLock *_atomic_write_lock = NULL;
/* --------------------------------- stat --------------------------------- */
/* XXXMB - Can't convert to PR_GetFileInfo because we directly exported
* the stat interface... Damn.
*/
NSAPI_PUBLIC int system_stat(char *path, struct stat *finfo)
{
#ifdef XP_WIN32
int chop, l;
/* The NT stat is very peculiar about directory names. */
/* XXX aruna - there is a bug here, maybe in the C runtime.
* Stating the same path in a separate program succeeds. From
* jblack's profiling, this needs to be replaced by the Win32
* calls anyway.*/
l = strlen(path);
if((path[l - 1] == '/') &&
(!(isalpha(path[0]) && (!strcmp(&path[1], ":/")))))
{
chop = 1;
path[--l] = '\0';
}
else chop = 0;
#endif /* XP_WIN32 */
#ifdef XP_UNIX
if(stat(path, finfo) == -1)
return -1;
#else /* XP_WIN32 */
if(_stat(path, (struct _stat *)finfo) == -1) {
/* XXXMB - this sucks;
* try to convert to an error code we'll expect...
*/
switch(errno) {
case ENOENT: PR_SetError(PR_FILE_NOT_FOUND_ERROR, errno); break;
default: PR_SetError(PR_UNKNOWN_ERROR, errno); break;
}
return -1;
}
/* NT sets the time fields to -1 if it thinks that the file
* is a device ( like com1.html, lpt1.html etc) In this case
* simply set last modified time to the current time....
*/
if (finfo->st_mtime == -1) {
finfo->st_mtime = time(NULL);
}
if (finfo->st_atime == -1) {
finfo->st_atime = 0;
}
if (finfo->st_ctime == -1) {
finfo->st_ctime = 0;
}
if(chop)
path[l++] = '/';
#endif /* XP_WIN32 */
if(S_ISREG(finfo->st_mode) && (path[strlen(path) - 1] == '/')) {
/* File with trailing slash */
errno = ENOENT;
return -1;
}
return 0;
}
NSAPI_PUBLIC int system_fread(SYS_FILE fd, char *buf, int sz)
{
/* XXXMB - this is the *one* function which does return a length
* instead of the IO_ERROR/IO_OKAY.
*/
return PR_Read(fd, buf, sz);
}
NSAPI_PUBLIC int system_fwrite(SYS_FILE fd, char *buf, int sz) {
int n,o,w;
for(n=sz,o=0; n; n-=w,o+=w) {
if((w = PR_Write(fd, &buf[o], n)) < 0)
return IO_ERROR;
}
return IO_OKAY;
}
/* ---------------------------- Standard UNIX ----------------------------- */
#ifdef XP_UNIX
#include <sys/file.h> /* flock */
NSAPI_PUBLIC int system_fwrite_atomic(SYS_FILE fd, char *buf, int sz)
{
int ret;
#if 0
if(flock(fd,LOCK_EX) == -1)
return IO_ERROR;
#endif
ret = system_fwrite(fd,buf,sz);
#if 0
if(flock(fd,LOCK_UN) == -1)
return IO_ERROR; /* ??? */
#endif
return ret;
}
/* -------------------------- system_nocoredumps -------------------------- */
NSAPI_PUBLIC int system_nocoredumps(void)
{
#ifdef BSD_RLIMIT
struct rlimit rl;
rl.rlim_cur = 0;
rl.rlim_max = 0;
return setrlimit(RLIMIT_CORE, &rl);
#else
#if defined(SNI)
/* C++ compiler seems to find more that one overloaded instance of exit() ?! */
#define EXITFUNC ::exit
#else
#define EXITFUNC exit
#endif
signal(SIGQUIT, EXITFUNC);
signal(SIGILL, EXITFUNC);
signal(SIGTRAP, EXITFUNC);
signal(SIGABRT, EXITFUNC);
signal(SIGIOT, EXITFUNC);
signal(SIGEMT, EXITFUNC);
signal(SIGFPE, EXITFUNC);
signal(SIGBUS, EXITFUNC);
signal(SIGSEGV, EXITFUNC);
signal(SIGSYS, EXITFUNC);
return 0;
#endif
}
#endif /* XP_UNIX */
/* --------------------------- file_setinherit ---------------------------- */
NSAPI_PUBLIC int file_setinherit(SYS_FILE fd, int value)
{
#if defined(XP_WIN32)
int ret;
// ret = SetHandleInformation((HANDLE)PR_FileDesc2NativeHandle(fd), 0, value?HANDLE_FLAG_INHERIT:0);
// This function did nothing before since the mask was set to 0.
ret = SetHandleInformation((HANDLE)PR_FileDesc2NativeHandle(fd), HANDLE_FLAG_INHERIT, value?HANDLE_FLAG_INHERIT:0);
return ret==0?-1:0;
#elif defined(XP_UNIX)
int flags = 0;
PRInt32 nativeFD;
PRFileDesc *bottom = fd;
while (bottom->lower != NULL) {
bottom = bottom->lower;
}
nativeFD = PR_FileDesc2NativeHandle(bottom);
#if 0
fprintf(stderr, "\nInfo(file_setinherit): Native file descriptor is %d\n", nativeFD);
#endif
flags = fcntl(nativeFD, F_GETFD, 0);
if(flags == -1)
return -1;
if(value)
flags &= (~FD_CLOEXEC);
else
flags |= FD_CLOEXEC;
fcntl(nativeFD, F_SETFD, flags);
return 0;
/* Comment out for ns security/ nspr integration (HACK for NOW)
int flags = fcntl(PR_FileDesc2NativeHandle(fd), F_GETFD, 0);
if(flags == -1)
return -1;
if(value)
flags &= (~FD_CLOEXEC);
else
flags |= FD_CLOEXEC;
fcntl(PR_FileDesc2NativeHandle(fd), F_SETFD, flags);
return 0;
*/
#endif
}
NSAPI_PUBLIC SYS_FILE system_fopenRO(char *p)
{
SYS_FILE f = PR_Open(p, PR_RDONLY, 0);
if (!f)
return SYS_ERROR_FD;
return f;
}
NSAPI_PUBLIC SYS_FILE system_fopenWA(char *p)
{
SYS_FILE f = PR_Open(p, PR_RDWR|PR_CREATE_FILE|PR_APPEND, 0644);
if (!f)
return SYS_ERROR_FD;
return f;
}
NSAPI_PUBLIC SYS_FILE system_fopenRW(char *p)
{
SYS_FILE f = PR_Open(p, PR_RDWR|PR_CREATE_FILE, 0644);
if (!f)
return SYS_ERROR_FD;
return f;
}
NSAPI_PUBLIC SYS_FILE system_fopenWT(char *p)
{
SYS_FILE f = PR_Open(p, PR_RDWR|PR_CREATE_FILE|PR_TRUNCATE, 0644);
if (!f)
return SYS_ERROR_FD;
return f;
}
NSAPI_PUBLIC int system_fclose(SYS_FILE fd)
{
return (PR_Close(fd));
}
#ifdef FILE_WIN32
int CgiBuffering;
NSAPI_PUBLIC SYS_FILE system_fopen(char *path, int access, int flags)
{
char p2[MAX_PATH];
SYS_FILE ret;
HANDLE fd;
if (strlen(path) >= MAX_PATH) {
return SYS_ERROR_FD;
}
file_unix2local(path, p2);
fd = CreateFile(p2, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, flags, 0, NULL);
ret = PR_ImportFile((int32)fd);
if(ret == INVALID_HANDLE_VALUE)
return SYS_ERROR_FD;
return ret;
}
NSAPI_PUBLIC int system_pread(SYS_FILE fd, char *buf, int BytesToRead) {
unsigned long BytesRead = 0;
int result = 0;
BOOLEAN TimeoutSet = FALSE;
/* XXXMB - nspr20 should be able to do this; but right now it doesn't
* return proper error info.
* fix it later...
*/
if(ReadFile((HANDLE)PR_FileDesc2NativeHandle(fd), (LPVOID)buf, BytesToRead, &BytesRead, NULL) == FALSE) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
return IO_EOF;
} else {
return IO_ERROR;
}
}
return (BytesRead ? BytesRead : IO_EOF);
}
NSAPI_PUBLIC int system_pwrite(SYS_FILE fd, char *buf, int BytesToWrite)
{
unsigned long BytesWritten;
if (WriteFile((HANDLE)PR_FileDesc2NativeHandle(fd), (LPVOID)buf,
BytesToWrite, &BytesWritten, NULL) == FALSE) {
return IO_ERROR;
}
return BytesWritten;
}
NSAPI_PUBLIC int system_fwrite_atomic(SYS_FILE fd, char *buf, int sz)
{
int ret;
#if 0
if(system_flock(fd) == IO_ERROR)
return IO_ERROR;
#endif
/* XXXMB - this is technically thread unsafe, but it catches any
* callers of fwrite_atomic when we're single threaded and just coming
* to life.
*/
if (!_atomic_write_lock) {
_atomic_write_lock = PR_NewLock();
}
PR_Lock(_atomic_write_lock);
ret = system_fwrite(fd,buf,sz);
PR_Unlock(_atomic_write_lock);
#if 0
if(system_ulock(fd) == IO_ERROR)
return IO_ERROR;
#endif
return ret;
}
NSAPI_PUBLIC void file_unix2local(char *path, char *p2)
{
/* Try to handle UNIX-style paths */
if((!strchr(path, FILE_PATHSEP))) {
int x;
for(x = 0; path[x]; x++)
p2[x] = (path[x] == '/' ? '\\' : path[x]);
p2[x] = '\0';
}
else
strcpy(p2, path);
}
NSAPI_PUBLIC int system_nocoredumps(void)
{
return 0;
}
/* --------------------------- system_winerr ------------------------------ */
#include <winsock.h>
#include <errno.h>
#include "util.h"
NSAPI_PUBLIC char *system_winsockerr(void)
{
int errn = WSAGetLastError();
return FindError(errn);
}
NSAPI_PUBLIC char *system_winerr(void)
{
int errn = GetLastError();
if (errn == 0)
errn = WSAGetLastError();
return FindError(errn);
}
/* ------------------------- Dir related stuff ---------------------------- */
NSAPI_PUBLIC SYS_DIR dir_open(char *pathp)
{
dir_s *ret = (dir_s *) MALLOC(sizeof(dir_s));
char path[MAX_PATH];
int l;
if (strlen(pathp) >= MAX_PATH) {
return NULL;
}
l = util_sprintf(path, "%s", pathp) - 1;
path[strlen(pathp)] = '\0';
if(path[strlen(path) - 1] != FILE_PATHSEP)
strcpy (path + strlen(path), "\\*.*");
else
util_sprintf(path, "%s*.*", path);
ret->de.d_name = NULL;
if( (ret->dp = FindFirstFile(path, &ret->fdata)) != INVALID_HANDLE_VALUE)
return ret;
FREE(ret);
return NULL;
}
NSAPI_PUBLIC SYS_DIRENT *dir_read(SYS_DIR ds)
{
if(FindNextFile(ds->dp, &ds->fdata) == FALSE)
return NULL;
if(ds->de.d_name)
FREE(ds->de.d_name);
ds->de.d_name = STRDUP(ds->fdata.cFileName);
return &ds->de;
}
NSAPI_PUBLIC void dir_close(SYS_DIR ds)
{
FindClose(ds->dp);
if(ds->de.d_name)
FREE(ds->de.d_name);
FREE(ds);
}
#endif /* FILE_WIN32 */
NSAPI_PUBLIC int file_notfound(void)
{
#ifdef FILE_WIN32
int errn = PR_GetError();
return (errn == PR_FILE_NOT_FOUND_ERROR);
#else
return (errno == ENOENT);
#endif
}
NSAPI_PUBLIC int dir_create_all(char *dir)
{
struct stat fi;
char *t;
#ifdef XP_WIN32
t = dir + 3;
#else /* XP_UNIX */
t = dir + 1;
#endif
while(1) {
t = strchr(t, FILE_PATHSEP);
if(t) *t = '\0';
if(stat(dir, &fi) == -1) {
if(dir_create(dir) == -1)
return -1;
}
if(t) *t++ = FILE_PATHSEP;
else break;
}
return 0;
}
#ifdef XP_UNIX
#if !defined(SNI) && !defined(LINUX)
extern char *sys_errlist[];
#endif /* SNI */
#endif
#ifdef NET_SSL
#define ERRMSG_SIZE 35
#ifdef THREAD_ANY
static int errmsg_key = -1;
#include "systhr.h"
/* Removed for ns security integration
#include "xp_error.h"
*/
#else
static char errmsg[ERRMSG_SIZE];
#endif
#include "util.h"
static char *_errmsg_new(int code)
{
char *ret;
#ifdef THREAD_ANY
if(!(ret = (char *) systhread_getdata(errmsg_key))) {
ret = (char *) PERM_MALLOC(256);
systhread_setdata(errmsg_key, (void *)ret);
}
#else
ret = errmsg;
#endif
util_snprintf(ret, ERRMSG_SIZE, "libsec code %d", code);
#ifndef MCC_BATMAN
PR_SetError(0,0);
#endif
return ret;
}
#endif
void system_errmsg_init(void)
{
if (errmsg_key == -1) {
#if defined(THREAD_ANY) && defined(NET_SSL)
errmsg_key = systhread_newkey();
#endif
#ifdef XP_WIN32
HashNtErrors();
#endif
if (!_atomic_write_lock)
_atomic_write_lock = PR_NewLock();
}
}
NSAPI_PUBLIC int system_errmsg_fn(char **buff, size_t maxlen)
{
char static_error[128];
char *lmsg = 0; /* Local message pointer */
size_t msglen = 0;
int sys_error = 0;
PRErrorCode nscp_error;
#ifdef XP_WIN32
LPTSTR sysmsg = 0;
#endif
/* Grab the OS error message */
#ifdef XP_WIN32
sys_error = GetLastError();
#else
sys_error = errno;
#endif
nscp_error = PR_GetError();
/* If there is a NSPR error, but it is "unknown", try to get the OSError
* and use that instead.
*/
if (nscp_error == PR_UNKNOWN_ERROR)
errno = PR_GetOSError();
if (nscp_error != 0 && nscp_error != PR_UNKNOWN_ERROR){
char *nscp_error_msg;
nscp_error_msg = nscperror_lookup(nscp_error);
if(nscp_error_msg){
PR_SetError(0, 0);
lmsg = nscp_error_msg;
} else {
util_snprintf(static_error, ERRMSG_SIZE, "unknown error %d", nscp_error);
lmsg = static_error;
}
} else {
#if defined(XP_WIN32)
msglen = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
GetLastError(),
LOCALE_SYSTEM_DEFAULT,
(LPTSTR)&sysmsg,
0,
0);
if (msglen > 0)
lmsg = sysmsg;
else
lmsg = system_winerr();
SetLastError(0);
#else
/* replaced
#if defined(SNI) || defined(LINUX)
/ C++ platform has no definition for sys_errlist /
lmsg = strerror(errno);
#else
lmsg = sys_errlist[errno];
#endif
with lmsg =strerror(errno);*/
lmsg=strerror(errno);
errno = 0;
#endif
}
/* At this point lmsg points to something. */
msglen = strlen(lmsg);
if (*buff == NULL)
*buff = STRDUP(lmsg);
else if (maxlen > msglen)
memcpy(*buff, lmsg, msglen+1);
else
msglen = 0;
#ifdef XP_WIN32
/* NT's FormatMessage() dynamically allocated the msg; free it */
if (sysmsg)
LocalFree(sysmsg);
#endif
return msglen;
}
NSAPI_PUBLIC char *
system_errmsg(void)
{
char *buff = 0;
if (errmsg_key == -1)
return "unknown early startup error";
// rmaxwell - This is extremely lame.
// Allocate a buffer in thread local storage to
// hold the last error message.
// The whole error message facility is broken and should be
// updated to get error strings out of the code.
if(!(buff = (char *) systhread_getdata(errmsg_key))) {
buff = (char *) PERM_MALLOC(errbuf_size);
systhread_setdata(errmsg_key, (void *)buff);
}
system_errmsg_fn(&buff, errbuf_size);
if (buff == 0)
buff = "Could not retrieve system error message";
return buff;
}
NSAPI_PUBLIC int
system_rename(char *oldpath, char *newpath)
{
return rename(oldpath, newpath);
}
NSAPI_PUBLIC int
system_unlink(char *path)
{
return PR_Delete(path)==PR_FAILURE?-1:0;
}
NSAPI_PUBLIC int system_lseek(SYS_FILE fd, int off, int wh)
{
switch (wh) {
case 0:
return PR_Seek(fd, off, PR_SEEK_SET);
break;
case 1:
return PR_Seek(fd, off, PR_SEEK_CUR);
break;
case 2:
return PR_Seek(fd, off, PR_SEEK_END);
break;
default:
return -1;
}
}
NSAPI_PUBLIC int
system_tlock(SYS_FILE fd)
{
return PR_TLockFile(fd);
}
NSAPI_PUBLIC int
system_flock(SYS_FILE fd)
{
return PR_LockFile(fd);
}
NSAPI_PUBLIC int
system_ulock(SYS_FILE fd)
{
return PR_UnlockFile(fd);
}
|