]> git.saurik.com Git - apt.git/blob - methods/gzip.cc
use utimes instead of utimensat/futimens
[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 <sys/time.h>
22 #include <unistd.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 Res.Size = To.FileSize();
98 To.Close();
99
100 if (Failed == true)
101 return false;
102
103 // Transfer the modification times
104 struct stat Buf;
105 if (stat(Path.c_str(),&Buf) != 0)
106 return _error->Errno("stat",_("Failed to stat"));
107
108 struct timeval times[2];
109 times[0].tv_sec = Buf.st_atime;
110 Res.LastModified = times[1].tv_sec = Buf.st_mtime;
111 times[0].tv_usec = times[1].tv_usec = 0;
112 if (utimes(Itm->DestFile.c_str(), times) != 0)
113 return _error->Errno("utimes",_("Failed to set modification time"));
114
115 // Return a Done response
116 Res.TakeHashes(Hash);
117
118 URIDone(Res);
119 return true;
120 }
121 /*}}}*/
122
123 int main(int argc, char *argv[])
124 {
125 setlocale(LC_ALL, "");
126
127 Prog = strrchr(argv[0],'/');
128 ++Prog;
129
130 GzipMethod Mth;
131 return Mth.Run();
132 }