file.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: file.cc,v 1.8 2000/01/27 04:15:10 jgg Exp $
  4. /* ######################################################################
  5. File URI method for APT
  6. This simply checks that the file specified exists, if so the relevent
  7. information is returned. If a .gz filename is specified then the file
  8. name with .gz removed will also be checked and information about it
  9. will be returned in Alt-*
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #include <apt-pkg/acquire-method.h>
  14. #include <apt-pkg/error.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. /*}}}*/
  18. class FileMethod : public pkgAcqMethod
  19. {
  20. virtual bool Fetch(FetchItem *Itm);
  21. public:
  22. FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
  23. };
  24. // FileMethod::Fetch - Fetch a file /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. bool FileMethod::Fetch(FetchItem *Itm)
  28. {
  29. URI Get = Itm->Uri;
  30. string File = Get.Path;
  31. FetchResult Res;
  32. if (Get.Host.empty() == false)
  33. return _error->Error("Invalid URI, local URIS must not start with //");
  34. // See if the file exists
  35. struct stat Buf;
  36. if (stat(File.c_str(),&Buf) == 0)
  37. {
  38. Res.Size = Buf.st_size;
  39. Res.Filename = File;
  40. Res.LastModified = Buf.st_mtime;
  41. Res.IMSHit = false;
  42. if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
  43. Res.IMSHit = true;
  44. }
  45. // See if we can compute a file without a .gz exentsion
  46. string::size_type Pos = File.rfind(".gz");
  47. if (Pos + 3 == File.length())
  48. {
  49. File = string(File,0,Pos);
  50. if (stat(File.c_str(),&Buf) == 0)
  51. {
  52. FetchResult AltRes;
  53. AltRes.Size = Buf.st_size;
  54. AltRes.Filename = File;
  55. AltRes.LastModified = Buf.st_mtime;
  56. AltRes.IMSHit = false;
  57. if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
  58. AltRes.IMSHit = true;
  59. URIDone(Res,&AltRes);
  60. return true;
  61. }
  62. }
  63. if (Res.Filename.empty() == true)
  64. return _error->Error("File not found");
  65. URIDone(Res);
  66. return true;
  67. }
  68. /*}}}*/
  69. int main()
  70. {
  71. FileMethod Mth;
  72. return Mth.Run();
  73. }