file.cc 2.2 KB

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