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