]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
methods/cdrom.cc: fix compile error
[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 54
84cc6f73
MV
55 // if the file is empty, just rename it and return
56 if(From.Size() == 0)
57 {
60a9e375 58 rename(Path.c_str(), Itm->DestFile.c_str());
84cc6f73
MV
59 return true;
60 }
61
63b1700f
AL
62 int GzOut[2];
63 if (pipe(GzOut) < 0)
dc738e7a 64 return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
63b1700f 65
93bf083d 66 // Fork gzip
b3d44315 67 pid_t Process = ExecFork();
93bf083d 68 if (Process == 0)
92173b19 69 {
63b1700f 70 close(GzOut[0]);
93bf083d 71 dup2(From.Fd(),STDIN_FILENO);
63b1700f 72 dup2(GzOut[1],STDOUT_FILENO);
93bf083d 73 From.Close();
63b1700f 74 close(GzOut[1]);
93bf083d
AL
75 SetCloseExec(STDIN_FILENO,false);
76 SetCloseExec(STDOUT_FILENO,false);
77
78 const char *Args[3];
2d33c52c
AL
79 string Tmp = _config->Find(GzPathOption,Prog);
80 Args[0] = Tmp.c_str();
93bf083d
AL
81 Args[1] = "-d";
82 Args[2] = 0;
83 execvp(Args[0],(char **)Args);
63b1700f 84 _exit(100);
93bf083d
AL
85 }
86 From.Close();
63b1700f
AL
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 {
dc738e7a 109 _error->Errno("read", _("Read error from %s process"),Prog);
63b1700f
AL
110 Failed = true;
111 break;
112 }
113
114 if (Count == 0)
115 break;
116
117 Hash.Add(Buffer,Count);
678bc33e 118 if (To.Write(Buffer,Count) == false)
2204bd80 119 {
678bc33e 120 Failed = true;
504bbe47 121 FromGz.Close();
678bc33e 122 break;
2204bd80 123 }
63b1700f 124 }
93bf083d
AL
125
126 // Wait for gzip to finish
2204bd80 127 if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
93bf083d
AL
128 {
129 To.OpFail();
1ae93c94
AL
130 return false;
131 }
132
93bf083d
AL
133 To.Close();
134
63b1700f
AL
135 if (Failed == true)
136 return false;
137
93bf083d
AL
138 // Transfer the modification times
139 struct stat Buf;
4509574a 140 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 141 return _error->Errno("stat",_("Failed to stat"));
92173b19 142
93bf083d
AL
143 struct utimbuf TimeBuf;
144 TimeBuf.actime = Buf.st_atime;
145 TimeBuf.modtime = Buf.st_mtime;
be4401bf 146 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
dc738e7a 147 return _error->Errno("utime",_("Failed to set modification time"));
92173b19 148
18ef0a78 149 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
dc738e7a 150 return _error->Errno("stat",_("Failed to stat"));
18ef0a78 151
93bf083d 152 // Return a Done response
93bf083d 153 Res.LastModified = Buf.st_mtime;
18ef0a78 154 Res.Size = Buf.st_size;
a7c835af 155 Res.TakeHashes(Hash);
63b1700f 156
93bf083d 157 URIDone(Res);
92173b19 158
93bf083d
AL
159 return true;
160}
161 /*}}}*/
162
2204bd80 163int main(int argc, char *argv[])
93bf083d 164{
b25423f6
MZ
165 setlocale(LC_ALL, "");
166
93bf083d 167 GzipMethod Mth;
2204bd80
AL
168
169 Prog = strrchr(argv[0],'/');
170 Prog++;
171
93bf083d 172 return Mth.Run();
92173b19 173}