]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/time.h> | |
25 | #include <string> | |
26 | #include <vector> | |
27 | ||
28 | #include <apti18n.h> | |
29 | /*}}}*/ | |
30 | ||
31 | const char *Prog; | |
32 | ||
33 | class GzipMethod : public pkgAcqMethod | |
34 | { | |
35 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; | |
36 | virtual bool Configuration(std::string Message) APT_OVERRIDE; | |
37 | ||
38 | public: | |
39 | ||
40 | GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {}; | |
41 | }; | |
42 | ||
43 | bool GzipMethod::Configuration(std::string Message) | |
44 | { | |
45 | if (pkgAcqMethod::Configuration(Message) == false) | |
46 | return false; | |
47 | ||
48 | DropPrivsOrDie(); | |
49 | ||
50 | return true; | |
51 | } | |
52 | ||
53 | // GzipMethod::Fetch - Decompress the passed URI /*{{{*/ | |
54 | // --------------------------------------------------------------------- | |
55 | /* */ | |
56 | bool GzipMethod::Fetch(FetchItem *Itm) | |
57 | { | |
58 | URI Get = Itm->Uri; | |
59 | std::string Path = Get.Host + Get.Path; // To account for relative paths | |
60 | ||
61 | FetchResult Res; | |
62 | Res.Filename = Itm->DestFile; | |
63 | URIStart(Res); | |
64 | ||
65 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
66 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
67 | for (; compressor != compressors.end(); ++compressor) | |
68 | if (compressor->Name == Prog) | |
69 | break; | |
70 | if (compressor == compressors.end()) | |
71 | return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog); | |
72 | ||
73 | // Open the source and destination files | |
74 | FileFd From, To; | |
75 | if (_config->FindB("Method::Compress", false) == false) | |
76 | { | |
77 | From.Open(Path, FileFd::ReadOnly, *compressor); | |
78 | if(From.FileSize() == 0) | |
79 | return _error->Error(_("Empty files can't be valid archives")); | |
80 | To.Open(Itm->DestFile, FileFd::WriteAtomic); | |
81 | } | |
82 | else | |
83 | { | |
84 | From.Open(Path, FileFd::ReadOnly); | |
85 | To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor); | |
86 | } | |
87 | To.EraseOnFailure(); | |
88 | ||
89 | if (From.IsOpen() == false || From.Failed() == true || | |
90 | To.IsOpen() == false || To.Failed() == true) | |
91 | return false; | |
92 | ||
93 | // Read data from source, generate checksums and write | |
94 | Hashes Hash(Itm->ExpectedHashes); | |
95 | bool Failed = false; | |
96 | while (1) | |
97 | { | |
98 | unsigned char Buffer[4*1024]; | |
99 | unsigned long long Count = 0; | |
100 | ||
101 | if (!From.Read(Buffer,sizeof(Buffer),&Count)) | |
102 | { | |
103 | To.OpFail(); | |
104 | return false; | |
105 | } | |
106 | if (Count == 0) | |
107 | break; | |
108 | ||
109 | Hash.Add(Buffer,Count); | |
110 | if (To.Write(Buffer,Count) == false) | |
111 | { | |
112 | Failed = true; | |
113 | break; | |
114 | } | |
115 | } | |
116 | ||
117 | From.Close(); | |
118 | Res.Size = To.FileSize(); | |
119 | To.Close(); | |
120 | ||
121 | if (Failed == true) | |
122 | return false; | |
123 | ||
124 | // Transfer the modification times | |
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 | // Return a Done response | |
137 | Res.TakeHashes(Hash); | |
138 | ||
139 | URIDone(Res); | |
140 | return true; | |
141 | } | |
142 | /*}}}*/ | |
143 | ||
144 | int main(int, char *argv[]) | |
145 | { | |
146 | setlocale(LC_ALL, ""); | |
147 | ||
148 | Prog = strrchr(argv[0],'/'); | |
149 | ++Prog; | |
150 | ||
151 | GzipMethod Mth; | |
152 | ||
153 | return Mth.Run(); | |
154 | } |