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 /*{{{*/
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/acquire-method.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/hashes.h>
29 class GzipMethod
: public pkgAcqMethod
31 virtual bool Fetch(FetchItem
*Itm
);
35 GzipMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
39 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
40 // ---------------------------------------------------------------------
42 bool GzipMethod::Fetch(FetchItem
*Itm
)
45 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
48 Res
.Filename
= Itm
->DestFile
;
51 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
52 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
53 for (; compressor
!= compressors
.end(); ++compressor
)
54 if (compressor
->Name
== Prog
)
56 if (compressor
== compressors
.end())
57 return _error
->Error("Extraction of file %s requires unknown compressor %s", Path
.c_str(), Prog
);
59 // Open the source and destination files
61 From
.Open(Path
, FileFd::ReadOnly
, *compressor
);
63 if(From
.FileSize() == 0)
64 return _error
->Error(_("Empty files can't be valid archives"));
66 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
68 if (_error
->PendingError() == true)
71 // Read data from source, generate checksums and write
76 unsigned char Buffer
[4*1024];
77 unsigned long long Count
= 0;
79 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
87 Hash
.Add(Buffer
,Count
);
88 if (To
.Write(Buffer
,Count
) == false)
100 // Transfer the modification times
102 if (stat(Path
.c_str(),&Buf
) != 0)
103 return _error
->Errno("stat",_("Failed to stat"));
105 struct timespec times
[2];
106 times
[0].tv_sec
= Buf
.st_atime
;
107 times
[1].tv_sec
= Buf
.st_mtime
;
108 times
[0].tv_nsec
= times
[1].tv_nsec
= 0;
109 if (futimens(To
.Fd(), times
) != 0)
112 return _error
->Errno("futimens",_("Failed to set modification time"));
114 Res
.Size
= To
.FileSize();
117 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
118 return _error
->Errno("stat",_("Failed to stat"));
120 // Return a Done response
121 Res
.LastModified
= Buf
.st_mtime
;
122 Res
.TakeHashes(Hash
);
129 int main(int argc
, char *argv
[])
131 setlocale(LC_ALL
, "");
133 Prog
= strrchr(argv
[0],'/');