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