Resync
[ntk/apt.git] / methods / file.cc
CommitLineData
24231681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
dc738e7a 3// $Id: file.cc,v 1.9 2003/02/10 07:34:41 doogie 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 /*{{{*/
dc738e7a 16#include <apti18n.h>
93bf083d
AL
17#include <apt-pkg/acquire-method.h>
18#include <apt-pkg/error.h>
24231681
AL
19
20#include <sys/stat.h>
21#include <unistd.h>
24231681
AL
22 /*}}}*/
23
93bf083d 24class FileMethod : public pkgAcqMethod
24231681 25{
be4401bf 26 virtual bool Fetch(FetchItem *Itm);
93bf083d
AL
27
28 public:
29
e331f6ed 30 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
93bf083d 31};
9391a747 32
93bf083d
AL
33// FileMethod::Fetch - Fetch a file /*{{{*/
34// ---------------------------------------------------------------------
35/* */
be4401bf 36bool FileMethod::Fetch(FetchItem *Itm)
9391a747 37{
be4401bf 38 URI Get = Itm->Uri;
93bf083d
AL
39 string File = Get.Path;
40 FetchResult Res;
7f25bdff 41 if (Get.Host.empty() == false)
dc738e7a 42 return _error->Error(_("Invalid URI, local URIS must not start with //"));
7f25bdff 43
93bf083d
AL
44 // See if the file exists
45 struct stat Buf;
46 if (stat(File.c_str(),&Buf) == 0)
24231681 47 {
93bf083d
AL
48 Res.Size = Buf.st_size;
49 Res.Filename = File;
50 Res.LastModified = Buf.st_mtime;
51 Res.IMSHit = false;
c5ccf175 52 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d
AL
53 Res.IMSHit = true;
54 }
55
56 // See if we can compute a file without a .gz exentsion
57 string::size_type Pos = File.rfind(".gz");
58 if (Pos + 3 == File.length())
59 {
60 File = string(File,0,Pos);
61 if (stat(File.c_str(),&Buf) == 0)
24231681 62 {
93bf083d
AL
63 FetchResult AltRes;
64 AltRes.Size = Buf.st_size;
65 AltRes.Filename = File;
66 AltRes.LastModified = Buf.st_mtime;
67 AltRes.IMSHit = false;
c5ccf175 68 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
93bf083d 69 AltRes.IMSHit = true;
24231681 70
93bf083d
AL
71 URIDone(Res,&AltRes);
72 return true;
24231681
AL
73 }
74 }
75
93bf083d 76 if (Res.Filename.empty() == true)
dc738e7a 77 return _error->Error(_("File not found"));
93bf083d
AL
78
79 URIDone(Res);
80 return true;
81}
82 /*}}}*/
83
84int main()
85{
86 FileMethod Mth;
87 return Mth.Run();
9391a747 88}