Merge remote-tracking branch 'donkult/feature/generalize-gzipindex' into debian/sid
[ntk/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
AL
18#include <apt-pkg/acquire-method.h>
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>
24231681 23
453b82a3 24#include <string>
24231681 25#include <sys/stat.h>
453b82a3 26
d77559ac 27#include <apti18n.h>
24231681
AL
28 /*}}}*/
29
93bf083d 30class FileMethod : public pkgAcqMethod
24231681 31{
be4401bf 32 virtual bool Fetch(FetchItem *Itm);
93bf083d
AL
33
34 public:
35
e331f6ed 36 FileMethod() : pkgAcqMethod("1.0",SingleInstance | 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
AL
50 // See if the file exists
51 struct stat Buf;
52 if (stat(File.c_str(),&Buf) == 0)
24231681 53 {
93bf083d
AL
54 Res.Size = Buf.st_size;
55 Res.Filename = File;
56 Res.LastModified = Buf.st_mtime;
57 Res.IMSHit = false;
c5ccf175 58 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d
AL
59 Res.IMSHit = true;
60 }
61
62 // See if we can compute a file without a .gz exentsion
8f3ba4e8 63 std::string::size_type Pos = File.rfind(".gz");
93bf083d
AL
64 if (Pos + 3 == File.length())
65 {
8f3ba4e8 66 File = std::string(File,0,Pos);
93bf083d 67 if (stat(File.c_str(),&Buf) == 0)
24231681 68 {
93bf083d
AL
69 FetchResult AltRes;
70 AltRes.Size = Buf.st_size;
71 AltRes.Filename = File;
72 AltRes.LastModified = Buf.st_mtime;
73 AltRes.IMSHit = false;
c5ccf175 74 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d 75 AltRes.IMSHit = true;
24231681 76
93bf083d
AL
77 URIDone(Res,&AltRes);
78 return true;
24231681
AL
79 }
80 }
81
93bf083d 82 if (Res.Filename.empty() == true)
dc738e7a 83 return _error->Error(_("File not found"));
13e8426f
MV
84
85 Hashes Hash;
86 FileFd Fd(Res.Filename, FileFd::ReadOnly);
109eb151 87 Hash.AddFD(Fd);
13e8426f 88 Res.TakeHashes(Hash);
93bf083d
AL
89 URIDone(Res);
90 return true;
91}
92 /*}}}*/
93
94int main()
95{
b25423f6
MZ
96 setlocale(LC_ALL, "");
97
93bf083d
AL
98 FileMethod Mth;
99 return Mth.Run();
9391a747 100}