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