]>
git.saurik.com Git - apt.git/blob - methods/bzip2.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Bzip2 method - Take a file URI in and decompress it into the target
8 While the method is named "bzip2" it handles also other compression
9 types as it calls binaries based on the name of the method,
10 so it can also be used to handle gzip, lzma and others if named
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 Bzip2Method
: public pkgAcqMethod
34 virtual bool Fetch(FetchItem
*Itm
);
38 Bzip2Method() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
42 // Bzip2Method::Fetch - Decompress the passed URI /*{{{*/
43 // ---------------------------------------------------------------------
45 bool Bzip2Method::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(From
.FileSize() == 0)
60 return _error
->Error(_("Empty files can't be valid archives"));
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::WriteAtomic
);
92 if (_error
->PendingError() == true)
95 // Read data from bzip2, generate checksums and write
100 unsigned char Buffer
[4*1024];
102 ssize_t Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
103 if (Count
< 0 && errno
== EINTR
)
108 _error
->Errno("read", _("Read error from %s process"),Prog
);
116 Hash
.Add(Buffer
,Count
);
117 if (To
.Write(Buffer
,Count
) == false)
125 // Wait for bzip2 to finish
126 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
137 // Transfer the modification times
139 if (stat(Path
.c_str(),&Buf
) != 0)
140 return _error
->Errno("stat",_("Failed to stat"));
142 struct utimbuf TimeBuf
;
143 TimeBuf
.actime
= Buf
.st_atime
;
144 TimeBuf
.modtime
= Buf
.st_mtime
;
145 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
146 return _error
->Errno("utime",_("Failed to set modification time"));
148 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
149 return _error
->Errno("stat",_("Failed to stat"));
151 // Return a Done response
152 Res
.LastModified
= Buf
.st_mtime
;
153 Res
.Size
= Buf
.st_size
;
154 Res
.TakeHashes(Hash
);
162 int main(int argc
, char *argv
[])
164 setlocale(LC_ALL
, "");
168 Prog
= strrchr(argv
[0],'/');