]>
Commit | Line | Data |
---|---|---|
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> | |
23e64f6d | 21 | #include "aptmethod.h" |
92173b19 | 22 | |
453b82a3 | 23 | #include <string.h> |
92173b19 | 24 | #include <sys/stat.h> |
246bbb61 | 25 | #include <sys/time.h> |
453b82a3 DK |
26 | #include <string> |
27 | #include <vector> | |
28 | ||
d77559ac | 29 | #include <apti18n.h> |
92173b19 AL |
30 | /*}}}*/ |
31 | ||
23e64f6d | 32 | class GzipMethod : public aptMethod |
92173b19 | 33 | { |
23e64f6d | 34 | std::string const Prog; |
3b302846 | 35 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; |
9983999d | 36 | |
23e64f6d | 37 | public: |
9983999d | 38 | |
258b9e51 | 39 | explicit GzipMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.1",SingleInstance | SendConfig), Prog(pProg) {}; |
23e64f6d | 40 | }; |
63b1700f | 41 | |
93bf083d AL |
42 | // GzipMethod::Fetch - Decompress the passed URI /*{{{*/ |
43 | // --------------------------------------------------------------------- | |
2204bd80 | 44 | /* */ |
be4401bf | 45 | bool 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()) | |
23e64f6d | 60 | return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog.c_str()); |
d6bbcaad | 61 | |
63b1700f | 62 | // Open the source and destination files |
af9e40c9 | 63 | FileFd From; |
0ec6b98b DK |
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")); | |
0ec6b98b DK |
69 | } |
70 | else | |
0ec6b98b | 71 | From.Open(Path, FileFd::ReadOnly); |
af9e40c9 DK |
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(); | |
0ec6b98b | 86 | } |
0ec6b98b | 87 | |
0ec6b98b | 88 | |
127e6df3 | 89 | // Read data from source, generate checksums and write |
9224ce3d | 90 | Hashes Hash(Itm->ExpectedHashes); |
63b1700f | 91 | bool Failed = false; |
af9e40c9 | 92 | Res.Size = 0; |
63b1700f AL |
93 | while (1) |
94 | { | |
95 | unsigned char Buffer[4*1024]; | |
650faab0 | 96 | unsigned long long Count = 0; |
63b1700f | 97 | |
127e6df3 | 98 | if (!From.Read(Buffer,sizeof(Buffer),&Count)) |
63b1700f | 99 | { |
af9e40c9 DK |
100 | if (To.IsOpen()) |
101 | To.OpFail(); | |
127e6df3 | 102 | return false; |
63b1700f | 103 | } |
63b1700f AL |
104 | if (Count == 0) |
105 | break; | |
af9e40c9 | 106 | Res.Size += Count; |
127e6df3 | 107 | |
63b1700f | 108 | Hash.Add(Buffer,Count); |
af9e40c9 | 109 | if (To.IsOpen() && To.Write(Buffer,Count) == false) |
2204bd80 | 110 | { |
678bc33e AL |
111 | Failed = true; |
112 | break; | |
2204bd80 | 113 | } |
63b1700f | 114 | } |
93bf083d | 115 | |
127e6df3 | 116 | From.Close(); |
246bbb61 | 117 | To.Close(); |
9ce3cfc9 | 118 | |
63b1700f AL |
119 | if (Failed == true) |
120 | return false; | |
9ce3cfc9 | 121 | |
93bf083d | 122 | // Transfer the modification times |
af9e40c9 DK |
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 | } | |
9ce3cfc9 | 136 | |
93bf083d | 137 | // Return a Done response |
a7c835af | 138 | Res.TakeHashes(Hash); |
63b1700f | 139 | |
93bf083d | 140 | URIDone(Res); |
93bf083d AL |
141 | return true; |
142 | } | |
143 | /*}}}*/ | |
144 | ||
65512241 | 145 | int main(int, char *argv[]) |
93bf083d | 146 | { |
b25423f6 MZ |
147 | setlocale(LC_ALL, ""); |
148 | ||
23e64f6d | 149 | GzipMethod Mth(flNotDir(argv[0])); |
93bf083d | 150 | return Mth.Run(); |
92173b19 | 151 | } |