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