andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #include andre@0: #include andre@0: andre@0: #ifdef XP_UNIX andre@0: #if defined(AIX) || defined(QNX) andre@0: /* To pick up sysconf */ andre@0: #include andre@0: #else andre@0: /* To pick up getrlimit, setrlimit */ andre@0: #include andre@0: #include andre@0: #endif andre@0: #endif /* XP_UNIX */ andre@0: andre@0: extern PRLock *_pr_flock_lock; andre@0: extern PRCondVar *_pr_flock_cv; andre@0: andre@0: static PRInt32 PR_CALLBACK FileRead(PRFileDesc *fd, void *buf, PRInt32 amount) andre@0: { andre@0: PRInt32 rv = 0; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: rv = -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: rv = -1; andre@0: } andre@0: if (rv == -1) andre@0: return rv; andre@0: andre@0: rv = _PR_MD_READ(fd, buf, amount); andre@0: if (rv < 0) { andre@0: PR_ASSERT(rv == -1); andre@0: } andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, ("read -> %d", rv)); andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK FileWrite(PRFileDesc *fd, const void *buf, PRInt32 amount) andre@0: { andre@0: PRInt32 rv = 0; andre@0: PRInt32 temp, count; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: rv = -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: rv = -1; andre@0: } andre@0: if (rv != 0) andre@0: return rv; andre@0: andre@0: count = 0; andre@0: #if !defined(_PR_HAVE_O_APPEND) /* Bugzilla: 4090, 276330 */ andre@0: if (fd->secret->appendMode) { andre@0: if (PR_Seek64(fd, 0, PR_SEEK_END) == -1) { andre@0: return -1; andre@0: } andre@0: } /* if (fd->secret->appendMode...) */ andre@0: #endif /* _PR_HAVE_O_APPEND */ andre@0: while (amount > 0) { andre@0: temp = _PR_MD_WRITE(fd, buf, amount); andre@0: if (temp < 0) { andre@0: count = -1; andre@0: break; andre@0: } andre@0: count += temp; andre@0: if (fd->secret->nonblocking) { andre@0: break; andre@0: } andre@0: buf = (const void*) ((const char*)buf + temp); andre@0: amount -= temp; andre@0: } andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, ("write -> %d", count)); andre@0: return count; andre@0: } andre@0: andre@0: static PROffset32 PR_CALLBACK FileSeek(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence) andre@0: { andre@0: PROffset32 result; andre@0: andre@0: result = _PR_MD_LSEEK(fd, offset, whence); andre@0: return result; andre@0: } andre@0: andre@0: static PROffset64 PR_CALLBACK FileSeek64(PRFileDesc *fd, PROffset64 offset, PRSeekWhence whence) andre@0: { andre@0: PROffset64 result; andre@0: andre@0: result = _PR_MD_LSEEK64(fd, offset, whence); andre@0: return result; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK FileAvailable(PRFileDesc *fd) andre@0: { andre@0: PRInt32 result, cur, end; andre@0: andre@0: cur = _PR_MD_LSEEK(fd, 0, PR_SEEK_CUR); andre@0: andre@0: if (cur >= 0) andre@0: end = _PR_MD_LSEEK(fd, 0, PR_SEEK_END); andre@0: andre@0: if ((cur < 0) || (end < 0)) { andre@0: return -1; andre@0: } andre@0: andre@0: result = end - cur; andre@0: _PR_MD_LSEEK(fd, cur, PR_SEEK_SET); andre@0: andre@0: return result; andre@0: } andre@0: andre@0: static PRInt64 PR_CALLBACK FileAvailable64(PRFileDesc *fd) andre@0: { andre@0: PRInt64 result, cur, end; andre@0: PRInt64 minus_one; andre@0: andre@0: LL_I2L(minus_one, -1); andre@0: cur = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_CUR); andre@0: andre@0: if (LL_GE_ZERO(cur)) andre@0: end = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_END); andre@0: andre@0: if (!LL_GE_ZERO(cur) || !LL_GE_ZERO(end)) return minus_one; andre@0: andre@0: LL_SUB(result, end, cur); andre@0: (void)_PR_MD_LSEEK64(fd, cur, PR_SEEK_SET); andre@0: andre@0: return result; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK PipeAvailable(PRFileDesc *fd) andre@0: { andre@0: PRInt32 rv; andre@0: rv = _PR_MD_PIPEAVAILABLE(fd); andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt64 PR_CALLBACK PipeAvailable64(PRFileDesc *fd) andre@0: { andre@0: PRInt64 rv; andre@0: LL_I2L(rv, _PR_MD_PIPEAVAILABLE(fd)); andre@0: return rv; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK PipeSync(PRFileDesc *fd) andre@0: { andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK FileGetInfo(PRFileDesc *fd, PRFileInfo *info) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_GETOPENFILEINFO(fd, info); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK FileGetInfo64(PRFileDesc *fd, PRFileInfo64 *info) andre@0: { andre@0: /* $$$$ NOT YET IMPLEMENTED */ andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_GETOPENFILEINFO64(fd, info); andre@0: if (rv < 0) return PR_FAILURE; andre@0: else return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd) andre@0: { andre@0: PRInt32 result; andre@0: result = _PR_MD_FSYNC(fd); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK FileClose(PRFileDesc *fd) andre@0: { andre@0: if (!fd || !fd->secret andre@0: || (fd->secret->state != _PR_FILEDESC_OPEN andre@0: && fd->secret->state != _PR_FILEDESC_CLOSED)) { andre@0: PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: if (fd->secret->state == _PR_FILEDESC_OPEN) { andre@0: if (_PR_MD_CLOSE_FILE(fd->secret->md.osfd) < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: fd->secret->state = _PR_FILEDESC_CLOSED; andre@0: } andre@0: PR_FreeFileDesc(fd); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRInt16 PR_CALLBACK FilePoll( andre@0: PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags) andre@0: { andre@0: *out_flags = 0; andre@0: return in_flags; andre@0: } /* FilePoll */ andre@0: andre@0: static PRIOMethods _pr_fileMethods = { andre@0: PR_DESC_FILE, andre@0: FileClose, andre@0: FileRead, andre@0: FileWrite, andre@0: FileAvailable, andre@0: FileAvailable64, andre@0: FileSync, andre@0: FileSeek, andre@0: FileSeek64, andre@0: FileGetInfo, andre@0: FileGetInfo64, andre@0: (PRWritevFN)_PR_InvalidInt, andre@0: (PRConnectFN)_PR_InvalidStatus, andre@0: (PRAcceptFN)_PR_InvalidDesc, andre@0: (PRBindFN)_PR_InvalidStatus, andre@0: (PRListenFN)_PR_InvalidStatus, andre@0: (PRShutdownFN)_PR_InvalidStatus, andre@0: (PRRecvFN)_PR_InvalidInt, andre@0: (PRSendFN)_PR_InvalidInt, andre@0: (PRRecvfromFN)_PR_InvalidInt, andre@0: (PRSendtoFN)_PR_InvalidInt, andre@0: FilePoll, andre@0: (PRAcceptreadFN)_PR_InvalidInt, andre@0: (PRTransmitfileFN)_PR_InvalidInt, andre@0: (PRGetsocknameFN)_PR_InvalidStatus, andre@0: (PRGetpeernameFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRGetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSendfileFN)_PR_InvalidInt, andre@0: (PRConnectcontinueFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: PR_IMPLEMENT(const PRIOMethods*) PR_GetFileMethods(void) andre@0: { andre@0: return &_pr_fileMethods; andre@0: } andre@0: andre@0: static PRIOMethods _pr_pipeMethods = { andre@0: PR_DESC_PIPE, andre@0: FileClose, andre@0: FileRead, andre@0: FileWrite, andre@0: PipeAvailable, andre@0: PipeAvailable64, andre@0: PipeSync, andre@0: (PRSeekFN)_PR_InvalidInt, andre@0: (PRSeek64FN)_PR_InvalidInt64, andre@0: (PRFileInfoFN)_PR_InvalidStatus, andre@0: (PRFileInfo64FN)_PR_InvalidStatus, andre@0: (PRWritevFN)_PR_InvalidInt, andre@0: (PRConnectFN)_PR_InvalidStatus, andre@0: (PRAcceptFN)_PR_InvalidDesc, andre@0: (PRBindFN)_PR_InvalidStatus, andre@0: (PRListenFN)_PR_InvalidStatus, andre@0: (PRShutdownFN)_PR_InvalidStatus, andre@0: (PRRecvFN)_PR_InvalidInt, andre@0: (PRSendFN)_PR_InvalidInt, andre@0: (PRRecvfromFN)_PR_InvalidInt, andre@0: (PRSendtoFN)_PR_InvalidInt, andre@0: FilePoll, andre@0: (PRAcceptreadFN)_PR_InvalidInt, andre@0: (PRTransmitfileFN)_PR_InvalidInt, andre@0: (PRGetsocknameFN)_PR_InvalidStatus, andre@0: (PRGetpeernameFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRGetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSendfileFN)_PR_InvalidInt, andre@0: (PRConnectcontinueFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: PR_IMPLEMENT(const PRIOMethods*) PR_GetPipeMethods(void) andre@0: { andre@0: return &_pr_pipeMethods; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_Open(const char *name, PRIntn flags, PRIntn mode) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd = 0; andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: /* Map pr open flags and mode to os specific flags */ andre@0: andre@0: osfd = _PR_MD_OPEN(name, flags, mode); andre@0: if (osfd != -1) { andre@0: fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); andre@0: if (!fd) { andre@0: (void) _PR_MD_CLOSE_FILE(osfd); andre@0: } else { andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: fd->secret->appendMode = appendMode; andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); andre@0: } andre@0: } andre@0: return fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_OpenFile( andre@0: const char *name, PRIntn flags, PRIntn mode) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd = 0; andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: /* Map pr open flags and mode to os specific flags */ andre@0: andre@0: osfd = _PR_MD_OPEN_FILE(name, flags, mode); andre@0: if (osfd != -1) { andre@0: fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); andre@0: if (!fd) { andre@0: (void) _PR_MD_CLOSE_FILE(osfd); andre@0: } else { andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: fd->secret->appendMode = appendMode; andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); andre@0: } andre@0: } andre@0: return fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_GetSysfdTableMax(void) andre@0: { andre@0: #if defined(XP_UNIX) && !defined(AIX) && !defined(QNX) andre@0: struct rlimit rlim; andre@0: andre@0: if ( getrlimit(RLIMIT_NOFILE, &rlim) < 0) { andre@0: /* XXX need to call PR_SetError() */ andre@0: return -1; andre@0: } andre@0: andre@0: return rlim.rlim_max; andre@0: #elif defined(AIX) || defined(QNX) andre@0: return sysconf(_SC_OPEN_MAX); andre@0: #elif defined(WIN32) andre@0: /* andre@0: * There is a systemwide limit of 65536 user handles. andre@0: */ andre@0: return 16384; andre@0: #elif defined (WIN16) andre@0: return FOPEN_MAX; andre@0: #elif defined(XP_OS2) andre@0: ULONG ulReqCount = 0; andre@0: ULONG ulCurMaxFH = 0; andre@0: DosSetRelMaxFH(&ulReqCount, &ulCurMaxFH); andre@0: return ulCurMaxFH; andre@0: #elif defined(XP_BEOS) andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return -1; andre@0: #else andre@0: write me; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_SetSysfdTableSize(int table_size) andre@0: { andre@0: #if defined(XP_UNIX) && !defined(AIX) && !defined(QNX) andre@0: struct rlimit rlim; andre@0: PRInt32 tableMax = PR_GetSysfdTableMax(); andre@0: andre@0: if (tableMax < 0) andre@0: return -1; andre@0: andre@0: if (tableMax > FD_SETSIZE) andre@0: tableMax = FD_SETSIZE; andre@0: andre@0: rlim.rlim_max = tableMax; andre@0: andre@0: /* Grow as much as we can; even if too big */ andre@0: if ( rlim.rlim_max < table_size ) andre@0: rlim.rlim_cur = rlim.rlim_max; andre@0: else andre@0: rlim.rlim_cur = table_size; andre@0: andre@0: if ( setrlimit(RLIMIT_NOFILE, &rlim) < 0) { andre@0: /* XXX need to call PR_SetError() */ andre@0: return -1; andre@0: } andre@0: andre@0: return rlim.rlim_cur; andre@0: #elif defined(XP_OS2) andre@0: PRInt32 tableMax = PR_GetSysfdTableMax(); andre@0: if (table_size > tableMax) { andre@0: APIRET rc = NO_ERROR; andre@0: rc = DosSetMaxFH(table_size); andre@0: if (rc == NO_ERROR) andre@0: return table_size; andre@0: else andre@0: return -1; andre@0: } andre@0: return tableMax; andre@0: #elif defined(AIX) || defined(QNX) \ andre@0: || defined(WIN32) || defined(WIN16) || defined(XP_BEOS) andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return -1; andre@0: #else andre@0: write me; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_Delete(const char *name) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_DELETE(name); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetFileInfo(const char *fn, PRFileInfo *info) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_GETFILEINFO(fn, info); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetFileInfo64(const char *fn, PRFileInfo64 *info) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: rv = _PR_MD_GETFILEINFO64(fn, info); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else { andre@0: return PR_SUCCESS; andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_Rename(const char *from, const char *to) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_RENAME(from, to); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_Access(const char *name, PRAccessHow how) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_ACCESS(name, how); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: ** Import an existing OS file to NSPR andre@0: */ andre@0: PR_IMPLEMENT(PRFileDesc*) PR_ImportFile(PROsfd osfd) andre@0: { andre@0: PRFileDesc *fd = NULL; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); andre@0: if( !fd ) { andre@0: (void) _PR_MD_CLOSE_FILE(osfd); andre@0: } else { andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); andre@0: } andre@0: andre@0: return fd; andre@0: } andre@0: andre@0: /* andre@0: ** Import an existing OS pipe to NSPR andre@0: */ andre@0: PR_IMPLEMENT(PRFileDesc*) PR_ImportPipe(PROsfd osfd) andre@0: { andre@0: PRFileDesc *fd = NULL; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: fd = PR_AllocFileDesc(osfd, &_pr_pipeMethods); andre@0: if( !fd ) { andre@0: (void) _PR_MD_CLOSE_FILE(osfd); andre@0: } else { andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); andre@0: #ifdef WINNT andre@0: fd->secret->md.sync_file_io = PR_TRUE; andre@0: #endif andre@0: } andre@0: andre@0: return fd; andre@0: } andre@0: andre@0: #ifndef NO_NSPR_10_SUPPORT andre@0: /* andre@0: ** PR_Stat() for Win16 is defined in w16io.c andre@0: ** it is a hack to circumvent problems in Gromit and Java andre@0: ** See also: BugSplat: 98516. andre@0: */ andre@0: #if !defined(WIN16) andre@0: /* andre@0: * This function is supposed to be for backward compatibility with andre@0: * nspr 1.0. Therefore, it still uses the nspr 1.0 error-reporting andre@0: * mechanism -- returns a PRInt32, which is the error code when the call andre@0: * fails. andre@0: * andre@0: * If we need this function in nspr 2.0, it should be changed to andre@0: * return PRStatus, as follows: andre@0: * andre@0: * PR_IMPLEMENT(PRStatus) PR_Stat(const char *name, struct stat *buf) andre@0: * { andre@0: * PRInt32 rv; andre@0: * andre@0: * rv = _PR_MD_STAT(name, buf); andre@0: * if (rv < 0) andre@0: * return PR_FAILURE; andre@0: * else andre@0: * return PR_SUCCESS; andre@0: * } andre@0: * andre@0: * -- wtc, 2/14/97. andre@0: */ andre@0: PR_IMPLEMENT(PRInt32) PR_Stat(const char *name, struct stat *buf) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: rv = _PR_MD_STAT(name, buf); andre@0: return rv; andre@0: } andre@0: andre@0: #endif /* !defined(WIN16) */ andre@0: #endif /* ! NO_NSPR_10_SUPPORT */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_LockFile(PRFileDesc *fd) andre@0: { andre@0: PRStatus status = PR_SUCCESS; andre@0: andre@0: #ifdef WINNT andre@0: if (!fd->secret->md.io_model_committed) { andre@0: PRInt32 rv; andre@0: rv = _md_Associate((HANDLE)fd->secret->md.osfd); andre@0: PR_ASSERT(0 != rv); andre@0: fd->secret->md.io_model_committed = PR_TRUE; andre@0: } andre@0: #endif andre@0: andre@0: PR_Lock(_pr_flock_lock); andre@0: while (fd->secret->lockCount == -1) andre@0: PR_WaitCondVar(_pr_flock_cv, PR_INTERVAL_NO_TIMEOUT); andre@0: if (fd->secret->lockCount == 0) { andre@0: fd->secret->lockCount = -1; andre@0: PR_Unlock(_pr_flock_lock); andre@0: status = _PR_MD_LOCKFILE(fd->secret->md.osfd); andre@0: PR_Lock(_pr_flock_lock); andre@0: fd->secret->lockCount = (status == PR_SUCCESS) ? 1 : 0; andre@0: PR_NotifyAllCondVar(_pr_flock_cv); andre@0: } else { andre@0: fd->secret->lockCount++; andre@0: } andre@0: PR_Unlock(_pr_flock_lock); andre@0: andre@0: return status; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_TLockFile(PRFileDesc *fd) andre@0: { andre@0: PRStatus status = PR_SUCCESS; andre@0: andre@0: #ifdef WINNT andre@0: if (!fd->secret->md.io_model_committed) { andre@0: PRInt32 rv; andre@0: rv = _md_Associate((HANDLE)fd->secret->md.osfd); andre@0: PR_ASSERT(0 != rv); andre@0: fd->secret->md.io_model_committed = PR_TRUE; andre@0: } andre@0: #endif andre@0: andre@0: PR_Lock(_pr_flock_lock); andre@0: if (fd->secret->lockCount == 0) { andre@0: status = _PR_MD_TLOCKFILE(fd->secret->md.osfd); andre@0: PR_ASSERT(status == PR_SUCCESS || fd->secret->lockCount == 0); andre@0: if (status == PR_SUCCESS) andre@0: fd->secret->lockCount = 1; andre@0: } else { andre@0: fd->secret->lockCount++; andre@0: } andre@0: PR_Unlock(_pr_flock_lock); andre@0: andre@0: return status; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_UnlockFile(PRFileDesc *fd) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: andre@0: PR_Lock(_pr_flock_lock); andre@0: if (fd->secret->lockCount == 1) { andre@0: rv = _PR_MD_UNLOCKFILE(fd->secret->md.osfd); andre@0: if (rv == PR_SUCCESS) andre@0: fd->secret->lockCount = 0; andre@0: } else { andre@0: fd->secret->lockCount--; andre@0: } andre@0: PR_Unlock(_pr_flock_lock); andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_CreatePipe( andre@0: PRFileDesc **readPipe, andre@0: PRFileDesc **writePipe andre@0: ) andre@0: { andre@0: #if defined(WIN32) && !defined(WINCE) andre@0: HANDLE readEnd, writeEnd; andre@0: SECURITY_ATTRIBUTES pipeAttributes; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: ZeroMemory(&pipeAttributes, sizeof(pipeAttributes)); andre@0: pipeAttributes.nLength = sizeof(pipeAttributes); andre@0: pipeAttributes.bInheritHandle = TRUE; andre@0: if (CreatePipe(&readEnd, &writeEnd, &pipeAttributes, 0) == 0) { andre@0: PR_SetError(PR_UNKNOWN_ERROR, GetLastError()); andre@0: return PR_FAILURE; andre@0: } andre@0: *readPipe = PR_AllocFileDesc((PROsfd)readEnd, &_pr_pipeMethods); andre@0: if (NULL == *readPipe) { andre@0: CloseHandle(readEnd); andre@0: CloseHandle(writeEnd); andre@0: return PR_FAILURE; andre@0: } andre@0: *writePipe = PR_AllocFileDesc((PROsfd)writeEnd, &_pr_pipeMethods); andre@0: if (NULL == *writePipe) { andre@0: PR_Close(*readPipe); andre@0: CloseHandle(writeEnd); andre@0: return PR_FAILURE; andre@0: } andre@0: #ifdef WINNT andre@0: (*readPipe)->secret->md.sync_file_io = PR_TRUE; andre@0: (*writePipe)->secret->md.sync_file_io = PR_TRUE; andre@0: #endif andre@0: (*readPipe)->secret->inheritable = _PR_TRI_TRUE; andre@0: (*writePipe)->secret->inheritable = _PR_TRI_TRUE; andre@0: return PR_SUCCESS; andre@0: #elif defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS) andre@0: #ifdef XP_OS2 andre@0: HFILE pipefd[2]; andre@0: #else andre@0: int pipefd[2]; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #ifdef XP_OS2 andre@0: if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) { andre@0: #else andre@0: if (pipe(pipefd) == -1) { andre@0: #endif andre@0: /* XXX map pipe error */ andre@0: PR_SetError(PR_UNKNOWN_ERROR, errno); andre@0: return PR_FAILURE; andre@0: } andre@0: *readPipe = PR_AllocFileDesc(pipefd[0], &_pr_pipeMethods); andre@0: if (NULL == *readPipe) { andre@0: close(pipefd[0]); andre@0: close(pipefd[1]); andre@0: return PR_FAILURE; andre@0: } andre@0: *writePipe = PR_AllocFileDesc(pipefd[1], &_pr_pipeMethods); andre@0: if (NULL == *writePipe) { andre@0: PR_Close(*readPipe); andre@0: close(pipefd[1]); andre@0: return PR_FAILURE; andre@0: } andre@0: #ifndef XP_BEOS /* Pipes are nonblocking on BeOS */ andre@0: _PR_MD_MAKE_NONBLOCK(*readPipe); andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(*readPipe, PR_FALSE); andre@0: #ifndef XP_BEOS /* Pipes are nonblocking on BeOS */ andre@0: _PR_MD_MAKE_NONBLOCK(*writePipe); andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(*writePipe, PR_FALSE); andre@0: return PR_SUCCESS; andre@0: #else andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: andre@0: #ifdef MOZ_UNICODE andre@0: /* ================ UTF16 Interfaces ================================ */ andre@0: PR_IMPLEMENT(PRFileDesc*) PR_OpenFileUTF16( andre@0: const PRUnichar *name, PRIntn flags, PRIntn mode) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd = 0; andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: /* Map pr open flags and mode to os specific flags */ andre@0: osfd = _PR_MD_OPEN_FILE_UTF16(name, flags, mode); andre@0: if (osfd != -1) { andre@0: fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); andre@0: if (!fd) { andre@0: (void) _PR_MD_CLOSE_FILE(osfd); andre@0: } else { andre@0: #if !defined(_PR_HAVE_O_APPEND) andre@0: fd->secret->appendMode = appendMode; andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); andre@0: } andre@0: } andre@0: return fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetFileInfo64UTF16(const PRUnichar *fn, PRFileInfo64 *info) andre@0: { andre@0: PRInt32 rv; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: rv = _PR_MD_GETFILEINFO64_UTF16(fn, info); andre@0: if (rv < 0) { andre@0: return PR_FAILURE; andre@0: } else { andre@0: return PR_SUCCESS; andre@0: } andre@0: } andre@0: andre@0: /* ================ UTF16 Interfaces ================================ */ andre@0: #endif /* MOZ_UNICODE */