]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz 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 | #include <apti18n.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 | pid_t 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 | FromGz.Close(); | |
115 | break; | |
116 | } | |
117 | } | |
118 | ||
119 | // Wait for gzip to finish | |
120 | if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false) | |
121 | { | |
122 | To.OpFail(); | |
123 | return false; | |
124 | } | |
125 | ||
126 | To.Close(); | |
127 | ||
128 | if (Failed == true) | |
129 | return false; | |
130 | ||
131 | // Transfer the modification times | |
132 | struct stat Buf; | |
133 | if (stat(Path.c_str(),&Buf) != 0) | |
134 | return _error->Errno("stat",_("Failed to stat")); | |
135 | ||
136 | struct utimbuf TimeBuf; | |
137 | TimeBuf.actime = Buf.st_atime; | |
138 | TimeBuf.modtime = Buf.st_mtime; | |
139 | if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0) | |
140 | return _error->Errno("utime",_("Failed to set modification time")); | |
141 | ||
142 | if (stat(Itm->DestFile.c_str(),&Buf) != 0) | |
143 | return _error->Errno("stat",_("Failed to stat")); | |
144 | ||
145 | // Return a Done response | |
146 | Res.LastModified = Buf.st_mtime; | |
147 | Res.Size = Buf.st_size; | |
148 | Res.TakeHashes(Hash); | |
149 | ||
150 | URIDone(Res); | |
151 | ||
152 | return true; | |
153 | } | |
154 | /*}}}*/ | |
155 | ||
156 | int main(int argc, char *argv[]) | |
157 | { | |
158 | setlocale(LC_ALL, ""); | |
159 | ||
160 | GzipMethod Mth; | |
161 | ||
162 | Prog = strrchr(argv[0],'/'); | |
163 | Prog++; | |
164 | ||
165 | return Mth.Run(); | |
166 | } |