]>
git.saurik.com Git - apt.git/blob - methods/gzip.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: gzip.cc,v 1.15 2001/03/13 06:51:46 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 Args
[0] = _config
->Find(GzPathOption
,Prog
).c_str();
74 execvp(Args
[0],(char **)Args
);
80 FileFd
FromGz(GzOut
[0]); // For autoclose
81 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
83 if (_error
->PendingError() == true)
86 // Read data from gzip, generate checksums and write
91 unsigned char Buffer
[4*1024];
94 Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
95 if (Count
< 0 && errno
== EINTR
)
100 _error
->Errno("read", "Read error from %s process",Prog
);
108 Hash
.Add(Buffer
,Count
);
109 if (To
.Write(Buffer
,Count
) == false)
116 // Wait for gzip to finish
117 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
128 // Transfer the modification times
130 if (stat(Path
.c_str(),&Buf
) != 0)
131 return _error
->Errno("stat","Failed to stat");
133 struct utimbuf TimeBuf
;
134 TimeBuf
.actime
= Buf
.st_atime
;
135 TimeBuf
.modtime
= Buf
.st_mtime
;
136 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
137 return _error
->Errno("utime","Failed to set modification time");
139 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
140 return _error
->Errno("stat","Failed to stat");
142 // Return a Done response
143 Res
.LastModified
= Buf
.st_mtime
;
144 Res
.Size
= Buf
.st_size
;
145 Res
.TakeHashes(Hash
);
153 int main(int argc
, char *argv
[])
157 Prog
= strrchr(argv
[0],'/');