]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
Drop Privileges to "Debian-apt" in most acquire methods
[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);
92173b19 36
93bf083d
AL
37 public:
38
874ef47d 39 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
93bf083d 40};
92173b19 41
63b1700f 42
93bf083d
AL
43// GzipMethod::Fetch - Decompress the passed URI /*{{{*/
44// ---------------------------------------------------------------------
2204bd80 45/* */
be4401bf 46bool GzipMethod::Fetch(FetchItem *Itm)
92173b19 47{
be4401bf 48 URI Get = Itm->Uri;
8f3ba4e8 49 std::string Path = Get.Host + Get.Path; // To account for relative paths
4509574a 50
b98f2859
AL
51 FetchResult Res;
52 Res.Filename = Itm->DestFile;
53 URIStart(Res);
d6bbcaad
DK
54
55 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
56 std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
57 for (; compressor != compressors.end(); ++compressor)
58 if (compressor->Name == Prog)
59 break;
60 if (compressor == compressors.end())
61 return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
62
63b1700f 63 // Open the source and destination files
0ec6b98b
DK
64 FileFd From, To;
65 if (_config->FindB("Method::Compress", false) == false)
66 {
67 From.Open(Path, FileFd::ReadOnly, *compressor);
68 if(From.FileSize() == 0)
69 return _error->Error(_("Empty files can't be valid archives"));
70 To.Open(Itm->DestFile, FileFd::WriteAtomic);
71 }
72 else
73 {
74 From.Open(Path, FileFd::ReadOnly);
75 To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
76 }
63b1700f 77 To.EraseOnFailure();
0ec6b98b
DK
78
79 if (From.IsOpen() == false || From.Failed() == true ||
80 To.IsOpen() == false || To.Failed() == true)
63b1700f 81 return false;
0ec6b98b 82
127e6df3 83 // Read data from source, generate checksums and write
63b1700f
AL
84 Hashes Hash;
85 bool Failed = false;
86 while (1)
87 {
88 unsigned char Buffer[4*1024];
650faab0 89 unsigned long long Count = 0;
63b1700f 90
127e6df3 91 if (!From.Read(Buffer,sizeof(Buffer),&Count))
63b1700f 92 {
127e6df3 93 To.OpFail();
94 return false;
63b1700f 95 }
63b1700f
AL
96 if (Count == 0)
97 break;
127e6df3 98
63b1700f 99 Hash.Add(Buffer,Count);
678bc33e 100 if (To.Write(Buffer,Count) == false)
2204bd80 101 {
678bc33e
AL
102 Failed = true;
103 break;
2204bd80 104 }
63b1700f 105 }
93bf083d 106
127e6df3 107 From.Close();
246bbb61
DK
108 Res.Size = To.FileSize();
109 To.Close();
9ce3cfc9 110
63b1700f
AL
111 if (Failed == true)
112 return false;
9ce3cfc9 113
93bf083d
AL
114 // Transfer the modification times
115 struct stat Buf;
4509574a 116 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 117 return _error->Errno("stat",_("Failed to stat"));
92173b19 118
246bbb61 119 struct timeval times[2];
9ce3cfc9 120 times[0].tv_sec = Buf.st_atime;
246bbb61
DK
121 Res.LastModified = times[1].tv_sec = Buf.st_mtime;
122 times[0].tv_usec = times[1].tv_usec = 0;
123 if (utimes(Itm->DestFile.c_str(), times) != 0)
124 return _error->Errno("utimes",_("Failed to set modification time"));
9ce3cfc9 125
93bf083d 126 // Return a Done response
a7c835af 127 Res.TakeHashes(Hash);
63b1700f 128
93bf083d 129 URIDone(Res);
93bf083d
AL
130 return true;
131}
132 /*}}}*/
133
65512241 134int main(int, char *argv[])
93bf083d 135{
b25423f6
MZ
136 setlocale(LC_ALL, "");
137
3927c6da
MV
138 DropPrivs();
139
d6bbcaad
DK
140 Prog = strrchr(argv[0],'/');
141 ++Prog;
142
93bf083d
AL
143 GzipMethod Mth;
144 return Mth.Run();
92173b19 145}