]>
Commit | Line | Data |
---|---|---|
24231681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz 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 /*{{{*/ | |
ea542140 DK |
16 | #include <config.h> |
17 | ||
93bf083d AL |
18 | #include <apt-pkg/acquire-method.h> |
19 | #include <apt-pkg/error.h> | |
13e8426f MV |
20 | #include <apt-pkg/hashes.h> |
21 | #include <apt-pkg/fileutl.h> | |
24231681 AL |
22 | |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
d77559ac | 25 | #include <apti18n.h> |
24231681 AL |
26 | /*}}}*/ |
27 | ||
93bf083d | 28 | class FileMethod : public pkgAcqMethod |
24231681 | 29 | { |
be4401bf | 30 | virtual bool Fetch(FetchItem *Itm); |
93bf083d AL |
31 | |
32 | public: | |
33 | ||
e331f6ed | 34 | FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {}; |
93bf083d | 35 | }; |
9391a747 | 36 | |
93bf083d AL |
37 | // FileMethod::Fetch - Fetch a file /*{{{*/ |
38 | // --------------------------------------------------------------------- | |
39 | /* */ | |
be4401bf | 40 | bool FileMethod::Fetch(FetchItem *Itm) |
9391a747 | 41 | { |
be4401bf | 42 | URI Get = Itm->Uri; |
93bf083d AL |
43 | string File = Get.Path; |
44 | FetchResult Res; | |
7f25bdff | 45 | if (Get.Host.empty() == false) |
dc738e7a | 46 | return _error->Error(_("Invalid URI, local URIS must not start with //")); |
7f25bdff | 47 | |
93bf083d AL |
48 | // See if the file exists |
49 | struct stat Buf; | |
50 | if (stat(File.c_str(),&Buf) == 0) | |
24231681 | 51 | { |
93bf083d AL |
52 | Res.Size = Buf.st_size; |
53 | Res.Filename = File; | |
54 | Res.LastModified = Buf.st_mtime; | |
55 | Res.IMSHit = false; | |
c5ccf175 | 56 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
93bf083d AL |
57 | Res.IMSHit = true; |
58 | } | |
59 | ||
60 | // See if we can compute a file without a .gz exentsion | |
61 | string::size_type Pos = File.rfind(".gz"); | |
62 | if (Pos + 3 == File.length()) | |
63 | { | |
64 | File = string(File,0,Pos); | |
65 | if (stat(File.c_str(),&Buf) == 0) | |
24231681 | 66 | { |
93bf083d AL |
67 | FetchResult AltRes; |
68 | AltRes.Size = Buf.st_size; | |
69 | AltRes.Filename = File; | |
70 | AltRes.LastModified = Buf.st_mtime; | |
71 | AltRes.IMSHit = false; | |
c5ccf175 | 72 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
93bf083d | 73 | AltRes.IMSHit = true; |
24231681 | 74 | |
93bf083d AL |
75 | URIDone(Res,&AltRes); |
76 | return true; | |
24231681 AL |
77 | } |
78 | } | |
79 | ||
93bf083d | 80 | if (Res.Filename.empty() == true) |
dc738e7a | 81 | return _error->Error(_("File not found")); |
13e8426f MV |
82 | |
83 | Hashes Hash; | |
84 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
85 | Hash.AddFD(Fd.Fd(), Fd.Size()); | |
86 | Res.TakeHashes(Hash); | |
93bf083d AL |
87 | URIDone(Res); |
88 | return true; | |
89 | } | |
90 | /*}}}*/ | |
91 | ||
92 | int main() | |
93 | { | |
b25423f6 MZ |
94 | setlocale(LC_ALL, ""); |
95 | ||
93bf083d AL |
96 | FileMethod Mth; |
97 | return Mth.Run(); | |
9391a747 | 98 | } |