| 1 | // -*- mode: cpp; mode: fold -*- |
| 2 | // Description /*{{{*/ |
| 3 | // $Id: file.cc,v 1.8 2000/01/27 04:15:10 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 | if (Get.Host.empty() == false) |
| 41 | return _error->Error("Invalid URI, local URIS must not start with //"); |
| 42 | |
| 43 | // See if the file exists |
| 44 | struct stat Buf; |
| 45 | if (stat(File.c_str(),&Buf) == 0) |
| 46 | { |
| 47 | Res.Size = Buf.st_size; |
| 48 | Res.Filename = File; |
| 49 | Res.LastModified = Buf.st_mtime; |
| 50 | Res.IMSHit = false; |
| 51 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
| 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) |
| 61 | { |
| 62 | FetchResult AltRes; |
| 63 | AltRes.Size = Buf.st_size; |
| 64 | AltRes.Filename = File; |
| 65 | AltRes.LastModified = Buf.st_mtime; |
| 66 | AltRes.IMSHit = false; |
| 67 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
| 68 | AltRes.IMSHit = true; |
| 69 | |
| 70 | URIDone(Res,&AltRes); |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 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(); |
| 87 | } |