]>
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)
118 // Wait for gzip to finish
119 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
130 // Transfer the modification times
132 if (stat(Path
.c_str(),&Buf
) != 0)
133 return _error
->Errno("stat",_("Failed to stat"));
135 struct utimbuf TimeBuf
;
136 TimeBuf
.actime
= Buf
.st_atime
;
137 TimeBuf
.modtime
= Buf
.st_mtime
;
138 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
139 return _error
->Errno("utime",_("Failed to set modification time"));
141 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
142 return _error
->Errno("stat",_("Failed to stat"));
144 // Return a Done response
145 Res
.LastModified
= Buf
.st_mtime
;
146 Res
.Size
= Buf
.st_size
;
147 Res
.TakeHashes(Hash
);
155 int main(int argc
, char *argv
[])
157 setlocale(LC_ALL
, "");
161 Prog
= strrchr(argv
[0],'/');