]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
*** empty log message ***
[apt.git] / methods / gzip.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: gzip.cc,v 1.17 2003/02/10 07:34:41 doogie 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 <apti18n.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/acquire-method.h>
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/hashes.h>
18
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <utime.h>
22 #include <stdio.h>
23 #include <errno.h>
24 /*}}}*/
25
26 const char *Prog;
27
28 class GzipMethod : public pkgAcqMethod
29 {
30 virtual bool Fetch(FetchItem *Itm);
31
32 public:
33
34 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
35 };
36
37
38 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 bool GzipMethod::Fetch(FetchItem *Itm)
42 {
43 URI Get = Itm->Uri;
44 string Path = Get.Host + Get.Path; // To account for relative paths
45
46 string GzPathOption = "Dir::bin::"+string(Prog);
47
48 FetchResult Res;
49 Res.Filename = Itm->DestFile;
50 URIStart(Res);
51
52 // Open the source and destination files
53 FileFd From(Path,FileFd::ReadOnly);
54
55 int GzOut[2];
56 if (pipe(GzOut) < 0)
57 return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
58
59 // Fork gzip
60 int Process = ExecFork();
61 if (Process == 0)
62 {
63 close(GzOut[0]);
64 dup2(From.Fd(),STDIN_FILENO);
65 dup2(GzOut[1],STDOUT_FILENO);
66 From.Close();
67 close(GzOut[1]);
68 SetCloseExec(STDIN_FILENO,false);
69 SetCloseExec(STDOUT_FILENO,false);
70
71 const char *Args[3];
72 string Tmp = _config->Find(GzPathOption,Prog);
73 Args[0] = Tmp.c_str();
74 Args[1] = "-d";
75 Args[2] = 0;
76 execvp(Args[0],(char **)Args);
77 _exit(100);
78 }
79 From.Close();
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 {
102 _error->Errno("read", _("Read error from %s process"),Prog);
103 Failed = true;
104 break;
105 }
106
107 if (Count == 0)
108 break;
109
110 Hash.Add(Buffer,Count);
111 if (To.Write(Buffer,Count) == false)
112 {
113 Failed = true;
114 break;
115 }
116 }
117
118 // Wait for gzip to finish
119 if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
120 {
121 To.OpFail();
122 return false;
123 }
124
125 To.Close();
126
127 if (Failed == true)
128 return false;
129
130 // Transfer the modification times
131 struct stat Buf;
132 if (stat(Path.c_str(),&Buf) != 0)
133 return _error->Errno("stat",_("Failed to stat"));
134
135 struct utimbuf TimeBuf;
136 TimeBuf.actime = Buf.st_atime;
137 TimeBuf.modtime = Buf.st_mtime;
138 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
139 return _error->Errno("utime",_("Failed to set modification time"));
140
141 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
142 return _error->Errno("stat",_("Failed to stat"));
143
144 // Return a Done response
145 Res.LastModified = Buf.st_mtime;
146 Res.Size = Buf.st_size;
147 Res.TakeHashes(Hash);
148
149 URIDone(Res);
150
151 return true;
152 }
153 /*}}}*/
154
155 int main(int argc, char *argv[])
156 {
157 GzipMethod Mth;
158
159 Prog = strrchr(argv[0],'/');
160 Prog++;
161
162 return Mth.Run();
163 }