file.cc 2.5 KB

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