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>
33 class GzipMethod
: public pkgAcqMethod
35 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
36 virtual bool Configuration(std::string Message
) APT_OVERRIDE
;
40 GzipMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
43 bool GzipMethod::Configuration(std::string Message
)
45 if (pkgAcqMethod::Configuration(Message
) == false)
53 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
54 // ---------------------------------------------------------------------
56 bool GzipMethod::Fetch(FetchItem
*Itm
)
59 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
62 Res
.Filename
= Itm
->DestFile
;
65 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
66 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
67 for (; compressor
!= compressors
.end(); ++compressor
)
68 if (compressor
->Name
== Prog
)
70 if (compressor
== compressors
.end())
71 return _error
->Error("Extraction of file %s requires unknown compressor %s", Path
.c_str(), Prog
);
73 // Open the source and destination files
75 if (_config
->FindB("Method::Compress", false) == false)
77 From
.Open(Path
, FileFd::ReadOnly
, *compressor
);
78 if(From
.FileSize() == 0)
79 return _error
->Error(_("Empty files can't be valid archives"));
82 From
.Open(Path
, FileFd::ReadOnly
);
83 if (From
.IsOpen() == false || From
.Failed() == true)
87 if (Itm
->DestFile
!= "/dev/null")
89 if (_config
->FindB("Method::Compress", false) == false)
90 To
.Open(Itm
->DestFile
, FileFd::WriteAtomic
);
92 To
.Open(Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
, *compressor
);
94 if (To
.IsOpen() == false || To
.Failed() == true)
100 // Read data from source, generate checksums and write
101 Hashes
Hash(Itm
->ExpectedHashes
);
106 unsigned char Buffer
[4*1024];
107 unsigned long long Count
= 0;
109 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
119 Hash
.Add(Buffer
,Count
);
120 if (To
.IsOpen() && To
.Write(Buffer
,Count
) == false)
133 // Transfer the modification times
134 if (Itm
->DestFile
!= "/dev/null")
137 if (stat(Path
.c_str(),&Buf
) != 0)
138 return _error
->Errno("stat",_("Failed to stat"));
140 struct timeval times
[2];
141 times
[0].tv_sec
= Buf
.st_atime
;
142 Res
.LastModified
= times
[1].tv_sec
= Buf
.st_mtime
;
143 times
[0].tv_usec
= times
[1].tv_usec
= 0;
144 if (utimes(Itm
->DestFile
.c_str(), times
) != 0)
145 return _error
->Errno("utimes",_("Failed to set modification time"));
148 // Return a Done response
149 Res
.TakeHashes(Hash
);
156 int main(int, char *argv
[])
158 setlocale(LC_ALL
, "");
160 Prog
= strrchr(argv
[0],'/');