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