]>
git.saurik.com Git - apt-legacy.git/blob - methods/gzip.cc
2 #include <mach-o/nlist.h>
5 // -*- mode: cpp; mode: fold -*-
7 // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
8 /* ######################################################################
10 GZip method - Take a file URI in and decompress it into the target
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/acquire-method.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/hashes.h>
32 class GzipMethod
: public pkgAcqMethod
34 virtual bool Fetch(FetchItem
*Itm
);
38 GzipMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
42 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
43 // ---------------------------------------------------------------------
45 bool GzipMethod::Fetch(FetchItem
*Itm
)
48 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
50 string GzPathOption
= "Dir::bin::"+string(Prog
);
53 Res
.Filename
= Itm
->DestFile
;
56 // Open the source and destination files
57 FileFd
From(Path
,FileFd::ReadOnly
);
59 // if the file is empty, just rename it and return
62 rename(Path
.c_str(), Itm
->DestFile
.c_str());
68 return _error
->Errno("pipe",_("Couldn't open pipe for %s"),Prog
);
71 pid_t Process
= ExecFork();
75 dup2(From
.Fd(),STDIN_FILENO
);
76 dup2(GzOut
[1],STDOUT_FILENO
);
79 SetCloseExec(STDIN_FILENO
,false);
80 SetCloseExec(STDOUT_FILENO
,false);
83 string Tmp
= _config
->Find(GzPathOption
,Prog
);
84 Args
[0] = Tmp
.c_str();
87 execvp(Args
[0],(char **)Args
);
93 FileFd
FromGz(GzOut
[0]); // For autoclose
94 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
96 if (_error
->PendingError() == true)
99 // Read data from gzip, generate checksums and write
104 unsigned char Buffer
[4*1024];
107 Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
108 if (Count
< 0 && errno
== EINTR
)
113 _error
->Errno("read", _("Read error from %s process"),Prog
);
121 Hash
.Add(Buffer
,Count
);
122 if (To
.Write(Buffer
,Count
) == false)
130 // Wait for gzip to finish
131 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
142 // Transfer the modification times
144 if (stat(Path
.c_str(),&Buf
) != 0)
145 return _error
->Errno("stat",_("Failed to stat"));
147 struct utimbuf TimeBuf
;
148 TimeBuf
.actime
= Buf
.st_atime
;
149 TimeBuf
.modtime
= Buf
.st_mtime
;
150 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
151 return _error
->Errno("utime",_("Failed to set modification time"));
153 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
154 return _error
->Errno("stat",_("Failed to stat"));
156 // Return a Done response
157 Res
.LastModified
= Buf
.st_mtime
;
158 Res
.Size
= Buf
.st_size
;
159 Res
.TakeHashes(Hash
);
167 int main(int argc
, char *argv
[])
169 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
171 memset(nl
, 0, sizeof(nl
));
172 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
173 nlist("/usr/lib/libc.dylib", nl
);
174 if (nl
[0].n_type
!= N_UNDF
)
175 *(int *) nl
[0].n_value
= 0;
178 setlocale(LC_ALL
, "");
182 Prog
= strrchr(argv
[0],'/');