]> git.saurik.com Git - apt.git/blame - methods/gzip.cc
trigger the usage of the fallback code for kfreebsd also in the
[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>
21#include <unistd.h>
22#include <utime.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();
93bf083d
AL
97 To.Close();
98
63b1700f
AL
99 if (Failed == true)
100 return false;
101
93bf083d
AL
102 // Transfer the modification times
103 struct stat Buf;
4509574a 104 if (stat(Path.c_str(),&Buf) != 0)
dc738e7a 105 return _error->Errno("stat",_("Failed to stat"));
92173b19 106
93bf083d
AL
107 struct utimbuf TimeBuf;
108 TimeBuf.actime = Buf.st_atime;
109 TimeBuf.modtime = Buf.st_mtime;
be4401bf 110 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
dc738e7a 111 return _error->Errno("utime",_("Failed to set modification time"));
92173b19 112
18ef0a78 113 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
dc738e7a 114 return _error->Errno("stat",_("Failed to stat"));
18ef0a78 115
93bf083d 116 // Return a Done response
93bf083d 117 Res.LastModified = Buf.st_mtime;
18ef0a78 118 Res.Size = Buf.st_size;
a7c835af 119 Res.TakeHashes(Hash);
63b1700f 120
93bf083d 121 URIDone(Res);
92173b19 122
93bf083d
AL
123 return true;
124}
125 /*}}}*/
126
2204bd80 127int main(int argc, char *argv[])
93bf083d 128{
b25423f6
MZ
129 setlocale(LC_ALL, "");
130
d6bbcaad
DK
131 Prog = strrchr(argv[0],'/');
132 ++Prog;
133
93bf083d
AL
134 GzipMethod Mth;
135 return Mth.Run();
92173b19 136}