]> git.saurik.com Git - apt.git/blame - methods/file.cc
add a simple webserver for our testcases
[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
8 This simply checks that the file specified exists, if so the relevent
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
AL
23
24#include <sys/stat.h>
25#include <unistd.h>
d77559ac 26#include <apti18n.h>
24231681
AL
27 /*}}}*/
28
93bf083d 29class FileMethod : public pkgAcqMethod
24231681 30{
be4401bf 31 virtual bool Fetch(FetchItem *Itm);
93bf083d
AL
32
33 public:
34
e331f6ed 35 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
93bf083d 36};
9391a747 37
93bf083d
AL
38// FileMethod::Fetch - Fetch a file /*{{{*/
39// ---------------------------------------------------------------------
40/* */
be4401bf 41bool FileMethod::Fetch(FetchItem *Itm)
9391a747 42{
be4401bf 43 URI Get = Itm->Uri;
8f3ba4e8 44 std::string File = Get.Path;
93bf083d 45 FetchResult Res;
7f25bdff 46 if (Get.Host.empty() == false)
dc738e7a 47 return _error->Error(_("Invalid URI, local URIS must not start with //"));
7f25bdff 48
93bf083d
AL
49 // See if the file exists
50 struct stat Buf;
51 if (stat(File.c_str(),&Buf) == 0)
24231681 52 {
93bf083d
AL
53 Res.Size = Buf.st_size;
54 Res.Filename = File;
55 Res.LastModified = Buf.st_mtime;
56 Res.IMSHit = false;
c5ccf175 57 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d
AL
58 Res.IMSHit = true;
59 }
60
61 // See if we can compute a file without a .gz exentsion
8f3ba4e8 62 std::string::size_type Pos = File.rfind(".gz");
93bf083d
AL
63 if (Pos + 3 == File.length())
64 {
8f3ba4e8 65 File = std::string(File,0,Pos);
93bf083d 66 if (stat(File.c_str(),&Buf) == 0)
24231681 67 {
93bf083d
AL
68 FetchResult AltRes;
69 AltRes.Size = Buf.st_size;
70 AltRes.Filename = File;
71 AltRes.LastModified = Buf.st_mtime;
72 AltRes.IMSHit = false;
c5ccf175 73 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d 74 AltRes.IMSHit = true;
24231681 75
93bf083d
AL
76 URIDone(Res,&AltRes);
77 return true;
24231681
AL
78 }
79 }
80
93bf083d 81 if (Res.Filename.empty() == true)
dc738e7a 82 return _error->Error(_("File not found"));
13e8426f
MV
83
84 Hashes Hash;
85 FileFd Fd(Res.Filename, FileFd::ReadOnly);
109eb151 86 Hash.AddFD(Fd);
13e8426f 87 Res.TakeHashes(Hash);
93bf083d
AL
88 URIDone(Res);
89 return true;
90}
91 /*}}}*/
92
93int main()
94{
b25423f6
MZ
95 setlocale(LC_ALL, "");
96
93bf083d
AL
97 FileMethod Mth;
98 return Mth.Run();
9391a747 99}