]> git.saurik.com Git - apt.git/blame - methods/file.cc
rework hashsum verification in the acquire system
[apt.git] / methods / file.cc
CommitLineData
24231681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $
24231681
AL
4/* ######################################################################
5
6 File URI method for APT
7
1e3f4083 8 This simply checks that the file specified exists, if so the relevant
24231681
AL
9 information is returned. If a .gz filename is specified then the file
10 name with .gz removed will also be checked and information about it
11 will be returned in Alt-*
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
ea542140
DK
16#include <config.h>
17
93bf083d 18#include <apt-pkg/acquire-method.h>
a09f6eb8 19#include <apt-pkg/aptconfiguration.h>
93bf083d 20#include <apt-pkg/error.h>
13e8426f
MV
21#include <apt-pkg/hashes.h>
22#include <apt-pkg/fileutl.h>
472ff00e 23#include <apt-pkg/strutl.h>
24231681 24
453b82a3 25#include <string>
24231681 26#include <sys/stat.h>
453b82a3 27
d77559ac 28#include <apti18n.h>
24231681
AL
29 /*}}}*/
30
93bf083d 31class FileMethod : public pkgAcqMethod
24231681 32{
be4401bf 33 virtual bool Fetch(FetchItem *Itm);
93bf083d
AL
34
35 public:
36
a09f6eb8 37 FileMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig | LocalOnly) {};
93bf083d 38};
9391a747 39
93bf083d
AL
40// FileMethod::Fetch - Fetch a file /*{{{*/
41// ---------------------------------------------------------------------
42/* */
be4401bf 43bool FileMethod::Fetch(FetchItem *Itm)
9391a747 44{
be4401bf 45 URI Get = Itm->Uri;
8f3ba4e8 46 std::string File = Get.Path;
93bf083d 47 FetchResult Res;
7f25bdff 48 if (Get.Host.empty() == false)
dc738e7a 49 return _error->Error(_("Invalid URI, local URIS must not start with //"));
7f25bdff 50
93bf083d
AL
51 // See if the file exists
52 struct stat Buf;
53 if (stat(File.c_str(),&Buf) == 0)
24231681 54 {
93bf083d
AL
55 Res.Size = Buf.st_size;
56 Res.Filename = File;
57 Res.LastModified = Buf.st_mtime;
58 Res.IMSHit = false;
c5ccf175 59 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
448c38bd
DK
60 {
61 unsigned long long const filesize = Itm->ExpectedHashes.FileSize();
62 if (filesize != 0 && filesize == Res.Size)
63 Res.IMSHit = true;
64 }
93bf083d 65 }
a09f6eb8
DK
66
67 // See if the uncompressed file exists and reuse it
68 std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions();
69 for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
93bf083d 70 {
a09f6eb8 71 if (APT::String::Endswith(File, *ext) == true)
24231681 72 {
a09f6eb8
DK
73 std::string const unfile = File.substr(0, File.length() - ext->length() - 1);
74 if (stat(unfile.c_str(),&Buf) == 0)
75 {
76 FetchResult AltRes;
77 AltRes.Size = Buf.st_size;
78 AltRes.Filename = unfile;
79 AltRes.LastModified = Buf.st_mtime;
80 AltRes.IMSHit = false;
81 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
82 AltRes.IMSHit = true;
83
84 URIDone(Res,&AltRes);
85 return true;
86 }
87 // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
88 }
24231681 89 }
a09f6eb8 90
93bf083d 91 if (Res.Filename.empty() == true)
dc738e7a 92 return _error->Error(_("File not found"));
13e8426f 93
9224ce3d 94 Hashes Hash(Itm->ExpectedHashes);
13e8426f 95 FileFd Fd(Res.Filename, FileFd::ReadOnly);
109eb151 96 Hash.AddFD(Fd);
13e8426f 97 Res.TakeHashes(Hash);
93bf083d
AL
98 URIDone(Res);
99 return true;
100}
101 /*}}}*/
102
103int main()
104{
b25423f6
MZ
105 setlocale(LC_ALL, "");
106
93bf083d
AL
107 FileMethod Mth;
108 return Mth.Run();
9391a747 109}