]>
git.saurik.com Git - apt.git/blob - methods/gzip.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 GZip method - Take a file URI in and decompress it into the target
9 ##################################################################### */
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 #include <apt-pkg/hashes.h>
28 class GzipMethod
: public pkgAcqMethod
30 virtual bool Fetch(FetchItem
*Itm
);
34 GzipMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
38 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
39 // ---------------------------------------------------------------------
41 bool GzipMethod::Fetch(FetchItem
*Itm
)
44 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
46 string GzPathOption
= "Dir::bin::"+string(Prog
);
49 Res
.Filename
= Itm
->DestFile
;
52 // Open the source and destination files
53 FileFd
From(Path
,FileFd::ReadOnly
);
57 return _error
->Errno("pipe",_("Couldn't open pipe for %s"),Prog
);
60 pid_t Process
= ExecFork();
64 dup2(From
.Fd(),STDIN_FILENO
);
65 dup2(GzOut
[1],STDOUT_FILENO
);
68 SetCloseExec(STDIN_FILENO
,false);
69 SetCloseExec(STDOUT_FILENO
,false);
72 string Tmp
= _config
->Find(GzPathOption
,Prog
);
73 Args
[0] = Tmp
.c_str();
76 execvp(Args
[0],(char **)Args
);
82 FileFd
FromGz(GzOut
[0]); // For autoclose
83 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
85 if (_error
->PendingError() == true)
88 // Read data from gzip, generate checksums and write
93 unsigned char Buffer
[4*1024];
96 Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
97 if (Count
< 0 && errno
== EINTR
)
102 _error
->Errno("read", _("Read error from %s process"),Prog
);
110 Hash
.Add(Buffer
,Count
);
111 if (To
.Write(Buffer
,Count
) == false)
119 // Wait for gzip to finish
120 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
131 // Transfer the modification times
133 if (stat(Path
.c_str(),&Buf
) != 0)
134 return _error
->Errno("stat",_("Failed to stat"));
136 struct utimbuf TimeBuf
;
137 TimeBuf
.actime
= Buf
.st_atime
;
138 TimeBuf
.modtime
= Buf
.st_mtime
;
139 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
140 return _error
->Errno("utime",_("Failed to set modification time"));
142 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
143 return _error
->Errno("stat",_("Failed to stat"));
145 // Return a Done response
146 Res
.LastModified
= Buf
.st_mtime
;
147 Res
.Size
= Buf
.st_size
;
148 Res
.TakeHashes(Hash
);
156 int main(int argc
, char *argv
[])
158 setlocale(LC_ALL
, "");
162 Prog
= strrchr(argv
[0],'/');