]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.h
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Acquire Worker - Worker process manager
7 Each worker class is associated with exaclty one subprocess.
9 ##################################################################### */
12 /** \addtogroup acquire
15 * \file acquire-worker.h
18 #ifndef PKGLIB_ACQUIRE_WORKER_H
19 #define PKGLIB_ACQUIRE_WORKER_H
21 #include <apt-pkg/acquire.h>
22 #include <apt-pkg/weakptr.h>
24 #include <sys/types.h>
28 /** \brief A fetch subprocess.
30 * A worker process is responsible for one stage of the fetch. This
31 * class encapsulates the communications protocol between the master
32 * process and the worker, from the master end.
34 * Each worker is intrinsically placed on two linked lists. The
35 * Queue list (maintained in the #NextQueue variable) is maintained
36 * by the pkgAcquire::Queue class; it represents the set of workers
37 * assigned to a particular queue. The Acquire list (maintained in
38 * the #NextAcquire variable) is maintained by the pkgAcquire class;
39 * it represents the set of active workers for a particular
42 * \todo Like everything else in the Acquire system, this has way too
43 * many protected items.
45 * \sa pkgAcqMethod, pkgAcquire::Item, pkgAcquire
47 class pkgAcquire::Worker
: public WeakPointable
49 /** \brief dpointer placeholder (for later in case we need it) */
52 friend class pkgAcquire
;
57 /** \brief The next link on the Queue list.
59 * \todo This is always NULL; is it just for future use?
63 /** \brief The next link on the Acquire list. */
66 /** \brief The Queue with which this worker is associated. */
69 /** \brief The download progress indicator to which progress
70 * messages should be sent.
72 pkgAcquireStatus
*Log
;
74 /** \brief The configuration of this method. On startup, the
75 * target of this pointer is filled in with basic data about the
76 * method, as reported by the worker.
80 /** \brief The access method to be used by this worker.
82 * \todo Doesn't this duplicate Config->Access?
86 /** \brief The PID of the subprocess. */
89 /** \brief A file descriptor connected to the standard output of
92 * Used to read messages and data from the subprocess.
96 /** \brief A file descriptor connected to the standard input of the
99 * Used to send commands and configuration data to the subprocess.
103 /** \brief The socket to send SCM_RIGHTS message through
106 int PrivSepSocketFdChild
;
108 /** \brief Set to \b true if the worker is in a state in which it
109 * might generate data or command responses.
111 * \todo Is this right? It's a guess.
115 /** \brief Set to \b true if the worker is in a state in which it
116 * is legal to send commands to it.
118 * \todo Is this right?
122 /** If \b true, debugging output will be sent to std::clog. */
125 /** \brief The raw text values of messages received from the
126 * worker, in sequence.
128 std::vector
<std::string
> MessageQueue
;
130 /** \brief Buffers pending writes to the subprocess.
132 * \todo Wouldn't a std::dequeue be more appropriate?
134 std::string OutQueue
;
136 /** \brief Common code for the constructor.
138 * Initializes NextQueue and NextAcquire to NULL; Process, InFd,
139 * and OutFd to -1, OutReady and InReady to \b false, and Debug
144 /** \brief Retrieve any available messages from the subprocess.
146 * The messages are retrieved as in \link strutl.h ReadMessages()\endlink, and
147 * #MethodFailure() is invoked if an error occurs; in particular,
148 * if the pipe to the subprocess dies unexpectedly while a message
151 * \return \b true if the messages were successfully read, \b
156 /** \brief Parse and dispatch pending messages.
158 * This dispatches the message in a manner appropriate for its
161 * \todo Several message types lack separate handlers.
163 * \sa Capabilities(), SendConfiguration(), MediaChange()
167 /** \brief Read and dispatch any pending messages from the
170 * \return \b false if the subprocess died unexpectedly while a
171 * message was being transmitted.
175 /** \brief Send any pending commands to the subprocess.
177 * This method will fail if there is no pending output.
179 * \return \b true if all commands were succeeded, \b false if an
180 * error occurred (in which case MethodFailure() will be invoked).
184 /** \brief Handle a 100 Capabilities response from the subprocess.
186 * \param Message the raw text of the message from the subprocess.
188 * The message will be parsed and its contents used to fill
189 * #Config. If #Config is NULL, this routine is a NOP.
193 bool Capabilities(std::string Message
);
195 /** \brief Send a 601 Configuration message (containing the APT
196 * configuration) to the subprocess.
198 * The APT configuration will be send to the subprocess in a
199 * message of the following form:
203 * Config-Item: Fully-Qualified-Item=Val
204 * Config-Item: Fully-Qualified-Item=Val
208 * \return \b true if the command was successfully sent, \b false
211 bool SendConfiguration();
213 /** \brief Handle a 403 Media Change message.
215 * \param Message the raw text of the message; the Media field
216 * indicates what type of media should be changed, and the Drive
217 * field indicates where the media is located.
219 * Invokes pkgAcquireStatus::MediaChange(Media, Drive) to ask the
220 * user to swap disks; informs the subprocess of the result (via
221 * 603 Media Changed, with the Failed field set to \b true if the
222 * user cancelled the media change).
224 bool MediaChange(std::string Message
);
226 /** \brief Invoked when the worked process dies unexpectedly.
228 * Waits for the subprocess to terminate and generates an error if
229 * it terminated abnormally, then closes and blanks out all file
230 * descriptors. Discards all pending messages from the
235 bool MethodFailure();
237 /** \brief Invoked when a fetch job is completed, either
238 * successfully or unsuccessfully.
240 * Resets the status information for the worker process.
246 /** \brief The queue entry that is currently being downloaded. */
247 pkgAcquire::Queue::QItem
*CurrentItem
;
249 /** \brief The most recent status string received from the
254 /** \brief How many bytes of the file have been downloaded. Zero
255 * if the current progress of the file cannot be determined.
257 unsigned long long CurrentSize
;
259 /** \brief The total number of bytes to be downloaded. Zero if the
260 * total size of the final is unknown.
262 unsigned long long TotalSize
;
264 /** \brief How much of the file was already downloaded prior to
265 * starting this worker.
267 unsigned long long ResumePoint
;
269 /** \brief Tell the subprocess to download the given item.
271 * \param Item the item to queue up.
272 * \return \b true if the item was successfully enqueued.
274 * Queues up a 600 URI Acquire message for the given item to be
275 * sent at the next possible moment. Does \e not flush the output
278 bool QueueItem(pkgAcquire::Queue::QItem
*Item
);
280 /** \brief Start up the worker and fill in #Config.
282 * Reads the first message from the worker, which is assumed to be
283 * a 100 Capabilities message.
285 * \return \b true if all operations completed successfully.
289 /** \brief Update the worker statistics (CurrentSize, TotalSize,
294 /** \return The fetch method configuration. */
295 inline const MethodConfig
*GetConf() const {return Config
;};
297 /** \brief Create a new Worker to download files.
299 * \param OwnerQ The queue into which this worker should be
302 * \param Config A location in which to store information about
305 * \param Log The download progress indicator that should be used
306 * to report the progress of this worker.
308 Worker(Queue
*OwnerQ
,MethodConfig
*Config
,pkgAcquireStatus
*Log
);
310 /** \brief Create a new Worker that should just retrieve
311 * information about the fetch method.
313 * Nothing in particular forces you to refrain from actually
314 * downloading stuff, but the various status callbacks won't be
317 * \param Config A location in which to store information about
320 explicit Worker(MethodConfig
*Config
);
322 /** \brief Clean up this worker.
324 * Closes the file descriptors; if MethodConfig::NeedsCleanup is
325 * \b false, also rudely interrupts the worker with a SIGINT.
330 APT_HIDDEN
void PrepareFiles(char const * const caller
, pkgAcquire::Queue::QItem
const * const Itm
);