]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
More portability thingies
[apt.git] / methods / gzip.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: gzip.cc,v 1.9 1999/12/10 23:40:29 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
39 FetchResult Res;
40 Res.Filename = Itm->DestFile;
41 URIStart(Res);
42
43 // Open the source and destintation files
44 FileFd From(Get.Path,FileFd::ReadOnly);
45 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
46 To.EraseOnFailure();
47 if (_error->PendingError() == true)
48 return false;
49
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)
57 {
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
75 if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
76 {
77 To.OpFail();
78 return false;
79 }
80
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");
87
88 struct utimbuf TimeBuf;
89 TimeBuf.actime = Buf.st_atime;
90 TimeBuf.modtime = Buf.st_mtime;
91 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
92 return _error->Errno("utime","Failed to set modification time");
93
94 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
95 return _error->Errno("stat","Failed to stat");
96
97 // Return a Done response
98 Res.LastModified = Buf.st_mtime;
99 Res.Size = Buf.st_size;
100 URIDone(Res);
101
102 return true;
103 }
104 /*}}}*/
105
106 int main()
107 {
108 GzipMethod Mth;
109 return Mth.Run();
110 }