]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
prepare re-enable fnmatch() once we have proper reporting
[apt.git] / methods / gzip.cc
CommitLineData
92173b19
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
92173b19
AL
4/* ######################################################################
5
6 GZip method - Take a file URI in and decompress it into the target
7 file.
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
ea542140
DK
12#include <config.h>
13
92173b19
AL
14#include <apt-pkg/fileutl.h>
15#include <apt-pkg/error.h>
93bf083d 16#include <apt-pkg/acquire-method.h>
cdcc6d34 17#include <apt-pkg/strutl.h>
63b1700f 18#include <apt-pkg/hashes.h>
92173b19
AL
19
20#include <sys/stat.h>
246bbb61 21#include <sys/time.h>
92173b19 22#include <unistd.h>
92173b19 23#include <stdio.h>
63b1700f 24#include <errno.h>
d77559ac 25#include <apti18n.h>
92173b19
AL
26 /*}}}*/
27
d6bbcaad
DK
28const char *Prog;
29
93bf083d 30class GzipMethod : public pkgAcqMethod
92173b19 31{
be4401bf 32 virtual bool Fetch(FetchItem *Itm);
92173b19 33
93bf083d
AL
34 public:
35
874ef47d 36 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
93bf083d 37};
92173b19 38
63b1700f 39
93bf083d
AL
40// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
41// ---------------------------------------------------------------------
2204bd80 42/* */
be4401bf 43bool GzipMethod::Fetch(FetchItem *Itm)
92173b19 44{
be4401bf 45 URI Get = Itm->Uri;
8f3ba4e8 46 std::string Path = Get.Host + Get.Path; // To account for relative paths
4509574a 47
b98f2859
AL
48 FetchResult Res;
49 Res.Filename = Itm->DestFile;
50 URIStart(Res);
d6bbcaad
DK
51
52 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
53 std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
54 for (; compressor != compressors.end(); ++compressor)
55 if (compressor->Name == Prog)
56 break;
57 if (compressor == compressors.end())
58 return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
59
63b1700f 60 // Open the source and destination files
d6bbcaad
DK
61 FileFd From;
62 From.Open(Path, FileFd::ReadOnly, *compressor);
63b1700f 63
4260fd39 64 if(From.FileSize() == 0)
5d885723 65 return _error->Error(_("Empty files can't be valid archives"));
84cc6f73 66
22041bd2 67 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
63b1700f
AL
68 To.EraseOnFailure();
69 if (_error->PendingError() == true)
70 return false;
71
127e6df3 72 // Read data from source, generate checksums and write
63b1700f
AL
73 Hashes Hash;
74 bool Failed = false;
75 while (1)
76 {
77 unsigned char Buffer[4*1024];
650faab0 78 unsigned long long Count = 0;
63b1700f 79
127e6df3 80 if (!From.Read(Buffer,sizeof(Buffer),&Count))
63b1700f 81 {
127e6df3 82 To.OpFail();
83 return false;
63b1700f 84 }
63b1700f
AL
85 if (Count == 0)
86 break;
127e6df3 87
63b1700f 88 Hash.Add(Buffer,Count);
678bc33e 89 if (To.Write(Buffer,Count) == false)
2204bd80 90 {
678bc33e
AL
91 Failed = true;
92 break;
2204bd80 93 }
63b1700f 94 }
93bf083d 95
127e6df3 96 From.Close();
246bbb61
DK
97 Res.Size = To.FileSize();
98 To.Close();
9ce3cfc9 99
63b1700f
AL
100 if (Failed == true)
101 return false;
9ce3cfc9 102
93bf083d
AL
103 // Transfer the modification times
104 struct stat Buf;
4509574a 105 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 106 return _error->Errno("stat",_("Failed to stat"));
92173b19 107
246bbb61 108 struct timeval times[2];
9ce3cfc9 109 times[0].tv_sec = Buf.st_atime;
246bbb61
DK
110 Res.LastModified = times[1].tv_sec = Buf.st_mtime;
111 times[0].tv_usec = times[1].tv_usec = 0;
112 if (utimes(Itm->DestFile.c_str(), times) != 0)
113 return _error->Errno("utimes",_("Failed to set modification time"));
9ce3cfc9 114
93bf083d 115 // Return a Done response
a7c835af 116 Res.TakeHashes(Hash);
63b1700f 117
93bf083d 118 URIDone(Res);
93bf083d
AL
119 return true;
120}
121 /*}}}*/
122
2204bd80 123int main(int argc, char *argv[])
93bf083d 124{
b25423f6
MZ
125 setlocale(LC_ALL, "");
126
d6bbcaad
DK
127 Prog = strrchr(argv[0],'/');
128 ++Prog;
129
93bf083d
AL
130 GzipMethod Mth;
131 return Mth.Run();
92173b19 132}