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