]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
Fixed passive connect waiting bug
[apt.git] / methods / gzip.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: gzip.cc,v 1.10 2000/03/18 07:39:33 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
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <utime.h>
20 #include <stdio.h>
21 /*}}}*/
22
23 class GzipMethod : public pkgAcqMethod
24 {
25 virtual bool Fetch(FetchItem *Itm);
26
27 public:
28
29 GzipMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
30 };
31
32 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
33 // ---------------------------------------------------------------------
34 /* */
35 bool GzipMethod::Fetch(FetchItem *Itm)
36 {
37 URI Get = Itm->Uri;
38 string Path = Get.Host + Get.Path; // To account for relative paths
39
40 FetchResult Res;
41 Res.Filename = Itm->DestFile;
42 URIStart(Res);
43
44 // Open the source and destintation files
45 FileFd From(Path,FileFd::ReadOnly);
46 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
47 To.EraseOnFailure();
48 if (_error->PendingError() == true)
49 return false;
50
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)
58 {
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 if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
77 {
78 To.OpFail();
79 return false;
80 }
81
82 To.Close();
83
84 // Transfer the modification times
85 struct stat Buf;
86 if (stat(Path.c_str(),&Buf) != 0)
87 return _error->Errno("stat","Failed to stat");
88
89 struct utimbuf TimeBuf;
90 TimeBuf.actime = Buf.st_atime;
91 TimeBuf.modtime = Buf.st_mtime;
92 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
93 return _error->Errno("utime","Failed to set modification time");
94
95 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
96 return _error->Errno("stat","Failed to stat");
97
98 // Return a Done response
99 Res.LastModified = Buf.st_mtime;
100 Res.Size = Buf.st_size;
101 URIDone(Res);
102
103 return true;
104 }
105 /*}}}*/
106
107 int main()
108 {
109 GzipMethod Mth;
110 return Mth.Run();
111 }