]> git.saurik.com Git - apt.git/blame - methods/file.cc
fix and document on the fly compressor config
[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>
30c8107e 24#include "aptmethod.h"
24231681 25
453b82a3 26#include <string>
24231681 27#include <sys/stat.h>
453b82a3 28
d77559ac 29#include <apti18n.h>
24231681
AL
30 /*}}}*/
31
30c8107e 32class FileMethod : public aptMethod
24231681 33{
3b302846 34 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
0940230d 35
93bf083d 36 public:
30c8107e 37 FileMethod() : aptMethod("file", "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 51 struct stat Buf;
9f697f69
DK
52 // deal with destination files which might linger around
53 if (lstat(Itm->DestFile.c_str(), &Buf) == 0)
54 {
55 if ((Buf.st_mode & S_IFREG) != 0)
56 {
57 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
58 {
59 HashStringList const hsl = Itm->ExpectedHashes;
60 if (Itm->ExpectedHashes.VerifyFile(File))
ff86d7df
DK
61 {
62 Res.Filename = Itm->DestFile;
9f697f69 63 Res.IMSHit = true;
ff86d7df 64 }
9f697f69
DK
65 }
66 }
67 }
68 if (Res.IMSHit != true)
ce1f3a2c 69 RemoveFile("file", Itm->DestFile);
9f697f69 70
30c8107e 71 int olderrno = 0;
9f697f69 72 // See if the file exists
93bf083d 73 if (stat(File.c_str(),&Buf) == 0)
24231681 74 {
93bf083d
AL
75 Res.Size = Buf.st_size;
76 Res.Filename = File;
77 Res.LastModified = Buf.st_mtime;
78 Res.IMSHit = false;
c5ccf175 79 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
448c38bd
DK
80 {
81 unsigned long long const filesize = Itm->ExpectedHashes.FileSize();
82 if (filesize != 0 && filesize == Res.Size)
83 Res.IMSHit = true;
84 }
ff86d7df 85
30c8107e 86 CalculateHashes(Itm, Res);
93bf083d 87 }
30c8107e
DK
88 else
89 olderrno = errno;
ff86d7df
DK
90 if (Res.IMSHit == false)
91 URIStart(Res);
a09f6eb8
DK
92
93 // See if the uncompressed file exists and reuse it
9f697f69
DK
94 FetchResult AltRes;
95 AltRes.Filename.clear();
a09f6eb8
DK
96 std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions();
97 for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
93bf083d 98 {
a09f6eb8 99 if (APT::String::Endswith(File, *ext) == true)
24231681 100 {
e169fa4a 101 std::string const unfile = File.substr(0, File.length() - ext->length());
a09f6eb8
DK
102 if (stat(unfile.c_str(),&Buf) == 0)
103 {
a09f6eb8
DK
104 AltRes.Size = Buf.st_size;
105 AltRes.Filename = unfile;
106 AltRes.LastModified = Buf.st_mtime;
107 AltRes.IMSHit = false;
108 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
109 AltRes.IMSHit = true;
9f697f69 110 break;
a09f6eb8
DK
111 }
112 // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
113 }
24231681 114 }
a09f6eb8 115
9f697f69
DK
116 if (AltRes.Filename.empty() == false)
117 URIDone(Res,&AltRes);
118 else if (Res.Filename.empty() == false)
119 URIDone(Res);
120 else
30c8107e
DK
121 {
122 errno = olderrno;
123 return _error->Errno(File.c_str(), _("File not found"));
124 }
13e8426f 125
93bf083d
AL
126 return true;
127}
128 /*}}}*/
129
130int main()
131{
b25423f6
MZ
132 setlocale(LC_ALL, "");
133
93bf083d
AL
134 FileMethod Mth;
135 return Mth.Run();
9391a747 136}