file.cc 2.4 KB

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