file.cc 2.5 KB

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