]>
git.saurik.com Git - apt.git/blob - methods/bzip2.cc
ad5db6cfba3774aa9800287ece61d20c0b228344
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 /*{{{*/
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/acquire-method.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/hashes.h>
34 class Bzip2Method
: public pkgAcqMethod
36 virtual bool Fetch(FetchItem
*Itm
);
40 Bzip2Method() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
44 // Bzip2Method::Fetch - Decompress the passed URI /*{{{*/
45 // ---------------------------------------------------------------------
47 bool Bzip2Method::Fetch(FetchItem
*Itm
)
50 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
52 std::string GzPathOption
= "Dir::bin::" + std::string(Prog
);
55 Res
.Filename
= Itm
->DestFile
;
58 // Open the source and destination files
59 FileFd
From(Path
,FileFd::ReadOnly
);
61 if(From
.FileSize() == 0)
62 return _error
->Error(_("Empty files can't be valid archives"));
66 return _error
->Errno("pipe",_("Couldn't open pipe for %s"),Prog
);
69 pid_t Process
= ExecFork();
73 dup2(From
.Fd(),STDIN_FILENO
);
74 dup2(GzOut
[1],STDOUT_FILENO
);
77 SetCloseExec(STDIN_FILENO
,false);
78 SetCloseExec(STDOUT_FILENO
,false);
81 std::string Tmp
= _config
->Find(GzPathOption
,Prog
);
82 Args
[0] = Tmp
.c_str();
85 execvp(Args
[0],(char **)Args
);
91 FileFd
FromGz(GzOut
[0]); // For autoclose
92 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
94 if (_error
->PendingError() == true)
97 // Read data from bzip2, generate checksums and write
102 unsigned char Buffer
[4*1024];
104 ssize_t Count
= read(GzOut
[0],Buffer
,sizeof(Buffer
));
105 if (Count
< 0 && errno
== EINTR
)
110 _error
->Errno("read", _("Read error from %s process"),Prog
);
118 Hash
.Add(Buffer
,Count
);
119 if (To
.Write(Buffer
,Count
) == false)
127 // Wait for bzip2 to finish
128 if (ExecWait(Process
,_config
->Find(GzPathOption
,Prog
).c_str(),false) == false)
139 // Transfer the modification times
141 if (stat(Path
.c_str(),&Buf
) != 0)
142 return _error
->Errno("stat",_("Failed to stat"));
144 struct utimbuf TimeBuf
;
145 TimeBuf
.actime
= Buf
.st_atime
;
146 TimeBuf
.modtime
= Buf
.st_mtime
;
147 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
148 return _error
->Errno("utime",_("Failed to set modification time"));
150 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
151 return _error
->Errno("stat",_("Failed to stat"));
153 // Return a Done response
154 Res
.LastModified
= Buf
.st_mtime
;
155 Res
.Size
= Buf
.st_size
;
156 Res
.TakeHashes(Hash
);
164 int main(int argc
, char *argv
[])
166 setlocale(LC_ALL
, "");
170 Prog
= strrchr(argv
[0],'/');