]> git.saurik.com Git - apt.git/blob - methods/copy.cc
Added copy method
[apt.git] / methods / copy.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: copy.cc,v 1.1 1998/10/25 01:57:07 jgg Exp $
4 /* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination URI.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <apt-pkg/fileutl.h>
13 #include <strutl.h>
14 #include <apt-pkg/error.h>
15
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 /*}}}*/
20
21 // Fail - Generate a failure message /*{{{*/
22 // ---------------------------------------------------------------------
23 /* */
24 void Fail(string URI)
25 {
26 string Err = "Undetermined Error";
27 if (_error->empty() == false)
28 _error->PopMessage(Err);
29
30 printf("400 URI Failure\n"
31 "URI: %s\n"
32 "Message: %s\n\n",URI.c_str(),Err.c_str());
33 _error->Discard();
34 }
35 /*}}}*/
36
37 int main()
38 {
39 setlinebuf(stdout);
40 SetNonBlock(STDIN_FILENO,true);
41
42 printf("100 Capabilities\n"
43 "Version: 1.0\n"
44 "Pipeline: true\n\n");
45
46 vector<string> Messages;
47 while (1)
48 {
49 if (WaitFd(STDIN_FILENO) == false ||
50 ReadMessages(STDIN_FILENO,Messages) == false)
51 return 0;
52
53 while (Messages.empty() == false)
54 {
55 string Message = Messages.front();
56 Messages.erase(Messages.begin());
57
58 // Fetch the message number
59 char *End;
60 int Number = strtol(Message.c_str(),&End,10);
61 if (End == Message.c_str())
62 {
63 cerr << "Malformed message!" << endl;
64 return 100;
65 }
66
67 // We only understand 600 URI Fetch messages
68 if (Number != 600)
69 continue;
70
71 // Grab the URI bit
72 string URI = LookupTag(Message,"URI");
73 string Target = LookupTag(Message,"Filename");
74
75 // Grab the filename
76 string::size_type Pos = URI.find(':');
77 if (Pos == string::npos)
78 {
79 _error->Error("Invalid message");
80 Fail(URI);
81 continue;
82 }
83 string File = string(URI,Pos+1);
84
85 // Start the reply message
86 string Result = "201 URI Done";
87 Result += "\nURI: " + URI;
88 Result += "\nFileName: " + Target;
89
90 // See if the file exists
91 FileFd From(File,FileFd::ReadOnly);
92 FileFd To(Target,FileFd::WriteEmpty);
93 To.EraseOnFailure();
94 if (_error->PendingError() == true)
95 {
96 Fail(URI);
97 continue;
98 }
99
100 // Copy the file
101 if (CopyFile(From,To) == false)
102 {
103 Fail(URI);
104 continue;
105 }
106
107 // Send the message
108 Result += "\n\n";
109 if (write(STDOUT_FILENO,Result.begin(),Result.length()) !=
110 (signed)Result.length())
111 return 100;
112 }
113 }
114
115 return 0;
116 }