]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
merge patch from Daniel Hartwig to Show a error message if {,dist-}upgrade is used...
[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 <utime.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <apti18n.h>
26 /*}}}*/
27
28 const char *Prog;
29
30 class GzipMethod : public pkgAcqMethod
31 {
32 virtual bool Fetch(FetchItem *Itm);
33
34 public:
35
36 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
37 };
38
39
40 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
41 // ---------------------------------------------------------------------
42 /* */
43 bool GzipMethod::Fetch(FetchItem *Itm)
44 {
45 URI Get = Itm->Uri;
46 std::string Path = Get.Host + Get.Path; // To account for relative paths
47
48 FetchResult Res;
49 Res.Filename = Itm->DestFile;
50 URIStart(Res);
51
52 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
53 std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
54 for (; compressor != compressors.end(); ++compressor)
55 if (compressor->Name == Prog)
56 break;
57 if (compressor == compressors.end())
58 return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
59
60 // Open the source and destination files
61 FileFd From;
62 From.Open(Path, FileFd::ReadOnly, *compressor);
63
64 if(From.FileSize() == 0)
65 return _error->Error(_("Empty files can't be valid archives"));
66
67 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
68 To.EraseOnFailure();
69 if (_error->PendingError() == true)
70 return false;
71
72 // Read data from source, generate checksums and write
73 Hashes Hash;
74 bool Failed = false;
75 while (1)
76 {
77 unsigned char Buffer[4*1024];
78 unsigned long long Count = 0;
79
80 if (!From.Read(Buffer,sizeof(Buffer),&Count))
81 {
82 To.OpFail();
83 return false;
84 }
85 if (Count == 0)
86 break;
87
88 Hash.Add(Buffer,Count);
89 if (To.Write(Buffer,Count) == false)
90 {
91 Failed = true;
92 break;
93 }
94 }
95
96 From.Close();
97 To.Close();
98
99 if (Failed == true)
100 return false;
101
102 // Transfer the modification times
103 struct stat Buf;
104 if (stat(Path.c_str(),&Buf) != 0)
105 return _error->Errno("stat",_("Failed to stat"));
106
107 struct utimbuf TimeBuf;
108 TimeBuf.actime = Buf.st_atime;
109 TimeBuf.modtime = Buf.st_mtime;
110 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
111 return _error->Errno("utime",_("Failed to set modification time"));
112
113 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
114 return _error->Errno("stat",_("Failed to stat"));
115
116 // Return a Done response
117 Res.LastModified = Buf.st_mtime;
118 Res.Size = Buf.st_size;
119 Res.TakeHashes(Hash);
120
121 URIDone(Res);
122
123 return true;
124 }
125 /*}}}*/
126
127 int main(int argc, char *argv[])
128 {
129 setlocale(LC_ALL, "");
130
131 Prog = strrchr(argv[0],'/');
132 ++Prog;
133
134 GzipMethod Mth;
135 return Mth.Run();
136 }