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