]> git.saurik.com Git - apt.git/blame - methods/file.cc
apt-key: change to / before find to satisfy its CWD needs
[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
a09f6eb8 18#include <apt-pkg/aptconfiguration.h>
93bf083d 19#include <apt-pkg/error.h>
13e8426f
MV
20#include <apt-pkg/hashes.h>
21#include <apt-pkg/fileutl.h>
472ff00e 22#include <apt-pkg/strutl.h>
30c8107e 23#include "aptmethod.h"
24231681 24
453b82a3 25#include <string>
24231681 26#include <sys/stat.h>
453b82a3 27
d77559ac 28#include <apti18n.h>
24231681
AL
29 /*}}}*/
30
30c8107e 31class FileMethod : public aptMethod
24231681 32{
3b302846 33 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
0940230d 34
93bf083d 35 public:
30c8107e 36 FileMethod() : aptMethod("file", "1.0", SingleInstance | SendConfig | LocalOnly) {};
93bf083d 37};
9391a747 38
93bf083d
AL
39// FileMethod::Fetch - Fetch a file /*{{{*/
40// ---------------------------------------------------------------------
41/* */
be4401bf 42bool FileMethod::Fetch(FetchItem *Itm)
9391a747 43{
be4401bf 44 URI Get = Itm->Uri;
8f3ba4e8 45 std::string File = Get.Path;
93bf083d 46 FetchResult Res;
7f25bdff 47 if (Get.Host.empty() == false)
dc738e7a 48 return _error->Error(_("Invalid URI, local URIS must not start with //"));
7f25bdff 49
93bf083d 50 struct stat Buf;
9f697f69
DK
51 // deal with destination files which might linger around
52 if (lstat(Itm->DestFile.c_str(), &Buf) == 0)
53 {
54 if ((Buf.st_mode & S_IFREG) != 0)
55 {
56 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
57 {
58 HashStringList const hsl = Itm->ExpectedHashes;
59 if (Itm->ExpectedHashes.VerifyFile(File))
ff86d7df
DK
60 {
61 Res.Filename = Itm->DestFile;
9f697f69 62 Res.IMSHit = true;
ff86d7df 63 }
9f697f69
DK
64 }
65 }
66 }
67 if (Res.IMSHit != true)
ce1f3a2c 68 RemoveFile("file", Itm->DestFile);
9f697f69 69
30c8107e 70 int olderrno = 0;
9f697f69 71 // See if the file exists
93bf083d 72 if (stat(File.c_str(),&Buf) == 0)
24231681 73 {
93bf083d
AL
74 Res.Size = Buf.st_size;
75 Res.Filename = File;
76 Res.LastModified = Buf.st_mtime;
77 Res.IMSHit = false;
c5ccf175 78 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
448c38bd
DK
79 {
80 unsigned long long const filesize = Itm->ExpectedHashes.FileSize();
81 if (filesize != 0 && filesize == Res.Size)
82 Res.IMSHit = true;
83 }
ff86d7df 84
30c8107e 85 CalculateHashes(Itm, Res);
93bf083d 86 }
30c8107e
DK
87 else
88 olderrno = errno;
ff86d7df
DK
89 if (Res.IMSHit == false)
90 URIStart(Res);
a09f6eb8
DK
91
92 // See if the uncompressed file exists and reuse it
9f697f69
DK
93 FetchResult AltRes;
94 AltRes.Filename.clear();
a09f6eb8
DK
95 std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions();
96 for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
93bf083d 97 {
a09f6eb8 98 if (APT::String::Endswith(File, *ext) == true)
24231681 99 {
e169fa4a 100 std::string const unfile = File.substr(0, File.length() - ext->length());
a09f6eb8
DK
101 if (stat(unfile.c_str(),&Buf) == 0)
102 {
a09f6eb8
DK
103 AltRes.Size = Buf.st_size;
104 AltRes.Filename = unfile;
105 AltRes.LastModified = Buf.st_mtime;
106 AltRes.IMSHit = false;
107 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
108 AltRes.IMSHit = true;
9f697f69 109 break;
a09f6eb8
DK
110 }
111 // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
112 }
24231681 113 }
a09f6eb8 114
9f697f69
DK
115 if (AltRes.Filename.empty() == false)
116 URIDone(Res,&AltRes);
117 else if (Res.Filename.empty() == false)
118 URIDone(Res);
119 else
30c8107e
DK
120 {
121 errno = olderrno;
122 return _error->Errno(File.c_str(), _("File not found"));
123 }
13e8426f 124
93bf083d
AL
125 return true;
126}
127 /*}}}*/
128
129int main()
130{
8b79c94a 131 return FileMethod().Run();
9391a747 132}