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