Working acquire code
[ntk/apt.git] / methods / gzip.cc
CommitLineData
92173b19
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
bfd22fc0 3// $Id: gzip.cc,v 1.5 1998/11/05 07:21:47 jgg Exp $
92173b19
AL
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>
93bf083d 14#include <apt-pkg/acquire-method.h>
92173b19
AL
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
93bf083d 24class GzipMethod : public pkgAcqMethod
92173b19 25{
be4401bf 26 virtual bool Fetch(FetchItem *Itm);
92173b19 27
93bf083d
AL
28 public:
29
30 GzipMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
31};
92173b19 32
93bf083d
AL
33// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
34// ---------------------------------------------------------------------
35/* */
be4401bf 36bool GzipMethod::Fetch(FetchItem *Itm)
92173b19 37{
be4401bf 38 URI Get = Itm->Uri;
bfd22fc0 39
93bf083d
AL
40 // Open the source and destintation files
41 FileFd From(Get.Path,FileFd::ReadOnly);
be4401bf 42 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
93bf083d
AL
43 To.EraseOnFailure();
44 if (_error->PendingError() == true)
45 return false;
92173b19 46
93bf083d
AL
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)
92173b19 54 {
93bf083d
AL
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");
92173b19 91
93bf083d
AL
92 struct utimbuf TimeBuf;
93 TimeBuf.actime = Buf.st_atime;
94 TimeBuf.modtime = Buf.st_mtime;
be4401bf 95 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
93bf083d 96 return _error->Errno("utime","Failed to set modification time");
92173b19 97
93bf083d
AL
98 // Return a Done response
99 FetchResult Res;
100 Res.LastModified = Buf.st_mtime;
be4401bf 101 Res.Filename = Itm->DestFile;
93bf083d 102 URIDone(Res);
92173b19 103
93bf083d
AL
104 return true;
105}
106 /*}}}*/
107
108int main()
109{
110 GzipMethod Mth;
111 return Mth.Run();
92173b19 112}