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