]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
use pdiff for Translation-* files if available (Closes: #657902)
[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 class GzipMethod : public pkgAcqMethod
29 {
30 virtual bool Fetch(FetchItem *Itm);
31
32 public:
33
34 GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
35 };
36
37
38 // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 bool GzipMethod::Fetch(FetchItem *Itm)
42 {
43 URI Get = Itm->Uri;
44 std::string Path = Get.Host + Get.Path; // To account for relative paths
45
46 FetchResult Res;
47 Res.Filename = Itm->DestFile;
48 URIStart(Res);
49
50 // Open the source and destination files
51 FileFd From(Path,FileFd::ReadOnly, FileFd::Gzip);
52
53 if(From.FileSize() == 0)
54 return _error->Error(_("Empty files can't be valid archives"));
55
56 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
57 To.EraseOnFailure();
58 if (_error->PendingError() == true)
59 return false;
60
61 // Read data from source, generate checksums and write
62 Hashes Hash;
63 bool Failed = false;
64 while (1)
65 {
66 unsigned char Buffer[4*1024];
67 unsigned long long Count = 0;
68
69 if (!From.Read(Buffer,sizeof(Buffer),&Count))
70 {
71 To.OpFail();
72 return false;
73 }
74 if (Count == 0)
75 break;
76
77 Hash.Add(Buffer,Count);
78 if (To.Write(Buffer,Count) == false)
79 {
80 Failed = true;
81 break;
82 }
83 }
84
85 From.Close();
86 To.Close();
87
88 if (Failed == true)
89 return false;
90
91 // Transfer the modification times
92 struct stat Buf;
93 if (stat(Path.c_str(),&Buf) != 0)
94 return _error->Errno("stat",_("Failed to stat"));
95
96 struct utimbuf TimeBuf;
97 TimeBuf.actime = Buf.st_atime;
98 TimeBuf.modtime = Buf.st_mtime;
99 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
100 return _error->Errno("utime",_("Failed to set modification time"));
101
102 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
103 return _error->Errno("stat",_("Failed to stat"));
104
105 // Return a Done response
106 Res.LastModified = Buf.st_mtime;
107 Res.Size = Buf.st_size;
108 Res.TakeHashes(Hash);
109
110 URIDone(Res);
111
112 return true;
113 }
114 /*}}}*/
115
116 int main(int argc, char *argv[])
117 {
118 setlocale(LC_ALL, "");
119
120 GzipMethod Mth;
121 return Mth.Run();
122 }