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