]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
* methods/gzip.cc:
[apt.git] / methods / gzip.cc
CommitLineData
92173b19
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 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
b3d44315 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 113 Failed = true;
504bbe47 114 FromGz.Close();
678bc33e 115 break;
2204bd80 116 }
63b1700f 117 }
93bf083d
AL
118
119 // Wait for gzip to finish
2204bd80 120 if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
93bf083d
AL
121 {
122 To.OpFail();
1ae93c94
AL
123 return false;
124 }
125
93bf083d
AL
126 To.Close();
127
63b1700f
AL
128 if (Failed == true)
129 return false;
130
93bf083d
AL
131 // Transfer the modification times
132 struct stat Buf;
4509574a 133 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 134 return _error->Errno("stat",_("Failed to stat"));
92173b19 135
93bf083d
AL
136 struct utimbuf TimeBuf;
137 TimeBuf.actime = Buf.st_atime;
138 TimeBuf.modtime = Buf.st_mtime;
be4401bf 139 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
dc738e7a 140 return _error->Errno("utime",_("Failed to set modification time"));
92173b19 141
18ef0a78 142 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
dc738e7a 143 return _error->Errno("stat",_("Failed to stat"));
18ef0a78 144
93bf083d 145 // Return a Done response
93bf083d 146 Res.LastModified = Buf.st_mtime;
18ef0a78 147 Res.Size = Buf.st_size;
a7c835af 148 Res.TakeHashes(Hash);
63b1700f 149
93bf083d 150 URIDone(Res);
92173b19 151
93bf083d
AL
152 return true;
153}
154 /*}}}*/
155
2204bd80 156int main(int argc, char *argv[])
93bf083d 157{
b25423f6
MZ
158 setlocale(LC_ALL, "");
159
93bf083d 160 GzipMethod Mth;
2204bd80
AL
161
162 Prog = strrchr(argv[0],'/');
163 Prog++;
164
93bf083d 165 return Mth.Run();
92173b19 166}