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