Merge apt--authentication--0
[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 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 /*{{{*/
16 #include <apt-pkg/acquire-method.h>
17 #include <apt-pkg/error.h>
18
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <apti18n.h>
22 /*}}}*/
23
24 class FileMethod : public pkgAcqMethod
25 {
26 virtual bool Fetch(FetchItem *Itm);
27
28 public:
29
30 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
31 };
32
33 // FileMethod::Fetch - Fetch a file /*{{{*/
34 // ---------------------------------------------------------------------
35 /* */
36 bool FileMethod::Fetch(FetchItem *Itm)
37 {
38 URI Get = Itm->Uri;
39 string File = Get.Path;
40 FetchResult Res;
41 if (Get.Host.empty() == false)
42 return _error->Error(_("Invalid URI, local URIS must not start with //"));
43
44 // See if the file exists
45 struct stat Buf;
46 if (stat(File.c_str(),&Buf) == 0)
47 {
48 Res.Size = Buf.st_size;
49 Res.Filename = File;
50 Res.LastModified = Buf.st_mtime;
51 Res.IMSHit = false;
52 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
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)
62 {
63 FetchResult AltRes;
64 AltRes.Size = Buf.st_size;
65 AltRes.Filename = File;
66 AltRes.LastModified = Buf.st_mtime;
67 AltRes.IMSHit = false;
68 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
69 AltRes.IMSHit = true;
70
71 URIDone(Res,&AltRes);
72 return true;
73 }
74 }
75
76 if (Res.Filename.empty() == true)
77 return _error->Error(_("File not found"));
78
79 URIDone(Res);
80 return true;
81 }
82 /*}}}*/
83
84 int main()
85 {
86 setlocale(LC_ALL, "");
87
88 FileMethod Mth;
89 return Mth.Run();
90 }