]> git.saurik.com Git - apt.git/blame_incremental - methods/gzip.cc
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / methods / gzip.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
4/* ######################################################################
5
6 GZip method - Take a file URI in and decompress it into the target
7 file.
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
12#include <apt-pkg/fileutl.h>
13#include <apt-pkg/error.h>
14#include <apt-pkg/acquire-method.h>
15#include <apt-pkg/strutl.h>
16#include <apt-pkg/hashes.h>
17
18#include <sys/stat.h>
19#include <unistd.h>
20#include <utime.h>
21#include <stdio.h>
22#include <errno.h>
23#include <apti18n.h>
24 /*}}}*/
25
26const char *Prog;
27
28class GzipMethod : public pkgAcqMethod
29{
30 virtual bool Fetch(FetchItem *Itm);
31
32 public:
33
34 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
35};
36
37
38// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41bool GzipMethod::Fetch(FetchItem *Itm)
42{
43 URI Get = Itm->Uri;
44 string Path = Get.Host + Get.Path; // To account for relative paths
45
46 string GzPathOption = "Dir::bin::"+string(Prog);
47
48 FetchResult Res;
49 Res.Filename = Itm->DestFile;
50 URIStart(Res);
51
52 // Open the source and destination files
53 FileFd From(Path,FileFd::ReadOnly);
54
55 // if the file is empty, just rename it and return
56 if(From.Size() == 0)
57 {
58 rename(Path.c_str(), Itm->DestFile.c_str());
59 return true;
60 }
61
62 int GzOut[2];
63 if (pipe(GzOut) < 0)
64 return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
65
66 // Fork gzip
67 pid_t Process = ExecFork();
68 if (Process == 0)
69 {
70 close(GzOut[0]);
71 dup2(From.Fd(),STDIN_FILENO);
72 dup2(GzOut[1],STDOUT_FILENO);
73 From.Close();
74 close(GzOut[1]);
75 SetCloseExec(STDIN_FILENO,false);
76 SetCloseExec(STDOUT_FILENO,false);
77
78 const char *Args[3];
79 string Tmp = _config->Find(GzPathOption,Prog);
80 Args[0] = Tmp.c_str();
81 Args[1] = "-d";
82 Args[2] = 0;
83 execvp(Args[0],(char **)Args);
84 _exit(100);
85 }
86 From.Close();
87 close(GzOut[1]);
88
89 FileFd FromGz(GzOut[0]); // For autoclose
90 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
91 To.EraseOnFailure();
92 if (_error->PendingError() == true)
93 return false;
94
95 // Read data from gzip, generate checksums and write
96 Hashes Hash;
97 bool Failed = false;
98 while (1)
99 {
100 unsigned char Buffer[4*1024];
101 unsigned long Count;
102
103 Count = read(GzOut[0],Buffer,sizeof(Buffer));
104 if (Count < 0 && errno == EINTR)
105 continue;
106
107 if (Count < 0)
108 {
109 _error->Errno("read", _("Read error from %s process"),Prog);
110 Failed = true;
111 break;
112 }
113
114 if (Count == 0)
115 break;
116
117 Hash.Add(Buffer,Count);
118 if (To.Write(Buffer,Count) == false)
119 {
120 Failed = true;
121 FromGz.Close();
122 break;
123 }
124 }
125
126 // Wait for gzip to finish
127 if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
128 {
129 To.OpFail();
130 return false;
131 }
132
133 To.Close();
134
135 if (Failed == true)
136 return false;
137
138 // Transfer the modification times
139 struct stat Buf;
140 if (stat(Path.c_str(),&Buf) != 0)
141 return _error->Errno("stat",_("Failed to stat"));
142
143 struct utimbuf TimeBuf;
144 TimeBuf.actime = Buf.st_atime;
145 TimeBuf.modtime = Buf.st_mtime;
146 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
147 return _error->Errno("utime",_("Failed to set modification time"));
148
149 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
150 return _error->Errno("stat",_("Failed to stat"));
151
152 // Return a Done response
153 Res.LastModified = Buf.st_mtime;
154 Res.Size = Buf.st_size;
155 Res.TakeHashes(Hash);
156
157 URIDone(Res);
158
159 return true;
160}
161 /*}}}*/
162
163int main(int argc, char *argv[])
164{
165 setlocale(LC_ALL, "");
166
167 GzipMethod Mth;
168
169 Prog = strrchr(argv[0],'/');
170 Prog++;
171
172 return Mth.Run();
173}