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/configuration.h>
15 #include <apt-pkg/acquire-method.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/aptconfiguration.h>
21 #include "aptmethod.h"
32 class GzipMethod
: public aptMethod
34 std::string
const Prog
;
35 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
39 GzipMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.1",SingleInstance
| SendConfig
), Prog(pProg
) {};
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
.c_str());
62 // Open the source and destination files
64 if (_config
->FindB("Method::Compress", false) == false)
66 From
.Open(Path
, FileFd::ReadOnly
, *compressor
);
67 if(From
.FileSize() == 0)
68 return _error
->Error(_("Empty files can't be valid archives"));
71 From
.Open(Path
, FileFd::ReadOnly
);
72 if (From
.IsOpen() == false || From
.Failed() == true)
76 if (Itm
->DestFile
!= "/dev/null")
78 if (_config
->FindB("Method::Compress", false) == false)
79 To
.Open(Itm
->DestFile
, FileFd::WriteAtomic
);
81 To
.Open(Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
, *compressor
);
83 if (To
.IsOpen() == false || To
.Failed() == true)
89 // Read data from source, generate checksums and write
90 Hashes
Hash(Itm
->ExpectedHashes
);
95 unsigned char Buffer
[4*1024];
96 unsigned long long Count
= 0;
98 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
108 Hash
.Add(Buffer
,Count
);
109 if (To
.IsOpen() && To
.Write(Buffer
,Count
) == false)
122 // Transfer the modification times
123 if (Itm
->DestFile
!= "/dev/null")
126 if (stat(Path
.c_str(),&Buf
) != 0)
127 return _error
->Errno("stat",_("Failed to stat"));
129 struct timeval times
[2];
130 times
[0].tv_sec
= Buf
.st_atime
;
131 Res
.LastModified
= times
[1].tv_sec
= Buf
.st_mtime
;
132 times
[0].tv_usec
= times
[1].tv_usec
= 0;
133 if (utimes(Itm
->DestFile
.c_str(), times
) != 0)
134 return _error
->Errno("utimes",_("Failed to set modification time"));
137 // Return a Done response
138 Res
.TakeHashes(Hash
);
145 int main(int, char *argv
[])
147 setlocale(LC_ALL
, "");
149 GzipMethod
Mth(flNotDir(argv
[0]));