file.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: file.cc,v 1.3 1998/10/23 00:50:02 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/fileutl.h>
  14. #include <strutl.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. /*}}}*/
  19. // Fail - Generate a failure message /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* */
  22. void Fail(string URI)
  23. {
  24. printf("400 URI Failure\n"
  25. "URI: %s\n"
  26. "Message: File does not exist\n\n",URI.c_str());
  27. }
  28. /*}}}*/
  29. int main()
  30. {
  31. setlinebuf(stdout);
  32. SetNonBlock(STDIN_FILENO,true);
  33. printf("100 Capabilities\n"
  34. "Version: 1.0\n"
  35. "Pipeline: true\n\n");
  36. vector<string> Messages;
  37. while (1)
  38. {
  39. if (WaitFd(STDIN_FILENO) == false ||
  40. ReadMessages(STDIN_FILENO,Messages) == false)
  41. return 0;
  42. while (Messages.empty() == false)
  43. {
  44. string Message = Messages.front();
  45. Messages.erase(Messages.begin());
  46. // Fetch the message number
  47. char *End;
  48. int Number = strtol(Message.c_str(),&End,10);
  49. if (End == Message.c_str())
  50. {
  51. cerr << "Malformed message!" << endl;
  52. return 100;
  53. }
  54. // We only understand 600 URI Fetch messages
  55. if (Number != 600)
  56. continue;
  57. // Grab the URI bit
  58. string URI = LookupTag(Message,"URI");
  59. // Grab the filename
  60. string::size_type Pos = URI.find(':');
  61. if (Pos == string::npos)
  62. {
  63. Fail(URI);
  64. continue;
  65. }
  66. string File = string(URI,Pos+1);
  67. // Grab the modification time
  68. time_t LastMod;
  69. string LTime = LookupTag(Message,"Last-Modified");
  70. if (LTime.empty() == false && StrToTime(LTime,LastMod) == false)
  71. LTime = string();
  72. // Start the reply message
  73. string Result = "201 URI Done";
  74. Result += "\nURI: " + URI;
  75. // See if the file exists
  76. struct stat Buf;
  77. bool Ok = false;
  78. if (stat(File.c_str(),&Buf) == 0)
  79. {
  80. char S[300];
  81. sprintf(S,"\nSize: %ld",Buf.st_size);
  82. Result += "\nFilename: " + File;
  83. Result += S;
  84. Result += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  85. if (LTime.empty() == false && LastMod == Buf.st_mtime)
  86. Result += "\nIMS-Hit: true";
  87. Ok = true;
  88. }
  89. // See if we can compute a file without a .gz exentsion
  90. Pos = File.rfind(".gz");
  91. if (Pos + 3 == File.length())
  92. {
  93. File = string(File,0,Pos);
  94. if (stat(File.c_str(),&Buf) == 0)
  95. {
  96. char S[300];
  97. sprintf(S,"\nAlt-Size: %ld",Buf.st_size);
  98. Result += "\nAlt-Filename: " + File;
  99. Result += S;
  100. Result += "\nAlt-Last-Modified: " + TimeRFC1123(Buf.st_mtime);
  101. if (LTime.empty() == false && LastMod == Buf.st_mtime)
  102. Result += "\nAlt-IMS-Hit: true";
  103. Ok = true;
  104. }
  105. }
  106. // Did we find something?
  107. if (Ok == false)
  108. {
  109. Fail(URI);
  110. continue;
  111. }
  112. Result += "\n\n";
  113. // Send the message
  114. if (write(STDOUT_FILENO,Result.begin(),Result.length()) !=
  115. (signed)Result.length())
  116. return 100;
  117. }
  118. }
  119. return 0;
  120. }