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