📄 prfile.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "primpl.h"#include <string.h>#include <fcntl.h>#ifdef XP_UNIX#if defined(AIX) || defined(QNX)/* To pick up sysconf */#include <unistd.h>#else/* To pick up getrlimit, setrlimit */#include <sys/time.h>#include <sys/resource.h>#endif#endif /* XP_UNIX */extern PRLock *_pr_flock_lock;extern PRCondVar *_pr_flock_cv;static PRInt32 PR_CALLBACK FileRead(PRFileDesc *fd, void *buf, PRInt32 amount){ PRInt32 rv = 0; PRThread *me = _PR_MD_CURRENT_THREAD(); if (_PR_PENDING_INTERRUPT(me)) { me->flags &= ~_PR_INTERRUPT; PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); rv = -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); rv = -1; } if (rv == -1) return rv; rv = _PR_MD_READ(fd, buf, amount); if (rv < 0) { PR_ASSERT(rv == -1); } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("read -> %d", rv)); return rv;}static PRInt32 PR_CALLBACK FileWrite(PRFileDesc *fd, const void *buf, PRInt32 amount){ PRInt32 rv = 0; PRInt32 temp, count; PRThread *me = _PR_MD_CURRENT_THREAD(); if (_PR_PENDING_INTERRUPT(me)) { me->flags &= ~_PR_INTERRUPT; PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); rv = -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); rv = -1; } if (rv != 0) return rv; count = 0;#if !defined(XP_UNIX) /* BugZilla: 4090 */ if ( PR_TRUE == fd->secret->appendMode ) { rv = PR_Seek(fd, 0, PR_SEEK_END ); if ( -1 == rv ) { return rv; } } /* if (fd->secret->appendMode...) */#endif /* XP_UNIX */ while (amount > 0) { temp = _PR_MD_WRITE(fd, buf, amount); if (temp < 0) { count = -1; break; } count += temp; if (fd->secret->nonblocking) { break; } buf = (const void*) ((const char*)buf + temp); amount -= temp; } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("write -> %d", count)); return count;}static PROffset32 PR_CALLBACK FileSeek(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence){ PROffset32 result; result = _PR_MD_LSEEK(fd, offset, whence); return result;}static PROffset64 PR_CALLBACK FileSeek64(PRFileDesc *fd, PROffset64 offset, PRSeekWhence whence){#ifdef XP_MAC#pragma unused( fd, offset, whence )#endif PROffset64 result; result = _PR_MD_LSEEK64(fd, offset, whence); return result;}static PRInt32 PR_CALLBACK FileAvailable(PRFileDesc *fd){ PRInt32 result, cur, end; cur = _PR_MD_LSEEK(fd, 0, PR_SEEK_CUR); if (cur >= 0) end = _PR_MD_LSEEK(fd, 0, PR_SEEK_END); if ((cur < 0) || (end < 0)) { return -1; } result = end - cur; _PR_MD_LSEEK(fd, cur, PR_SEEK_SET); return result;}static PRInt64 PR_CALLBACK FileAvailable64(PRFileDesc *fd){#ifdef XP_MAC#pragma unused( fd )#endif PRInt64 result, cur, end; PRInt64 minus_one; LL_I2L(minus_one, -1); cur = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_CUR); if (LL_GE_ZERO(cur)) end = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_END); if (!LL_GE_ZERO(cur) || !LL_GE_ZERO(end)) return minus_one; LL_SUB(result, end, cur); (void)_PR_MD_LSEEK64(fd, cur, PR_SEEK_SET); return result;}static PRInt32 PR_CALLBACK PipeAvailable(PRFileDesc *fd){ PRInt32 rv; rv = _PR_MD_PIPEAVAILABLE(fd); return rv; }static PRInt64 PR_CALLBACK PipeAvailable64(PRFileDesc *fd){ PRInt64 rv; LL_I2L(rv, _PR_MD_PIPEAVAILABLE(fd)); return rv; }static PRStatus PR_CALLBACK PipeSync(PRFileDesc *fd){#if defined(XP_MAC)#pragma unused (fd)#endif return PR_SUCCESS;}static PRStatus PR_CALLBACK FileGetInfo(PRFileDesc *fd, PRFileInfo *info){ PRInt32 rv; rv = _PR_MD_GETOPENFILEINFO(fd, info); if (rv < 0) { return PR_FAILURE; } else return PR_SUCCESS;}static PRStatus PR_CALLBACK FileGetInfo64(PRFileDesc *fd, PRFileInfo64 *info){#ifdef XP_MAC#pragma unused( fd, info )#endif /* $$$$ NOT YET IMPLEMENTED */ PRInt32 rv; rv = _PR_MD_GETOPENFILEINFO64(fd, info); if (rv < 0) return PR_FAILURE; else return PR_SUCCESS;}static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd){ PRInt32 result; result = _PR_MD_FSYNC(fd); if (result < 0) { return PR_FAILURE; } return PR_SUCCESS;}static PRStatus PR_CALLBACK FileClose(PRFileDesc *fd){ if (!fd || !fd->secret || (fd->secret->state != _PR_FILEDESC_OPEN && fd->secret->state != _PR_FILEDESC_CLOSED)) { PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); return PR_FAILURE; } if (fd->secret->state == _PR_FILEDESC_OPEN) { if (_PR_MD_CLOSE_FILE(fd->secret->md.osfd) < 0) { return PR_FAILURE; } fd->secret->state = _PR_FILEDESC_CLOSED; } PR_FreeFileDesc(fd); return PR_SUCCESS;}static PRInt16 PR_CALLBACK FilePoll( PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags){#ifdef XP_MAC#pragma unused( fd, in_flags )#endif *out_flags = 0; return in_flags;} /* FilePoll */static PRIOMethods _pr_fileMethods = { PR_DESC_FILE, FileClose, FileRead, FileWrite, FileAvailable, FileAvailable64, FileSync, FileSeek, FileSeek64, FileGetInfo, FileGetInfo64, (PRWritevFN)_PR_InvalidInt, (PRConnectFN)_PR_InvalidStatus, (PRAcceptFN)_PR_InvalidDesc, (PRBindFN)_PR_InvalidStatus, (PRListenFN)_PR_InvalidStatus, (PRShutdownFN)_PR_InvalidStatus, (PRRecvFN)_PR_InvalidInt, (PRSendFN)_PR_InvalidInt, (PRRecvfromFN)_PR_InvalidInt, (PRSendtoFN)_PR_InvalidInt, FilePoll, (PRAcceptreadFN)_PR_InvalidInt, (PRTransmitfileFN)_PR_InvalidInt, (PRGetsocknameFN)_PR_InvalidStatus, (PRGetpeernameFN)_PR_InvalidStatus, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, (PRSendfileFN)_PR_InvalidInt, (PRConnectcontinueFN)_PR_InvalidStatus, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt};PR_IMPLEMENT(const PRIOMethods*) PR_GetFileMethods(void){ return &_pr_fileMethods;}static PRIOMethods _pr_pipeMethods = { PR_DESC_PIPE, FileClose, FileRead, FileWrite, PipeAvailable, PipeAvailable64, PipeSync, (PRSeekFN)_PR_InvalidInt, (PRSeek64FN)_PR_InvalidInt64, (PRFileInfoFN)_PR_InvalidStatus, (PRFileInfo64FN)_PR_InvalidStatus, (PRWritevFN)_PR_InvalidInt, (PRConnectFN)_PR_InvalidStatus, (PRAcceptFN)_PR_InvalidDesc, (PRBindFN)_PR_InvalidStatus, (PRListenFN)_PR_InvalidStatus, (PRShutdownFN)_PR_InvalidStatus, (PRRecvFN)_PR_InvalidInt, (PRSendFN)_PR_InvalidInt, (PRRecvfromFN)_PR_InvalidInt, (PRSendtoFN)_PR_InvalidInt, FilePoll, (PRAcceptreadFN)_PR_InvalidInt, (PRTransmitfileFN)_PR_InvalidInt, (PRGetsocknameFN)_PR_InvalidStatus, (PRGetpeernameFN)_PR_InvalidStatus, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, (PRSendfileFN)_PR_InvalidInt, (PRConnectcontinueFN)_PR_InvalidStatus, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt};PR_IMPLEMENT(const PRIOMethods*) PR_GetPipeMethods(void){ return &_pr_pipeMethods;}PR_IMPLEMENT(PRFileDesc*) PR_Open(const char *name, PRIntn flags, PRIntn mode){ PRInt32 osfd; PRFileDesc *fd = 0;#if !defined(XP_UNIX) /* BugZilla: 4090 */ PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE;#endif if (!_pr_initialized) _PR_ImplicitInitialization(); /* Map pr open flags and mode to os specific flags */ osfd = _PR_MD_OPEN(name, flags, mode); if (osfd != -1) { fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); if (!fd) { (void) _PR_MD_CLOSE_FILE(osfd); } else {#if !defined(XP_UNIX) /* BugZilla: 4090 */ fd->secret->appendMode = appendMode;#endif _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); } } return fd;}PR_IMPLEMENT(PRFileDesc*) PR_OpenFile( const char *name, PRIntn flags, PRIntn mode){ PRInt32 osfd; PRFileDesc *fd = 0;#if !defined(XP_UNIX) /* BugZilla: 4090 */ PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE;#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -