]>
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 | ||
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> | |
92173b19 | 22 | #include <stdio.h> |
63b1700f | 23 | #include <errno.h> |
d77559ac | 24 | #include <apti18n.h> |
92173b19 AL |
25 | /*}}}*/ |
26 | ||
d6bbcaad DK |
27 | const char *Prog; |
28 | ||
93bf083d | 29 | class GzipMethod : public pkgAcqMethod |
92173b19 | 30 | { |
be4401bf | 31 | virtual bool Fetch(FetchItem *Itm); |
92173b19 | 32 | |
93bf083d AL |
33 | public: |
34 | ||
874ef47d | 35 | GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {}; |
93bf083d | 36 | }; |
92173b19 | 37 | |
63b1700f | 38 | |
93bf083d AL |
39 | // GzipMethod::Fetch - Decompress the passed URI /*{{{*/ |
40 | // --------------------------------------------------------------------- | |
2204bd80 | 41 | /* */ |
be4401bf | 42 | bool GzipMethod::Fetch(FetchItem *Itm) |
92173b19 | 43 | { |
be4401bf | 44 | URI Get = Itm->Uri; |
8f3ba4e8 | 45 | std::string Path = Get.Host + Get.Path; // To account for relative paths |
4509574a | 46 | |
b98f2859 AL |
47 | FetchResult Res; |
48 | Res.Filename = Itm->DestFile; | |
49 | URIStart(Res); | |
d6bbcaad DK |
50 | |
51 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
52 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
53 | for (; compressor != compressors.end(); ++compressor) | |
54 | if (compressor->Name == Prog) | |
55 | break; | |
56 | if (compressor == compressors.end()) | |
57 | return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog); | |
58 | ||
63b1700f | 59 | // Open the source and destination files |
d6bbcaad DK |
60 | FileFd From; |
61 | From.Open(Path, FileFd::ReadOnly, *compressor); | |
63b1700f | 62 | |
4260fd39 | 63 | if(From.FileSize() == 0) |
5d885723 | 64 | return _error->Error(_("Empty files can't be valid archives")); |
84cc6f73 | 65 | |
22041bd2 | 66 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); |
63b1700f AL |
67 | To.EraseOnFailure(); |
68 | if (_error->PendingError() == true) | |
69 | return false; | |
70 | ||
127e6df3 | 71 | // Read data from source, generate checksums and write |
63b1700f AL |
72 | Hashes Hash; |
73 | bool Failed = false; | |
74 | while (1) | |
75 | { | |
76 | unsigned char Buffer[4*1024]; | |
650faab0 | 77 | unsigned long long Count = 0; |
63b1700f | 78 | |
127e6df3 | 79 | if (!From.Read(Buffer,sizeof(Buffer),&Count)) |
63b1700f | 80 | { |
127e6df3 | 81 | To.OpFail(); |
82 | return false; | |
63b1700f | 83 | } |
63b1700f AL |
84 | if (Count == 0) |
85 | break; | |
127e6df3 | 86 | |
63b1700f | 87 | Hash.Add(Buffer,Count); |
678bc33e | 88 | if (To.Write(Buffer,Count) == false) |
2204bd80 | 89 | { |
678bc33e AL |
90 | Failed = true; |
91 | break; | |
2204bd80 | 92 | } |
63b1700f | 93 | } |
93bf083d | 94 | |
127e6df3 | 95 | From.Close(); |
9ce3cfc9 | 96 | |
63b1700f AL |
97 | if (Failed == true) |
98 | return false; | |
9ce3cfc9 | 99 | |
93bf083d AL |
100 | // Transfer the modification times |
101 | struct stat Buf; | |
4509574a | 102 | if (stat(Path.c_str(),&Buf) != 0) |
dc738e7a | 103 | return _error->Errno("stat",_("Failed to stat")); |
92173b19 | 104 | |
9ce3cfc9 DK |
105 | struct timespec times[2]; |
106 | times[0].tv_sec = Buf.st_atime; | |
107 | times[1].tv_sec = Buf.st_mtime; | |
108 | times[0].tv_nsec = times[1].tv_nsec = 0; | |
109 | if (futimens(To.Fd(), times) != 0) | |
110 | { | |
111 | To.OpFail(); | |
112 | return _error->Errno("futimens",_("Failed to set modification time")); | |
113 | } | |
114 | Res.Size = To.FileSize(); | |
115 | To.Close(); | |
92173b19 | 116 | |
18ef0a78 | 117 | if (stat(Itm->DestFile.c_str(),&Buf) != 0) |
dc738e7a | 118 | return _error->Errno("stat",_("Failed to stat")); |
9ce3cfc9 | 119 | |
93bf083d | 120 | // Return a Done response |
93bf083d | 121 | Res.LastModified = Buf.st_mtime; |
a7c835af | 122 | Res.TakeHashes(Hash); |
63b1700f | 123 | |
93bf083d | 124 | URIDone(Res); |
93bf083d AL |
125 | return true; |
126 | } | |
127 | /*}}}*/ | |
128 | ||
2204bd80 | 129 | int main(int argc, char *argv[]) |
93bf083d | 130 | { |
b25423f6 MZ |
131 | setlocale(LC_ALL, ""); |
132 | ||
d6bbcaad DK |
133 | Prog = strrchr(argv[0],'/'); |
134 | ++Prog; | |
135 | ||
93bf083d AL |
136 | GzipMethod Mth; |
137 | return Mth.Run(); | |
92173b19 | 138 | } |