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: /* michael@0: ** Class implementation for normal and special file I/O (ref: prio.h) michael@0: */ michael@0: michael@0: #include "rcfileio.h" michael@0: michael@0: #include michael@0: michael@0: RCFileIO::RCFileIO(): RCIO(RCIO::file) { } michael@0: michael@0: RCFileIO::~RCFileIO() { if (NULL != fd) (void)Close(); } michael@0: michael@0: PRInt64 RCFileIO::Available() michael@0: { return fd->methods->available(fd); } michael@0: michael@0: PRStatus RCFileIO::Close() michael@0: { PRStatus rv = fd->methods->close(fd); fd = NULL; return rv; } michael@0: michael@0: PRStatus RCFileIO::Delete(const char* filename) { return PR_Delete(filename); } michael@0: michael@0: PRStatus RCFileIO::FileInfo(RCFileInfo* info) const michael@0: { return fd->methods->fileInfo64(fd, &info->info); } michael@0: michael@0: PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info) michael@0: { return PR_GetFileInfo64(name, &info->info); } michael@0: michael@0: PRStatus RCFileIO::Fsync() michael@0: { return fd->methods->fsync(fd); } michael@0: michael@0: PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode) michael@0: { michael@0: fd = PR_Open(filename, flags, mode); michael@0: return (NULL == fd) ? PR_FAILURE : PR_SUCCESS; michael@0: } /* RCFileIO::Open */ michael@0: michael@0: PRInt32 RCFileIO::Read(void *buf, PRSize amount) michael@0: { return fd->methods->read(fd, buf, amount); } michael@0: michael@0: PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how) michael@0: { michael@0: PRSeekWhence whence; michael@0: switch (how) michael@0: { michael@0: case RCFileIO::set: whence = PR_SEEK_SET; break; michael@0: case RCFileIO::current: whence = PR_SEEK_CUR; break; michael@0: case RCFileIO::end: whence = PR_SEEK_END; break; michael@0: default: whence = (PRSeekWhence)-1; michael@0: } michael@0: return fd->methods->seek64(fd, offset, whence); michael@0: } /* RCFileIO::Seek */ michael@0: michael@0: PRInt32 RCFileIO::Write(const void *buf, PRSize amount) michael@0: { return fd->methods->write(fd, buf, amount); } michael@0: michael@0: PRInt32 RCFileIO::Writev( michael@0: const PRIOVec *iov, PRSize size, const RCInterval& timeout) michael@0: { return fd->methods->writev(fd, iov, size, timeout); } michael@0: michael@0: RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special) michael@0: { michael@0: PRFileDesc* fd; michael@0: PRSpecialFD which; michael@0: RCFileIO* spec = NULL; michael@0: michael@0: switch (special) michael@0: { michael@0: case RCFileIO::input: which = PR_StandardInput; break; michael@0: case RCFileIO::output: which = PR_StandardOutput; break; michael@0: case RCFileIO::error: which = PR_StandardError; break; michael@0: default: which = (PRSpecialFD)-1; michael@0: } michael@0: fd = PR_GetSpecialFD(which); michael@0: if (NULL != fd) michael@0: { michael@0: spec = new RCFileIO(); michael@0: if (NULL != spec) spec->fd = fd; michael@0: } michael@0: return spec; michael@0: } /* RCFileIO::GetSpecialFile */ michael@0: michael@0: michael@0: /* michael@0: ** The following methods have been made non-virtual and private. These michael@0: ** default implementations are intended to NEVER be called. They michael@0: ** are not valid for this type of I/O class (normal and special file). michael@0: */ michael@0: PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRStatus RCFileIO::GetLocalName(RCNetAddr*) const michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRStatus RCFileIO::GetPeerName(RCNetAddr*) const michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRStatus RCFileIO::Listen(PRIntn) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRInt16 RCFileIO::Poll(PRInt16, PRInt16*) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return 0; } michael@0: michael@0: PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: PRInt32 RCFileIO::Send( michael@0: const void*, PRSize, PRIntn, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: PRInt32 RCFileIO::Sendto( michael@0: const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return NULL; } michael@0: michael@0: PRStatus RCFileIO::Bind(const RCNetAddr&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRInt32 RCFileIO::AcceptRead( michael@0: RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } michael@0: michael@0: PRInt32 RCFileIO::TransmitFile( michael@0: RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&) michael@0: { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } michael@0: michael@0: /* michael@0: ** Class implementation for file information object (ref: prio.h) michael@0: */ michael@0: michael@0: RCFileInfo::~RCFileInfo() { } michael@0: michael@0: RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase() michael@0: { info = her.info; } /* RCFileInfo::RCFileInfo */ michael@0: michael@0: RCTime RCFileInfo::CreationTime() const { return RCTime(info.creationTime); } michael@0: michael@0: RCTime RCFileInfo::ModifyTime() const { return RCTime(info.modifyTime); } michael@0: michael@0: RCFileInfo::FileType RCFileInfo::Type() const michael@0: { michael@0: RCFileInfo::FileType type; michael@0: switch (info.type) michael@0: { michael@0: case PR_FILE_FILE: type = RCFileInfo::file; break; michael@0: case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break; michael@0: default: type = RCFileInfo::other; michael@0: } michael@0: return type; michael@0: } /* RCFileInfo::Type */