]>
Commit | Line | Data |
---|---|---|
24231681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
c5ccf175 | 3 | // $Id: file.cc,v 1.8 2000/01/27 04:15:10 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 | ||
e331f6ed | 29 | FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {}; |
93bf083d | 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; | |
7f25bdff AL |
40 | if (Get.Host.empty() == false) |
41 | return _error->Error("Invalid URI, local URIS must not start with //"); | |
42 | ||
93bf083d AL |
43 | // See if the file exists |
44 | struct stat Buf; | |
45 | if (stat(File.c_str(),&Buf) == 0) | |
24231681 | 46 | { |
93bf083d AL |
47 | Res.Size = Buf.st_size; |
48 | Res.Filename = File; | |
49 | Res.LastModified = Buf.st_mtime; | |
50 | Res.IMSHit = false; | |
c5ccf175 | 51 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
93bf083d AL |
52 | Res.IMSHit = true; |
53 | } | |
54 | ||
55 | // See if we can compute a file without a .gz exentsion | |
56 | string::size_type Pos = File.rfind(".gz"); | |
57 | if (Pos + 3 == File.length()) | |
58 | { | |
59 | File = string(File,0,Pos); | |
60 | if (stat(File.c_str(),&Buf) == 0) | |
24231681 | 61 | { |
93bf083d AL |
62 | FetchResult AltRes; |
63 | AltRes.Size = Buf.st_size; | |
64 | AltRes.Filename = File; | |
65 | AltRes.LastModified = Buf.st_mtime; | |
66 | AltRes.IMSHit = false; | |
c5ccf175 | 67 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
93bf083d | 68 | AltRes.IMSHit = true; |
24231681 | 69 | |
93bf083d AL |
70 | URIDone(Res,&AltRes); |
71 | return true; | |
24231681 AL |
72 | } |
73 | } | |
74 | ||
93bf083d AL |
75 | if (Res.Filename.empty() == true) |
76 | return _error->Error("File not found"); | |
77 | ||
78 | URIDone(Res); | |
79 | return true; | |
80 | } | |
81 | /*}}}*/ | |
82 | ||
83 | int main() | |
84 | { | |
85 | FileMethod Mth; | |
86 | return Mth.Run(); | |
9391a747 | 87 | } |