]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: gzip.cc,v 1.16 2001/05/27 04:29:30 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 | string Tmp = _config->Find(GzPathOption,Prog); | |
72 | Args[0] = Tmp.c_str(); | |
73 | Args[1] = "-d"; | |
74 | Args[2] = 0; | |
75 | execvp(Args[0],(char **)Args); | |
76 | _exit(100); | |
77 | } | |
78 | From.Close(); | |
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 | { | |
101 | _error->Errno("read", "Read error from %s process",Prog); | |
102 | Failed = true; | |
103 | break; | |
104 | } | |
105 | ||
106 | if (Count == 0) | |
107 | break; | |
108 | ||
109 | Hash.Add(Buffer,Count); | |
110 | if (To.Write(Buffer,Count) == false) | |
111 | { | |
112 | Failed = true; | |
113 | break; | |
114 | } | |
115 | } | |
116 | ||
117 | // Wait for gzip to finish | |
118 | if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false) | |
119 | { | |
120 | To.OpFail(); | |
121 | return false; | |
122 | } | |
123 | ||
124 | To.Close(); | |
125 | ||
126 | if (Failed == true) | |
127 | return false; | |
128 | ||
129 | // Transfer the modification times | |
130 | struct stat Buf; | |
131 | if (stat(Path.c_str(),&Buf) != 0) | |
132 | return _error->Errno("stat","Failed to stat"); | |
133 | ||
134 | struct utimbuf TimeBuf; | |
135 | TimeBuf.actime = Buf.st_atime; | |
136 | TimeBuf.modtime = Buf.st_mtime; | |
137 | if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0) | |
138 | return _error->Errno("utime","Failed to set modification time"); | |
139 | ||
140 | if (stat(Itm->DestFile.c_str(),&Buf) != 0) | |
141 | return _error->Errno("stat","Failed to stat"); | |
142 | ||
143 | // Return a Done response | |
144 | Res.LastModified = Buf.st_mtime; | |
145 | Res.Size = Buf.st_size; | |
146 | Res.TakeHashes(Hash); | |
147 | ||
148 | URIDone(Res); | |
149 | ||
150 | return true; | |
151 | } | |
152 | /*}}}*/ | |
153 | ||
154 | int main(int argc, char *argv[]) | |
155 | { | |
156 | GzipMethod Mth; | |
157 | ||
158 | Prog = strrchr(argv[0],'/'); | |
159 | Prog++; | |
160 | ||
161 | return Mth.Run(); | |
162 | } |