|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 ** Class implementation for normal and special file I/O (ref: prio.h) |
|
8 */ |
|
9 |
|
10 #include "rcfileio.h" |
|
11 |
|
12 #include <string.h> |
|
13 |
|
14 RCFileIO::RCFileIO(): RCIO(RCIO::file) { } |
|
15 |
|
16 RCFileIO::~RCFileIO() { if (NULL != fd) (void)Close(); } |
|
17 |
|
18 PRInt64 RCFileIO::Available() |
|
19 { return fd->methods->available(fd); } |
|
20 |
|
21 PRStatus RCFileIO::Close() |
|
22 { PRStatus rv = fd->methods->close(fd); fd = NULL; return rv; } |
|
23 |
|
24 PRStatus RCFileIO::Delete(const char* filename) { return PR_Delete(filename); } |
|
25 |
|
26 PRStatus RCFileIO::FileInfo(RCFileInfo* info) const |
|
27 { return fd->methods->fileInfo64(fd, &info->info); } |
|
28 |
|
29 PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info) |
|
30 { return PR_GetFileInfo64(name, &info->info); } |
|
31 |
|
32 PRStatus RCFileIO::Fsync() |
|
33 { return fd->methods->fsync(fd); } |
|
34 |
|
35 PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode) |
|
36 { |
|
37 fd = PR_Open(filename, flags, mode); |
|
38 return (NULL == fd) ? PR_FAILURE : PR_SUCCESS; |
|
39 } /* RCFileIO::Open */ |
|
40 |
|
41 PRInt32 RCFileIO::Read(void *buf, PRSize amount) |
|
42 { return fd->methods->read(fd, buf, amount); } |
|
43 |
|
44 PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how) |
|
45 { |
|
46 PRSeekWhence whence; |
|
47 switch (how) |
|
48 { |
|
49 case RCFileIO::set: whence = PR_SEEK_SET; break; |
|
50 case RCFileIO::current: whence = PR_SEEK_CUR; break; |
|
51 case RCFileIO::end: whence = PR_SEEK_END; break; |
|
52 default: whence = (PRSeekWhence)-1; |
|
53 } |
|
54 return fd->methods->seek64(fd, offset, whence); |
|
55 } /* RCFileIO::Seek */ |
|
56 |
|
57 PRInt32 RCFileIO::Write(const void *buf, PRSize amount) |
|
58 { return fd->methods->write(fd, buf, amount); } |
|
59 |
|
60 PRInt32 RCFileIO::Writev( |
|
61 const PRIOVec *iov, PRSize size, const RCInterval& timeout) |
|
62 { return fd->methods->writev(fd, iov, size, timeout); } |
|
63 |
|
64 RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special) |
|
65 { |
|
66 PRFileDesc* fd; |
|
67 PRSpecialFD which; |
|
68 RCFileIO* spec = NULL; |
|
69 |
|
70 switch (special) |
|
71 { |
|
72 case RCFileIO::input: which = PR_StandardInput; break; |
|
73 case RCFileIO::output: which = PR_StandardOutput; break; |
|
74 case RCFileIO::error: which = PR_StandardError; break; |
|
75 default: which = (PRSpecialFD)-1; |
|
76 } |
|
77 fd = PR_GetSpecialFD(which); |
|
78 if (NULL != fd) |
|
79 { |
|
80 spec = new RCFileIO(); |
|
81 if (NULL != spec) spec->fd = fd; |
|
82 } |
|
83 return spec; |
|
84 } /* RCFileIO::GetSpecialFile */ |
|
85 |
|
86 |
|
87 /* |
|
88 ** The following methods have been made non-virtual and private. These |
|
89 ** default implementations are intended to NEVER be called. They |
|
90 ** are not valid for this type of I/O class (normal and special file). |
|
91 */ |
|
92 PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&) |
|
93 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
94 |
|
95 PRStatus RCFileIO::GetLocalName(RCNetAddr*) const |
|
96 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
97 |
|
98 PRStatus RCFileIO::GetPeerName(RCNetAddr*) const |
|
99 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
100 |
|
101 PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const |
|
102 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
103 |
|
104 PRStatus RCFileIO::Listen(PRIntn) |
|
105 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
106 |
|
107 PRInt16 RCFileIO::Poll(PRInt16, PRInt16*) |
|
108 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return 0; } |
|
109 |
|
110 PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&) |
|
111 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
112 |
|
113 PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&) |
|
114 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
115 |
|
116 PRInt32 RCFileIO::Send( |
|
117 const void*, PRSize, PRIntn, const RCInterval&) |
|
118 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
119 |
|
120 PRInt32 RCFileIO::Sendto( |
|
121 const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&) |
|
122 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
123 |
|
124 RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&) |
|
125 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return NULL; } |
|
126 |
|
127 PRStatus RCFileIO::Bind(const RCNetAddr&) |
|
128 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
129 |
|
130 PRInt32 RCFileIO::AcceptRead( |
|
131 RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&) |
|
132 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
133 |
|
134 PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*) |
|
135 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
136 |
|
137 PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow) |
|
138 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
139 |
|
140 PRInt32 RCFileIO::TransmitFile( |
|
141 RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&) |
|
142 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; } |
|
143 |
|
144 /* |
|
145 ** Class implementation for file information object (ref: prio.h) |
|
146 */ |
|
147 |
|
148 RCFileInfo::~RCFileInfo() { } |
|
149 |
|
150 RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase() |
|
151 { info = her.info; } /* RCFileInfo::RCFileInfo */ |
|
152 |
|
153 RCTime RCFileInfo::CreationTime() const { return RCTime(info.creationTime); } |
|
154 |
|
155 RCTime RCFileInfo::ModifyTime() const { return RCTime(info.modifyTime); } |
|
156 |
|
157 RCFileInfo::FileType RCFileInfo::Type() const |
|
158 { |
|
159 RCFileInfo::FileType type; |
|
160 switch (info.type) |
|
161 { |
|
162 case PR_FILE_FILE: type = RCFileInfo::file; break; |
|
163 case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break; |
|
164 default: type = RCFileInfo::other; |
|
165 } |
|
166 return type; |
|
167 } /* RCFileInfo::Type */ |