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/acquire-method.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/hashes.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/aptconfiguration.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 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
51 Res
.Filename
= Itm
->DestFile
;
54 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
55 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
56 for (; compressor
!= compressors
.end(); ++compressor
)
57 if (compressor
->Name
== Prog
)
59 if (compressor
== compressors
.end())
60 return _error
->Error("Extraction of file %s requires unknown compressor %s", Path
.c_str(), Prog
);
62 // Open the source and destination files
64 From
.Open(Path
, FileFd::ReadOnly
, *compressor
);
66 if(From
.FileSize() == 0)
67 return _error
->Error(_("Empty files can't be valid archives"));
69 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
71 if (_error
->PendingError() == true)
74 // Read data from source, generate checksums and write
79 unsigned char Buffer
[4*1024];
80 unsigned long long Count
= 0;
82 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
90 Hash
.Add(Buffer
,Count
);
91 if (To
.Write(Buffer
,Count
) == false)
99 Res
.Size
= To
.FileSize();
105 // Transfer the modification times
107 if (stat(Path
.c_str(),&Buf
) != 0)
108 return _error
->Errno("stat",_("Failed to stat"));
110 struct timeval times
[2];
111 times
[0].tv_sec
= Buf
.st_atime
;
112 Res
.LastModified
= times
[1].tv_sec
= Buf
.st_mtime
;
113 times
[0].tv_usec
= times
[1].tv_usec
= 0;
114 if (utimes(Itm
->DestFile
.c_str(), times
) != 0)
115 return _error
->Errno("utimes",_("Failed to set modification time"));
117 // Return a Done response
118 Res
.TakeHashes(Hash
);
125 int main(int, char *argv
[])
127 setlocale(LC_ALL
, "");
129 Prog
= strrchr(argv
[0],'/');