]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
fix test to not spoil output with warnings
[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
0ec6b98b 14#include <apt-pkg/configuration.h>
93bf083d 15#include <apt-pkg/acquire-method.h>
453b82a3
DK
16#include <apt-pkg/error.h>
17#include <apt-pkg/fileutl.h>
63b1700f 18#include <apt-pkg/hashes.h>
453b82a3
DK
19#include <apt-pkg/strutl.h>
20#include <apt-pkg/aptconfiguration.h>
92173b19 21
453b82a3 22#include <string.h>
92173b19 23#include <sys/stat.h>
246bbb61 24#include <sys/time.h>
453b82a3
DK
25#include <string>
26#include <vector>
27
d77559ac 28#include <apti18n.h>
92173b19
AL
29 /*}}}*/
30
d6bbcaad
DK
31const char *Prog;
32
93bf083d 33class GzipMethod : public pkgAcqMethod
92173b19 34{
be4401bf 35 virtual bool Fetch(FetchItem *Itm);
9983999d 36 virtual bool Configuration(std::string Message);
92173b19 37
93bf083d
AL
38 public:
39
874ef47d 40 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
93bf083d 41};
92173b19 42
9983999d
MV
43bool GzipMethod::Configuration(std::string Message)
44{
45 if (pkgAcqMethod::Configuration(Message) == false)
46 return false;
47
48 DropPrivsOrDie();
49
50 return true;
51}
63b1700f 52
93bf083d
AL
53// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
54// ---------------------------------------------------------------------
2204bd80 55/* */
be4401bf 56bool GzipMethod::Fetch(FetchItem *Itm)
92173b19 57{
be4401bf 58 URI Get = Itm->Uri;
8f3ba4e8 59 std::string Path = Get.Host + Get.Path; // To account for relative paths
4509574a 60
b98f2859
AL
61 FetchResult Res;
62 Res.Filename = Itm->DestFile;
63 URIStart(Res);
d6bbcaad
DK
64
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)
69 break;
70 if (compressor == compressors.end())
71 return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
72
63b1700f 73 // Open the source and destination files
0ec6b98b
DK
74 FileFd From, To;
75 if (_config->FindB("Method::Compress", false) == false)
76 {
77 From.Open(Path, FileFd::ReadOnly, *compressor);
78 if(From.FileSize() == 0)
79 return _error->Error(_("Empty files can't be valid archives"));
80 To.Open(Itm->DestFile, FileFd::WriteAtomic);
81 }
82 else
83 {
84 From.Open(Path, FileFd::ReadOnly);
85 To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
86 }
63b1700f 87 To.EraseOnFailure();
0ec6b98b
DK
88
89 if (From.IsOpen() == false || From.Failed() == true ||
90 To.IsOpen() == false || To.Failed() == true)
63b1700f 91 return false;
0ec6b98b 92
127e6df3 93 // Read data from source, generate checksums and write
63b1700f
AL
94 Hashes Hash;
95 bool Failed = false;
96 while (1)
97 {
98 unsigned char Buffer[4*1024];
650faab0 99 unsigned long long Count = 0;
63b1700f 100
127e6df3 101 if (!From.Read(Buffer,sizeof(Buffer),&Count))
63b1700f 102 {
127e6df3 103 To.OpFail();
104 return false;
63b1700f 105 }
63b1700f
AL
106 if (Count == 0)
107 break;
127e6df3 108
63b1700f 109 Hash.Add(Buffer,Count);
678bc33e 110 if (To.Write(Buffer,Count) == false)
2204bd80 111 {
678bc33e
AL
112 Failed = true;
113 break;
2204bd80 114 }
63b1700f 115 }
93bf083d 116
127e6df3 117 From.Close();
246bbb61
DK
118 Res.Size = To.FileSize();
119 To.Close();
9ce3cfc9 120
63b1700f
AL
121 if (Failed == true)
122 return false;
9ce3cfc9 123
93bf083d
AL
124 // Transfer the modification times
125 struct stat Buf;
4509574a 126 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 127 return _error->Errno("stat",_("Failed to stat"));
92173b19 128
246bbb61 129 struct timeval times[2];
9ce3cfc9 130 times[0].tv_sec = Buf.st_atime;
246bbb61
DK
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"));
9ce3cfc9 135
93bf083d 136 // Return a Done response
a7c835af 137 Res.TakeHashes(Hash);
63b1700f 138
93bf083d 139 URIDone(Res);
93bf083d
AL
140 return true;
141}
142 /*}}}*/
143
65512241 144int main(int, char *argv[])
93bf083d 145{
b25423f6
MZ
146 setlocale(LC_ALL, "");
147
d6bbcaad
DK
148 Prog = strrchr(argv[0],'/');
149 ++Prog;
150
93bf083d 151 GzipMethod Mth;
7b18d559 152
93bf083d 153 return Mth.Run();
92173b19 154}