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