]>
Commit | Line | Data |
---|---|---|
24231681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
be4401bf | 3 | // $Id: file.cc,v 1.5 1998/11/01 05:27:41 jgg 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> | |
24231681 AL |
18 | |
19 | #include <sys/stat.h> | |
20 | #include <unistd.h> | |
24231681 AL |
21 | /*}}}*/ |
22 | ||
93bf083d | 23 | class FileMethod : public pkgAcqMethod |
24231681 | 24 | { |
be4401bf | 25 | virtual bool Fetch(FetchItem *Itm); |
93bf083d AL |
26 | |
27 | public: | |
28 | ||
29 | FileMethod() : pkgAcqMethod("1.0",SingleInstance) {}; | |
30 | }; | |
9391a747 | 31 | |
93bf083d AL |
32 | // FileMethod::Fetch - Fetch a file /*{{{*/ |
33 | // --------------------------------------------------------------------- | |
34 | /* */ | |
be4401bf | 35 | bool FileMethod::Fetch(FetchItem *Itm) |
9391a747 | 36 | { |
be4401bf | 37 | URI Get = Itm->Uri; |
93bf083d AL |
38 | string File = Get.Path; |
39 | FetchResult Res; | |
24231681 | 40 | |
93bf083d AL |
41 | // See if the file exists |
42 | struct stat Buf; | |
43 | if (stat(File.c_str(),&Buf) == 0) | |
24231681 | 44 | { |
93bf083d AL |
45 | Res.Size = Buf.st_size; |
46 | Res.Filename = File; | |
47 | Res.LastModified = Buf.st_mtime; | |
48 | Res.IMSHit = false; | |
be4401bf | 49 | if (Itm->LastModified == Buf.st_mtime) |
93bf083d AL |
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) | |
24231681 | 59 | { |
93bf083d AL |
60 | FetchResult AltRes; |
61 | AltRes.Size = Buf.st_size; | |
62 | AltRes.Filename = File; | |
63 | AltRes.LastModified = Buf.st_mtime; | |
64 | AltRes.IMSHit = false; | |
be4401bf | 65 | if (Itm->LastModified == Buf.st_mtime) |
93bf083d | 66 | AltRes.IMSHit = true; |
24231681 | 67 | |
93bf083d AL |
68 | URIDone(Res,&AltRes); |
69 | return true; | |
24231681 AL |
70 | } |
71 | } | |
72 | ||
93bf083d AL |
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(); | |
9391a747 | 85 | } |