]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
fbfd3bbac4fad299f7cef1595f20ea29dc5eb0ea
[apt.git] / methods / gzip.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
5
6 GZip method - Take a file URI in and decompress it into the target
7 file.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <config.h>
13
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/acquire-method.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/aptconfiguration.h>
21 #include "aptmethod.h"
22
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <string>
27 #include <vector>
28
29 #include <apti18n.h>
30 /*}}}*/
31
32 class GzipMethod : public aptMethod
33 {
34 std::string const Prog;
35 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
36
37 public:
38
39 GzipMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.1",SingleInstance | SendConfig), Prog(pProg) {};
40 };
41
42 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 bool GzipMethod::Fetch(FetchItem *Itm)
46 {
47 URI Get = Itm->Uri;
48 std::string Path = Get.Host + Get.Path; // To account for relative paths
49
50 FetchResult Res;
51 Res.Filename = Itm->DestFile;
52 URIStart(Res);
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.c_str());
61
62 // Open the source and destination files
63 FileFd From;
64 if (_config->FindB("Method::Compress", false) == false)
65 {
66 From.Open(Path, FileFd::ReadOnly, *compressor);
67 if(From.FileSize() == 0)
68 return _error->Error(_("Empty files can't be valid archives"));
69 }
70 else
71 From.Open(Path, FileFd::ReadOnly);
72 if (From.IsOpen() == false || From.Failed() == true)
73 return false;
74
75 FileFd To;
76 if (Itm->DestFile != "/dev/null")
77 {
78 if (_config->FindB("Method::Compress", false) == false)
79 To.Open(Itm->DestFile, FileFd::WriteAtomic);
80 else
81 To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
82
83 if (To.IsOpen() == false || To.Failed() == true)
84 return false;
85 To.EraseOnFailure();
86 }
87
88
89 // Read data from source, generate checksums and write
90 Hashes Hash(Itm->ExpectedHashes);
91 bool Failed = false;
92 Res.Size = 0;
93 while (1)
94 {
95 unsigned char Buffer[4*1024];
96 unsigned long long Count = 0;
97
98 if (!From.Read(Buffer,sizeof(Buffer),&Count))
99 {
100 if (To.IsOpen())
101 To.OpFail();
102 return false;
103 }
104 if (Count == 0)
105 break;
106 Res.Size += Count;
107
108 Hash.Add(Buffer,Count);
109 if (To.IsOpen() && To.Write(Buffer,Count) == false)
110 {
111 Failed = true;
112 break;
113 }
114 }
115
116 From.Close();
117 To.Close();
118
119 if (Failed == true)
120 return false;
121
122 // Transfer the modification times
123 if (Itm->DestFile != "/dev/null")
124 {
125 struct stat Buf;
126 if (stat(Path.c_str(),&Buf) != 0)
127 return _error->Errno("stat",_("Failed to stat"));
128
129 struct timeval times[2];
130 times[0].tv_sec = Buf.st_atime;
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"));
135 }
136
137 // Return a Done response
138 Res.TakeHashes(Hash);
139
140 URIDone(Res);
141 return true;
142 }
143 /*}}}*/
144
145 int main(int, char *argv[])
146 {
147 setlocale(LC_ALL, "");
148
149 GzipMethod Mth(flNotDir(argv[0]));
150 return Mth.Run();
151 }