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