]>
Commit | Line | Data |
---|---|---|
92173b19 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
1ae93c94 | 3 | // $Id: gzip.cc,v 1.9 1999/12/10 23:40:29 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> |
92173b19 AL |
16 | |
17 | #include <sys/stat.h> | |
18 | #include <unistd.h> | |
19 | #include <utime.h> | |
92173b19 AL |
20 | #include <stdio.h> |
21 | /*}}}*/ | |
22 | ||
93bf083d | 23 | class GzipMethod : public pkgAcqMethod |
92173b19 | 24 | { |
be4401bf | 25 | virtual bool Fetch(FetchItem *Itm); |
92173b19 | 26 | |
93bf083d AL |
27 | public: |
28 | ||
29 | GzipMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; | |
30 | }; | |
92173b19 | 31 | |
93bf083d AL |
32 | // GzipMethod::Fetch - Decompress the passed URI /*{{{*/ |
33 | // --------------------------------------------------------------------- | |
34 | /* */ | |
be4401bf | 35 | bool GzipMethod::Fetch(FetchItem *Itm) |
92173b19 | 36 | { |
be4401bf | 37 | URI Get = Itm->Uri; |
bfd22fc0 | 38 | |
b98f2859 AL |
39 | FetchResult Res; |
40 | Res.Filename = Itm->DestFile; | |
41 | URIStart(Res); | |
42 | ||
93bf083d AL |
43 | // Open the source and destintation files |
44 | FileFd From(Get.Path,FileFd::ReadOnly); | |
be4401bf | 45 | FileFd To(Itm->DestFile,FileFd::WriteEmpty); |
93bf083d AL |
46 | To.EraseOnFailure(); |
47 | if (_error->PendingError() == true) | |
48 | return false; | |
92173b19 | 49 | |
93bf083d AL |
50 | // Fork gzip |
51 | int Process = fork(); | |
52 | if (Process < 0) | |
53 | return _error->Errno("fork","Couldn't fork gzip"); | |
54 | ||
55 | // The child | |
56 | if (Process == 0) | |
92173b19 | 57 | { |
93bf083d AL |
58 | dup2(From.Fd(),STDIN_FILENO); |
59 | dup2(To.Fd(),STDOUT_FILENO); | |
60 | From.Close(); | |
61 | To.Close(); | |
62 | SetCloseExec(STDIN_FILENO,false); | |
63 | SetCloseExec(STDOUT_FILENO,false); | |
64 | ||
65 | const char *Args[3]; | |
66 | Args[0] = _config->Find("Dir::bin::gzip","gzip").c_str(); | |
67 | Args[1] = "-d"; | |
68 | Args[2] = 0; | |
69 | execvp(Args[0],(char **)Args); | |
70 | exit(100); | |
71 | } | |
72 | From.Close(); | |
73 | ||
74 | // Wait for gzip to finish | |
1ae93c94 | 75 | if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false) |
93bf083d AL |
76 | { |
77 | To.OpFail(); | |
1ae93c94 AL |
78 | return false; |
79 | } | |
80 | ||
93bf083d AL |
81 | To.Close(); |
82 | ||
83 | // Transfer the modification times | |
84 | struct stat Buf; | |
85 | if (stat(Get.Path.c_str(),&Buf) != 0) | |
86 | return _error->Errno("stat","Failed to stat"); | |
92173b19 | 87 | |
93bf083d AL |
88 | struct utimbuf TimeBuf; |
89 | TimeBuf.actime = Buf.st_atime; | |
90 | TimeBuf.modtime = Buf.st_mtime; | |
be4401bf | 91 | if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0) |
93bf083d | 92 | return _error->Errno("utime","Failed to set modification time"); |
92173b19 | 93 | |
18ef0a78 AL |
94 | if (stat(Itm->DestFile.c_str(),&Buf) != 0) |
95 | return _error->Errno("stat","Failed to stat"); | |
96 | ||
93bf083d | 97 | // Return a Done response |
93bf083d | 98 | Res.LastModified = Buf.st_mtime; |
18ef0a78 | 99 | Res.Size = Buf.st_size; |
93bf083d | 100 | URIDone(Res); |
92173b19 | 101 | |
93bf083d AL |
102 | return true; |
103 | } | |
104 | /*}}}*/ | |
105 | ||
106 | int main() | |
107 | { | |
108 | GzipMethod Mth; | |
109 | return Mth.Run(); | |
92173b19 | 110 | } |