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