]>
git.saurik.com Git - apt-legacy.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
);
55 // if the file is empty, just rename it and return
58 rename(Path
.c_str(), Itm
->DestFile
.c_str());
64 return _error
->Errno("pipe",_("Couldn't open pipe for %s"),Prog
);
67 pid_t Process
= ExecFork();
71 dup2(From
.Fd(),STDIN_FILENO
);
72 dup2(GzOut
[1],STDOUT_FILENO
);
75 SetCloseExec(STDIN_FILENO
,false);
76 SetCloseExec(STDOUT_FILENO
,false);
79 string Tmp
= _config
->Find(GzPathOption
,Prog
);
80 Args
[0] = Tmp
.c_str();
83 execvp(Args
[0],(char **)Args
);
89 FileFd
FromGz(GzOut
[0]); // For autoclose
90 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
92 if (_error
->PendingError() == true)
95 // Read data from gzip, generate checksums and write
100 unsigned char Buffer
[4*1024];
103 Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
104 if (Count
< 0 && errno
== EINTR
)
109 _error
->Errno("read", _("Read error from %s process"),Prog
);
117 Hash
.Add(Buffer
,Count
);
118 if (To
.Write(Buffer
,Count
) == false)
126 // Wait for gzip to finish
127 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
138 // Transfer the modification times
140 if (stat(Path
.c_str(),&Buf
) != 0)
141 return _error
->Errno("stat",_("Failed to stat"));
143 struct utimbuf TimeBuf
;
144 TimeBuf
.actime
= Buf
.st_atime
;
145 TimeBuf
.modtime
= Buf
.st_mtime
;
146 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
147 return _error
->Errno("utime",_("Failed to set modification time"));
149 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
150 return _error
->Errno("stat",_("Failed to stat"));
152 // Return a Done response
153 Res
.LastModified
= Buf
.st_mtime
;
154 Res
.Size
= Buf
.st_size
;
155 Res
.TakeHashes(Hash
);
163 int main(int argc
, char *argv
[])
165 setlocale(LC_ALL
, "");
169 Prog
= strrchr(argv
[0],'/');