1 /* $Id: jigdo-io.hh,v 1.16 2005/04/09 23:09:52 atterer Exp $ -*- C++ -*-
3 |_) /| Copyright (C) 2003 | richard@
4 | \/¯| Richard Atterer | atterer.org
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2. See
8 the file COPYING for details.
12 IO object for .jigdo downloads; download, gunzip, interpret
23 #include <datasource.hh>
25 #include <jigdo-io.fh>
26 #include <makeimagedl.hh>
30 //______________________________________________________________________
37 /** IO object for .jigdo downloads; download, gunzip, interpret */
38 class Job::JigdoIO : NoCopy, public Job::DataSource::IO, Gunzip::IO {
41 /** Create a new JigdoIO which is owned by m, gets data from download (will
42 register itself with download's IOPtr) and passes it on to childIo.
43 @param c Object which owns us (it is the MakeImageDl's child, but our
45 @param url URL of the .jigdo file */
46 JigdoIO(MakeImageDl::Child* c, const string& url/* IOPtr,
47 DataSource::IO* frontendIo*/);
50 inline MakeImageDl* master() const;
51 inline DataSource* source() const;
54 friend struct Job::JigdoIOTest;
56 /* Create object for an [Include]d file */
57 JigdoIO(MakeImageDl::Child* c, const string& url,
58 /*DataSource::IO* frontendIo,*/ JigdoIO* parentJigdo,
61 /** @return Root object of the include tree */
62 inline JigdoIO* root();
63 inline const JigdoIO* root() const;
64 /** @return true iff this object is the root of the include tree. */
65 inline bool isRoot() const;
66 /** Return the ptr to the image section candidate object; the JigdoIO which
67 might or might not contain the first [Image] section. If new data is
68 received for that object and that new data contains an [Image], we know
69 it's the first [Image]. If all data is recvd without any [Image]
70 turning up, we continue walking the include tree depth-first. */
71 inline JigdoIO* imgSectCandidate() const;
72 /** Set the ptr to the image section candidate object */
73 inline void setImgSectCandidate(JigdoIO* c);
74 // The methods below are called in various places to find 1st image section
75 inline void imgSect_newChild(JigdoIO* child); // Child created after [Incl.
76 inline void imgSect_parsed(); // [Image] occurred in current .jigdo data
77 /* End of current file without any [Image]. Returns 1 if OK and all
79 inline XStatus imgSect_eof();
81 // Create error message with URL and line number
82 void generateError(const string& msg);
83 void generateError(const char* msg);
84 // As above, but directly pass on error string, do not add URL/line
85 void generateError_plain(const string& err);
86 // True after above was called
87 inline bool failed() const;
88 // Called by gunzip_data(): New .jigdo line ready. Arg is empty on exit.
89 void jigdoLine(string* l);
90 void include(string* url); // "[Include http://xxx]" found
91 void entry(string* label, string* data, unsigned valueOff);
92 /* Called at the end of a [Section] (=start of another section or EOF)
93 Returns FAILURE if there is an error. */
96 // Virtual methods from DataSource::IO
97 virtual void job_deleted();
98 virtual void job_succeeded();
99 virtual void job_failed(const string& message);
100 virtual void job_message(const string& message);
101 virtual void dataSource_dataSize(uint64 n);
102 virtual void dataSource_data(const byte* data, unsigned size,
105 // Virtual methods from Gunzip::IO
106 virtual void gunzip_deleted();
107 virtual void gunzip_data(Gunzip*, byte* decompressed, unsigned size);
108 virtual void gunzip_needOut(Gunzip*);
109 virtual void gunzip_failed(string* message);
111 MakeImageDl::Child* childDl;
112 string urlVal; // Absolute URL of this .jigdo file
114 /* Representation of the tree of [Include] directives. Most of the time,
115 the order of data in the .jigdo files is not relevant, with one
116 exception: We must interpret the first [Image] section only, and ignore
117 all following ones. */
118 JigdoIO* parent; // .jigdo file which [Include]d us, or null if top-level
119 int includeLine; // If parent!=null, line num of [Include] in parent
120 JigdoIO* firstChild; // First file we [Include], or null if none
121 JigdoIO* next; // Right sibling, or null if none
122 /* For the root object, contains imgSectCandidate, else ptr to root object.
123 Don't access directly, use accessor methods. */
124 JigdoIO* rootAndImageSectionCandidate;
126 int line; // Line number, for errors. 0 if no data yet, -1 if finished
127 bool finished() { return line < 0; }
128 void setFinished() { line = -1; }
129 string section; // Current section name, empty if none yet
131 // Info about first image section of this .jigdo, if any
132 int imageSectionLine; // 0 if no [Image] found yet
134 string imageInfo, imageShortInfo;
135 SmartPtr<PartUrlMapping> templateUrls; // Can contain a list of altern. URLs
138 /* Transparent gunzipping of .jigdo file. GUNZIP_BUF_SIZE is also the max
139 size a single line in the .jigdo is allowed to have */
140 static const unsigned GUNZIP_BUF_SIZE = 16384;
142 byte gunzipBuf[GUNZIP_BUF_SIZE];
144 //______________________________________________________________________
146 Job::JigdoIO* Job::JigdoIO::root() {
147 if (isRoot()) return this; else return rootAndImageSectionCandidate;
149 const Job::JigdoIO* Job::JigdoIO::root() const {
150 if (isRoot()) return this; else return rootAndImageSectionCandidate;
152 bool Job::JigdoIO::isRoot() const {
156 Job::JigdoIO* Job::JigdoIO::imgSectCandidate() const {
158 return rootAndImageSectionCandidate;
160 return rootAndImageSectionCandidate->rootAndImageSectionCandidate;
162 void Job::JigdoIO::setImgSectCandidate(JigdoIO* c) {
164 rootAndImageSectionCandidate = c;
166 rootAndImageSectionCandidate->rootAndImageSectionCandidate = c;
169 Job::MakeImageDl* Job::JigdoIO::master() const { return childDl->master(); }
170 Job::DataSource* Job::JigdoIO::source() const { return childDl->source(); }
172 bool Job::JigdoIO::failed() const {
173 return (imageName.length() == 1 && imageName[0] == '\0');
174 //return (childFailedId != 0);