Sync
[ntk/apt.git] / methods / file.cc
CommitLineData
24231681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: file.cc,v 1.3 1998/10/23 00:50:02 jgg Exp $
4/* ######################################################################
5
6 File URI method for APT
7
8 This simply checks that the file specified exists, if so the relevent
9 information is returned. If a .gz filename is specified then the file
10 name with .gz removed will also be checked and information about it
11 will be returned in Alt-*
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#include <apt-pkg/fileutl.h>
17#include <strutl.h>
18
19#include <sys/stat.h>
20#include <unistd.h>
9391a747 21#include <stdio.h>
24231681
AL
22 /*}}}*/
23
24// Fail - Generate a failure message /*{{{*/
25// ---------------------------------------------------------------------
26/* */
27void Fail(string URI)
28{
29 printf("400 URI Failure\n"
30 "URI: %s\n"
31 "Message: File does not exist\n\n",URI.c_str());
32}
33 /*}}}*/
9391a747
AL
34
35int main()
36{
24231681
AL
37 setlinebuf(stdout);
38 SetNonBlock(STDIN_FILENO,true);
39
9391a747
AL
40 printf("100 Capabilities\n"
41 "Version: 1.0\n"
24231681
AL
42 "Pipeline: true\n\n");
43
44 vector<string> Messages;
45 while (1)
46 {
47 if (WaitFd(STDIN_FILENO) == false ||
48 ReadMessages(STDIN_FILENO,Messages) == false)
49 return 0;
50
51 while (Messages.empty() == false)
52 {
53 string Message = Messages.front();
54 Messages.erase(Messages.begin());
55
56 // Fetch the message number
57 char *End;
58 int Number = strtol(Message.c_str(),&End,10);
59 if (End == Message.c_str())
60 {
61 cerr << "Malformed message!" << endl;
62 return 100;
63 }
64
65 // We only understand 600 URI Fetch messages
66 if (Number != 600)
67 continue;
68
69 // Grab the URI bit
70 string URI = LookupTag(Message,"URI");
71
72 // Grab the filename
73 string::size_type Pos = URI.find(':');
74 if (Pos == string::npos)
75 {
76 Fail(URI);
77 continue;
78 }
79 string File = string(URI,Pos+1);
80
81 // Grab the modification time
82 time_t LastMod;
83 string LTime = LookupTag(Message,"Last-Modified");
84 if (LTime.empty() == false && StrToTime(LTime,LastMod) == false)
85 LTime = string();
86
87 // Start the reply message
88 string Result = "201 URI Done";
89 Result += "\nURI: " + URI;
90
91 // See if the file exists
92 struct stat Buf;
93 bool Ok = false;
94 if (stat(File.c_str(),&Buf) == 0)
95 {
96 char S[300];
97 sprintf(S,"\nSize: %ld",Buf.st_size);
98
99 Result += "\nFilename: " + File;
100 Result += S;
101 Result += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
102 if (LTime.empty() == false && LastMod == Buf.st_mtime)
103 Result += "\nIMS-Hit: true";
104
105 Ok = true;
106 }
107
108 // See if we can compute a file without a .gz exentsion
109 Pos = File.rfind(".gz");
110 if (Pos + 3 == File.length())
111 {
112 File = string(File,0,Pos);
113 if (stat(File.c_str(),&Buf) == 0)
114 {
115 char S[300];
116 sprintf(S,"\nAlt-Size: %ld",Buf.st_size);
117
118 Result += "\nAlt-Filename: " + File;
119 Result += S;
120 Result += "\nAlt-Last-Modified: " + TimeRFC1123(Buf.st_mtime);
121 if (LTime.empty() == false && LastMod == Buf.st_mtime)
122 Result += "\nAlt-IMS-Hit: true";
123
124 Ok = true;
125 }
126 }
127
128 // Did we find something?
129 if (Ok == false)
130 {
131 Fail(URI);
132 continue;
133 }
134 Result += "\n\n";
135
136 // Send the message
137 if (write(STDOUT_FILENO,Result.begin(),Result.length()) !=
138 (signed)Result.length())
139 return 100;
140 }
141 }
142
143 return 0;
9391a747 144}