6 * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
7 * Copyright (C) 2009, 2010, MinGW Project
10 * Implementation of the package download machinery for mingw-get.
13 * This is free software. Permission is granted to copy, modify and
14 * redistribute this software, under the provisions of the GNU General
15 * Public License, Version 3, (or, at your option, any later version),
16 * as published by the Free Software Foundation; see the file COPYING
17 * for licensing details.
19 * Note, in particular, that this software is provided "as is", in the
20 * hope that it may prove useful, but WITHOUT WARRANTY OF ANY KIND; not
21 * even an implied WARRANTY OF MERCHANTABILITY, nor of FITNESS FOR ANY
22 * PARTICULAR PURPOSE. Under no circumstances will the author, or the
23 * MinGW Project, accept liability for any damages, however caused,
24 * arising from the use of this software.
27 #define WIN32_LEAN_AND_MEAN
42 class pkgInternetAgent
44 /* A minimal, locally implemented class, instantiated ONCE as a
45 * global object, to ensure that wininet's global initialisation is
46 * completed at the proper time, without us doing it explicitly.
49 HINTERNET SessionHandle;
52 inline pkgInternetAgent():SessionHandle( NULL )
56 if( InternetAttemptConnect( 0 ) == ERROR_SUCCESS )
57 SessionHandle = InternetOpen
58 ( "MinGW Installer", INTERNET_OPEN_TYPE_PRECONFIG,
62 inline ~pkgInternetAgent()
66 if( SessionHandle != NULL )
67 Close( SessionHandle );
70 /* Remaining methods are simple inline wrappers for the
71 * wininet functions we plan to use...
73 inline HINTERNET OpenURL( const char *URL )
75 return InternetOpenUrl( SessionHandle, URL, NULL, 0, 0, 0 );
77 inline DWORD QueryStatus( HINTERNET id )
79 DWORD ok, idx = 0, len = sizeof( ok );
80 if( HttpQueryInfo( id, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &ok, &len, &idx ) )
84 inline int Read( HINTERNET dl, char *buf, size_t max, DWORD *count )
86 return InternetReadFile( dl, buf, max, count );
88 inline int Close( HINTERNET id )
90 return InternetCloseHandle( id );
94 /* This is the one and only instantiation of an object of this class.
96 static pkgInternetAgent pkgDownloadAgent;
98 const char *pkgActionItem::ArchivePath()
100 /* Specify where downloaded packages are cached,
101 * within the local file system.
103 return "%R" "var/cache/mingw-get/packages" "%/M/%F";
106 class pkgInternetStreamingAgent
108 /* Another locally implemented class; each individual file download
109 * gets its own instance of this, either as-is for basic data transfer,
110 * or as a specialised derivative of this base class.
113 const char *filename;
114 const char *dest_template;
121 virtual int TransferData( int );
124 pkgInternetStreamingAgent( const char*, const char* );
125 virtual ~pkgInternetStreamingAgent();
127 virtual int Get( const char* );
128 inline const char *DestFile(){ return dest_file; }
131 pkgInternetStreamingAgent::pkgInternetStreamingAgent
132 ( const char *local_name, const char *dest_specification )
134 /* Constructor for the pkgInternetStreamingAgent class.
136 filename = local_name;
137 dest_template = dest_specification;
138 dest_file = (char *)(malloc( mkpath( NULL, dest_template, filename, NULL ) ));
139 if( dest_file != NULL )
140 mkpath( dest_file, dest_template, filename, NULL );
143 pkgInternetStreamingAgent::~pkgInternetStreamingAgent()
145 /* Destructor needs to free the heap memory allocated by the
146 * constructor, for storage of "dest_file" name.
148 free( (void *)(dest_file) );
151 int pkgInternetStreamingAgent::TransferData( int fd )
153 /* In the case of this base class implementation,
154 * we simply read the file's data from the Internet source,
155 * and write a verbatim copy to the destination file.
157 char buf[8192]; DWORD count, tally = 0;
158 do { dl_status = pkgDownloadAgent.Read( dl_host, buf, sizeof( buf ), &count );
159 dmh_printf( "\rdownloading: %s: %I32d b", filename, tally += count );
160 write( fd, buf, count );
161 } while( dl_status && (count > 0) );
162 dmh_printf( "\rdownloading: %s: %I32d b\n", filename, tally );
166 static const char *get_host_info
167 ( pkgXmlNode *ref, const char *property, const char *fallback = NULL )
169 /* Helper function to retrieve host information from the XML catalogue.
171 * Call with property = "url", to retrieve the URL template to pass as
172 * "fmt" argument to mkpath(), or with property = "mirror", to retrieve
173 * the substitution text for the "modifier" argument.
175 const char *uri = NULL;
178 /* Starting from the "ref" package entry in the catalogue...
180 pkgXmlNode *host = ref->FindFirstAssociate( download_host_key );
181 while( host != NULL )
183 /* Examine its associate tags; if we find one of type
184 * "download-host", with the requisite property, then we
185 * immediately return that property value...
187 if( (uri = host->GetPropVal( property, NULL )) != NULL )
190 /* Otherwise, we look for any other candidate tags
191 * associated with the same catalogue entry...
193 host = host->FindNextAssociate( download_host_key );
195 /* Failing an immediate match, extend the search to the
196 * ancestors of the initial reference entry...
198 ref = ref->GetParent();
200 /* ...and ultimately, if no match is found, we return the
201 * specified "fallback" property value.
207 int set_transit_path( const char *path, const char *file, char *buf = NULL )
209 /* Helper to define the transitional path name for downloaded files,
210 * used to save the file data while the download is in progress.
212 static const char *transit_dir = "/.in-transit";
213 return mkpath( buf, path, file, transit_dir );
216 int pkgInternetStreamingAgent::Get( const char *from_url )
218 /* Download a file from the specified internet URL.
220 * Before download commences, we accept that this may fail...
224 /* Set up a "transit-file" to receive the downloaded content.
226 char transit_file[set_transit_path( dest_template, filename )];
227 int fd; set_transit_path( dest_template, filename, transit_file );
229 if( (fd = set_output_stream( transit_file, 0644 )) >= 0 )
231 /* The "transit-file" is ready to receive incoming data...
232 * Configure and invoke the download handler to copy the data
233 * from the appropriate host URL, to this "transit-file".
235 if( (dl_host = pkgDownloadAgent.OpenURL( from_url )) != NULL )
237 if( pkgDownloadAgent.QueryStatus( dl_host ) == HTTP_STATUS_OK )
239 /* With the download transaction fully specified, we may
240 * request processing of the file transfer...
242 dl_status = TransferData( fd );
245 /* We are done with the URL handle; close it.
247 pkgDownloadAgent.Close( dl_host );
250 /* Always close the "transit-file", whether the download
251 * was successful, or not...
256 * When successful, we move the "transit-file" to its
257 * final downloaded location...
259 rename( transit_file, dest_file );
261 /* ...otherwise, we discard the incomplete "transit-file",
262 * leaving the caller to diagnose the failure.
264 unlink( transit_file );
267 /* Report success or failure to the caller...
272 void pkgActionItem::DownloadArchiveFiles( pkgActionItem *current )
274 /* Update the local package cache, to ensure that all packages needed
275 * to complete the current set of scheduled actions are present; if any
276 * are missing, invoke an Internet download agent to fetch them. This
277 * requires us to walk the action list...
279 while( current != NULL )
281 /* ...while we haven't run off the end...
283 if( (current->flags & ACTION_INSTALL) == ACTION_INSTALL )
285 /* For all packages specified in the current action list,
286 * for which an "install" action is scheduled, and for which
287 * no associated archive file is present in the local archive
288 * cache, place an Internet download agent on standby to fetch
289 * the required archive from a suitable internet mirror host.
291 const char *package_name = current->Selection()->ArchiveName();
292 pkgInternetStreamingAgent download( package_name, current->ArchivePath() );
294 /* Check if the required archive is already available locally...
296 if( (access( download.DestFile(), R_OK ) != 0) && (errno == ENOENT) )
298 /* ...if not, ask the download agent to fetch it...
300 const char *url_template = get_host_info( current->Selection(), uri_key );
301 if( url_template != NULL )
303 /* ...from the URL constructed from the template specified in
304 * the package repository catalogue (configuration database)...
306 const char *mirror = get_host_info( current->Selection(), mirror_key );
307 char package_url[mkpath( NULL, url_template, package_name, mirror )];
308 mkpath( package_url, url_template, package_name, mirror );
309 if( ! (download.Get( package_url ) > 0) )
310 dmh_notify( DMH_ERROR,
311 "Get package: %s: download failed\n", package_url
315 /* Cannot download; the repository catalogue didn't specify a
316 * template, from which to construct a download URL...
318 dmh_notify( DMH_ERROR,
319 "Get package: %s: no URL specified for download\n", package_name
323 /* Repeat download action, for any additional packages specified
324 * in the current "actions" list.
326 current = current->next;
330 #define DATA_CACHE_PATH "%R" "var/cache/mingw-get/data"
331 #define WORKING_DATA_PATH "%R" "var/lib/mingw-get/data"
333 /* Internet servers host package catalogues in lzma compressed format;
334 * we will decompress them "on the fly", as we download them. To achieve
335 * this, we will use a variant of the pkgInternetStreamingAgent, using a
336 * specialised TransferData method; additionally, this will incorporate
337 * a special derivative of a pkgLzmaArchiveStream, with its GetRawData
338 * method adapted to stream data from an internet URI, instead of
339 * reading from a local file.
341 * To derive the pkgInternetLzmaStreamingAgent, we need to include the
342 * specialised declarations of a pkgArchiveStream, in order to make the
343 * declaration of pkgLzmaArchiveStream available as our base class.
345 #define PKGSTRM_H_SPECIAL 1
348 class pkgInternetLzmaStreamingAgent :
349 public pkgInternetStreamingAgent, public pkgLzmaArchiveStream
351 /* Specialisation of the pkgInternetStreamingAgent base class,
352 * providing decompressed copies of LZMA encoded files downloaded
353 * from the Internet; (the LZMA decompression capability is derived
354 * from the pkgLzmaArchiveStream base class).
357 /* We need a specialised constructor...
359 pkgInternetLzmaStreamingAgent( const char*, const char* );
362 /* Specialisation requires overrides for each of this pair of
363 * methods, (the first from the pkgLzmaArchiveStream base class;
364 * the second from pkgInternetStreamingAgent).
366 virtual int GetRawData( int, uint8_t*, size_t );
367 virtual int TransferData( int );
370 /* This specialisation of the pkgInternetStreamingAgent class needs its
371 * own constructor, simply to invoke the constructors for the base classes,
372 * (since neither is instantiated by a default constructor).
374 pkgInternetLzmaStreamingAgent::pkgInternetLzmaStreamingAgent
375 ( const char *local_name, const char *dest_specification ):
376 pkgInternetStreamingAgent( local_name, dest_specification ),
378 * Note that, when we come to initialise the lzma streaming component
379 * of this derived class, we will be streaming directly from the internet,
380 * rather than from a file stream, so we don't require a file descriptor
381 * for the input stream; however, the class semantics still expect one.
382 * To avoid accidental association with an existing file stream, we
383 * use a negative value, (which is never a valid file descriptor);
384 * however, we must not choose -1, since the class implementation
385 * will decline to process the stream; hence, we choose -2.
387 pkgLzmaArchiveStream( -2 ){}
389 int pkgInternetLzmaStreamingAgent::GetRawData( int fd, uint8_t *buf, size_t max )
391 /* Fetch raw (compressed) data from the Internet host, and load it into
392 * the decompression filter's input buffer, whence the TransferData routine
393 * may retrieve it, via the filter, as an uncompressed stream.
396 dl_status = pkgDownloadAgent.Read( dl_host, (char *)(buf), max, &count );
400 int pkgInternetLzmaStreamingAgent::TransferData( int fd )
402 /* In this case, we read the file's data from the Internet source,
403 * stream it through the lzma decompression filter, and write a copy
404 * of the resultant decompressed data to the destination file.
406 char buf[8192]; DWORD count;
407 do { count = pkgLzmaArchiveStream::Read( buf, sizeof( buf ) );
408 write( fd, buf, count );
409 } while( dl_status && (count > 0) );
413 static const char *serial_number( const char *catalogue )
415 /* Local helper function to retrieve issue numbers from any repository
416 * package catalogue; returns the result as a duplicate of the internal
417 * string, allocated on the heap (courtesy of the strdup() function).
420 pkgXmlDocument src( catalogue );
423 && ((issue = src.GetRoot()->GetPropVal( issue_key, NULL )) != NULL) )
425 * Found an issue number; return a copy...
427 return strdup( issue );
429 /* If we get to here, we couldn't get a valid issue number;
430 * whatever the reason, return NULL to indicate failure.
435 void pkgXmlDocument::SyncRepository( const char *name, pkgXmlNode *repository )
437 /* Fetch a named package catalogue from a specified Internet repository.
439 * Package catalogues are XML files; the master copy on the Internet host
440 * must be stored in lzma compressed format, and named to comply with the
441 * convention "%F.xml.lzma", in which "%F" represents the value of the
442 * "name" argument passed to this pkgXmlDocument class method.
444 const char *url_template;
445 if( (url_template = repository->GetPropVal( uri_key, NULL )) != NULL )
447 /* Initialise a streaming agent, to manage the catalogue download;
448 * (note that we must include the "%/M" placeholder in the template
449 * for the local name, to accommodate the name of the intermediate
450 * "in-transit" directory used by the streaming agent).
452 pkgInternetLzmaStreamingAgent download( name, DATA_CACHE_PATH "%/M/%F.xml" );
454 /* Construct the full URI for the master catalogue, and stream it to
455 * a locally cached, decompressed copy of the XML file.
457 const char *mirror = repository->GetPropVal( mirror_key, NULL );
458 char catalogue_url[mkpath( NULL, url_template, name, mirror )];
459 mkpath( catalogue_url, url_template, name, mirror );
460 if( download.Get( catalogue_url ) <= 0 )
461 dmh_notify( DMH_ERROR,
462 "Sync Repository: %s: download failed\n", catalogue_url
466 /* We will only replace our current working copy of this catalogue,
467 * (if one already exists), with the copy we just downloaded, if this
468 * downloaded copy bears an issue number indicating that it is more
469 * recent than the working copy.
471 const char *repository_version, *working_version;
472 if( (repository_version = serial_number( download.DestFile() )) != NULL )
474 /* Identify the location for the working copy, (if it exists).
476 const char *working_copy_path_name = WORKING_DATA_PATH "/%F.xml";
477 char working_copy[mkpath( NULL, working_copy_path_name, name, NULL )];
478 mkpath( working_copy, working_copy_path_name, name, NULL );
480 /* Compare issue serial numbers...
482 if( ((working_version = serial_number( working_copy )) == NULL)
483 || ((strcmp( repository_version, working_version )) > 0) )
485 /* In these circumstances, we couldn't identify an issue number
486 * for the working copy of the catalogue; (maybe there is no such
487 * catalogue, or maybe it doesn't specify a valid issue number);
488 * in either case, we promote the downloaded copy in its place.
490 * FIXME: we assume that the working file and the downloaded copy
491 * are stored on the same physical file system device, so we may
492 * replace the former by simply deleting it, and renaming the
493 * latter with its original path name; we make no provision for
494 * replacing the working version by physical data copying.
496 unlink( working_copy );
497 rename( download.DestFile(), working_copy );
500 /* The issue numbers, returned by the serial_number() function, were
501 * allocated on the heap; free them to avoid leaking memory!
503 free( (void *)(repository_version) );
505 * The working copy issue number may be represented by a NULL pointer;
506 * while it may be safe to call free on this, it just *seems* wrong, so
507 * we check it first, to be certain.
509 if( working_version != NULL )
510 free( (void *)(working_version) );
513 /* If the downloaded copy of the catalogue is still in the download cache,
514 * we have chosen to keep a previous working copy, so we have no further
515 * use for the downloaded copy; discard it, noting that we don't need to
516 * confirm its existence because this will fail silently, if it is no
519 unlink( download.DestFile() );
523 /* $RCSfile$: end of file */