cleanup headers and especially #includes everywhere
[ntk/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
93bf083d 14#include <apt-pkg/acquire-method.h>
453b82a3
DK
15#include <apt-pkg/error.h>
16#include <apt-pkg/fileutl.h>
63b1700f 17#include <apt-pkg/hashes.h>
453b82a3
DK
18#include <apt-pkg/strutl.h>
19#include <apt-pkg/aptconfiguration.h>
92173b19 20
453b82a3 21#include <string.h>
92173b19 22#include <sys/stat.h>
246bbb61 23#include <sys/time.h>
453b82a3
DK
24#include <string>
25#include <vector>
26
d77559ac 27#include <apti18n.h>
92173b19
AL
28 /*}}}*/
29
d6bbcaad
DK
30const char *Prog;
31
93bf083d 32class GzipMethod : public pkgAcqMethod
92173b19 33{
be4401bf 34 virtual bool Fetch(FetchItem *Itm);
92173b19 35
93bf083d
AL
36 public:
37
874ef47d 38 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
93bf083d 39};
92173b19 40
63b1700f 41
93bf083d
AL
42// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
43// ---------------------------------------------------------------------
2204bd80 44/* */
be4401bf 45bool GzipMethod::Fetch(FetchItem *Itm)
92173b19 46{
be4401bf 47 URI Get = Itm->Uri;
8f3ba4e8 48 std::string Path = Get.Host + Get.Path; // To account for relative paths
4509574a 49
b98f2859
AL
50 FetchResult Res;
51 Res.Filename = Itm->DestFile;
52 URIStart(Res);
d6bbcaad
DK
53
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)
58 break;
59 if (compressor == compressors.end())
60 return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
61
63b1700f 62 // Open the source and destination files
d6bbcaad
DK
63 FileFd From;
64 From.Open(Path, FileFd::ReadOnly, *compressor);
63b1700f 65
4260fd39 66 if(From.FileSize() == 0)
5d885723 67 return _error->Error(_("Empty files can't be valid archives"));
84cc6f73 68
22041bd2 69 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
63b1700f
AL
70 To.EraseOnFailure();
71 if (_error->PendingError() == true)
72 return false;
73
127e6df3 74 // Read data from source, generate checksums and write
63b1700f
AL
75 Hashes Hash;
76 bool Failed = false;
77 while (1)
78 {
79 unsigned char Buffer[4*1024];
650faab0 80 unsigned long long Count = 0;
63b1700f 81
127e6df3 82 if (!From.Read(Buffer,sizeof(Buffer),&Count))
63b1700f 83 {
127e6df3 84 To.OpFail();
85 return false;
63b1700f 86 }
63b1700f
AL
87 if (Count == 0)
88 break;
127e6df3 89
63b1700f 90 Hash.Add(Buffer,Count);
678bc33e 91 if (To.Write(Buffer,Count) == false)
2204bd80 92 {
678bc33e
AL
93 Failed = true;
94 break;
2204bd80 95 }
63b1700f 96 }
93bf083d 97
127e6df3 98 From.Close();
246bbb61
DK
99 Res.Size = To.FileSize();
100 To.Close();
9ce3cfc9 101
63b1700f
AL
102 if (Failed == true)
103 return false;
9ce3cfc9 104
93bf083d
AL
105 // Transfer the modification times
106 struct stat Buf;
4509574a 107 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 108 return _error->Errno("stat",_("Failed to stat"));
92173b19 109
246bbb61 110 struct timeval times[2];
9ce3cfc9 111 times[0].tv_sec = Buf.st_atime;
246bbb61
DK
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"));
9ce3cfc9 116
93bf083d 117 // Return a Done response
a7c835af 118 Res.TakeHashes(Hash);
63b1700f 119
93bf083d 120 URIDone(Res);
93bf083d
AL
121 return true;
122}
123 /*}}}*/
124
65512241 125int main(int, char *argv[])
93bf083d 126{
b25423f6
MZ
127 setlocale(LC_ALL, "");
128
d6bbcaad
DK
129 Prog = strrchr(argv[0],'/');
130 ++Prog;
131
93bf083d
AL
132 GzipMethod Mth;
133 return Mth.Run();
92173b19 134}