]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.cc
gzip method
[apt.git] / apt-pkg / acquire-item.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-item.cc,v 1.4 1998/10/24 04:57:56 jgg Exp $
4 /* ######################################################################
5
6 Acquire Item - Item to acquire
7
8 Each item can download to exactly one file at a time. This means you
9 cannot create an item that fetches two uri's to two files at the same
10 time. The pkgAcqIndex class creates a second class upon instantiation
11 to fetch the other index files because of this.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #ifdef __GNUG__
17 #pragma implementation "apt-pkg/acquire-item.h"
18 #endif
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/configuration.h>
21 #include <strutl.h>
22
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <stdio.h>
28 /*}}}*/
29
30 // Acquire::Item::Item - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), QueueCounter(0)
34 {
35 Owner->Add(this);
36 Status = StatIdle;
37 }
38 /*}}}*/
39 // Acquire::Item::~Item - Destructor /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 pkgAcquire::Item::~Item()
43 {
44 Owner->Remove(this);
45 }
46 /*}}}*/
47 // Acquire::Item::Failed - Item failed to download /*{{{*/
48 // ---------------------------------------------------------------------
49 /* */
50 void pkgAcquire::Item::Failed(string Message)
51 {
52 Status = StatError;
53 ErrorText = LookupTag(Message,"Message");
54 if (QueueCounter <= 1)
55 Owner->Dequeue(this);
56 }
57 /*}}}*/
58 // Acquire::Item::Done - Item downloaded OK /*{{{*/
59 // ---------------------------------------------------------------------
60 /* */
61 void pkgAcquire::Item::Done(string,unsigned long,string)
62 {
63 Status = StatDone;
64 ErrorText = string();
65 Owner->Dequeue(this);
66 }
67 /*}}}*/
68
69 // AcqIndex::AcqIndex - Constructor /*{{{*/
70 // ---------------------------------------------------------------------
71 /* The package file is added to the queue and a second class is
72 instantiated to fetch the revision file */
73 pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,const pkgSourceList::Item *Location) :
74 Item(Owner), Location(Location)
75 {
76 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
77 DestFile += URItoFileName(Location->PackagesURI());
78
79 QueueURI(Location->PackagesURI() + ".gz",Location->PackagesInfo());
80
81 // Create the Release fetch class
82 new pkgAcqIndexRel(Owner,Location);
83 }
84 /*}}}*/
85 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
86 // ---------------------------------------------------------------------
87 /* The only header we use is the last-modified header. */
88 string pkgAcqIndex::Custom600Headers()
89 {
90 string Final = _config->FindDir("Dir::State::lists");
91 Final += URItoFileName(Location->PackagesURI());
92
93 struct stat Buf;
94 if (stat(Final.c_str(),&Buf) != 0)
95 return string();
96
97 return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
98 }
99 /*}}}*/
100 // AcqIndexRel::pkgAcqIndexRel - Constructor /*{{{*/
101 // ---------------------------------------------------------------------
102 /* The Release file is added to the queue */
103 pkgAcqIndexRel::pkgAcqIndexRel(pkgAcquire *Owner,
104 const pkgSourceList::Item *Location) :
105 Item(Owner), Location(Location)
106 {
107 DestFile = _config->FindDir("Dir::State::lists") + "partial/";
108 DestFile += URItoFileName(Location->ReleaseURI());
109
110 QueueURI(Location->ReleaseURI(),Location->ReleaseInfo());
111 }
112 /*}}}*/
113 // AcqIndexRel::Custom600Headers - Insert custom request headers /*{{{*/
114 // ---------------------------------------------------------------------
115 /* The only header we use is the last-modified header. */
116 string pkgAcqIndexRel::Custom600Headers()
117 {
118 string Final = _config->FindDir("Dir::State::lists");
119 Final += URItoFileName(Location->ReleaseURI());
120
121 struct stat Buf;
122 if (stat(Final.c_str(),&Buf) != 0)
123 return string();
124
125 return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
126 }
127 /*}}}*/
128 // AcqIndexRel::Done - Item downloaded OK /*{{{*/
129 // ---------------------------------------------------------------------
130 /* The release file was not placed into the download directory then
131 a copy URI is generated and it is copied there otherwise the file
132 in the partial directory is moved into .. and the URI is finished. */
133 void pkgAcqIndexRel::Done(string Message,unsigned long Size,string MD5)
134 {
135 Item::Done(Message,Size,MD5);
136
137 string FileName = LookupTag(Message,"Filename");
138 if (FileName.empty() == true)
139 {
140 Status = StatError;
141 ErrorText = "Method gave a blank filename";
142 }
143
144 // We have to copy it into place
145 if (FileName != DestFile)
146 {
147 QueueURI("copy:" + FileName,string());
148 return;
149 }
150
151 // Done, move it into position
152 string FinalFile = _config->FindDir("Dir::State::lists");
153 FinalFile += URItoFileName(Location->ReleaseURI());
154
155 if (rename(DestFile.c_str(),FinalFile.c_str()) != 0)
156 {
157 char S[300];
158 sprintf(S,"rename failed, %s (%s -> %s).",strerror(errno),
159 DestFile.c_str(),FinalFile.c_str());
160 Status = StatError;
161 ErrorText = S;
162 }
163 }
164 /*}}}*/