]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
fix apt-get download truncation (closes: #736962)
[apt.git] / methods / gzip.cc
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/fileutl.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/acquire-method.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/hashes.h>
19
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <apti18n.h>
25 /*}}}*/
26
27 const char *Prog;
28
29 class GzipMethod : public pkgAcqMethod
30 {
31 virtual bool Fetch(FetchItem *Itm);
32
33 public:
34
35 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
36 };
37
38
39 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 bool GzipMethod::Fetch(FetchItem *Itm)
43 {
44 URI Get = Itm->Uri;
45 std::string Path = Get.Host + Get.Path; // To account for relative paths
46
47 FetchResult Res;
48 Res.Filename = Itm->DestFile;
49 URIStart(Res);
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
59 // Open the source and destination files
60 FileFd From;
61 From.Open(Path, FileFd::ReadOnly, *compressor);
62
63 if(From.FileSize() == 0)
64 return _error->Error(_("Empty files can't be valid archives"));
65
66 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
67 To.EraseOnFailure();
68 if (_error->PendingError() == true)
69 return false;
70
71 // Read data from source, generate checksums and write
72 Hashes Hash;
73 bool Failed = false;
74 while (1)
75 {
76 unsigned char Buffer[4*1024];
77 unsigned long long Count = 0;
78
79 if (!From.Read(Buffer,sizeof(Buffer),&Count))
80 {
81 To.OpFail();
82 return false;
83 }
84 if (Count == 0)
85 break;
86
87 Hash.Add(Buffer,Count);
88 if (To.Write(Buffer,Count) == false)
89 {
90 Failed = true;
91 break;
92 }
93 }
94
95 From.Close();
96
97 if (Failed == true)
98 return false;
99
100 // Transfer the modification times
101 struct stat Buf;
102 if (stat(Path.c_str(),&Buf) != 0)
103 return _error->Errno("stat",_("Failed to stat"));
104
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();
116
117 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
118 return _error->Errno("stat",_("Failed to stat"));
119
120 // Return a Done response
121 Res.LastModified = Buf.st_mtime;
122 Res.TakeHashes(Hash);
123
124 URIDone(Res);
125 return true;
126 }
127 /*}}}*/
128
129 int main(int argc, char *argv[])
130 {
131 setlocale(LC_ALL, "");
132
133 Prog = strrchr(argv[0],'/');
134 ++Prog;
135
136 GzipMethod Mth;
137 return Mth.Run();
138 }