]>
Commit | Line | Data |
---|---|---|
bac41a7b A |
1 | /* |
2 | * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved. | |
3 | * | |
4 | * The contents of this file constitute Original Code as defined in and are | |
5 | * subject to the Apple Public Source License Version 1.2 (the 'License'). | |
6 | * You may not use this file except in compliance with the License. Please obtain | |
7 | * a copy of the License at http://www.apple.com/publicsource and read it before | |
8 | * using this file. | |
9 | * | |
10 | * This Original Code and all software distributed under the License are | |
11 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS | |
12 | * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT | |
13 | * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
14 | * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the | |
15 | * specific language governing rights and limitations under the License. | |
16 | */ | |
17 | ||
18 | ||
19 | // | |
20 | // file-protocol - File protocol objects | |
21 | // | |
22 | #include "file-protocol.h" | |
23 | #include "netmanager.h" | |
24 | #include "neterror.h" | |
25 | #include "netparameters.h" | |
26 | ||
27 | ||
28 | namespace Security { | |
29 | namespace Network { | |
30 | ||
31 | ||
32 | // | |
33 | // Construct the protocol object | |
34 | // | |
35 | FileProtocol::FileProtocol(Manager &mgr) : Protocol(mgr, "file") | |
36 | { | |
37 | } | |
38 | ||
39 | ||
40 | // | |
41 | // Create a Transfer object for our protocol | |
42 | // | |
43 | FileProtocol::FileTransfer *FileProtocol::makeTransfer(const Target &target, Operation operation) | |
44 | { | |
45 | switch (operation) { | |
46 | case download: | |
47 | return new Reader(*this, target); | |
48 | break; | |
49 | case upload: | |
50 | return new Writer(*this, target); | |
51 | default: | |
52 | Error::throwMe(); | |
53 | } | |
54 | } | |
55 | ||
56 | ||
57 | FileProtocol::FileTransfer::FileTransfer(FileProtocol &proto, const Target &tgt, Operation op) | |
58 | : Transfer(proto, tgt, op) | |
59 | { | |
60 | } | |
61 | ||
62 | int FileProtocol::FileTransfer::fileDesc() const | |
63 | { return *this; } | |
64 | ||
65 | ||
66 | void FileProtocol::FileTransfer::transitError(const CssmCommonError &error) | |
67 | { | |
68 | fail(); | |
69 | } | |
70 | ||
71 | ||
72 | // | |
73 | // Read transfers | |
74 | // | |
75 | FileProtocol::Reader::Reader(FileProtocol &proto, const Target &tgt) | |
76 | : FileTransfer(proto, tgt, download) | |
77 | { | |
78 | } | |
79 | ||
80 | void FileProtocol::Reader::start() | |
81 | { | |
82 | open(target.path.c_str()); | |
83 | ||
84 | // notify any observer that we are under way. | |
85 | observe(Observer::resourceFound); | |
86 | observe(Observer::downloading); | |
87 | ||
88 | setFlag(O_NONBLOCK); | |
89 | int restartOffset = getv<int>(kNetworkRestartPosition, 0); | |
90 | if (restartOffset) | |
91 | seek(restartOffset); | |
92 | size_t size = fileSize() - restartOffset; | |
93 | mode(sink(), size); | |
94 | sink().setSize(size); | |
95 | protocol.manager.addIO(this); | |
96 | } | |
97 | ||
98 | void FileProtocol::Reader::transit(Event event, char *, size_t) | |
99 | { | |
100 | assert(event == autoReadDone); | |
101 | protocol.manager.removeIO(this); | |
102 | finish(); | |
103 | } | |
104 | ||
105 | ||
106 | // | |
107 | // Write transfers | |
108 | // | |
109 | FileProtocol::Writer::Writer(FileProtocol &proto, const Target &tgt) | |
110 | : FileTransfer(proto, tgt, upload) | |
111 | { | |
112 | } | |
113 | ||
114 | void FileProtocol::Writer::start() | |
115 | { | |
116 | open(target.path.c_str(), O_WRONLY | O_CREAT); | |
117 | ||
118 | // notify any observer that we are under way. | |
119 | observe(Observer::resourceFound); | |
120 | observe(Observer::uploading); | |
121 | ||
122 | int restartOffset = getv<int>(kNetworkRestartPosition, 0); | |
123 | if (restartOffset) | |
124 | seek(restartOffset); | |
125 | protocol.manager.addIO(this); | |
126 | disable(input); | |
127 | mode(source(), fileSize() - restartOffset); | |
128 | } | |
129 | ||
130 | void FileProtocol::Writer::transit(Event event, char *, size_t) | |
131 | { | |
132 | assert(event == autoWriteDone); | |
133 | protocol.manager.removeIO(this); | |
134 | finish(); | |
135 | } | |
136 | ||
137 | ||
138 | } // end namespace Network | |
139 | } // end namespace Security |