]>
git.saurik.com Git - apt.git/blob - methods/gzip.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: gzip.cc,v 1.16 2001/05/27 04:29:30 jgg 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>
27 class GzipMethod
: public pkgAcqMethod
29 virtual bool Fetch(FetchItem
*Itm
);
33 GzipMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
37 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
38 // ---------------------------------------------------------------------
40 bool GzipMethod::Fetch(FetchItem
*Itm
)
43 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
45 string GzPathOption
= "Dir::bin::"+string(Prog
);
48 Res
.Filename
= Itm
->DestFile
;
51 // Open the source and destination files
52 FileFd
From(Path
,FileFd::ReadOnly
);
56 return _error
->Errno("pipe","Couldn't open pipe for %s",Prog
);
59 int Process
= ExecFork();
63 dup2(From
.Fd(),STDIN_FILENO
);
64 dup2(GzOut
[1],STDOUT_FILENO
);
67 SetCloseExec(STDIN_FILENO
,false);
68 SetCloseExec(STDOUT_FILENO
,false);
71 string Tmp
= _config
->Find(GzPathOption
,Prog
);
72 Args
[0] = Tmp
.c_str();
75 execvp(Args
[0],(char **)Args
);
81 FileFd
FromGz(GzOut
[0]); // For autoclose
82 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
84 if (_error
->PendingError() == true)
87 // Read data from gzip, generate checksums and write
92 unsigned char Buffer
[4*1024];
95 Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
96 if (Count
< 0 && errno
== EINTR
)
101 _error
->Errno("read", "Read error from %s process",Prog
);
109 Hash
.Add(Buffer
,Count
);
110 if (To
.Write(Buffer
,Count
) == false)
117 // Wait for gzip to finish
118 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
129 // Transfer the modification times
131 if (stat(Path
.c_str(),&Buf
) != 0)
132 return _error
->Errno("stat","Failed to stat");
134 struct utimbuf TimeBuf
;
135 TimeBuf
.actime
= Buf
.st_atime
;
136 TimeBuf
.modtime
= Buf
.st_mtime
;
137 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
138 return _error
->Errno("utime","Failed to set modification time");
140 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
141 return _error
->Errno("stat","Failed to stat");
143 // Return a Done response
144 Res
.LastModified
= Buf
.st_mtime
;
145 Res
.Size
= Buf
.st_size
;
146 Res
.TakeHashes(Hash
);
154 int main(int argc
, char *argv
[])
158 Prog
= strrchr(argv
[0],'/');