Sync
[ntk/apt.git] / methods / file.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: file.cc,v 1.4 1998/10/30 07:53:52 jgg 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 /*}}}*/
22
23 class FileMethod : public pkgAcqMethod
24 {
25 virtual bool Fetch(string Message,URI Get);
26
27 public:
28
29 FileMethod() : pkgAcqMethod("1.0",SingleInstance) {};
30 };
31
32 // FileMethod::Fetch - Fetch a file /*{{{*/
33 // ---------------------------------------------------------------------
34 /* */
35 bool FileMethod::Fetch(string Message,URI Get)
36 {
37 string File = Get.Path;
38 FetchResult Res;
39
40 // See if the file exists
41 struct stat Buf;
42 if (stat(File.c_str(),&Buf) == 0)
43 {
44 Res.Size = Buf.st_size;
45 Res.Filename = File;
46 Res.LastModified = Buf.st_mtime;
47 Res.IMSHit = false;
48 if (LastModified == Buf.st_mtime)
49 Res.IMSHit = true;
50 }
51
52 // See if we can compute a file without a .gz exentsion
53 string::size_type Pos = File.rfind(".gz");
54 if (Pos + 3 == File.length())
55 {
56 File = string(File,0,Pos);
57 if (stat(File.c_str(),&Buf) == 0)
58 {
59 FetchResult AltRes;
60 AltRes.Size = Buf.st_size;
61 AltRes.Filename = File;
62 AltRes.LastModified = Buf.st_mtime;
63 AltRes.IMSHit = false;
64 if (LastModified == Buf.st_mtime)
65 AltRes.IMSHit = true;
66
67 URIDone(Res,&AltRes);
68 return true;
69 }
70 }
71
72 if (Res.Filename.empty() == true)
73 return _error->Error("File not found");
74
75 URIDone(Res);
76 return true;
77 }
78 /*}}}*/
79
80 int main()
81 {
82 FileMethod Mth;
83 return Mth.Run();
84 }