]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
SHA-1 hashing algorithm
[apt.git] / methods / gzip.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: gzip.cc,v 1.11 2001/03/06 03:11:22 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.1",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 string GzipPath = _config->Find("Dir::bin::gzip","gzip");
40
41 FetchResult Res;
42 Res.Filename = Itm->DestFile;
43 URIStart(Res);
44
45 // Open the source and destintation files
46 FileFd From(Path,FileFd::ReadOnly);
47 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
48 To.EraseOnFailure();
49 if (_error->PendingError() == true)
50 return false;
51
52 // Fork gzip
53 int Process = fork();
54 if (Process < 0)
55 return _error->Errno("fork",string("Couldn't fork "+GzipPath).c_str());
56
57 // The child
58 if (Process == 0)
59 {
60 dup2(From.Fd(),STDIN_FILENO);
61 dup2(To.Fd(),STDOUT_FILENO);
62 From.Close();
63 To.Close();
64 SetCloseExec(STDIN_FILENO,false);
65 SetCloseExec(STDOUT_FILENO,false);
66
67 const char *Args[3];
68 Args[0] = GzipPath.c_str();
69 Args[1] = "-d";
70 Args[2] = 0;
71 execvp(Args[0],(char **)Args);
72 exit(100);
73 }
74 From.Close();
75
76 // Wait for gzip to finish
77 if (ExecWait(Process,GzipPath.c_str(),false) == false)
78 {
79 To.OpFail();
80 return false;
81 }
82
83 To.Close();
84
85 // Transfer the modification times
86 struct stat Buf;
87 if (stat(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(Itm->DestFile.c_str(),&TimeBuf) != 0)
94 return _error->Errno("utime","Failed to set modification time");
95
96 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
97 return _error->Errno("stat","Failed to stat");
98
99 // Return a Done response
100 Res.LastModified = Buf.st_mtime;
101 Res.Size = Buf.st_size;
102 URIDone(Res);
103
104 return true;
105 }
106 /*}}}*/
107
108 int main()
109 {
110 GzipMethod Mth;
111 return Mth.Run();
112 }