tighten filtering of kernel images in apt.auto-removal
[ntk/apt.git] / methods / file.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
5
6 File URI method for APT
7
8 This simply checks that the file specified exists, if so the relevant
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 /*{{{*/
16 #include <config.h>
17
18 #include <apt-pkg/acquire-method.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/hashes.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/strutl.h>
23
24 #include <string>
25 #include <sys/stat.h>
26
27 #include <apti18n.h>
28 /*}}}*/
29
30 class FileMethod : public pkgAcqMethod
31 {
32 virtual bool Fetch(FetchItem *Itm);
33
34 public:
35
36 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
37 };
38
39 // FileMethod::Fetch - Fetch a file /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 bool FileMethod::Fetch(FetchItem *Itm)
43 {
44 URI Get = Itm->Uri;
45 std::string File = Get.Path;
46 FetchResult Res;
47 if (Get.Host.empty() == false)
48 return _error->Error(_("Invalid URI, local URIS must not start with //"));
49
50 // See if the file exists
51 struct stat Buf;
52 if (stat(File.c_str(),&Buf) == 0)
53 {
54 Res.Size = Buf.st_size;
55 Res.Filename = File;
56 Res.LastModified = Buf.st_mtime;
57 Res.IMSHit = false;
58 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
59 Res.IMSHit = true;
60 }
61
62 // See if we can compute a file without a .gz exentsion
63 std::string::size_type Pos = File.rfind(".gz");
64 if (Pos + 3 == File.length())
65 {
66 File = std::string(File,0,Pos);
67 if (stat(File.c_str(),&Buf) == 0)
68 {
69 FetchResult AltRes;
70 AltRes.Size = Buf.st_size;
71 AltRes.Filename = File;
72 AltRes.LastModified = Buf.st_mtime;
73 AltRes.IMSHit = false;
74 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
75 AltRes.IMSHit = true;
76
77 URIDone(Res,&AltRes);
78 return true;
79 }
80 }
81
82 if (Res.Filename.empty() == true)
83 return _error->Error(_("File not found"));
84
85 Hashes Hash;
86 FileFd Fd(Res.Filename, FileFd::ReadOnly);
87 Hash.AddFD(Fd);
88 Res.TakeHashes(Hash);
89 URIDone(Res);
90 return true;
91 }
92 /*}}}*/
93
94 int main()
95 {
96 setlocale(LC_ALL, "");
97
98 FileMethod Mth;
99 return Mth.Run();
100 }